Essential Kubernetes Commands Reference
Master the most used Kubernetes commands for daily operations. Complete kubectl reference for pods, deployments, services, debugging, and cluster management.
π‘ Quick Answer: Start every debug session with
kubectl get pods(status),kubectl describe pod <name>(events/errors),kubectl logs <pod> [-c container] [--previous](why it crashed), andkubectl exec -it <pod> -- sh(shell access). For everything else β deployments, networking, storage, RBAC β this reference below.
The Problem
Kubernetes troubleshooting means knowing which of dozens of kubectl subcommands surfaces the information you actually need β the wrong one (or the wrong flag) sends you searching instead of debugging.
The Solution
Pod Commands
# List pods
kubectl get pods # Current namespace
kubectl get pods -A # All namespaces
kubectl get pods -o wide # Show node, IP
kubectl get pods -w # Watch for changes
# Create / Delete
kubectl run nginx --image=nginx:1.27
kubectl delete pod nginx
kubectl delete pod nginx --grace-period=0 --force
# Logs
kubectl logs my-pod
kubectl logs my-pod -c sidecar # Specific container
kubectl logs my-pod --previous # Previous crash
kubectl logs my-pod -f --tail=100 # Follow, last 100 lines
kubectl logs -l app=web # By label
# Execute
kubectl exec -it my-pod -- bash
kubectl exec my-pod -- cat /etc/config/app.yaml
# Copy files
kubectl cp my-pod:/tmp/dump.sql ./dump.sql
kubectl cp ./config.yaml my-pod:/etc/config/
# Port forward
kubectl port-forward my-pod 8080:80
kubectl port-forward svc/my-service 8080:80Deployment Commands
# Create
kubectl create deployment web --image=nginx --replicas=3
# Scale
kubectl scale deployment web --replicas=5
# Update image
kubectl set image deployment/web nginx=nginx:1.28
# Rollout
kubectl rollout status deployment/web
kubectl rollout history deployment/web
kubectl rollout undo deployment/web
kubectl rollout undo deployment/web --to-revision=2
kubectl rollout restart deployment/web
# Autoscale
kubectl autoscale deployment web --min=2 --max=10 --cpu-percent=80Resource Management
# Get resources
kubectl get all # Pods, services, deployments
kubectl get nodes
kubectl get namespaces
kubectl get events --sort-by='.lastTimestamp'
# Describe (detailed info + events)
kubectl describe pod my-pod
kubectl describe node worker-1
# Delete
kubectl delete -f manifest.yaml
kubectl delete deployment,svc,cm -l app=web
# Apply / Diff
kubectl apply -f manifest.yaml
kubectl diff -f manifest.yaml # Preview changes
# Resource usage
kubectl top nodes
kubectl top pods --sort-by=memoryContext & Config
# Switch namespace
kubectl config set-context --current --namespace=production
# Switch cluster
kubectl config use-context my-cluster
# View config
kubectl config view
kubectl config get-contexts
kubectl cluster-infoAdvanced
# JSON path
kubectl get pods -o jsonpath='{.items[*].metadata.name}'
kubectl get nodes -o jsonpath='{range .items[*]}{.metadata.name}{" "}{.status.addresses[0].address}{"
"}{end}'
# Custom columns
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName
# Label operations
kubectl label pod my-pod env=prod
kubectl get pods -l env=prod,tier=frontend
# Auth check
kubectl auth can-i create deployments
kubectl auth can-i --list --as=system:serviceaccount:default:my-sa
# API resources
kubectl api-resources # List all resource types
kubectl explain pod.spec.containers # DocumentationDebug with Ephemeral Containers
kubectl debug -it my-pod --image=busybox --target=my-container # shares process namespace
kubectl debug -it my-pod --image=nicolaka/netshoot # network debugging tools
kubectl debug node/my-node -it --image=busybox # debug a node directlyService, Ingress, and Storage Debugging
# Service DNS and connectivity
kubectl get endpoints my-service
kubectl run tmp --image=nicolaka/netshoot --rm -it -- \
sh -c "curl http://my-service:8080; nslookup my-service"
# Ingress
kubectl describe ingress my-ingress
kubectl logs -n ingress-nginx -l app.kubernetes.io/component=controller
# PersistentVolumeClaims
kubectl describe pvc my-pvc
kubectl exec my-pod -- df -h
kubectl exec my-pod -- mount | grep my-volumeRBAC Debugging
kubectl auth can-i get pods
kubectl auth can-i get pods --as=system:serviceaccount:default:my-sa
kubectl auth can-i --list --as=system:serviceaccount:default:my-saUseful One-Liners
# All pods not Running/Succeeded, across every namespace
kubectl get pods -A --field-selector=status.phase!=Running,status.phase!=Succeeded
# Every image currently running in the cluster
kubectl get pods -A -o jsonpath='{range .items[*]}{.spec.containers[*].image}{"\n"}{end}' | sort -u
# Pods sorted by restart count β find what's flapping
kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'
# Force-delete a pod stuck in Terminating
kubectl delete pod my-pod --grace-period=0 --forcegraph TD
A[kubectl] --> B[get - list resources]
A --> C[describe - detailed info]
A --> D[logs - container output]
A --> E[exec - run commands]
A --> F[apply - create/update]
A --> G[delete - remove]
A --> H[rollout - manage deployments]Frequently Asked Questions
kubectl get vs describe?
get shows a summary table. describe shows full details including events, conditions, and related resources. Use describe when troubleshooting.
How to see all resources in a namespace?
kubectl get all -n my-namespace shows common resources. For everything: kubectl api-resources --verbs=list -o name | xargs -n1 kubectl get -n my-namespace --ignore-not-found
Best Practices
describebeforelogsβ events (ImagePullBackOff, FailedScheduling, OOMKilled) often explain the problem before you need a single log line--previouson logs is easy to forget β after a crash-restart, plainkubectl logsshows the new containerβs (empty) logs, not the crash- Use
--field-selector/-lto filter at the server, notgrepon the client β cheaper on large clusters and works with-w/watch kubectl auth can-i --as=<sa>to debug RBAC as a specific ServiceAccount instead of guessing from the Role/RoleBinding YAML- Force-delete (
--grace-period=0 --force) is a last resort β it skips graceful termination, so reach for it only on pods genuinely stuck Terminating
Key Takeaways
getβdescribeβlogsβexecis the standard debugging funnel, in that orderkubectl debug(ephemeral containers) is the modern way to attach debugging tools to a pod or node without restarting it- RBAC issues are fastest to confirm with
kubectl auth can-i --as=<serviceaccount>, not by re-reading RoleBindings - One-liners built on
--field-selectorandjsonpath/custom-columnsturn ad-hocgrep-on-getinto fast, reusable queries kubectl explain <resource>.<field>is built-in field-level API documentation β faster than searching docs for an obscure spec field

Recommended
Kubernetes Recipes β The Complete Book100+ production-ready patterns with detailed explanations, best practices, and copy-paste YAML. Everything in one place.
Get the Book βLearn by Doing
CopyPasteLearn β Hands-on Cloud & DevOps CoursesMaster Kubernetes, Ansible, Terraform, and MLOps with interactive, copy-paste-run lessons. Start free.
Browse Courses βπ Deepen Your Skills β Hands-on Courses
Courses by CopyPasteLearn.com β Learn IT by Doing
