Kubernetes Cheat Sheet
Last updated
Contexts and cluster info
A context is a cluster, a user and a default namespace, stored in ~/.kube/config.
| What it does | Command |
|---|---|
| Show the client and server versions | kubectl version |
| Show the cluster's addresses | kubectl cluster-info |
| List contexts | kubectl config get-contexts |
| Show the current context | kubectl config current-context |
| Switch to another context | kubectl config use-context prod |
| Set the default namespace for this context | kubectl config set-context --current --namespace=dev |
| Show the merged kubeconfig | kubectl config view |
| List the nodes | kubectl get nodes |
| List every resource type and its short name | kubectl api-resources |
| Explain a field of a resource | kubectl explain deployment.spec.replicas |
Listing resources
kubectl get lists resources of a type. Add -n for another namespace.
| What it does | Command |
|---|---|
| List pods in the current namespace | kubectl get pods |
| List pods in every namespace | kubectl get pods -A |
| List pods in one namespace | kubectl get pods -n kube-system |
| Add node and IP columns | kubectl get pods -o wide |
| Show the labels | kubectl get pods --show-labels |
| Filter by label | kubectl get pods -l app=web |
| Filter by field | kubectl get pods --field-selector status.phase=Running |
| Keep watching for changes | kubectl get pods -w |
| List deployments, services and pods together | kubectl get deploy,svc,pods |
| List the common resources | kubectl get all |
| Show one resource as YAML | kubectl get deployment web -o yaml |
| List services | kubectl get svc |
| List namespaces | kubectl get ns |
| List recent events, oldest first | kubectl get events --sort-by=.metadata.creationTimestamp |
Describe, logs and exec
Where to look when a pod is not Running.
| What it does | Command |
|---|---|
| Show a pod's details and events | kubectl describe pod web-7d4b9c8f6-x2k8p |
| Describe a deployment | kubectl describe deployment web |
| Show a pod's logs | kubectl logs web-7d4b9c8f6-x2k8p |
| Follow the logs | kubectl logs -f web-7d4b9c8f6-x2k8p |
| Logs of the container before it restarted | kubectl logs --previous web-7d4b9c8f6-x2k8p |
| Logs of one container in the pod | kubectl logs web-7d4b9c8f6-x2k8p -c sidecar |
| Logs of a deployment's pods | kubectl logs deployment/web |
| Logs of every pod with a label | kubectl logs -l app=web --tail=50 |
| Open a shell in a pod | kubectl exec -it web-7d4b9c8f6-x2k8p -- sh |
| Run one command in a pod | kubectl exec web-7d4b9c8f6-x2k8p -- env |
| Forward local port 8080 to a pod's port 80 | kubectl port-forward pod/web-7d4b9c8f6-x2k8p 8080:80 |
| Forward to a service | kubectl port-forward svc/web 8080:80 |
| Copy a file out of a pod | kubectl cp web-7d4b9c8f6-x2k8p:/etc/nginx/nginx.conf ./nginx.conf |
| Show CPU and memory use (needs metrics-server) | kubectl top pods |
| Start a throwaway pod with a shell | kubectl run tmp --rm -it --image=busybox -- sh |
Creating and applying
apply creates a resource or updates it to match the file; it is the command to use with manifests in Git.
| What it does | Command |
|---|---|
| Create or update from a file | kubectl apply -f deployment.yaml |
| Apply every file in a folder | kubectl apply -f k8s/ |
| Apply a kustomization folder | kubectl apply -k overlays/prod |
| Show what apply would change | kubectl diff -f deployment.yaml |
| Create a deployment | kubectl create deployment web --image=nginx --replicas=3 |
| Start a single pod | kubectl run web --image=nginx |
| Expose a deployment as a service | kubectl expose deployment web --port=80 --target-port=8080 |
| Expose it on a port of every node | kubectl expose deployment web --type=NodePort --port=80 |
| Create a ConfigMap from values | kubectl create configmap app-config --from-literal=LOG_LEVEL=debug |
| Create a Secret from values | kubectl create secret generic db-pass --from-literal=password=s3cret |
| Create a ConfigMap from a file | kubectl create configmap nginx-conf --from-file=nginx.conf |
| Write YAML for a deployment without creating it | kubectl create deployment web --image=nginx --dry-run=client -o yaml > web.yaml |
| Run a Job once | kubectl create job hello --image=busybox -- echo hello |
| Edit a live resource in your editor | kubectl edit deployment web |
Deployments and rollouts
| What it does | Command |
|---|---|
| Scale to 5 replicas | kubectl scale deployment web --replicas=5 |
| Change the image (starts a rollout) | kubectl set image deployment/web nginx=nginx:1.27 |
| Watch a rollout until it finishes | kubectl rollout status deployment/web |
| Show the rollout history | kubectl rollout history deployment/web |
| Roll back to the previous version | kubectl rollout undo deployment/web |
| Roll back to a specific revision | kubectl rollout undo deployment/web --to-revision=2 |
| Restart every pod of a deployment | kubectl rollout restart deployment/web |
| Pause a rollout | kubectl rollout pause deployment/web |
| Resume it | kubectl rollout resume deployment/web |
| Autoscale on CPU use | kubectl autoscale deployment web --min=2 --max=10 --cpu-percent=70 |
| Set an environment variable | kubectl set env deployment/web LOG_LEVEL=debug |
| Set CPU and memory requests | kubectl set resources deployment web --requests=cpu=100m,memory=128Mi |
Deleting
A deployment recreates any pod you delete; delete the deployment to remove its pods for good.
| What it does | Command |
|---|---|
| Delete what a file created | kubectl delete -f deployment.yaml |
| Delete a pod | kubectl delete pod web-7d4b9c8f6-x2k8p |
| Delete a deployment and its pods | kubectl delete deployment web |
| Delete pods by label | kubectl delete pods -l app=web |
| Delete a pod at once, without waiting | kubectl delete pod web-7d4b9c8f6-x2k8p --grace-period=0 --force |
| Delete a namespace and everything in it | kubectl delete namespace dev |
Namespaces
| What it does | Command |
|---|---|
| List namespaces | kubectl get namespaces |
| Create a namespace | kubectl create namespace dev |
| Run any command in a namespace | kubectl get pods -n dev |
| Make it the default for this context | kubectl config set-context --current --namespace=dev |
| Show the current context's namespace | kubectl config view --minify -o jsonpath='{..namespace}' |
Labels and annotations
| What it does | Command |
|---|---|
| Add a label | kubectl label pods web-7d4b9c8f6-x2k8p tier=frontend |
| Change a label | kubectl label pods web-7d4b9c8f6-x2k8p tier=backend --overwrite |
| Remove a label | kubectl label pods web-7d4b9c8f6-x2k8p tier- |
| Add an annotation | kubectl annotate deployment web owner=team-a |
| Patch a field | kubectl patch deployment web -p '{"spec":{"replicas":2}}' |
Nodes
| What it does | Command |
|---|---|
| List nodes with their IPs and OS | kubectl get nodes -o wide |
| Show a node's capacity, conditions and pods | kubectl describe node worker-1 |
| Stop new pods landing on a node | kubectl cordon worker-1 |
| Move pods off a node before maintenance | kubectl drain worker-1 --ignore-daemonsets --delete-emptydir-data |
| Allow pods on the node again | kubectl uncordon worker-1 |
| Add a taint | kubectl taint nodes worker-1 gpu=true:NoSchedule |
| Remove the taint | kubectl taint nodes worker-1 gpu=true:NoSchedule- |
| Show node CPU and memory use (needs metrics-server) | kubectl top nodes |
Output formats
Add -o to any get to change what it prints.
| What it does | Command |
|---|---|
| Full object as YAML | kubectl get pod web-7d4b9c8f6-x2k8p -o yaml |
| Full object as JSON | kubectl get pod web-7d4b9c8f6-x2k8p -o json |
| Names only (type/name) | kubectl get pods -o name |
| Pick fields with JSONPath | kubectl get pods -o jsonpath='{.items[*].metadata.name}' |
| Your own columns | kubectl get pods -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeName |
| Sort by a field | kubectl get pods --sort-by=.status.startTime |
| Decode a Secret's value | kubectl get secret db-pass -o jsonpath='{.data.password}' | base64 -d |
Short names
Most resource types have a short name you can type instead.
| Resource | Short name |
|---|---|
| Pods | po |
| Deployments | deploy |
| ReplicaSets | rs |
| StatefulSets | sts |
| DaemonSets | ds |
| Services | svc |
| Ingresses | ing |
| ConfigMaps | cm |
| Namespaces | ns |
| Nodes | no |
| PersistentVolumeClaims | pvc |
| PersistentVolumes | pv |
| ServiceAccounts | sa |
| HorizontalPodAutoscalers | hpa |
| CronJobs | cj |
Every kubectl command you reach for when working with Kubernetes (k8s), in copy-paste tables: contexts and namespaces first, then listing and describing resources, logs and exec, applying manifests, deployments and rollouts, scaling, labels, nodes and output formats.
kubectl talks to whichever cluster your current context points at, so check kubectl config current-context before you delete anything. To try the commands without a cluster, open the terminal playground, which has a single-node Kubernetes simulator built in.
kubectl cheat sheet FAQ
What is kubectl?
kubectl get pods lists pods, kubectl apply -f creates or updates resources from YAML, kubectl logs and kubectl exec look inside running containers. Which cluster it talks to is set by the current context in ~/.kube/config.Are k8s and Kubernetes the same thing?
Yes. k8s is short for Kubernetes: a k, the 8 letters in the middle, and an s. A k8s cheat sheet is a kubectl cheat sheet, because kubectl is how you work with a cluster.
What is the difference between kubectl apply and kubectl create?
kubectl create makes a resource and fails if it already exists. kubectl apply creates it or updates it to match your file, so you can run it again after every change. Use apply for manifests you keep in Git, and create for quick one-off resources.How do I see why a pod is not starting?
kubectl describe pod <name> and read the Events at the bottom: image pull errors, failed scheduling and failing probes show up there. If the container started and crashed, kubectl logs --previous <name> shows the output of the run that crashed.How do I switch namespaces in kubectl?
-n dev to a single command, or make dev the default for your current context with kubectl config set-context --current --namespace=dev.How do I restart a deployment?
kubectl rollout restart deployment/web replaces every pod with a new one, a few at a time, with no downtime when there is more than one replica. kubectl rollout status deployment/web waits until it finishes.Can I practice kubectl commands online?
kubectl get, describe, apply, logs, exec, deployments, rollouts and services answer as on a real cluster, with nothing to install. A few commands, like port-forward and edit, need a real cluster such as kind or minikube.Is this kubectl cheat sheet free?
Yes. It is free to read, copy and print, with no sign-up.