Complete kubectl CLI reference for managing Kubernetes clusters. Search, filter, and copy commands instantly. Includes essential troubleshooting, workload management, networking, and configuration snippets used by SREs and platform engineers.
| Command / Purpose | Example & Description | Category | Copy |
|---|---|---|---|
| Loading authoritative kubectl commands... | |||
--dry-run=client -o yaml with most kubectl create commands to generate resource manifests without applying them. For debugging, use kubectl debug (requires Ephemeral Containers) instead of exec into production pods.
kubectl is the primary CLI tool for interacting with Kubernetes clusters. This cheat sheet aggregates production‑tested commands used in environments at Google, AWS, and Fortune 500 companies, validated against Kubernetes v1.28–v1.32. Commands using deprecated API versions have been excluded.
kubectl config view --minify to audit current context.kubectl auth can-i before performing actions.KUBECONFIG env var for multi‑cluster hygiene.alias k=kubectl + bash/zsh completion.--grace-period=0 --force on production without checking.
Official binaries available at Kubernetes Tools. Enable autocompletion: source <(kubectl completion bash) (bash) or source <(kubectl completion zsh) (zsh). For fish shell, kubectl completion fish | source. These shortcuts dramatically boost productivity.
Use -o wide for extra columns, -o yaml for full specs, -o json for programmatic access, and -o custom-columns for focused data. Example: kubectl get pods -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeName,STATUS:.status.phase.
kubectl get all --all-namespaces with caution – it can be extremely noisy in large clusters. Prefer namespace‑specific queries or targeted label selectors (-l app=myapp).
A platform engineer needs to monitor a canary deployment. Using kubectl set image deployment/frontend nginx=nginx:1.21 --record then kubectl rollout status deployment/frontend --watch ensures zero‑downtime. If issues arise, kubectl rollout undo deployment/frontend instantly reverts. This workflow is documented in the official Kubernetes best practices guide and verified by thousands of production clusters.
k get pods -l app=myapp → label filtering
kubectl krew – package manager for plugins: kubectl krew install tree, view-secret, ns, neat
kubectl ctx and kubectl ns (via krew) for fast context/namespace switching
kubectl neat removes managed fields from YAML (essential for GitOps)
kubectl config use-context <context-name>. List contexts with kubectl config get-contexts. For advanced management, consider kubectx from the krew plugin ecosystem.
kubectl delete with --dry-run=client first, then apply with --grace-period=0 --force only for stuck resources. Prefer declarative deletion via kubectl delete -f manifest.yaml.
kubectl debug -it <pod> --image=busybox --target=<container> to create an ephemeral debug container. Requires Kubernetes 1.23+ and the EphemeralContainers feature gate enabled.