Skip to content
ohsudo, Home
Kubernetes

kubectl Cheat Sheet

The kubectl commands you use every day: contexts, pods, logs, exec, deployments and rollouts, debugging, YAML output, resource usage and cleanup

Updated November 6, 2026 · Applies to Kubernetes 1.28+ · kubectl

Contexts and namespaces

Terminal window
kubectl config get-contexts # which clusters?
kubectl config current-context
kubectl config use-context staging # switch cluster
kubectl config set-context --current --namespace=shop # default namespace
kubectl get pods -n kube-system # one namespace
kubectl get pods -A # all namespaces

See what’s there

Terminal window
kubectl get pods # pods here
kubectl get pods -o wide # with node and IP
kubectl get pods -w # keep watching
kubectl get pods -l app=web # by label
kubectl get pods --show-labels
kubectl get deploy,svc,ingress # several kinds at once
kubectl get all # the common kinds (not everything)
kubectl get nodes
kubectl api-resources # every kind, with short names
kubectl explain deployment.spec.strategy # docs for any field

Debug a pod

Terminal window
kubectl describe pod web-7d9f8 # status and EVENTS: start here
kubectl logs web-7d9f8 # current logs
kubectl logs web-7d9f8 --previous # logs from before the last crash
kubectl logs -f web-7d9f8 -c sidecar # follow one container
kubectl logs -l app=web --tail=50 # from every pod with a label
kubectl exec -it web-7d9f8 -- sh # a shell inside
kubectl debug -it web-7d9f8 --image=busybox --target=app # a debug container
kubectl debug node/worker-2 -it --image=ubuntu # a shell on a node
kubectl get events --sort-by=.lastTimestamp
kubectl port-forward pod/web-7d9f8 8080:80
kubectl port-forward svc/web 8080:80

Apply and change

Terminal window
kubectl apply -f app.yaml # create or update from a file
kubectl apply -f ./k8s/ # every file in a folder
kubectl apply --dry-run=server -f app.yaml # check without changing anything
kubectl diff -f app.yaml # what would change?
kubectl delete -f app.yaml
kubectl edit deploy web # edit live (and lost on next apply)
kubectl label pod web-7d9f8 tier=front
kubectl annotate deploy web owner=ops

The Kubernetes Manifest Generator writes the YAML for an app.

Deployments and rollouts

Terminal window
kubectl create deployment web --image=nginx:1.27 --replicas=2
kubectl set image deploy/web nginx=nginx:1.28 # new version
kubectl rollout status deploy/web # wait until it's done
kubectl rollout history deploy/web
kubectl rollout undo deploy/web # back to the previous one
kubectl rollout restart deploy/web # new pods, same version
kubectl scale deploy/web --replicas=5
kubectl autoscale deploy/web --min=2 --max=10 --cpu-percent=80

Services and networking

Terminal window
kubectl expose deploy web --port=80 --target-port=8080
kubectl get endpoints web # no IPs = no pod matches or is Ready
kubectl run tmp --rm -it --image=busybox -- wget -qO- http://web:80

Config and secrets

Terminal window
kubectl create configmap settings --from-file=app.conf
kubectl create secret generic db --from-literal=user=app --from-file=password=./pw.txt
kubectl get secret db -o jsonpath='{.data.password}' | base64 -d
kubectl describe secret db # keys, without the values

Get exact output

Terminal window
kubectl get pod web-7d9f8 -o yaml # the full object
kubectl get pods -o name # just names, for scripts
kubectl get pods -o jsonpath='{.items[*].metadata.name}'
kubectl get pods -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeName
kubectl get pods --sort-by=.metadata.creationTimestamp
kubectl get pods --field-selector=status.phase=Failed

Resources and nodes

Terminal window
kubectl top nodes # needs metrics-server
kubectl top pods -A --sort-by=memory
kubectl describe node worker-2 # capacity, conditions, what runs there
kubectl cordon worker-2 # no new pods here
kubectl drain worker-2 --ignore-daemonsets
kubectl uncordon worker-2

Clean up

Terminal window
kubectl delete pod web-7d9f8 # a Deployment makes a new one
kubectl delete pods --field-selector=status.phase=Failed
kubectl delete pod web-7d9f8 --grace-period=0 --force # stuck pods: last resort

Common errors