Kubernetes kubectl Recipes & Solutions
Everyday battle-tested commands for Kubernetes developers, SREs, and cluster administrators. Port-forward internal services, exec into debugging shells, resolve CrashLoopBackOff failures, and execute rolling restarts with zero downtime.
How to Port-Forward a Kubernetes Pod or Service to Localhost
Forward local workstation TCP ports directly to a Kubernetes Service or Pod without exposing it via Ingress or LoadBalancer.
kubectl port-forward svc/my-service 8080:80 -n defaultHow to Exec into a Kubernetes Pod with an Interactive Bash Shell
Open an interactive terminal shell directly inside a running Kubernetes container for inspection, debugging, and testing.
kubectl exec -it my-pod -n default -- /bin/bashHow to Debug and Fix CrashLoopBackOff Pods in Kubernetes
Systematic diagnosis workflow to inspect exit codes, crash logs, OOMKilled events, and liveness probe failures in failing pods.
kubectl logs my-pod -n default --previous && kubectl describe pod my-pod -n defaultHow to Force Delete a Kubernetes Pod Stuck in Terminating State
Forcefully remove an unkillable pod stuck in Terminating status due to dead nodes, NFS volume locks, or hanging finalizers.
kubectl delete pod my-pod -n default --grace-period=0 --forceHow to Restart a Kubernetes Deployment with Zero Downtime
Trigger a clean rolling restart across all replica pods of a deployment without changing image tags or experiencing service downtime.
kubectl rollout restart deployment/my-app -n defaultHow to Check CPU and Memory Resource Usage of Pods and Nodes
Monitor live CPU and RAM consumption across all pods and cluster nodes using the Kubernetes metrics-server API.
kubectl top pods -A --sort-by=memoryHow to Create a Kubernetes Generic Secret from an .env File
Directly import local environment variable files (.env) into encrypted Kubernetes Secret objects without manual base64 encoding.
kubectl create secret generic app-secrets --from-env-file=.env -n defaultHow to Safely Drain a Kubernetes Node for Maintenance
Evict all workloads from a worker node in an orderly fashion before rebooting, upgrading the kernel, or resizing VM capacity.
kubectl drain node-1 --ignore-daemonsets --delete-emptydir-data --forceHow to Switch Kubernetes Context and Default Namespace
Quickly switch between Kubernetes clusters (contexts) and change your default active namespace without typing -n every time.
kubectl config use-context prod-cluster && kubectl config set-context --current --namespace=my-namespaceHow to Stream Live Logs from Kubernetes Pods and Deployments
Stream, tail, and aggregate real-time stdout/stderr log output across multiple pod replicas matching a label selector.
kubectl logs -f -l app=my-app -n default --max-log-requests=10 --tail=100Kubernetes Pod Troubleshooting Decision Matrix
Common failure states and recommended diagnostic commands
| Pod State | Probable Root Cause | Diagnostic Command | Recommended Fix |
|---|---|---|---|
| CrashLoopBackOff | App threw fatal exception or OOM killed | kubectl logs --previous | Fix app code, verify env secrets, bump memory limits |
| ImagePullBackOff | Registry 404, invalid tag, missing imagePullSecrets | kubectl describe pod | Verify Docker repository name & registry auth token |
| Pending | Insufficient CPU/memory on nodes, taint mismatch | kubectl describe pod | Add cluster worker nodes or reduce resource requests |
| Terminating (Stuck) | Dead node, volume unmount lock, blocking finalizer | kubectl delete pod --force | Force delete with grace-period=0 or patch finalizers |
| OOMKilled (Exit 137) | Process exceeded cgroup memory limit | kubectl top pod | Increase resources.limits.memory in Deployment YAML |
Frequently Asked Questions About Kubernetes kubectl Commands
Frequently Asked Questions
Everything you need to know regarding specifications, syntax, and security best practices.
First run `kubectl logs <pod> -n <ns> --previous` to inspect the exit code and application stack trace immediately before it crashed. Next, run `kubectl describe pod <pod> -n <ns>` to inspect Kubelet events for OOMKilled (Exit Code 137) or liveness probe failures.