kubectl Command Cheat Sheet
Nine groups from context to cluster, each with a ready-to-use example
90 commands
kubectl config get-contextsContext and listingList all contexts; switching to the wrong cluster is a classic outage.
kubectl config get-contexts
kubectl config use-context prodContext and listingSwitch the active context; confirm this before any change.
kubectl config use-context prod-cn
kubectl config current-contextContext and listingShow the active cluster, useful as a script precondition.
kubectl config current-context
kubectl config set-context --current --namespace=stagingContext and listingPin the default namespace to skip repeating -n.
kubectl config set-context --current --namespace=staging
kubectl get nsContext and listingList namespaces.
kubectl get ns
kubectl get all -n appContext and listingShow the main resources in a namespace at once.
kubectl get all -n app
kubectl get pods -o wideContext and listingAdd IP and node columns; essential for scheduling and network issues.
kubectl get pods -o wide -n app
kubectl get pods -AContext and listingList pods across all namespaces.
kubectl get pods -A --field-selector=status.phase!=Running
kubectl get pods -wContext and listingWatch the list change over time, handy during rollouts.
kubectl get pods -w -n app
kubectl get pod web-0 -o yamlContext and listingDump the full YAML of a resource, including runtime status.
kubectl get pod web-0 -o yaml > pod-backup.yaml
kubectl get deploy web -o yaml --exportContext and listingStrip cluster-generated fields so the YAML can be reapplied.
kubectl get deploy web -o yaml -n app
kubectl explain deploy.spec.strategyContext and listingLook up field meaning and allowed values without leaving the terminal.
kubectl explain deploy.spec.strategy --recursive
kubectl api-resourcesContext and listingList every resource type the cluster supports, with short names.
kubectl api-resources --namespaced=true
kubectl apply -f deploy.yamlContext and listingApply manifests declaratively; safe to run repeatedly.
kubectl apply -f k8s/ -n app
kubectl delete -f deploy.yamlContext and listingDelete resources described by a manifest.
kubectl delete -f k8s/ -n app
kubectl create ns stagingContext and listingCreate a namespace.
kubectl create ns staging
kubectl describe pod <pod>Pod operationsShow pod details; the Events section at the bottom is where clues live.
kubectl describe pod web-0 -n app
kubectl logs <pod>Pod operationsPrint container logs.
kubectl logs web-0 -n app --tail=200
kubectl logs -f <pod> -c appPod operationsFollow logs of one container; -c is required for multi-container pods.
kubectl logs -f web-0 -c app -n app
kubectl logs --previous <pod>Pod operationsRead logs from before the last crash; essential for CrashLoopBackOff.
kubectl logs web-0 --previous -n app
kubectl logs --since=10m <pod>Pod operationsShow logs from the last ten minutes.
kubectl logs web-0 --since=10m --timestamps -n app
kubectl exec -it <pod> -- shPod operationsGet a shell in a container; slim images usually lack bash.
kubectl exec -it web-0 -n app -- /bin/sh
kubectl exec <pod> -- envPod operationsShow the environment variables actually in effect.
kubectl exec web-0 -n app -- printenv
kubectl cp ./local.txt <pod>:/tmp/Pod operationsCopy files between the local machine and a pod.
kubectl cp ./dump.sql app/web-0:/tmp/dump.sql
kubectl port-forward <pod> 8080:80Pod operationsForward a pod port locally to debug services that are not exposed.
kubectl port-forward pod/web-0 8080:80 -n app
kubectl run tmp --rm -it --image=busybox -- shPod operationsThrowaway debug pod that cleans itself up on exit.
kubectl run tmp --rm -it --image=busybox:1.36 --restart=Never -- sh
kubectl get pod <pod> -o jsonpath="{.status.podIP}"Pod operationsRead a pod IP in a script-friendly way.
kubectl get pod web-0 -o jsonpath="{.status.podIP}" -n appkubectl delete pod <pod>Pod operationsDelete a pod; controller-managed pods are recreated automatically.
kubectl delete pod web-0 -n app
kubectl delete pod <pod> --grace-period=0 --forcePod operationsForce-delete a pod stuck in Terminating.
kubectl delete pod web-0 --grace-period=0 --force -n app
kubectl get pod <pod> -o jsonpath="{.status.containerStatuses[*].restartCount}"Pod operationsCheck restart counts to spot a crash loop.
kubectl get pod web-0 -o jsonpath="{.status.containerStatuses[*].restartCount}" -n appkubectl get deployDeploy and scaleList deployments with replica readiness.
kubectl get deploy -n app -o wide
kubectl scale deploy/web --replicas=3Deploy and scaleChange the replica count; the simplest way to scale.
kubectl scale deploy/web --replicas=3 -n app
kubectl set image deploy/web web=app:1.1Deploy and scaleRoll out a new image version.
kubectl set image deploy/web web=registry.example.com/app:1.1 -n app
kubectl edit deploy/webDeploy and scaleEdit the live spec; saving applies it immediately.
kubectl edit deploy/web -n app
kubectl patch deploy/web --type=merge -p "{\"spec\":{\"replicas\":2}}"Deploy and scalePatch a field with inline JSON, better than edit in scripts.
kubectl patch deploy/web --type=merge -p "{\"spec\":{\"replicas\":2}}" -n appkubectl autoscale deploy/web --min=2 --max=10 --cpu-percent=70Deploy and scaleSet up horizontal pod autoscaling.
kubectl autoscale deploy/web --min=2 --max=10 --cpu-percent=70 -n app
kubectl get rsDeploy and scaleList ReplicaSets; past versions live here.
kubectl get rs -n app
kubectl get deploy web -o jsonpath="{.spec.template.spec.containers[*].image}"Deploy and scaleConfirm which image is actually running.
kubectl get deploy web -o jsonpath="{.spec.template.spec.containers[*].image}" -n appkubectl annotate deploy/web note="release-1.1"Deploy and scaleAttach an annotation to record release info.
kubectl annotate deploy/web note="release-1.1" --overwrite -n app
kubectl label deploy/web tier=webDeploy and scaleAdd a label used by Service selectors and queries.
kubectl label deploy/web tier=web --overwrite -n app
kubectl rollout status deploy/webRollout and rollbackWatch rollout progress; always check after a release.
kubectl rollout status deploy/web -n app --timeout=120s
kubectl rollout history deploy/webRollout and rollbackShow revision history and change causes.
kubectl rollout history deploy/web -n app
kubectl rollout undo deploy/webRollout and rollbackRoll back to the previous revision; the fastest way to stop the bleeding.
kubectl rollout undo deploy/web -n app
kubectl rollout undo deploy/web --to-revision=2Rollout and rollbackRoll back to a specific revision; check history first.
kubectl rollout undo deploy/web --to-revision=2 -n app
kubectl rollout restart deploy/webRollout and rollbackRestart all pods in place, common after config changes.
kubectl rollout restart deploy/web -n app
kubectl rollout pause deploy/webRollout and rollbackPause a rollout so you can inspect before resuming.
kubectl rollout pause deploy/web -n app
kubectl rollout resume deploy/webRollout and rollbackResume a paused rollout.
kubectl rollout resume deploy/web -n app
kubectl get svcService and networkList services with ClusterIP and ports.
kubectl get svc -n app -o wide
kubectl get endpoints webService and networkShow the pod IPs behind a service; empty means the selector matches nothing.
kubectl get endpoints web -n app
kubectl describe svc webService and networkShow service details including selector and port mapping.
kubectl describe svc web -n app
kubectl expose deploy/web --port=80 --target-port=8080Service and networkExpose a deployment with a service.
kubectl expose deploy/web --port=80 --target-port=8080 --type=ClusterIP -n app
kubectl port-forward svc/web 8080:80Service and networkForward a service port to verify locally without Ingress.
kubectl port-forward svc/web 8080:80 -n app
kubectl get ingressService and networkList ingresses and the hostnames they expose.
kubectl get ingress -n app
kubectl describe ingress webService and networkShow ingress rules and resolved backends.
kubectl describe ingress web -n app
kubectl get netpolService and networkList network policies; check them when pods cannot reach each other.
kubectl get netpol -n app
kubectl get svc web -o jsonpath="{.spec.clusterIP}"Service and networkRead the ClusterIP for configs or scripts.
kubectl get svc web -o jsonpath="{.spec.clusterIP}" -n appkubectl get cmConfig and secretsList ConfigMaps.
kubectl get cm -n app
kubectl create cm app-config --from-file=app.yamlConfig and secretsCreate a ConfigMap from a file.
kubectl create cm app-config --from-file=config/app.yaml -n app
kubectl describe cm app-configConfig and secretsShow ConfigMap contents.
kubectl describe cm app-config -n app
kubectl get secretConfig and secretsList secrets; values are base64-encoded, not encrypted.
kubectl get secret -n app
kubectl create secret generic db --from-literal=password=changeitConfig and secretsCreate a secret from literals.
kubectl create secret generic db --from-literal=password=changeit -n app
kubectl get secret db -o jsonpath="{.data.password}" | base64 -dConfig and secretsDecode a secret value to debug a config that is not taking effect.
kubectl get secret db -o jsonpath="{.data.password}" -n app | base64 -dkubectl get saConfig and secretsList service accounts, the identity pods use to reach the API.
kubectl get sa -n app
kubectl auth can-i get pods --as=system:serviceaccount:app:webConfig and secretsCheck whether an identity has a permission; useful for RBAC debugging.
kubectl auth can-i get pods --as=system:serviceaccount:app:web -n app
kubectl get nodesNodes and schedulingList nodes and their status.
kubectl get nodes -o wide
kubectl describe node <node>Nodes and schedulingShow node details including allocated resources and taints.
kubectl describe node node-1
kubectl top nodesNodes and schedulingShow real node usage; requires metrics-server.
kubectl top nodes
kubectl top podsNodes and schedulingShow pod usage to find what is eating memory.
kubectl top pods -n app --sort-by=memory
kubectl cordon <node>Nodes and schedulingMark a node unschedulable without evicting existing pods.
kubectl cordon node-1
kubectl drain <node> --ignore-daemonsets --delete-emptydir-dataNodes and schedulingEvict pods from a node before maintenance.
kubectl drain node-1 --ignore-daemonsets --delete-emptydir-data
kubectl uncordon <node>Nodes and schedulingMake a node schedulable again.
kubectl uncordon node-1
kubectl taint nodes node-1 dedicated=gpu:NoScheduleNodes and schedulingTaint a node so only tolerant pods land on it.
kubectl taint nodes node-1 dedicated=gpu:NoSchedule
kubectl get events --sort-by=.lastTimestampTroubleshootingList events newest-first; cluster-level problems show up here.
kubectl get events --sort-by=.lastTimestamp -n app | tail -30
kubectl get events -wTroubleshootingWatch the event stream while reproducing an issue.
kubectl get events -w -n app
kubectl get pod --field-selector=status.phase=FailedTroubleshootingFilter for pods in a failed phase.
kubectl get pod --field-selector=status.phase=Failed -A
kubectl get pod <pod> -o jsonpath="{.status.conditions[?(@.type==\"Ready\")].reason}"TroubleshootingRead the not-ready reason faster than scrolling describe.
kubectl get pod web-0 -o jsonpath="{.status.conditions[?(@.type==\"Ready\")].reason}" -n appkubectl debug -it <pod> --image=busybox --target=appTroubleshootingInject an ephemeral container sharing the target process namespace.
kubectl debug -it web-0 --image=busybox:1.36 --target=app -n app
kubectl debug node/<node> -it --image=busyboxTroubleshootingStart a debug pod on a node with access to the host filesystem.
kubectl debug node/node-1 -it --image=busybox:1.36
kubectl logs <pod> --all-containersTroubleshootingShow logs from every container in a pod.
kubectl logs web-0 --all-containers --prefix -n app
kubectl wait --for=condition=Ready pod/web-0 --timeout=60sTroubleshootingWait until a pod is ready; useful as a script barrier.
kubectl wait --for=condition=Ready pod/web-0 --timeout=60s -n app
kubectl get pod web-0 -o json | jq .statusTroubleshootingPipe to jq for status fields when jsonpath gets unwieldy.
kubectl get pod web-0 -o json -n app | jq .status
kubectl get pod web-0 -o yaml --show-managed-fieldsTroubleshootingSee which controller last touched a field.
kubectl get pod web-0 -o yaml --show-managed-fields -n app
kubectl cluster-infoCluster and resourcesShow the control plane address and add-on locations.
kubectl cluster-info
kubectl version --shortCluster and resourcesShow client and server versions; mind the version skew.
kubectl version --short
kubectl apply --dry-run=server -f deploy.yamlCluster and resourcesServer-side dry run: real validation without persisting.
kubectl apply --dry-run=server -f deploy.yaml -n app
kubectl diff -f deploy.yamlCluster and resourcesPreview what apply would change before running it.
kubectl diff -f deploy.yaml -n app
kubectl get pv,pvcCluster and resourcesList volumes and claims; Pending usually means a storage class issue.
kubectl get pv,pvc -n app
kubectl get hpaCluster and resourcesShow autoscaler status and current metrics.
kubectl get hpa -n app
kubectl get crdCluster and resourcesList custom resource definitions installed in the cluster.
kubectl get crd | head -30
kubectl api-resources --verbs=list --namespaced -o nameCluster and resourcesList listable namespaced resources, handy for backup scripts.
kubectl api-resources --verbs=list --namespaced -o name
Something broken or missing?
Send feedback