CCloudSuccessTechworld with Randy
Cheat sheets / kubectl Cheat Sheet
One-page PDF

kubectl Cheat Sheet

The commands you actually use on call: inspect, debug, roll out, roll back, and find what's eating the node.

Look around

kubectl get pods -A -o wideEvery pod, its node and IP
kubectl get pods -n shop --field-selector=status.phase!=RunningOnly the pods that aren't healthy
kubectl get events -n shop --sort-by=.lastTimestampWhat happened, newest last
kubectl get deploy,rs,svc,ep -n shopThe whole chain from deployment to endpoints
kubectl api-resources --namespaced=trueEverything you can get in a namespace

Debug a pod

kubectl describe pod <pod> -n shopState, last state, exit code, events
kubectl logs <pod> -n shop --previousLogs from the container that crashed
kubectl logs deploy/checkout -n shop -f --since=10mFollow logs for a deployment
kubectl get pod <pod> -o jsonpath='{.status.containerStatuses[0].lastState}'Why the last container died (OOMKilled, 137)
kubectl exec -it <pod> -n shop -- shShell into a running container
kubectl debug -it <pod> --image=busybox --target=appEphemeral debug container in the pod

Services and networking

kubectl get endpointslices -n shop -l kubernetes.io/service-name=checkoutWhich pod IPs the service actually routes to
kubectl get pods -n shop --show-labelsCompare pod labels with the service selector
kubectl run tmp --rm -it --image=busybox -- wget -qO- http://checkout.shopTest a service from inside the cluster
kubectl port-forward svc/checkout 8080:80 -n shopReach a service from your laptop

Rollouts

kubectl rollout status deploy/checkout -n shopWait for a rollout to finish
kubectl rollout history deploy/checkout -n shopRevisions and change causes
kubectl rollout undo deploy/checkout -n shopBack to the previous ReplicaSet
kubectl set image deploy/checkout app=shop/checkout:v3 -n shopFix forward with a new image
kubectl rollout restart deploy/checkout -n shopRecreate pods without changing the spec

Capacity and resources

kubectl top nodes / kubectl top pods -n shopReal usage (needs metrics-server)
kubectl describe node <node> | grep -A8 AllocatedRequests vs allocatable on a node
kubectl get pods -A -o custom-columns=NS:.metadata.namespace,POD:.metadata.name,QOS:.status.qosClassQoS class of every pod
kubectl get pdb -APodDisruptionBudgets that can block drains
kubectl cordon <node> && kubectl drain <node> --ignore-daemonsets --delete-emptydir-dataTake a node out safely

Remember

  • Exit code 137 = SIGKILL. With reason OOMKilled, the container hit its memory limit.
  • rollout undo doesn't help if the service selector no longer matches the pods. Check endpoints first.
  • Pending pods? Read the scheduler event: Insufficient cpu usually means requests, not real usage.