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
Set default namespacekubectl config set-context --current --namespace=<ns>
Use another kubeconfigKUBECONFIG=~/.kube/staging kubectl get pods
Cluster infokubectl cluster-info kubectl version
Shell aliasalias k=kubectlEnable completion with: source <(kubectl completion bash)

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
All namespaceskubectl get pods -A
Common resourceskubectl get all
Multiple typeskubectl get deploy,svc,ingress
Output YAMLkubectl get deploy <name> -o yaml
JSONPathkubectl get pods -o jsonpath='{.items[*].metadata.name}'
Watch changeskubectl get pods -w
Explain a fieldkubectl explain deployment.spec.strategy

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"
Apply manifestkubectl apply -f deployment.yaml
Apply a directorykubectl apply -f k8s/
Apply with Kustomizekubectl apply -k overlays/prod
Preview changeskubectl diff -f deployment.yaml
Server-side dry runkubectl apply -f deployment.yaml --dry-run=server
Edit live objectkubectl edit deployment web
Patchkubectl patch deployment web -p '{"spec":{"replicas":2}}'
Delete from manifestkubectl delete -f deployment.yaml
Delete by labelkubectl delete pods -l app=web

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
Create deploymentkubectl create deployment web --image=nginx:1.27 --replicas=3
Scalekubectl scale deployment web --replicas=5
Autoscalekubectl autoscale deployment web --min=2 --max=10 --cpu-percent=70Requires metrics-server
Update imagekubectl set image deployment/web nginx=nginx:1.28Format: <container-name>=<image>
Rollout statuskubectl rollout status deployment/web
Rollout historykubectl rollout history deployment/web
Undo rolloutkubectl rollout undo deployment/web kubectl rollout undo deployment/web --to-revision=2
Restart podskubectl rollout restart deployment/webRolling restart — e.g. after a ConfigMap change
Pause / resumekubectl rollout pause deployment/web kubectl rollout resume deployment/web

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
Logs by labelkubectl logs -l app=web --tail=100
Deployment logskubectl logs deploy/web --since=1h
Shell into podkubectl exec -it <pod> -- sh
Run one commandkubectl exec <pod> -- env
Ephemeral debug containerkubectl debug -it <pod> --image=busybox --target=<container>

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
Expose deploymentkubectl expose deployment web --port=80 --target-port=8080 --type=ClusterIP
List serviceskubectl get svc
Endpoints behind a servicekubectl get endpoints web
Port-forward to servicekubectl port-forward svc/web 8080:80Then open http://localhost:8080
Port-forward to podkubectl port-forward pod/<pod> 5432:5432
List ingresseskubectl get ingress
Test DNS in clusterkubectl run tmp --rm -it --image=busybox --restart=Never -- nslookup web.default.svc.cluster.local

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
List namespaceskubectl get ns
Create namespacekubectl create namespace staging
Target a namespacekubectl get pods -n staging
Add labelkubectl label pod <pod> env=prod
Remove labelkubectl label pod <pod> env-
Filter by labelkubectl get pods -l app=web,env!=dev
Set-based selectorkubectl get pods -l 'env in (prod,staging)'
Node usagekubectl top nodesRequires metrics-server
Pod usagekubectl top pods -A --sort-by=memory
Per-container usagekubectl top pod <pod> --containers
List nodeskubectl get nodes -o wide
Cordon / uncordonkubectl cordon <node> kubectl uncordon <node>
Drain nodekubectl drain <node> --ignore-daemonsets --delete-emptydir-data

Pods

Run a podkubectl run nginx --image=nginx
Throwaway debug podkubectl run tmp --rm -it --image=busybox --restart=Never -- sh
Delete podkubectl delete pod <pod>Its controller (Deployment/ReplicaSet) will recreate it
Force delete stuck podkubectl delete pod <pod> --grace-period=0 --force
Copy fileskubectl cp <pod>:/app/log.txt ./log.txt

ConfigMaps & Secrets

ConfigMap from literalskubectl create configmap app-config --from-literal=LOG_LEVEL=debug
ConfigMap from filekubectl create configmap nginx-conf --from-file=nginx.conf
Secret from literalkubectl create secret generic db-pass --from-literal=password='s3cr3t'
Docker registry secretkubectl create secret docker-registry regcred --docker-server=<registry> --docker-username=<user> --docker-password=<pass>
Decode a secretkubectl get secret db-pass -o jsonpath='{.data.password}' | base64 -dSecrets are base64-encoded, not encrypted
Load all keys as envenvFrom: - configMapRef: name: app-config - secretRef: name: db-pass

Troubleshooting

Warnings onlykubectl get events --field-selector type=Warning -A
CrashLoopBackOffkubectl logs <pod> --previous kubectl describe pod <pod>Container keeps exiting — check the last logs, exit code, and probes
ImagePullBackOffkubectl describe pod <pod> | grep -A5 EventsWrong image/tag or missing imagePullSecrets
OOMKilledkubectl get pod <pod> -o jsonpath='{.status.containerStatuses[0].lastState.terminated.reason}'Raise memory limits or fix the leak
Not-running podskubectl get pods -A --field-selector=status.phase!=Running
Check permissionskubectl auth can-i create deployments -n staging

Minimal Deployment + Service YAML

deployment.yamlapiVersion: apps/v1 kind: Deployment metadata: name: web spec: replicas: 3 selector: matchLabels: app: web template: metadata: labels: app: web spec: containers: - name: web image: nginx:1.27 ports: - containerPort: 80 resources: requests: cpu: 100m memory: 128Mi limits: memory: 256Mi readinessProbe: httpGet: path: / port: 80
service.yamlapiVersion: v1 kind: Service metadata: name: web spec: type: ClusterIP selector: app: web ports: - port: 80 targetPort: 80selector must match the pod template labels

Keep the references coming

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