What is a kubectl cheatsheet?
kubectl is the command line of Kubernetes. With it you list what runs in a cluster, apply YAML files, read logs, open a shell in a pod, scale deployments and undo a bad release. The tool is consistent, with the shape kubectl <verb> <resource> <name> <flags>, but the number of verbs, resources and flags is large, and the names are easy to forget under pressure, for example in the middle of an incident. This cheatsheet is the short list to keep open: the command for the situation, ready to copy.
The commands are grouped in six sections: contexts and namespaces, get resources, create, apply and delete, logs and debugging, deployments and rollouts, and config maps, secrets and nodes. Each item has a plain description and one to three commands. Values you replace are written between angle brackets, such as <pod>, <namespace>, <name>, <image> and <node>.
Two habits keep you safe. Before anything that changes the cluster, check which cluster and namespace you are in: the first section is there for that. And read the description of commands that delete or interrupt work, such as kubectl delete, --force and kubectl drain. The page is plain reference material that runs in your browser, with no account and nothing sent anywhere. If you also build the images, see the Docker cheatsheet.
How to use the kubectl cheatsheet
- Open the page and scroll. The sections follow a normal working order: where am I, what is running, change it, debug it, roll it out.
- Press
/to focus the search box, or click it. Type what you want to do or part of a command:logs,rollout,port-forwardorsecret. Every word has to match. The search ignores accents and capital letters. - Use the section chips under the search box to jump to a section. While you search, only sections with results remain in the chips.
- Press the round copy button next to an item to copy its commands, one per line.
- Press Copy section in a section title to copy every command of the section.
- Replace the placeholders in angle brackets, add
-n <namespace>if the resource is not in your current namespace, and paste into your terminal. - Press Print for a clean sheet on paper or PDF, without the buttons.
A counter at the bottom shows how many items matched. If nothing matches, remove a word or try get, logs, scale or apply.
Examples
See the pods and where they run
Item "Show more columns (node, IP)":
kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE
web-6d8f7c9b5-x2k4p 1/1 Running 0 2m 10.244.1.7 node-aREADY 1/1 means one container of one is ready. RESTARTS that keeps growing is a sign of a crash loop. Add -A to list pods of every namespace.
Release a new image and undo it
kubectl set image deployment/web web=nginx:1.27
kubectl rollout status deployment/web
kubectl rollout undo deployment/webdeployment.apps/web image updated
deployment "web" successfully rolled out
deployment.apps/web rolled backrollout status waits until the new pods are ready. rollout undo goes back to the previous revision, and kubectl rollout history deployment/web lists the revisions.
Generate the YAML without creating anything
Item "Generate the YAML of a deployment without creating it":
kubectl create deployment web --image=nginx:1.27 --dry-run=client -o yamlapiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 1
# ...The output is cut here. Save it to a file, edit it, and send it with kubectl apply -f <file.yaml>. To check the file first, use kubectl diff -f <file.yaml>. You can read and convert it with the YAML viewer.
Read a secret
kubectl get secret <name> -o jsonpath='{.data.password}' | base64 -dValues inside a Secret are stored in Base64, and base64 -d decodes it. If you only have the text, the Base64 tool does the same in the browser.
Common errors and how to fix them
- The connection to the server localhost:8080 was refused:
kubectlfound no kubeconfig and fell back to a default. Checkkubectl config current-context, setKUBECONFIGor pass--kubeconfig <file>. - Error from server (NotFound): pods "x" not found: usually the right pod in the wrong namespace. Use
kubectl get pods -Ato find it and then-n <namespace>, or set a default withkubectl config set-context --current --namespace=<namespace>. - Error from server (Forbidden): your user lacks permission.
kubectl auth can-i <verb> <resource> -n <namespace>tells you what you are allowed to do. - Running a command on the wrong cluster: contexts can point to production. Run
kubectl config current-contextbeforeapply,delete,scaleordrain. - Pod stuck in ImagePullBackOff: the image name or tag is wrong, or the cluster has no credentials for the registry. Read
kubectl describe pod <pod>and look at the events at the end. For a private registry create a docker-registry secret. - Pod in CrashLoopBackOff: the container starts and dies.
kubectl logs <pod> --previousshows the output of the run that died, which the plainlogscommand no longer shows. - Pod stays Pending: no node can run it, usually for lack of CPU or memory, or a taint. The events of
kubectl describe pod <pod>say why. - kubectl delete -f deletes everything the file declares: and a label selector such as
kubectl delete pods -l app=<name>deletes all the matches. Run the matchinggetfirst to see the list. --grace-period=0 --forceremoves the pod record without waiting: the container may keep running on the node. Use it only on stuck pods.- A Secret is encoded, not encrypted: anyone who can read it can decode it. Do not paste real values in shared notes.
- No command matches your search: remove a word or try get, logs, scale or apply.
Frequently asked questions
How do I switch namespace without typing -n every time?
Run kubectl config set-context --current --namespace=<namespace>. It changes the default for the current context, until you set it again.
What is the difference between kubectl apply and kubectl create?
create makes a resource and fails if it already exists. apply creates it or updates it to match the file, so you can run it again after editing. For files kept in version control, apply is the usual choice.
How do I see why a pod does not start?
Start with kubectl describe pod <pod> and read the events at the bottom. Then kubectl logs <pod>, and add --previous if the container restarted.
How do I reach a pod from my machine?
kubectl port-forward pod/<pod> 8080:80 forwards local port 8080 to port 80 of the pod. Use svc/<service> to go through a service. It runs until you press Ctrl+C.
How do I restart the pods of a deployment?
kubectl rollout restart deployment/<name> replaces the pods one by one, so the service keeps answering.