Skip to content

Kubernetes Cheatsheet

A quick-lookup kubectl reference — grouped tables instead of prose. For deeper explanations and YAML examples, see Docker & Kubernetes.


🧭 Context & Namespace

Command What it does
kubectl config get-contexts List available clusters
kubectl config use-context <ctx> Switch cluster
kubectl config set-context --current --namespace=<ns> Set default namespace for current context
kubectl get ns List namespaces

📦 Everyday Reads

Command What it does
kubectl get pods -o wide Pods with node/IP
kubectl get all -n <ns> Everything in a namespace
kubectl describe pod <pod> Full detail + recent Events
kubectl get events --sort-by=.metadata.creationTimestamp \| tail Recent cluster events, oldest first
kubectl top pods / kubectl top nodes Live CPU/memory usage (needs metrics-server)
kubectl get pods --field-selector=status.phase!=Running Pods not in a healthy phase

🪵 Logs & Exec

Command What it does
kubectl logs -f <pod> Follow logs
kubectl logs <pod> -c <container> Logs from one container in a multi-container pod
kubectl logs <pod> --previous Logs from the container's last crash
kubectl exec -it <pod> -- sh Shell into a pod
kubectl cp <pod>:/path ./local Copy a file out of a pod
kubectl port-forward svc/<svc> 8080:80 Tunnel a Service to localhost

🚀 Rollouts & Scaling

Command What it does
kubectl apply -f deploy.yaml Create/update from manifest
kubectl rollout status deployment/<name> Watch a rollout finish
kubectl rollout undo deployment/<name> Roll back to the previous revision
kubectl rollout history deployment/<name> List revisions
kubectl scale deployment/<name> --replicas=5 Manual scale
kubectl set image deployment/<name> <container>=<image> Change image without editing YAML

🩺 Debugging a Broken Pod

Command What it does
kubectl describe pod <pod> Events section explains most scheduling/probe failures
kubectl get pod <pod> -o jsonpath='{.status.containerStatuses[0].lastState.terminated.reason}' Why the last container exit happened
kubectl debug <pod> -it --image=busybox --target=<container> Attach an ephemeral debug container
kubectl get endpoints <svc> Confirm which pods a Service is actually routing to
kubectl auth can-i <verb> <resource> --as=<user> Check RBAC permissions

Common describe findings and what they mean:

Event/State Usual cause
ImagePullBackOff Wrong image tag, private registry auth missing
CrashLoopBackOff App exits immediately — check logs --previous
0/1 Running (never Ready) Readiness probe failing — see the probes lab
Pending + no node events Insufficient CPU/memory on any node, or an unsatisfied nodeSelector/affinity
OOMKilled Container hit its memory limit

⚙️ Output Formats

kubectl get pods -o wide
kubectl get deployment web -o yaml
kubectl get pods -o jsonpath='{.items[*].metadata.name}'
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase

🔐 ConfigMaps & Secrets

Command What it does
kubectl create configmap app-cfg --from-literal=ENV=prod Create a ConfigMap from a literal
kubectl create secret generic app-secret --from-literal=DB_PASSWORD=change-me Create a Secret from a literal
kubectl get secret <name> -o jsonpath='{.data.<key>}' \| base64 -d Decode a Secret value

🧰 Resource Cleanup

Command What it does
kubectl delete pod <pod> Delete one pod (Deployment recreates it)
kubectl delete -f deploy.yaml Delete everything defined in a manifest
kubectl delete pods --field-selector=status.phase=Failed -A Sweep failed pods cluster-wide
kubectl get pods -A -o wide \| grep Evicted Find evicted pods (usually node pressure)

kubectl describe before kubectl logs — the Events section usually tells you which of the two you actually need to read next.