elefcode
← All cheat sheets

Tools

kubectl Cheat Sheet — Kubernetes Commands Reference

The kubectl commands you use daily to inspect, deploy, and debug workloads on a Kubernetes cluster.

Put it into practice with the matching tool.

Convert YAML and JSON →

Advertisement

Contexts and namespaces

kubectl config get-contextsList clusters/contexts
kubectl config use-context nameSwitch cluster
kubectl config set-context --current --namespace=appChange the default namespace
-n app / -ATarget one namespace / all namespaces

Viewing resources

kubectl get podsList pods (also deploy, svc, ingress, nodes)
kubectl get pods -o wideInclude node and IP
kubectl get pod p -o yamlFull resource definition
kubectl describe pod pDetails and recent events — first stop when debugging
kubectl get events --sort-by=.lastTimestampRecent cluster events
kubectl top podsCPU and memory use (needs metrics-server)

Logs and access

kubectl logs -f pFollow a pod's logs
kubectl logs p --previousLogs from the crashed previous container
kubectl logs deploy/web -c appLogs from a deployment's pod, one container
kubectl exec -it p -- shOpen a shell in a container
kubectl port-forward svc/web 8080:80Reach a service on localhost:8080
kubectl cp p:/path ./localCopy files out of a pod

Deploying

kubectl apply -f manifest.yamlCreate or update resources from a file
kubectl apply -k ./overlayApply a Kustomize directory
kubectl diff -f manifest.yamlPreview what apply would change
kubectl delete -f manifest.yamlDelete the resources in a file
kubectl create deploy web --image=nginx --dry-run=client -o yamlGenerate a manifest skeleton

Scaling and rollouts

kubectl scale deploy/web --replicas=3Change the replica count
kubectl set image deploy/web app=img:v2Update a container image
kubectl rollout status deploy/webWatch a rollout finish
kubectl rollout undo deploy/webRoll back to the previous version
kubectl rollout restart deploy/webRestart all pods gracefully

Pod status clues

CrashLoopBackOffContainer keeps exiting — check logs --previous
ImagePullBackOffWrong image name/tag or missing registry credentials
PendingNot scheduled — not enough resources or no matching node
OOMKilledExceeded its memory limit

Related guides

More cheat sheets