All Cheat Sheets

kubectl

kubectl commands for contexts, pods, deployments, rollouts, logs, debugging, port-forwarding and node maintenance.

Contexts & configuration

Show merged kubeconfigkubectl config view
List contextskubectl config get-contexts
Show current contextkubectl config current-context
Switch contextkubectl config use-context <context>
Set default namespace for current contextkubectl config set-context --current --namespace=<namespace>
Check client and server versionkubectl version

Viewing resources

List podskubectl get pods
Pods with node and IPkubectl get pods -o wide
Pods in all namespaceskubectl get pods --all-namespacesShort form: -A
Get a resource as YAMLkubectl get pod <pod> -o yaml
Describe a pod (events, status)kubectl describe pod <pod>
Filter by labelkubectl get pods -l app=<name>
Filter by fieldkubectl get pods --field-selector=status.phase=Running
Show labelskubectl get pods --show-labels
Events, oldest firstkubectl get events --sort-by=.metadata.creationTimestamp
Only warning eventskubectl events --types=Warning
Sort pods by restart countkubectl get pods --sort-by='.status.containerStatuses[0].restartCount'
List all resource typeskubectl api-resources
Field docs for a resourcekubectl explain pods

Creating & applying

Apply a manifestkubectl apply -f ./manifest.yaml
Apply every manifest in a directorykubectl apply -f ./dir
Preview changes before applyingkubectl diff -f ./manifest.yaml
Create a deploymentkubectl create deployment nginx --image=nginx
Generate YAML without creatingkubectl run nginx --image=nginx --dry-run=client -o yaml > pod.yaml
Create a one-off jobkubectl create job hello --image=busybox:1.28 -- echo "Hello World"

Deployments & rollouts

Update a container imagekubectl set image deployment/<name> <container>=<image>:<tag>
Watch rollout progresskubectl rollout status -w deployment/<name>
Rollout historykubectl rollout history deployment/<name>
Roll back to previous revisionkubectl rollout undo deployment/<name>
Roll back to a specific revisionkubectl rollout undo deployment/<name> --to-revision=2
Restart all pods of a deploymentkubectl rollout restart deployment/<name>
Scale a deploymentkubectl scale --replicas=3 deployment/<name>
Autoscale between limitskubectl autoscale deployment <name> --min=2 --max=10

Logs & debugging

Pod logskubectl logs <pod>
Stream logskubectl logs -f <pod>
Logs of one containerkubectl logs <pod> -c <container>
Logs of the previous (crashed) containerkubectl logs <pod> --previous
Logs from a deployment's podkubectl logs deploy/<name>
Shell into a podkubectl exec --stdin --tty <pod> -- /bin/sh
Run one command in a podkubectl exec <pod> -- ls /
Attach a debug containerkubectl debug <pod> -it --image=busybox:1.28
Throwaway interactive podkubectl run -i --tty busybox --image=busybox:1.28 -- sh
Pod CPU/memory usagekubectl top podNeeds metrics-server in the cluster

Networking & files

Forward a local port to a podkubectl port-forward <pod> 5000:6000
Forward to a servicekubectl port-forward svc/<service> 5000
Expose a deployment as a servicekubectl expose deployment <name> --port=80 --target-port=8000
Copy a file into a podkubectl cp /tmp/foo <pod>:/tmp/bar
Copy a file out of a podkubectl cp <namespace>/<pod>:/tmp/foo /tmp/bar

Labels, deleting & nodes

Add a labelkubectl label pods <pod> env=prod
Remove a labelkubectl label pods <pod> env-
Delete resources from a filekubectl delete -f ./manifest.yaml
Delete by labelkubectl delete pods,services -l app=<name>
Mark node unschedulablekubectl cordon <node>
Drain a node for maintenancekubectl drain <node>
Make node schedulable againkubectl uncordon <node>
Node CPU/memory usagekubectl top node

Keep the references coming

One email a week: new and trending developer tools, fresh comparisons, and what shipped. Unsubscribe in one click.