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

CommandWhat it does
kubectl config get-contextsList the clusters and users in your kubeconfig
kubectl config current-contextWhich 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-infoAddresses of the control plane and core services
kubectl versionClient and server versions
kubectl api-resourcesEvery resource type the cluster knows, with short names

Looking around

CommandWhat it does
kubectl get podsPods in the current namespace
kubectl get pods -APods in every namespace
kubectl get pods -o wideMore columns, including node and pod IP
kubectl get pods -l app=webPods with a label
kubectl get pods -wKeep watching for changes
kubectl get allPods, services, deployments and replica sets
kubectl get deploy,svc,ingressSeveral types at once
kubectl get pod <pod> -o yamlThe 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.creationTimestampRecent events, oldest first
kubectl get nodesThe cluster's nodes and their status
kubectl top podsCPU and memory per pod (needs metrics-server)
kubectl explain deployment.specBuilt-in docs for any field

Creating and changing

CommandWhat it does
kubectl apply -f <file>.yamlCreate or update what's in a file (or a folder)
kubectl apply -k <folder>Apply a kustomization
kubectl diff -f <file>.yamlShow what apply would change
kubectl delete -f <file>.yamlDelete what's in a file
kubectl create deployment web --image=nginxQuick deployment without a file
kubectl create namespace <ns>Make a namespace
kubectl create secret generic <name> --from-literal=KEY=valueMake 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 -- shA throwaway pod with a shell, deleted when you exit

Deployments

CommandWhat it does
kubectl scale deploy/<name> --replicas=3Change 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=80Add a horizontal pod autoscaler

Debugging

CommandWhat 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=50Recent output from every pod with a label
kubectl exec -it <pod> -- shOpen a shell in a pod
kubectl port-forward svc/<service> 8080:80Reach a service on localhost:8080
kubectl cp <pod>:/path ./localCopy 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 deploymentsCheck whether you're allowed to do something

Nodes

CommandWhat it does
kubectl cordon <node>Stop new pods being scheduled on a node
kubectl drain <node> --ignore-daemonsetsMove pods off a node before maintenance
kubectl uncordon <node>Let pods be scheduled there again
kubectl label nodes <node> disk=ssdLabel a node (for nodeSelector)
kubectl taint nodes <node> key=value:NoScheduleKeep 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

  1. Find the table for what you're doing, such as Debugging or Deployments.
  2. Type a word in the filter, such as "logs" or "rollout".
  3. Copy the command and replace the parts in angle brackets.
  4. 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.