kubectl cheat sheet
Grouped by the job you're doing, not alphabetically. Click a command to copy it.
Find out why something is broken
Start wide, then narrow down: events say what Kubernetes tried, logs say what the app said.
- Every Pod in every namespace, with its node and IP
- Only Pods that aren't running
- Status, last state, exit code and the events at the bottom
- What the cluster did recently, newest last
- Only warnings in the current namespace
- Logs of the container that crashed (CrashLoopBackOff)
- Follow one container's last 50 lines
- Logs from a Deployment's Pod, last 10 minutes
- Why the last run ended (OOMKilled, Error, exit code)
- Memory use per Pod (needs metrics-server)
Services, endpoints and DNS
A Service with no endpoints is the most common reason traffic goes nowhere.
- Which Pod IPs the Service sends to (empty = selector or readiness problem)
- The labels the Service looks for
- Pods with that label, and all their labels
- Call a Service from inside the cluster
- Check cluster DNS
- Reach a Service from your machine on localhost:8080
- Policies that may be blocking traffic
Pending Pods and nodes
Pending means the scheduler found no node; the reason is in the Pod's events.
- Nodes, their status, version and IPs
- Pressure conditions and how much is already requested
- Taints on every node
- Labels a nodeSelector or affinity can match
- Stop new Pods landing on a node
- Move Pods off a node before maintenance
- Allow Pods on the node again
- Disruption budgets that can block a drain
Deployments and rollouts
Change, watch, and undo if it goes wrong.
- Roll out a new image
- Wait for the rollout and see if it got stuck
- Earlier revisions
- Go back to the previous revision
- Restart every Pod, for example to reload a ConfigMap
- Change the number of replicas
- Create a HorizontalPodAutoscaler
- ReplicaSets: where FailedCreate errors (quota) show up
Write YAML fast (imperative, then edit)
Let kubectl write the skeleton, then edit the file in vim. Faster and fewer typos than typing YAML.
- Deployment skeleton
- Pod skeleton
- Service that matches the Deployment's labels
- ConfigMap from values
- Secret (base64 done for you)
- Job skeleton
- CronJob skeleton
- Ingress skeleton
- Field names without leaving the terminal
- Change a live object in your editor
RBAC and access
Ask the API server instead of reading every Role by hand.
- Is this user allowed to do this?
- Everything a ServiceAccount may do
- A Role
- Bind it to a ServiceAccount
- Where a user or ServiceAccount is bound
Clusters, contexts and namespaces
Most "it worked a minute ago" moments are the wrong context or namespace.
- Your clusters, with the current one starred
- Switch cluster
- Change the default namespace
- The server and user you're really using
- Short names, API groups, and what is namespaced
- Most of what's in a namespace
Get exactly the value you need
jsonpath and custom columns turn kubectl into a query tool.
- Your own table
- Name and IP per line
- Read one Secret value
- Oldest first
- The whole live object
- What apply would change
Questions
- How do I see why a Pod is not starting?
- kubectl describe pod <name> and read the Events at the bottom; then kubectl logs <name> --previous for a container that crashed.
- How do I generate YAML quickly with kubectl?
- Add --dry-run=client -o yaml to an imperative command, for example kubectl create deployment web --image=nginx --dry-run=client -o yaml > web.yaml, then edit the file.
- How do I check what a ServiceAccount is allowed to do?
- kubectl auth can-i --list --as=system:serviceaccount:<namespace>:<name>, or ask about one action: kubectl auth can-i get pods --as=…
Practise it on a real machine
Broken systems in your browser. The grader checks the result.