Skip to content

Menu

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-contexts

List clusters you can talk to, with a star on the active one

From the docs, not run by us
kubectl config current-context

Print the name of the context kubectl will use next

From the docs, not run by us
kubectl config use-context staging

Switch the default context saved in your kubeconfig

From the docs, not run by us
kubectl config set-context --current --namespace=payments

Make a namespace the default for the active context

From the docs, not run by us
kubectl --context prod -n payments get pods

Override context and namespace for a single command without changing defaults

From the docs, not run by us
kubectl get pods -A

List pods in every namespace

From the docs, not run by us
kubectl get pods -o wide

Add node name and pod IP columns to the pod list

From the docs, not run by us
kubectl get deploy,svc,ing

List several resource kinds in one call using short names

From the docs, not run by us
kubectl describe pod api-7d9f

Show the pod's spec, container states and recent events such as failed pulls

From the docs, not run by us
kubectl events --for pod/api-7d9f --watch

List recent events for one pod and keep watching for new ones

From the docs, not run by us
kubectl events --types=Warning

Show only Warning events in the namespace, such as failed pulls or probes

From the docs, not run by us
kubectl get services --sort-by=.metadata.name

Sort any list by a field path

From the docs, not run by us

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/api

Follow logs from a pod chosen from the deployment

From the docs, not run by us
kubectl logs api-7d9f --previous

Read logs from the last crashed instance of the container

From the docs, not run by us
kubectl logs api-7d9f -c sidecar --since=15m

Pick one container in a multi-container pod and limit to the last 15 minutes

From the docs, not run by us
kubectl exec -it api-7d9f -- sh

Open a shell in the pod's first container; everything after -- is the command

From the docs, not run by us
kubectl port-forward svc/api 8080:80

Send local port 8080 to port 80 of the service so you can curl it from your laptop

From the docs, not run by us
kubectl port-forward pod/api-7d9f :5000

Let kubectl choose a free local port for pod port 5000

From the docs, not run by us
kubectl diff -f k8s/

Show what applying the folder would change on the live cluster, as a diff

Try it: kubernetes-yaml-generatorFrom the docs, not run by us
kubectl apply -f k8s/

Create or update every manifest in a folder

From the docs, not run by us
kubectl apply -k overlays/prod

Build a kustomization directory and apply the result

From the docs, not run by us
kubectl apply -f deploy.yaml --dry-run=server

Have the API server validate and default the object without saving it

From the docs, not run by us
kubectl delete -f k8s/job.yaml

Delete exactly the objects defined in a file

From the docs, not run by us
kubectl delete pod api-7d9f

Delete one pod; its controller usually creates a replacement

From the docs, not run by us
kubectl rollout status deploy/api

Wait and report until the new ReplicaSet is fully rolled out or fails

From the docs, not run by us
kubectl rollout history deploy/api

List the stored revisions of a deployment

From the docs, not run by us
kubectl rollout undo deploy/api

Roll back to the previous revision

From the docs, not run by us
kubectl rollout undo deploy/api --to-revision=3

Roll back to a specific revision from the history

From the docs, not run by us
kubectl rollout restart deploy/api

Recreate all pods one by one without changing the spec, for example to reload a ConfigMap

From the docs, not run by us
kubectl scale deploy/api --replicas=5

Set the replica count directly; an autoscaler may change it back

From the docs, not run by us
kubectl debug api-7d9f -it --image=busybox --target=api

Attach an ephemeral busybox container that can see the api container's processes

From the docs, not run by us
kubectl debug api-7d9f -it --copy-to=api-debug --container=api -- sh

Make a copy of the pod with the app container's command replaced by a shell

From the docs, not run by us
kubectl debug node/worker-1 -it --image=busybox

Start a pod on the node in host namespaces with the node filesystem at /host

From the docs, not run by us
kubectl debug api-7d9f -it --image=nicolaka/netshoot --profile=netadmin

Use the netadmin profile to get network capabilities for tcpdump and similar tools

From the docs, not run by us
kubectl explain deployment.spec.strategy

Print the schema docs for one field path from the cluster's API

From the docs, not run by us
kubectl explain pod.spec --recursive

Print the whole field tree under a path, names and types only

From the docs, not run by us

On Windows, wrap a JSONPath template that contains spaces in double quotes and use single quotes inside it.

kubectl get deploy api -o yaml

Dump the live object as YAML, including status and server-set fields

From the docs, not run by us
kubectl get pods -o name

Print only kind/name, handy to feed into xargs

From the docs, not run by us
kubectl get pods -o jsonpath='{.items[*].metadata.name}'

Print all pod names on one line separated by spaces

From the docs, not run by us
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

From the docs, not run by us
kubectl get pods -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeName

Build a table with your own column names and field paths

From the docs, not run by us
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

From the docs, not run by us
kubectl get pods -l app=api,tier!=cache

Select pods with an equality and an inequality label match

From the docs, not run by us
kubectl get pods -l 'env in (staging,prod)'

Use a set-based selector to match any of several values

From the docs, not run by us
kubectl get pods --show-labels

Add a column with every label on each pod

From the docs, not run by us
kubectl label pod api-7d9f debug=true

Add a label to a running pod; add --overwrite to change an existing one

From the docs, not run by us
kubectl label pod api-7d9f debug-

Remove a label by putting a dash after its key

From the docs, not run by us

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 delete or apply while the active context points at production.

    Check kubectl config current-context first, or pass --context explicitly in scripts so they never rely on the default.

  • Reading kubectl logs on a restarting pod and seeing nothing useful.

    Add --previous to get the output of the container instance that crashed.

  • Fixing things with kubectl edit and then having the next apply from 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 jsonpath and getting an error.

    Output JSON and filter with jq and test(); kubectl JSONPath supports ?() filters but no regex.

  • Leaving kubectl port-forward open 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.

Sources

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.

What changed in AI dev tools, once a month

Renames, shutdowns, pricing changes and new MCP setups, each with a source.

Subscribe
Reviews and commentsShow

Comments

Sign in to leave a comment.

Loading comments…