← All cheatsheets

Containers

Kubernetes & kubectl — inspect, deploy, and roll out

Confirm which context and namespace you are pointed at, inspect what is running, apply a change, watch the rollout, and roll it back if it goes bad.

Docs-checkedkuberneteskubectldeployrolloutcontext

Quick reference — reach for these first

contextkubectl config current-contextALWAYS run this first — it names the cluster you are about to change. kubectl config get-contexts lists all; use-context NAME switches.
namespacekubectl config set-context --current --namespace=NSPin the namespace so later commands need no -n. Without it, commands hit 'default' and silently show nothing.
inspectkubectl get pods -n NS -o wideWhat is running, on which node, and its READY/STATUS/RESTARTS. Read this before and after any change.
applykubectl apply -f app.yaml -n NSDeclarative create-or-update from a manifest. Run kubectl diff -f app.yaml first to see what changes.
rolloutkubectl rollout status deployment/DEPLOY -n NSBlocks until the new pods are Available or it reports failure. rollout undo reverts to the prior ReplicaSet.

Deploy a change, then verify it before trusting it

kubectl deploy and verify flow Confirm context and namespace, apply the manifest, watch the rollout, check pods and endpoints, port-forward to test; if good you are done, if bad roll the deployment back. 1 · confirm context + -n NS 2 · apply -f (diff first) 3 · rollout status 4 · get pods + endpoints 5 · port- forward test good → done bad response / CrashLoop 6 · rollout undo Inspect before and after every change. undo reverts to the previous ReplicaSet in seconds.

Text equivalent: A left-to-right flow: step 1 confirm the context and namespace (accent), step 2 apply -f after a diff, step 3 rollout status, step 4 get pods and endpoints, step 5 port-forward to test. A good response ends at 'done'; a bad response or CrashLoop loops back to step 6, rollout undo, which reverts to the previous ReplicaSet.

Common tasks

Task · Command · Check

TaskCommandCheck
List pods widekubectl get pods -n NS -o wideREADY n/n, STATUS Running, low RESTARTS, and which node each is on.
Describe a resourcekubectl describe pod POD -n NSRead the Events at the bottom — scheduling, image pull, probe failures.
Follow logs livekubectl logs -f deploy/DEPLOY -n NSStreams all matching pods. Add --previous to read a crashed container's last run.
Exec into a containerkubectl exec -it POD -n NS -- shA shell opens; use for curl localhost, env, or ls inside the pod.
Port-forward to test locallykubectl port-forward svc/SVC 8080:80 -n NScurl localhost:8080 reaches the service without exposing it publicly.
Preview a changekubectl diff -f app.yaml -n NSShows the exact fields apply would add/change/remove. Empty = no-op.
Apply a manifest [CHANGES CLUSTER — prod: confirm context first]kubectl apply -f app.yaml -n NS'configured'/'created' lines. On a prod context, re-run current-context before this.
Watch the rolloutkubectl rollout status deploy/DEPLOY -n NS'successfully rolled out'. If it hangs, describe the new pods for the cause.
Roll back [REVERTS PROD]kubectl rollout undo deploy/DEPLOY -n NSReverts to the prior ReplicaSet; confirm with rollout status again.
Scale replicaskubectl scale deploy/DEPLOY --replicas=N -n NSget pods shows N pods; Deployment AVAILABLE matches N.
Recent eventskubectl get events -n NS --sort-by=.lastTimestampNewest last — Warning rows name pull, probe, or scheduling problems.
Resource usagekubectl top pods -n NSCPU/memory per pod (needs metrics-server). Spot a pod near its limit.
Discover a fieldkubectl explain deployment.spec.strategyAuthoritative field docs from the live API — no guessing manifest keys.
Delete resources [DELETES DATA / DISRUPTS SERVICE]kubectl delete -f app.yaml -n NSPrefer delete -f over delete pod. Confirm the namespace; this does not prompt.

Worked example — deploy web, verify, and roll back a bad image

DEPLOY & VERIFY

1
Confirm the target before touching anything.
kubectl config current-context
kubectl get pods -n NS
# staging-cluster
# web-7c9f  1/1  Running  0  5d
2
Preview, then apply the new manifest.
kubectl diff -f web.yaml -n NS
kubectl apply -f web.yaml -n NS
# deployment.apps/web configured
3
Watch the rollout — it stalls.
kubectl rollout status deploy/web -n NS
# Waiting for deployment "web" rollout to finish: 1 old replicas are pending termination...
4
Find why the new pod is unhealthy.
kubectl get pods -n NS
kubectl describe pod web-NEW -n NS | tail -n 5
# web-NEW  0/1  ImagePullBackOff  0  40s
# Failed to pull image "web:v2-typo": not found
5
Roll back to the last good ReplicaSet and confirm.
kubectl rollout undo deploy/web -n NS
kubectl rollout status deploy/web -n NS
# deployment.apps/web rolled back
# deployment "web" successfully rolled out
✓
Endpoints repopulate and a port-forward smoke test returns 200. The stall was a bad image tag, not the cluster — fix the tag in web.yaml and re-apply.

Troubleshooting — symptom → first check → next check

SymptomFirst checkEstablishesNext check
Changes hit the wrong clusterkubectl config current-contextWhich cluster kubectl points atconfig use-context NAME before any apply/delete
'No resources found'kubectl get pods -A | grep NAMERight object, wrong namespaceAdd -n NS or set the namespace on the context
ImagePullBackOff / ErrImagePullkubectl describe pod POD -n NSBad tag vs missing registry authFix image tag or add an imagePullSecret
Pod stuck Pendingkubectl describe pod POD -n NSNo node fits (CPU/mem, taints, PVC)Lower requests, add nodes, or check the PVC
rollout status hangskubectl get pods -n NS -wNew pods crash or fail probesLogs --previous + describe; then rollout undo
Deeper debugging neededSee the kubernetes-troubleshooting sheetCrashLoop, OOMKill, DNS, probes in depthFollow that sheet's decision tree

References

Assumes kubectl ≥ 1.29 against a reachable cluster on Linux/macOS; top needs metrics-server installed. DEPLOY, SVC, POD, NS, CONTEXT are CAPS placeholders — set your own.