All Cheat Sheets
kubectl
kubectl commands for contexts, pods, deployments, rollouts, logs, debugging, port-forwarding and node maintenance.
Contexts & configuration
Show merged kubeconfig
kubectl config viewList contexts
kubectl config get-contextsShow current context
kubectl config current-contextSwitch context
kubectl config use-context <context>Set default namespace for current context
kubectl config set-context --current --namespace=<namespace>Check client and server version
kubectl versionSet default namespace
kubectl config set-context --current --namespace=<ns>Use another kubeconfig
KUBECONFIG=~/.kube/staging kubectl get podsCluster info
kubectl cluster-info
kubectl versionShell alias
alias k=kubectlEnable completion with: source <(kubectl completion bash)Viewing resources
List pods
kubectl get podsPods with node and IP
kubectl get pods -o widePods in all namespaces
kubectl get pods --all-namespacesShort form: -AGet a resource as YAML
kubectl get pod <pod> -o yamlDescribe a pod (events, status)
kubectl describe pod <pod>Filter by label
kubectl get pods -l app=<name>Filter by field
kubectl get pods --field-selector=status.phase=RunningShow labels
kubectl get pods --show-labelsEvents, oldest first
kubectl get events --sort-by=.metadata.creationTimestampOnly warning events
kubectl events --types=WarningSort pods by restart count
kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'List all resource types
kubectl api-resourcesField docs for a resource
kubectl explain podsAll namespaces
kubectl get pods -ACommon resources
kubectl get allMultiple types
kubectl get deploy,svc,ingressOutput YAML
kubectl get deploy <name> -o yamlJSONPath
kubectl get pods -o jsonpath='{.items[*].metadata.name}'Watch changes
kubectl get pods -wExplain a field
kubectl explain deployment.spec.strategyCreating & applying
Apply a manifest
kubectl apply -f ./manifest.yamlApply every manifest in a directory
kubectl apply -f ./dirPreview changes before applying
kubectl diff -f ./manifest.yamlCreate a deployment
kubectl create deployment nginx --image=nginxGenerate YAML without creating
kubectl run nginx --image=nginx --dry-run=client -o yaml > pod.yamlCreate a one-off job
kubectl create job hello --image=busybox:1.28 -- echo "Hello World"Apply manifest
kubectl apply -f deployment.yamlApply a directory
kubectl apply -f k8s/Apply with Kustomize
kubectl apply -k overlays/prodPreview changes
kubectl diff -f deployment.yamlServer-side dry run
kubectl apply -f deployment.yaml --dry-run=serverEdit live object
kubectl edit deployment webPatch
kubectl patch deployment web -p '{"spec":{"replicas":2}}'Delete from manifest
kubectl delete -f deployment.yamlDelete by label
kubectl delete pods -l app=webDeployments & rollouts
Update a container image
kubectl set image deployment/<name> <container>=<image>:<tag>Watch rollout progress
kubectl rollout status -w deployment/<name>Rollout history
kubectl rollout history deployment/<name>Roll back to previous revision
kubectl rollout undo deployment/<name>Roll back to a specific revision
kubectl rollout undo deployment/<name> --to-revision=2Restart all pods of a deployment
kubectl rollout restart deployment/<name>Scale a deployment
kubectl scale --replicas=3 deployment/<name>Autoscale between limits
kubectl autoscale deployment <name> --min=2 --max=10Create deployment
kubectl create deployment web --image=nginx:1.27 --replicas=3Scale
kubectl scale deployment web --replicas=5Autoscale
kubectl autoscale deployment web --min=2 --max=10 --cpu-percent=70Requires metrics-serverUpdate image
kubectl set image deployment/web nginx=nginx:1.28Format: <container-name>=<image>Rollout status
kubectl rollout status deployment/webRollout history
kubectl rollout history deployment/webUndo rollout
kubectl rollout undo deployment/web
kubectl rollout undo deployment/web --to-revision=2Restart pods
kubectl rollout restart deployment/webRolling restart — e.g. after a ConfigMap changePause / resume
kubectl rollout pause deployment/web
kubectl rollout resume deployment/webLogs & debugging
Pod logs
kubectl logs <pod>Stream logs
kubectl logs -f <pod>Logs of one container
kubectl logs <pod> -c <container>Logs of the previous (crashed) container
kubectl logs <pod> --previousLogs from a deployment's pod
kubectl logs deploy/<name>Shell into a pod
kubectl exec --stdin --tty <pod> -- /bin/shRun one command in a pod
kubectl exec <pod> -- ls /Attach a debug container
kubectl debug <pod> -it --image=busybox:1.28Throwaway interactive pod
kubectl run -i --tty busybox --image=busybox:1.28 -- shPod CPU/memory usage
kubectl top podNeeds metrics-server in the clusterLogs by label
kubectl logs -l app=web --tail=100Deployment logs
kubectl logs deploy/web --since=1hShell into pod
kubectl exec -it <pod> -- shRun one command
kubectl exec <pod> -- envEphemeral debug container
kubectl debug -it <pod> --image=busybox --target=<container>Networking & files
Forward a local port to a pod
kubectl port-forward <pod> 5000:6000Forward to a service
kubectl port-forward svc/<service> 5000Expose a deployment as a service
kubectl expose deployment <name> --port=80 --target-port=8000Copy a file into a pod
kubectl cp /tmp/foo <pod>:/tmp/barCopy a file out of a pod
kubectl cp <namespace>/<pod>:/tmp/foo /tmp/barExpose deployment
kubectl expose deployment web --port=80 --target-port=8080 --type=ClusterIPList services
kubectl get svcEndpoints behind a service
kubectl get endpoints webPort-forward to service
kubectl port-forward svc/web 8080:80Then open http://localhost:8080Port-forward to pod
kubectl port-forward pod/<pod> 5432:5432List ingresses
kubectl get ingressTest DNS in cluster
kubectl run tmp --rm -it --image=busybox --restart=Never -- nslookup web.default.svc.cluster.localLabels, deleting & nodes
Add a label
kubectl label pods <pod> env=prodRemove a label
kubectl label pods <pod> env-Delete resources from a file
kubectl delete -f ./manifest.yamlDelete by label
kubectl delete pods,services -l app=<name>Mark node unschedulable
kubectl cordon <node>Drain a node for maintenance
kubectl drain <node>Make node schedulable again
kubectl uncordon <node>Node CPU/memory usage
kubectl top nodeList namespaces
kubectl get nsCreate namespace
kubectl create namespace stagingTarget a namespace
kubectl get pods -n stagingAdd label
kubectl label pod <pod> env=prodRemove label
kubectl label pod <pod> env-Filter by label
kubectl get pods -l app=web,env!=devSet-based selector
kubectl get pods -l 'env in (prod,staging)'Node usage
kubectl top nodesRequires metrics-serverPod usage
kubectl top pods -A --sort-by=memoryPer-container usage
kubectl top pod <pod> --containersList nodes
kubectl get nodes -o wideCordon / uncordon
kubectl cordon <node>
kubectl uncordon <node>Drain node
kubectl drain <node> --ignore-daemonsets --delete-emptydir-dataPods
Run a pod
kubectl run nginx --image=nginxThrowaway debug pod
kubectl run tmp --rm -it --image=busybox --restart=Never -- shDelete pod
kubectl delete pod <pod>Its controller (Deployment/ReplicaSet) will recreate itForce delete stuck pod
kubectl delete pod <pod> --grace-period=0 --forceCopy files
kubectl cp <pod>:/app/log.txt ./log.txtConfigMaps & Secrets
ConfigMap from literals
kubectl create configmap app-config --from-literal=LOG_LEVEL=debugConfigMap from file
kubectl create configmap nginx-conf --from-file=nginx.confSecret from literal
kubectl create secret generic db-pass --from-literal=password='s3cr3t'Docker registry secret
kubectl create secret docker-registry regcred --docker-server=<registry> --docker-username=<user> --docker-password=<pass>Decode a secret
kubectl get secret db-pass -o jsonpath='{.data.password}' | base64 -dSecrets are base64-encoded, not encryptedLoad all keys as env
envFrom:
- configMapRef:
name: app-config
- secretRef:
name: db-passTroubleshooting
Warnings only
kubectl get events --field-selector type=Warning -ACrashLoopBackOff
kubectl logs <pod> --previous
kubectl describe pod <pod>Container keeps exiting — check the last logs, exit code, and probesImagePullBackOff
kubectl describe pod <pod> | grep -A5 EventsWrong image/tag or missing imagePullSecretsOOMKilled
kubectl get pod <pod> -o jsonpath='{.status.containerStatuses[0].lastState.terminated.reason}'Raise memory limits or fix the leakNot-running pods
kubectl get pods -A --field-selector=status.phase!=RunningCheck permissions
kubectl auth can-i create deployments -n stagingMinimal Deployment + Service YAML
deployment.yaml
apiVersion: 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: 80service.yaml
apiVersion: v1
kind: Service
metadata:
name: web
spec:
type: ClusterIP
selector:
app: web
ports:
- port: 80
targetPort: 80selector must match the pod template labelsKeep the references coming
One email a week: new and trending developer tools, fresh comparisons, and what shipped. Unsubscribe in one click.


