Reference · 18 entries

kubectl Reference

The kubectl commands you use every day: get, describe, apply, rollout, logs, exec, debug, events, port-forward and auth can-i, with options and real examples run against Kubernetes 1.37.

kubectl 1.37 Version history

Every example was run against a real Kubernetes 1.37 cluster when this page was built, so the output is real. Each example creates its own namespace and deletes it at the end. For explanations of the concepts, see the Kubernetes tutorial.

Basics

Read, create, update and delete resources with kubectl get, describe, apply, delete, create and explain.

NameDescription
kubectl getList resources in a table, or print them as YAML, JSON, JSONPath or custom columns for scripts and debugging.
kubectl describeShow a detailed, human-readable view of a resource, including related events, the first place to look when debugging.
kubectl applyCreate or update resources from files, directories or Kustomize folders declaratively; the standard way to deploy manifests.
kubectl deleteDelete resources by name, file or label selector; deleting a namespace deletes everything inside it.
kubectl createCreate resources imperatively from the command line: namespaces, deployments, configmaps, secrets, jobs and more.
kubectl explainShow documentation for any resource field straight from the cluster's API schema, including nested fields.

Workloads

Roll out, roll back, scale and relabel workloads with kubectl rollout, scale, set image and label.

NameDescription
kubectl rolloutManage Deployment, StatefulSet and DaemonSet rollouts: watch status, view history, restart and undo to a previous revision.
kubectl scaleSet the number of replicas of a Deployment, ReplicaSet or StatefulSet; avoid it for workloads managed by an autoscaler.
kubectl set imageChange the image of one or more containers in a workload's pod template, which starts a rolling update.
kubectl labelAdd, change or remove labels on resources; labels drive selectors for Services, Deployments, network policies and queries.

Debugging

Find out what's wrong with kubectl logs, exec, port-forward, events and debug.

NameDescription
kubectl logsPrint a container's logs from a pod, deployment or job; follow them, limit lines, or read the previous crashed container.
kubectl execRun a command in a container of a running pod, for example a shell, a database client or a quick check.
kubectl port-forwardForward a local port to a pod or Service for testing and debugging without exposing it publicly.
kubectl eventsList recent cluster events (scheduling, image pulls, probe failures, kills) for a namespace or one resource, sorted by time.
kubectl debugDebug pods and nodes with ephemeral containers or copies, even when the image has no shell or tools.

Access and Configuration

Check permissions, preview changes and switch clusters with kubectl auth can-i, diff and config.

NameDescription
kubectl auth can-iCheck whether you, or another user or service account via --as, may perform an action; --list shows everything allowed.
kubectl diffShow what kubectl apply would change in the cluster, as a diff against the live objects, before you apply it.
kubectl configInspect and switch kubeconfig contexts (cluster, user and default namespace), and set the namespace for the current context.

Was this page helpful?