Skip to content
shellhacks.dev

kubectl Cheat Sheet

A searchable kubectl cheat sheet: contexts, pods, logs, exec and debug containers, apply and diff, rollouts and rollbacks, scaling, events, secrets, jobs, draining nodes and RBAC checks.

Free · No sign-up · Your inputs stay in your browser

Reference

kubectl cheat sheet

CommandDoesNotes
kubectl config get-contexts / use-context prodWhich cluster you are talking toCheck this before anything destructive.
kubectl get pods -A -o widePods in every namespace, with node and IP-n ns for one namespace.
kubectl get pods -wWatch pods change
kubectl describe pod web-7d9fEvents and state of a podThe Events section explains Pending and CrashLoopBackOff.
kubectl logs -f deploy/web --tail=100Follow a deployment's logs-p shows the previous, crashed container; -c picks a container.
kubectl exec -it web-7d9f -- shShell into a pod
kubectl debug -it web-7d9f --image=busybox --target=webEphemeral debug containerFor distroless images with no shell.
kubectl apply -f manifests/Create or update from files--dry-run=server -o yaml to preview.
kubectl diff -f manifests/What apply would change
kubectl rollout status deploy/webWait for a rollout
kubectl rollout undo deploy/webRoll back to the previous ReplicaSet--to-revision=N; history lists them.
kubectl rollout restart deploy/webRestart every pod gracefullyPicks up changed ConfigMaps and Secrets.
kubectl scale deploy/web --replicas=5Change the replica countAn HPA will override it.
kubectl set image deploy/web web=ghcr.io/acme/web:1.4.3Deploy a new image
kubectl port-forward svc/web 8080:80Reach a service from your machine
kubectl get events --sort-by=.lastTimestampRecent cluster events
kubectl top pods / top nodesCPU and memory useNeeds metrics-server.
kubectl get deploy web -o yamlThe live object as YAML-o jsonpath='{.spec.replicas}' for one field.
kubectl explain deployment.spec.strategyDocumentation for any field
kubectl create secret generic app --from-env-file=.envSecret from a .env file--dry-run=client -o yaml to write it to a file instead.
kubectl get secret app -o jsonpath='{.data.DB_PASSWORD}' | base64 -dRead one secret value
kubectl create job --from=cronjob/report report-nowRun a CronJob immediately
kubectl drain node-3 --ignore-daemonsets --delete-emptydir-dataEmpty a node for maintenancekubectl uncordon node-3 afterwards.
kubectl auth can-i delete pods --as=system:serviceaccount:app:webCheck RBAC permissions
kubectl delete pod web-7d9f --grace-period=0 --forceRemove a stuck podLast resort: it skips graceful shutdown.

deploy/, svc/ and cronjob/ prefixes let commands address a workload rather than one pod. alias k=kubectl and kubectl completion bash (or zsh) save a lot of typing; kubens and kubectx switch namespaces and clusters faster than config set-context.

Address workloads, not pods

Prefixes such as deploy/, svc/ and cronjob/ let commands target a workload instead of a pod with a random suffix: kubectl logs deploy/web picks a pod for you. The kubectl quick reference lists many more.

Speed

alias k=kubectl, shell completion (kubectl completion bash or zsh) and kubectx and kubens for switching clusters and namespaces save most of the typing.

Share

Frequently asked questions

How do I see logs of a pod that keeps crashing?
kubectl logs pod-name -p shows the previous container's logs. kubectl describe pod shows the restart reason and events.
How do I roll back a deployment?
kubectl rollout undo deploy/web returns to the previous revision; kubectl rollout history deploy/web lists revisions.
Is what I paste sent anywhere?
No. The tool runs entirely in your browser with JavaScript. Nothing you type or paste is sent to our server or stored.

Go deeper

Suggested reading

Keep planning

Similar tools