kubectl Cheat Sheet: Contexts, Rollouts, Debug and JSONPath
Checked against kubectl (Kubernetes CLI) Kubernetes 1.37 (latest patch v1.37.3, 25 Nov 2026) on 28 November 2026. Each section links to the official docs it was checked against.
This sheet is for people who work against a Kubernetes cluster from the terminal and want the commands that come up in a normal week. It follows the kubectl reference and quick reference for Kubernetes 1.37; the newest patch on GitHub was v1.37.3 from 25 November 2026. We had no cluster to test against, so every row is marked untested and each section links the reference page it came from.
When to use what
Pick your context and namespace before anything else. Most surprises with kubectl come from running a correct command against the wrong cluster, so check kubectl config current-context and set a default namespace with set-context --current rather than typing -n every time and forgetting it once.
For changes, prefer kubectl apply -f with files kept in git over imperative commands like create, edit or set image. Run kubectl diff -f first: it shows what the server would change, which catches drift that someone made by hand. Keep the imperative commands for experiments and emergencies. Our Kubernetes YAML generator gives you a valid Deployment or Service to start from, and the resource quantity converter helps when you are unsure what 500m or 1Gi means.
When something breaks, go in this order: get to see status, describe for events, logs --previous for the crash, then exec if the container has a shell. If it does not (distroless images), use kubectl debug to attach an ephemeral container with tools instead of rebuilding the image. For scripts, use -o jsonpath for one or two fields and -o json | jq for anything with conditions or regex, since kubectl JSONPath has no regex. If a command fails with Forbidden, the RBAC explorer helps you read which verbs a role grants. The container orchestration guide covers the concepts behind these objects.
kubectl config get-contextsList clusters you can talk to, with a star on the active one
kubectl config current-contextPrint the name of the context kubectl will use next
kubectl config use-context stagingSwitch the default context saved in your kubeconfig
kubectl config set-context --current --namespace=paymentsMake a namespace the default for the active context
kubectl --context prod -n payments get podsOverride context and namespace for a single command without changing defaults
kubectl get pods -AList pods in every namespace
kubectl get pods -o wideAdd node name and pod IP columns to the pod list
kubectl get deploy,svc,ingList several resource kinds in one call using short names
kubectl describe pod api-7d9fShow the pod's spec, container states and recent events such as failed pulls
kubectl events --for pod/api-7d9f --watchList recent events for one pod and keep watching for new ones
kubectl events --types=WarningShow only Warning events in the namespace, such as failed pulls or probes
kubectl get services --sort-by=.metadata.nameSort any list by a field path
Forwarding to a Service or Deployment picks one pod; when that pod ends, the session ends and you rerun the command.
kubectl logs -f deploy/apiFollow logs from a pod chosen from the deployment
kubectl logs api-7d9f --previousRead logs from the last crashed instance of the container
kubectl logs api-7d9f -c sidecar --since=15mPick one container in a multi-container pod and limit to the last 15 minutes
kubectl exec -it api-7d9f -- shOpen a shell in the pod's first container; everything after -- is the command
kubectl port-forward svc/api 8080:80Send local port 8080 to port 80 of the service so you can curl it from your laptop
kubectl port-forward pod/api-7d9f :5000Let kubectl choose a free local port for pod port 5000
kubectl diff -f k8s/Show what applying the folder would change on the live cluster, as a diff
kubectl apply -f k8s/Create or update every manifest in a folder
kubectl apply -k overlays/prodBuild a kustomization directory and apply the result
kubectl apply -f deploy.yaml --dry-run=serverHave the API server validate and default the object without saving it
kubectl delete -f k8s/job.yamlDelete exactly the objects defined in a file
kubectl delete pod api-7d9fDelete one pod; its controller usually creates a replacement
kubectl rollout status deploy/apiWait and report until the new ReplicaSet is fully rolled out or fails
kubectl rollout history deploy/apiList the stored revisions of a deployment
kubectl rollout undo deploy/apiRoll back to the previous revision
kubectl rollout undo deploy/api --to-revision=3Roll back to a specific revision from the history
kubectl rollout restart deploy/apiRecreate all pods one by one without changing the spec, for example to reload a ConfigMap
kubectl scale deploy/api --replicas=5Set the replica count directly; an autoscaler may change it back
kubectl debug api-7d9f -it --image=busybox --target=apiAttach an ephemeral busybox container that can see the api container's processes
kubectl debug api-7d9f -it --copy-to=api-debug --container=api -- shMake a copy of the pod with the app container's command replaced by a shell
kubectl debug node/worker-1 -it --image=busyboxStart a pod on the node in host namespaces with the node filesystem at /host
kubectl debug api-7d9f -it --image=nicolaka/netshoot --profile=netadminUse the netadmin profile to get network capabilities for tcpdump and similar tools
kubectl explain deployment.spec.strategyPrint the schema docs for one field path from the cluster's API
kubectl explain pod.spec --recursivePrint the whole field tree under a path, names and types only
On Windows, wrap a JSONPath template that contains spaces in double quotes and use single quotes inside it.
kubectl get deploy api -o yamlDump the live object as YAML, including status and server-set fields
kubectl get pods -o namePrint only kind/name, handy to feed into xargs
kubectl get pods -o jsonpath='{.items[*].metadata.name}'Print all pod names on one line separated by spaces
kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.phase}{"\n"}{end}'Print one pod per line with its phase, using range and end
kubectl get pods -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeNameBuild a table with your own column names and field paths
kubectl get pods -o json | jq -r '.items[] | select(.metadata.name | test("^api-")) | .metadata.name'Filter names with a regex through jq, since kubectl JSONPath has no regex support
kubectl get pods -l app=api,tier!=cacheSelect pods with an equality and an inequality label match
kubectl get pods -l 'env in (staging,prod)'Use a set-based selector to match any of several values
kubectl get pods --show-labelsAdd a column with every label on each pod
kubectl label pod api-7d9f debug=trueAdd a label to a running pod; add --overwrite to change an existing one
kubectl label pod api-7d9f debug-Remove a label by putting a dash after its key
Worked example: Find and roll back a bad deploy
A new image went out and the API is crash-looping. First confirm where you are and what the rollout looks like:
kubectl config current-context
kubectl rollout status deploy/api --timeout=60s
kubectl get pods -l app=api
kubectl logs deploy/api --previous
kubectl rollout history deploy/api
kubectl rollout undo deploy/api
kubectl rollout status deploy/api
The context check makes sure you are on the cluster you think you are. rollout status with a timeout stops watching after a minute instead of hanging; the rollout itself carries on in the cluster. The pod list will show the new pods in CrashLoopBackOff next to old ones still running, because a Deployment keeps old replicas until the new ones pass readiness. logs --previous shows the output of the container that crashed, which is what you want; the current instance may have only just started. rollout history lists the revisions, and undo with no flag goes back one. The final rollout status confirms the old ReplicaSet is fully back. Fix the image, then kubectl diff -f and apply your manifest again so git and the cluster agree. If the app was first written for Compose, the Compose to Kubernetes converter is a quick way to get its first manifests.
Common mistakes
Running
kubectl deleteorapplywhile the active context points at production.Check
kubectl config current-contextfirst, or pass--contextexplicitly in scripts so they never rely on the default.Reading
kubectl logson a restarting pod and seeing nothing useful.Add
--previousto get the output of the container instance that crashed.Fixing things with
kubectl editand then having the nextapplyfrom git undo them.Make the change in the manifest file, run
kubectl diff -f, then apply, so the repo stays the source of truth.Writing
=~regex filters in-o jsonpathand getting an error.Output JSON and filter with
jqandtest(); kubectl JSONPath supports?()filters but no regex.Leaving
kubectl port-forwardopen and wondering why it dies after a deploy.The forward is bound to one pod; rerun it after the pod is replaced, or use a Service through an ingress for anything long-lived.
Scaling a deployment by hand while a HorizontalPodAutoscaler manages it.
Change the HPA's min and max replicas instead; the autoscaler will overwrite a manual
scale.
Related cheat sheets
Sources
- https://kubernetes.io/docs/reference/kubectl/quick-reference/
- https://kubernetes.io/docs/reference/kubectl/jsonpath/
- https://kubernetes.io/docs/reference/kubectl/generated/kubectl_get/
- https://kubernetes.io/docs/reference/kubectl/generated/kubectl_events/
- https://kubernetes.io/docs/reference/kubectl/generated/kubectl_logs/
- https://kubernetes.io/docs/reference/kubectl/generated/kubectl_diff/
- https://kubernetes.io/docs/reference/kubectl/generated/kubectl_explain/
- https://kubernetes.io/docs/reference/kubectl/generated/kubectl_debug/
- https://kubernetes.io/docs/reference/kubectl/generated/kubectl_port-forward/
- https://kubernetes.io/docs/reference/kubectl/generated/kubectl_rollout/kubectl_rollout_undo/
- https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
- https://kubernetes.io/docs/reference/kubectl/generated/kubectl_label/
- https://kubernetes.io/docs/reference/kubectl/generated/kubectl_apply/
- https://kubernetes.io/docs/reference/kubectl/generated/kubectl_rollout/kubectl_rollout_status/
- https://kubernetes.io/releases/
- https://github.com/kubernetes/kubernetes/releases
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.