DevOps & Cloud Infrastructure Reference

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.

Networking & AccessSAFE

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 default
3 execution stepsView Recipe
Debug & InspectionCAUTION

How 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/bash
3 execution stepsView Recipe
TroubleshootingSAFE

How 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 default
3 execution stepsView Recipe
TroubleshootingDESTRUCTIVE

How 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 --force
3 execution stepsView Recipe
Deployments & WorkloadsSAFE

How 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 default
3 execution stepsView Recipe
Monitoring & ResourcesSAFE

How 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=memory
3 execution stepsView Recipe
Config & SecretsSAFE

How 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 default
3 execution stepsView Recipe
Cluster & NodesDESTRUCTIVE

How 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 --force
3 execution stepsView Recipe
Cluster & NodesSAFE

How 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-namespace
3 execution stepsView Recipe
Debug & InspectionSAFE

How 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=100
3 execution stepsView Recipe

Kubernetes Pod Troubleshooting Decision Matrix

Common failure states and recommended diagnostic commands

Pod StateProbable Root CauseDiagnostic CommandRecommended Fix
CrashLoopBackOffApp threw fatal exception or OOM killedkubectl logs --previousFix app code, verify env secrets, bump memory limits
ImagePullBackOffRegistry 404, invalid tag, missing imagePullSecretskubectl describe podVerify Docker repository name & registry auth token
PendingInsufficient CPU/memory on nodes, taint mismatchkubectl describe podAdd cluster worker nodes or reduce resource requests
Terminating (Stuck)Dead node, volume unmount lock, blocking finalizerkubectl delete pod --forceForce delete with grace-period=0 or patch finalizers
OOMKilled (Exit 137)Process exceeded cgroup memory limitkubectl top podIncrease resources.limits.memory in Deployment YAML

Frequently Asked Questions About Kubernetes kubectl Commands

Frequently Asked Questions

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.