kubectl cheatsheet — 40 commands every Kubernetes user needs
The kubectl commands you will actually use daily. Organized by task, with real examples and copy-paste ready.
kubectl has hundreds of commands. Only a handful get used daily. This cheatsheet covers the 40 that matter, organized by task.
Bookmark this page. Copy commands as needed.
Context and cluster info
# See current context (which cluster kubectl targets)
kubectl config current-context
# List all contexts
kubectl config get-contexts
# Switch context
kubectl config use-context <context-name>
# See cluster info
kubectl cluster-info
# List all API resources
kubectl api-resources
Getting resources
# List pods in current namespace
kubectl get pods
# List pods in specific namespace
kubectl get pods -n <namespace>
# List pods in ALL namespaces
kubectl get pods --all-namespaces
# short: kubectl get pods -A
Example output listing pods across every namespace:
# Wide output (shows node, IP)
kubectl get pods -o wide
# Watch pods in real-time
kubectl get pods -w
# List all resources in namespace
kubectl get all -n <namespace>
Describing resources (debugging)
# Full details of a resource
kubectl describe pod <pod-name>
# Same for other resources
kubectl describe deployment <name>
kubectl describe service <name>
kubectl describe node <name>
# See events (recent cluster activity)
kubectl get events --sort-by='.lastTimestamp'
# Events for specific resource
kubectl get events --field-selector involvedObject.name=<pod-name>
Example — describing a node returns pages of detail about capacity, taints, and allocated resources:
For a broader view, list every resource in the kube-system namespace:
Logs
# See logs of a pod
kubectl logs <pod-name>
# Follow logs live (like tail -f)
kubectl logs -f <pod-name>
# Logs from a specific container in multi-container pod
kubectl logs <pod-name> -c <container-name>
# Last 100 lines
kubectl logs --tail 100 <pod-name>
# Previous crashed container's logs
kubectl logs --previous <pod-name>
# Logs from all pods matching a label
kubectl logs -l app=myapp --tail=50
Executing commands in pods
# Interactive shell in a pod
kubectl exec -it <pod-name> -- bash
# If bash isn't available:
kubectl exec -it <pod-name> -- sh
# Run one-off command
kubectl exec <pod-name> -- ls /app
# Exec into specific container in multi-container pod
kubectl exec -it <pod-name> -c <container-name> -- bash
Port forwarding
# Forward local port to pod port
kubectl port-forward pod/<pod-name> 8080:80
# Forward to a service
kubectl port-forward svc/<service-name> 8080:80
# Forward on specific interface (allow external access)
kubectl port-forward --address 0.0.0.0 svc/<service-name> 8080:80
Applying and deleting resources
# Apply a YAML file
kubectl apply -f manifest.yaml
# Apply all YAMLs in a folder
kubectl apply -f ./manifests/
# Apply from URL
kubectl apply -f https://raw.githubusercontent.com/user/repo/main/manifest.yaml
# Delete by file
kubectl delete -f manifest.yaml
# Delete by resource
kubectl delete pod <pod-name>
kubectl delete deployment <name>
kubectl delete service <name>
# Delete ALL pods matching label
kubectl delete pods -l app=myapp
# Force delete stuck pod (rarely needed)
kubectl delete pod <pod-name> --grace-period=0 --force
Editing resources live
# Open resource in editor for edit
kubectl edit deployment <name>
# Set an image (triggers rollout)
kubectl set image deployment/<name> <container>=<new-image>
# Scale a deployment
kubectl scale deployment <name> --replicas=5
# Rollout status
kubectl rollout status deployment/<name>
# Rollout history
kubectl rollout history deployment/<name>
# Undo last rollout
kubectl rollout undo deployment/<name>
# Restart deployment (triggers new pods)
kubectl rollout restart deployment/<name>
Resource usage
# Node CPU/memory usage
kubectl top nodes
# Pod CPU/memory usage
kubectl top pods
# Pods in specific namespace
kubectl top pods -n <namespace>
Note: requires metrics-server installed in cluster.
Debugging permissions (RBAC)
# Can current user do X?
kubectl auth can-i create pods
kubectl auth can-i delete deployments -n prod
# Can specific user/SA do X?
kubectl auth can-i list pods --as=<user>
kubectl auth can-i list pods --as=system:serviceaccount:default:cicd
Output is a simple yes / no — perfect for scripting RBAC checks.
Getting output in different formats
# YAML output
kubectl get pod <name> -o yaml
# JSON output
kubectl get pod <name> -o json
# Custom columns
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase
# JSONPath (extract specific field)
kubectl get pods -o jsonpath='{.items[*].metadata.name}'
# Just names (space-separated)
kubectl get pods -o name
Namespaces
# List namespaces
kubectl get namespaces
# Create namespace
kubectl create namespace <name>
# Delete namespace (deletes ALL resources in it)
kubectl delete namespace <name>
# Set default namespace for current context
kubectl config set-context --current --namespace=<name>
ConfigMaps and Secrets
# Create ConfigMap from file
kubectl create configmap myconfig --from-file=config.yaml
# Create ConfigMap from literals
kubectl create configmap myconfig --from-literal=key1=value1 --from-literal=key2=value2
# Create Secret from literals
kubectl create secret generic mysecret --from-literal=password=abc123
# Create Docker registry credentials
kubectl create secret docker-registry regcred \
--docker-server=<url> \
--docker-username=<user> \
--docker-password=<pass>
# View decoded secret
kubectl get secret <name> -o jsonpath='{.data.password}' | base64 -d
Labels and selectors
# Label a resource
kubectl label pod <pod-name> environment=production
# Remove a label
kubectl label pod <pod-name> environment-
# Get resources matching label
kubectl get pods -l app=myapp
# Multiple label filters
kubectl get pods -l 'app=myapp,environment=prod'
# NOT equal
kubectl get pods -l 'environment!=dev'
Node management
# Cordon a node (prevent new pods)
kubectl cordon <node-name>
# Drain node (move pods off)
kubectl drain <node-name> --ignore-daemonsets
# Uncordon (allow scheduling again)
kubectl uncordon <node-name>
Common one-liners
# Delete all pods (they'll be recreated by Deployment)
kubectl delete pods --all -n <namespace>
# Restart all deployments in namespace
kubectl rollout restart deployment -n <namespace>
# Get all pods with high restart count
kubectl get pods --all-namespaces -o json | jq '.items[] | select(.status.containerStatuses[]?.restartCount > 5) | .metadata.name'
# See what's using the most memory
kubectl top pods --all-namespaces --sort-by=memory
# Watch pod status change
watch kubectl get pods
Alias for daily productivity
Add to your shell profile (.zshrc or .bashrc):
alias k=kubectl
alias kgp='kubectl get pods'
alias kgs='kubectl get svc'
alias kgd='kubectl get deployments'
alias kgn='kubectl get nodes'
alias kdp='kubectl describe pod'
alias kaf='kubectl apply -f'
alias kdf='kubectl delete -f'
alias kl='kubectl logs -f'
alias ke='kubectl exec -it'
Reload shell: source ~/.zshrc. Now typing kgp is kubectl get pods.
Enable autocomplete
For bash:
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
For zsh:
source <(kubectl completion zsh)
echo "source <(kubectl completion zsh)" >> ~/.zshrc
Now tab-completion works on kubectl commands, resources, namespaces, and more.
Config file locations
# Default location
~/.kube/config
# Point kubectl to specific config
export KUBECONFIG=/path/to/config
# Combine multiple configs
export KUBECONFIG=~/.kube/config:~/.kube/config-other
Common gotchas
“connection refused” — cluster not running (minikube start) or wrong context (kubectl config current-context)
“no resources found” — check namespace (kubectl get pods -A)
“error from server: forbidden” — RBAC issue (kubectl auth can-i)
“unable to connect to the server: x509” — expired certificates, common with older kubeconfig files
Slow responses — try --server=<direct-url> to bypass load balancer, or check kubectl cluster-info
Reproduce this yourself
Free Kubernetes environment: https://killercoda.com/playgrounds/scenario/kubernetes
Try each command above in the browser terminal. No local install needed.
Bottom line
Master these 40 commands and 90% of daily Kubernetes work is easier. The other 10% is looking up specific things when needed.
Bookmark this page. Use it as reference. Kubectl fluency comes from repetition, not memorization.
DevOps YAML Pack
36 production-ready configs — Kubernetes, Docker Compose, GitHub Actions, Terraform, Helm, Ansible. Every file heavily commented. Copy, paste, ship.
Get the pack — ₹499 →