kubectl Cheat Sheet — Essential Commands for Kubernetes 2026
Advertisement
Introduction
Why This Matters
kubectl is the primary CLI for interacting with any Kubernetes cluster — whether it's EKS, GKE, AKS, or a local kind cluster. Mastering kubectl commands dramatically speeds up debugging, deployment, and cluster administration. This cheat sheet covers the commands you'll use every day in 2026, organized by task so you can find exactly what you need in seconds.
Cluster and Context Management
# View current context
kubectl config current-context
# List all contexts
kubectl config get-contexts
# Switch context
kubectl config use-context my-cluster
# View cluster info
kubectl cluster-info
# Check cluster version
kubectl version --short
# Set default namespace for current context
kubectl config set-context --current --namespace=productionWorking With Namespaces
# List all namespaces
kubectl get namespaces
# Create a namespace
kubectl create namespace staging
# Delete a namespace
kubectl delete namespace staging
# Get all resources in a namespace
kubectl get all -n staging
# Get resources across ALL namespaces
kubectl get pods -APod Commands
# List pods in current namespace
kubectl get pods
# List pods with node and IP info
kubectl get pods -o wide
# Describe a pod (events, resource limits, image)
kubectl describe pod my-pod
# Get pod logs
kubectl logs my-pod
# Follow logs in real time
kubectl logs -f my-pod
# Logs from a specific container in a multi-container pod
kubectl logs my-pod -c my-container
# Previous container logs (after crash)
kubectl logs my-pod --previous
# Execute a command inside a pod
kubectl exec -it my-pod -- /bin/bash
# Execute in a specific container
kubectl exec -it my-pod -c my-container -- /bin/sh
# Copy files to/from a pod
kubectl cp my-pod:/app/logs/app.log ./app.log
kubectl cp ./config.yaml my-pod:/app/config.yaml
# Delete a pod (it will be recreated by its controller)
kubectl delete pod my-pod
# Force delete a stuck pod
kubectl delete pod my-pod --grace-period=0 --forceDeployments and ReplicaSets
# List deployments
kubectl get deployments
# Create a deployment
kubectl create deployment nginx --image=nginx:1.25
# Scale a deployment
kubectl scale deployment nginx --replicas=5
# Update a deployment image
kubectl set image deployment/nginx nginx=nginx:1.26
# View rollout status
kubectl rollout status deployment/nginx
# View rollout history
kubectl rollout history deployment/nginx
# Undo the last rollout
kubectl rollout undo deployment/nginx
# Undo to a specific revision
kubectl rollout undo deployment/nginx --to-revision=2
# Pause a rollout
kubectl rollout pause deployment/nginx
# Resume a paused rollout
kubectl rollout resume deployment/nginx
# Delete a deployment
kubectl delete deployment nginxServices and Networking
# List services
kubectl get services
# Expose a deployment as a ClusterIP service
kubectl expose deployment nginx --port=80 --target-port=80
# Expose as a NodePort service
kubectl expose deployment nginx --type=NodePort --port=80
# Port-forward to access a pod locally
kubectl port-forward pod/my-pod 8080:80
# Port-forward a service
kubectl port-forward svc/my-service 8080:80
# Get endpoints for a service
kubectl get endpoints my-service
# View DNS name for a service
kubectl run -it --rm debug --image=busybox --restart=Never -- nslookup my-serviceConfigMaps and Secrets
# Create a ConfigMap from literal values
kubectl create configmap app-config \
--from-literal=APP_ENV=production \
--from-literal=LOG_LEVEL=info
# Create ConfigMap from a file
kubectl create configmap app-config --from-file=config.yaml
# Create a Secret from literals
kubectl create secret generic db-credentials \
--from-literal=username=admin \
--from-literal=password=s3cr3t
# Create a TLS secret
kubectl create secret tls my-tls \
--cert=tls.crt --key=tls.key
# Decode a secret value
kubectl get secret db-credentials -o jsonpath='{.data.password}' | base64 --decodeResource Inspection and Output Formats
# Get resource as YAML
kubectl get deployment nginx -o yaml
# Get resource as JSON
kubectl get pod my-pod -o json
# Use jsonpath to extract a field
kubectl get pod my-pod -o jsonpath='{.status.podIP}'
# List pod names only
kubectl get pods -o jsonpath='{.items[*].metadata.name}'
# Custom columns output
kubectl get pods -o custom-columns='NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName'
# Watch resources for changes
kubectl get pods -w
# Sort by field
kubectl get pods --sort-by='.metadata.creationTimestamp'Debugging and Troubleshooting
# Get events in a namespace (sorted by time)
kubectl get events --sort-by='.lastTimestamp'
# Describe a node
kubectl describe node my-node
# Check resource usage (requires metrics-server)
kubectl top pods
kubectl top nodes
# Run a temporary debug pod
kubectl run -it --rm debug --image=busybox --restart=Never -- sh
# Run a debug pod with the same namespace as target
kubectl debug -it my-pod --image=busybox --target=my-container
# Check pod resource requests vs limits
kubectl describe pod my-pod | grep -A 5 "Limits\|Requests"Applying and Managing Manifests
# Apply a manifest file
kubectl apply -f deployment.yaml
# Apply all manifests in a directory
kubectl apply -f ./k8s/
# Apply from a URL
kubectl apply -f https://raw.githubusercontent.com/org/repo/main/deploy.yaml
# Diff current vs desired state
kubectl diff -f deployment.yaml
# Delete resources defined in a file
kubectl delete -f deployment.yaml
# Dry run (server-side)
kubectl apply -f deployment.yaml --dry-run=server
# Label a resource
kubectl label pod my-pod env=production
# Annotate a resource
kubectl annotate pod my-pod description="main web server"Common Mistakes
- Not specifying namespaces — resources in other namespaces are invisible without
-n namespaceor-A; set a default context namespace to avoid confusion. - Using
kubectl delete podto "redeploy" — deleting a pod just recreates it with the same image; update the Deployment to roll out new images. - Forgetting
--previouson logs — after a CrashLoopBackOff, logs show the current (empty) container; add--previousto see the crashed container's output. - Port-forwarding to a pod instead of a service — if the pod restarts, the tunnel breaks; forward to the Service for stability.
- Skipping
kubectl diff— always diff before applying changes in production to avoid accidental deletions.
Best Practices
- Use aliases — add
alias k=kubectlandalias kns='kubectl config set-context --current --namespace'to your shell profile. - Install kubectl plugins —
krewis the plugin manager; popular plugins includekubectl neat,kubectl tree, andkubectl ctx. - Use
--dry-run=server— server-side dry run catches admission webhook rejections that client-side dry run misses. - Pipe to
grepandjq— combine-o json | jqfor complex field extraction instead of complex jsonpath expressions. - Use
kubectl explain—kubectl explain deployment.spec.strategygives inline API documentation without leaving the terminal.
Key Takeaways
kubectl config use-contextswitches between clusters;kubectl config set-context --current --namespacesets the default namespace.kubectl logs --previousretrieves the logs from a crashed container — essential for debugging CrashLoopBackOff errors.kubectl rollout undo deployment/namereverts to the previous ReplicaSet, enabling instant rollback without redeploying.kubectl port-forward svc/name 8080:80creates a secure tunnel from your local machine to a Kubernetes Service.kubectl get pods -o jsonpathandkubectl get pods -o custom-columnsprovide powerful, scriptable output formats.kubectl diff -f manifest.yamlshows what will change before applying — a critical safety check for production changes.kubectl top podsandkubectl top nodesshow live CPU and memory usage when the metrics-server addon is installed.- The
krewplugin manager extends kubectl with tools likectx,ns,tree, andneatfor faster cluster navigation.
Advertisement
Related reading
AI Tools for DevOps — Generate Dockerfiles, CI/CD Pipelines, and Kubernetes Manifests5 min readKubernetes Guide 2026 — Deploy, Scale, and Manage Containers in Production5 min readHashiCorp Vault Secrets Management 2026 — Never Hardcode Secrets Again6 min readDevOps Engineer Roadmap 2026 — From Zero to $150K+ in 18 Months9 min readThe 12-Factor App in 2026 — Cloud-Native Best Practices for Modern Backend Systems10 min readDocker Guide 2026 — Containerize Node.js, Python, and Next.js Apps4 min read