Kubectl quick reference cheat sheet
Commands and options for Kubernetes v1.37.
1. Context & Configuration
| Command | Description |
|---|---|
kubectl config view |
Show merged kubeconfig settings. |
kubectl config view --raw |
Show settings including raw certificates/secrets. |
kubectl config get-contexts |
List all contexts. |
kubectl config use-context <name> |
Switch to a specific context. |
kubectl config current-context |
Display the active context. |
kubectl config set-context --current --namespace=<ns> |
Set default namespace for current context. |
Useful Aliases (Bash):
# Set/Show Context
kx() { [ "$1" ] && kubectl config use-context $1 || kubectl config current-context ; }
# Set/Show Namespace
kn() { [ "$1" ] && kubectl config set-context --current --namespace $1 || kubectl config view --minify | grep namespace | cut -d" " -f6 ; }
2. Creating & Applying Resources
| Command | Description |
|---|---|
kubectl apply -f <file.yaml> |
Create/Update resources from a file. |
kubectl apply -f ./dir/ |
Apply all manifests in a directory. |
kubectl apply -f - <<EOF ... EOF |
Apply resources from stdin (heredoc). |
kubectl create deployment <name> --image=<img> |
Create a single-instance deployment. |
kubectl explain pods |
Get documentation for pod manifests. |
3. Viewing & Finding Resources
| Command | Description |
|---|---|
kubectl get pods |
List pods in current namespace. |
kubectl get pods -A |
List pods in all namespaces (-A is shorthand for --all-namespaces). |
kubectl get pods -o wide |
List pods with more details (IP, Node, etc.). |
kubectl describe pods <name> |
Verbose output about a specific pod. |
kubectl get <res> --sort-by=<path> |
Sort output (e.g., --sort-by=.metadata.name). |
kubectl get pods --selector=<label> |
Filter by label (e.g., --selector=app=cassandra). |
kubectl diff -f <file.yaml> |
Compare local manifest vs. cluster state. |
kubectl top pod |
Show resource usage (CPU/Mem) for pods. |
4. Updating, Patching & Scaling
| Command | Description |
|---|---|
kubectl set image deployment/<dep> <container>=<img> |
Update container image (rolling update). |
kubectl rollout undo deployment/<dep> |
Rollback to previous revision. |
kubectl rollout status deployment/<dep> |
Watch rollout status. |
kubectl rollout restart deployment/<dep> |
Rolling restart. |
kubectl patch <res> <name> -p '<json>' |
Partially update a resource. |
kubectl edit <res> <name> |
Edit resource in your preferred editor. |
kubectl scale --replicas=<n> deployment/<dep> |
Scale replicas to a specific number. |
kubectl autoscale deployment <dep> --min=2 --max=10 |
Set up Horizontal Pod Autoscaler. |
5. Labels, Annotations & Taints
| Command | Description |
|---|---|
kubectl label pods <name> key=value |
Add a label. |
kubectl label pods <name> key- |
Remove a label. |
kubectl annotate pods <name> key=value |
Add an annotation. |
kubectl taint nodes <node> key=value:NoSchedule |
Add a taint to a node. |
6. Deleting Resources
| Command | Description |
|---|---|
kubectl delete -f <file.yaml> |
Delete resources defined in file. |
kubectl delete pod <name> --now |
Delete pod with no grace period. |
kubectl delete pods,services -l <label> |
Delete resources matching a label. |
kubectl -n <ns> delete pod --all |
Delete all pods in a namespace. |
7. Interacting with Running Pods
| Command | Description |
|---|---|
kubectl logs <pod> |
Dump pod logs (stdout). |
kubectl logs -f <pod> |
Stream logs. |
kubectl logs <pod> --previous |
Logs from a previously crashed container. |
kubectl exec -it <pod> -- /bin/sh |
Interactive shell access to a pod. |
kubectl exec <pod> -- <cmd> |
Run a single command in a pod. |
kubectl attach <pod> -i |
Attach to a running container. |
kubectl port-forward <pod> <local>:<remote> |
Forward local port to pod port. |
kubectl debug <pod> -it --image=busybox |
Create an ephemeral debug container in the pod. |
Copy Files:
# Local -> Remote
kubectl cp /tmp/foo_dir my-pod:/tmp/bar_dir
# Remote -> Local
kubectl cp my-pod:/tmp/foo /tmp/bar
8. Node & Cluster Management
| Command | Description |
|---|---|
kubectl top node |
Show metrics for all nodes. |
kubectl cordon <node> |
Mark node as unschedulable. |
kubectl drain <node> |
Evict all pods from a node (for maintenance). |
kubectl uncordon <node> |
Mark node as schedulable again. |
kubectl cluster-info |
Display addresses of master and services. |
kubectl get nodes --no-headers |
List only node names. |
9. Output Formatting
Use the -o or --output flag to change how data is printed.
| Flag | Description |
|---|---|
-o json |
JSON format. |
-o yaml |
YAML format. |
-o wide |
Plain text with extra columns (e.g., Node, IP). |
-o name |
Only resource names. |
-o jsonpath='{...}' |
Extract specific fields using JSONPath. |
-o custom-columns='Name:.metadata.name' |
Create a custom table. |
Useful JSONPath Examples:
# Get External IPs of all nodes
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
# List all images in the cluster
kubectl get pods -A -o custom-columns='IMAGE:.spec.containers[*].image'
10. Debugging & Verbosity
Control logging level with -v or --v.
--v=1: Reasonable default.--v=2: Steady state info (Recommended default).--v=4: Debug level.--v=8: Display HTTP request contents.
API Exploration:
kubectl api-resources --namespaced=true # List namespaced resources
kubectl api-resources --verbs=list,get # Resources that support list/get
11. Autocomplete Setup
Bash:
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
alias k=kubectl
complete -o default -F __start_kubectl k
Zsh:
source <(kubectl completion zsh)
echo '[[ $commands[kubectl] ]] && source <(kubectl completion zsh)' >> ~/.zshrc