kubectl Commands: Kubernetes Cheat Sheet
kubectl cheat sheet for Kubernetes: contexts and namespaces, get and describe, apply, scale and roll back deployments, logs, exec, port-forward and nodes.
kubectl commands for Kubernetes, grouped by job: switching clusters and namespaces, listing and describing what's running, applying YAML, scaling and rolling back deployments, debugging pods with logs, exec and port-forward, and preparing nodes for maintenance. Parts in angle brackets are what you replace, and most resource types have short names, such as deploy for deployments, svc for services and ns for namespaces (kubectl api-resources lists them all). When a pod won't start, kubectl describe pod and kubectl logs --previous answer most questions: the events at the bottom of describe show image pull errors, failed probes and scheduling problems. Commands act on the current namespace unless you add -n <namespace> or -A for all of them. The Docker Commands cheat sheet covers building the images your pods run.
Clusters and contexts
| Command | What it does |
|---|---|
| kubectl config get-contexts | List the clusters and users in your kubeconfig |
| kubectl config current-context | Which one you're using now |
| kubectl config use-context <name> | Switch to another cluster |
| kubectl config set-context --current --namespace=<ns> | Change the default namespace |
| kubectl cluster-info | Addresses of the control plane and core services |
| kubectl version | Client and server versions |
| kubectl api-resources | Every resource type the cluster knows, with short names |
Looking around
| Command | What it does |
|---|---|
| kubectl get pods | Pods in the current namespace |
| kubectl get pods -A | Pods in every namespace |
| kubectl get pods -o wide | More columns, including node and pod IP |
| kubectl get pods -l app=web | Pods with a label |
| kubectl get pods -w | Keep watching for changes |
| kubectl get all | Pods, services, deployments and replica sets |
| kubectl get deploy,svc,ingress | Several types at once |
| kubectl get pod <pod> -o yaml | The full object as YAML |
| kubectl describe pod <pod> | Details and recent events: the first stop when a pod won't start |
| kubectl get events --sort-by=.metadata.creationTimestamp | Recent events, oldest first |
| kubectl get nodes | The cluster's nodes and their status |
| kubectl top pods | CPU and memory per pod (needs metrics-server) |
| kubectl explain deployment.spec | Built-in docs for any field |
Creating and changing
| Command | What it does |
|---|---|
| kubectl apply -f <file>.yaml | Create or update what's in a file (or a folder) |
| kubectl apply -k <folder> | Apply a kustomization |
| kubectl diff -f <file>.yaml | Show what apply would change |
| kubectl delete -f <file>.yaml | Delete what's in a file |
| kubectl create deployment web --image=nginx | Quick deployment without a file |
| kubectl create namespace <ns> | Make a namespace |
| kubectl create secret generic <name> --from-literal=KEY=value | Make a secret |
| kubectl create configmap <name> --from-file=<path> | Make a config map from files |
| kubectl edit deploy/<name> | Edit a live object in your editor |
| kubectl delete pod <pod> | Delete a pod (its deployment makes a new one) |
| kubectl run tmp --rm -it --image=busybox -- sh | A throwaway pod with a shell, deleted when you exit |
Deployments
| Command | What it does |
|---|---|
| kubectl scale deploy/<name> --replicas=3 | Change the number of pods |
| kubectl set image deploy/<name> <container>=<image>:<tag> | Roll out a new image |
| kubectl rollout status deploy/<name> | Wait for a rollout to finish |
| kubectl rollout history deploy/<name> | Earlier revisions |
| kubectl rollout undo deploy/<name> | Go back to the previous revision |
| kubectl rollout restart deploy/<name> | Restart every pod, one batch at a time |
| kubectl autoscale deploy/<name> --min=2 --max=5 --cpu-percent=80 | Add a horizontal pod autoscaler |
Debugging
| Command | What it does |
|---|---|
| kubectl logs <pod> | A pod's output |
| kubectl logs -f <pod> -c <container> | Follow one container in a pod |
| kubectl logs --previous <pod> | Output from before the last restart (for crash loops) |
| kubectl logs -l app=web --tail=50 | Recent output from every pod with a label |
| kubectl exec -it <pod> -- sh | Open a shell in a pod |
| kubectl port-forward svc/<service> 8080:80 | Reach a service on localhost:8080 |
| kubectl cp <pod>:/path ./local | Copy a file out of a pod (needs tar in the image) |
| kubectl debug -it <pod> --image=busybox --target=<container> | Attach a debugging container to a running pod |
| kubectl auth can-i create deployments | Check whether you're allowed to do something |
Nodes
| Command | What it does |
|---|---|
| kubectl cordon <node> | Stop new pods being scheduled on a node |
| kubectl drain <node> --ignore-daemonsets | Move pods off a node before maintenance |
| kubectl uncordon <node> | Let pods be scheduled there again |
| kubectl label nodes <node> disk=ssd | Label a node (for nodeSelector) |
| kubectl taint nodes <node> key=value:NoSchedule | Keep pods off a node unless they tolerate the taint |
Common use cases
- Find a pod's logs or open a shell in it
- Roll a deployment back after a bad release
- Switch between clusters and namespaces safely
- Prepare for a Kubernetes exam or a new job
How to use
- Find the table for what you're doing, such as Debugging or Deployments.
- Type a word in the filter, such as "logs" or "rollout".
- Copy the command and replace the parts in angle brackets.
- Add -n <namespace> if the object isn't in your current namespace.
FAQ
How do I see why a pod is crashing?
Run kubectl describe pod <pod> and read the events at the end, then kubectl logs --previous <pod> for the output from before the last restart. CrashLoopBackOff means the container keeps exiting, so the logs usually say why.
What's the difference between apply and create?
create makes an object and fails if it already exists. apply creates it or updates it to match the file, so it's the one to use when you keep your YAML in version control and re-apply it after changes.
How do I roll back a deployment?
kubectl rollout undo deploy/<name> goes back to the previous revision. Add --to-revision=<n> for an older one; kubectl rollout history deploy/<name> lists them.
Why does kubectl top say metrics aren't available?
kubectl top reads from the metrics-server add-on. Many managed clusters have it already; on others it has to be installed before top works.
Related tools
- Docker Commands: Cheat Sheet with ExamplesDocker commands cheat sheet: build, run and stop containers, ports and volumes, logs and exec, networks…
- Linux Command CheatsheetCommon Linux terminal commands for file management and system administration.
- Git Commands: Cheat Sheet with ExamplesGit commands cheat sheet: set up, stage and commit, branch, merge and rebase, push and pull, history, stash…
- npm Commands: Cheat Sheet with Examplesnpm commands cheat sheet: install, update and remove packages, npm ci, run scripts and npx, audit and fix…
- Mac Keyboard ShortcutsmacOS keyboard shortcuts for productivity and navigation.
- MIME Type LookupMIME type reference for content type headers and file handling.
- YAML ValidatorIn Utilities