| name | k8s-debug |
| description | Kubernetes troubleshooting and debugging workflows. Use when diagnosing pod failures, networking issues, resource problems, CrashLoopBackOff, ImagePullBackOff, pending pods, OOMKilled, or any cluster issues. Covers systematic debugging approaches and common failure patterns. |
Kubernetes Troubleshooting
Diagnostic Commands Cheatsheet
kubectl get nodes
kubectl get pods -A | grep -v Running
kubectl get events -A --sort-by='.lastTimestamp' | tail -30
kubectl get all -n <ns>
kubectl get events -n <ns> --sort-by='.lastTimestamp'
kubectl top pods -n <ns>
Pod Status Troubleshooting
CrashLoopBackOff
Symptoms: Pod restarts repeatedly, status shows CrashLoopBackOff
Diagnosis:
kubectl describe pod <pod> -n <ns> | grep -A10 "Last State"
kubectl logs <pod> -n <ns> --previous
kubectl get pod <pod> -n <ns> -o jsonpath='{.status.containerStatuses[0].lastState.terminated.reason}'
Common Causes:
- Application crash (check logs)
- OOMKilled (increase memory limits)
- Liveness probe too aggressive
- Missing dependencies/config
- Permission issues
Fixes:
kubectl set resources deployment/<name> -n <ns> --limits=memory=1Gi
kubectl edit deployment/<name> -n <ns>
kubectl exec -it <pod> -n <ns> -- /bin/sh
ImagePullBackOff
Symptoms: Pod stuck in ImagePullBackOff or ErrImagePull
Diagnosis:
kubectl describe pod <pod> -n <ns> | grep -A5 "Events"
Common Causes:
- Image doesn't exist (typo in name/tag)
- Private registry without imagePullSecrets
- Registry authentication failed
- Network issues to registry
Fixes:
docker pull <image>
kubectl get pod <pod> -n <ns> -o jsonpath='{.spec.imagePullSecrets}'
kubectl create secret docker-registry regcred \
--docker-server=<registry> \
--docker-username=<user> \
--docker-password=<pass> \
-n <ns>
kubectl patch deployment <name> -n <ns> -p '{"spec":{"template":{"spec":{"imagePullSecrets":[{"name":"regcred"}]}}}}'
Pending
Symptoms: Pod stuck in Pending state
Diagnosis:
kubectl describe pod <pod> -n <ns> | grep -A10 "Events"
Common Causes:
- Insufficient CPU/memory
- Node selector/affinity mismatch
- Taint without toleration
- PVC not bound
- Resource quota exceeded
Fixes:
kubectl describe nodes | grep -A5 "Allocated resources"
kubectl get nodes -o custom-columns=NAME:.metadata.name,TAINTS:.spec.taints
kubectl get pvc -n <ns>
kubectl describe resourcequota -n <ns>
CreateContainerConfigError
Symptoms: Pod in CreateContainerConfigError state
Diagnosis:
kubectl describe pod <pod> -n <ns> | grep -A5 "Warning"
Common Causes:
- ConfigMap doesn't exist
- Secret doesn't exist
- Volume mount issues
Fixes:
kubectl get configmap <name> -n <ns>
kubectl get secret <name> -n <ns>
kubectl get pod <pod> -n <ns> -o jsonpath='{.spec.volumes[*].name}'
OOMKilled
Symptoms: Container terminated with OOMKilled reason, exit code 137
Diagnosis:
kubectl describe pod <pod> -n <ns> | grep -A5 "Last State"
kubectl top pod <pod> -n <ns>
kubectl get pod <pod> -n <ns> -o jsonpath='{.spec.containers[0].resources}'
Fixes:
kubectl set resources deployment/<name> -n <ns> --limits=memory=2Gi
kubectl patch deployment <name> -n <ns> --type='json' -p='[
{"op":"replace","path":"/spec/template/spec/containers/0/resources/limits/memory","value":"2Gi"}
]'
Service & Networking Issues
Service Not Reaching Pods
Diagnosis:
kubectl get endpoints <service> -n <ns>
kubectl get svc <service> -n <ns> -o jsonpath='{.spec.selector}'
kubectl get pods -n <ns> --show-labels
kubectl run debug --rm -it --image=busybox -n <ns> -- wget -qO- <service>:<port>
Common Causes:
- Selector doesn't match pod labels
- No ready pods (readiness probe failing)
- Wrong port configuration
- Network policy blocking traffic
Fixes:
kubectl patch svc <service> -n <ns> -p '{"spec":{"selector":{"app":"correct-label"}}}'
kubectl get pods -n <ns> -o wide
kubectl describe pod <pod> -n <ns> | grep -A10 "Readiness"
DNS Issues
Diagnosis:
kubectl run dns-test --rm -it --image=busybox -- nslookup kubernetes.default
kubectl run dns-test --rm -it --image=busybox -- nslookup <service>.<namespace>.svc.cluster.local
kubectl get pods -n kube-system -l k8s-app=kube-dns
kubectl logs -n kube-system -l k8s-app=kube-dns
Ingress Not Working
Diagnosis:
kubectl describe ingress <name> -n <ns>
kubectl logs -n ingress-nginx -l app.kubernetes.io/name=ingress-nginx
kubectl get svc <backend-service> -n <ns>
kubectl get endpoints <backend-service> -n <ns>
Volume Issues
PVC Stuck in Pending
Diagnosis:
kubectl describe pvc <name> -n <ns>
kubectl get storageclass
kubectl get pv
Common Causes:
- No matching StorageClass
- StorageClass provisioner not working
- Zone/region mismatch
- Capacity exceeded
Volume Mount Failures
Diagnosis:
kubectl describe pod <pod> -n <ns> | grep -A10 "Volumes"
kubectl describe pod <pod> -n <ns> | grep -A10 "Mounts"
kubectl get events -n <ns> | grep -i volume
Node Issues
Node NotReady
Diagnosis:
kubectl describe node <node> | grep -A10 "Conditions"
kubectl get events --field-selector involvedObject.name=<node>
journalctl -u kubelet -n 100
Common Causes:
- Kubelet not running
- Network issues
- Disk pressure
- Memory pressure
- Too many pods
Drain & Cordon
kubectl cordon <node>
kubectl drain <node> --ignore-daemonsets --delete-emptydir-data
kubectl uncordon <node>
Resource Debugging
Check Resource Pressure
kubectl top nodes
kubectl describe nodes | grep -A5 "Allocated resources"
kubectl top pods -n <ns> --sort-by=memory
kubectl top pods -n <ns> --sort-by=cpu
kubectl describe resourcequota -n <ns>
kubectl describe limitrange -n <ns>
Find Resource Hogs
kubectl top pods -A --sort-by=memory | head -20
kubectl top pods -A --sort-by=cpu | head -20
kubectl get pods -A -o json | jq -r '.items[] | select(.spec.containers[].resources.limits == null) | .metadata.namespace + "/" + .metadata.name'
Debug Containers
Ephemeral Debug Container (k8s 1.25+)
kubectl debug -it <pod> -n <ns> --image=busybox --target=<container>
kubectl debug -it <pod> -n <ns> --image=nicolaka/netshoot --target=<container>
Debug Pod Copy
kubectl debug <pod> -n <ns> --copy-to=debug-pod --container=<container> -- sleep infinity
kubectl exec -it debug-pod -n <ns> -- /bin/sh
Standalone Debug Pod
kubectl run netshoot --rm -it --image=nicolaka/netshoot -n <ns> -- /bin/bash
curl -v http://<service>:<port>/
nslookup <service>
ping <pod-ip>
traceroute <service>
nc -zv <service> <port>
Log Analysis
kubectl logs -f <pod> -n <ns>
kubectl logs <pod> -n <ns> --all-containers
kubectl logs <pod> -n <ns> --previous
kubectl logs <pod> -n <ns> --since=1h
kubectl logs <pod> -n <ns> --since-time="2024-01-01T00:00:00Z"
kubectl logs <pod> -n <ns> --tail=100
kubectl logs -l app=<label> -n <ns> --all-containers
kubectl logs <pod> -n <ns> --timestamps
Events
kubectl get events -n <ns> --sort-by='.lastTimestamp'
kubectl get events -n <ns> -w
kubectl get events -n <ns> --field-selector type=Warning
kubectl get events -n <ns> --field-selector involvedObject.name=<pod>
Quick Diagnostic Script
#!/usr/bin/env bash
NS="${1:?Usage: $0 <namespace>}"
echo "=== Namespace: $NS ==="
echo ""
echo "--- Pod Status ---"
kubectl get pods -n "$NS" -o wide
echo ""
echo "--- Non-Running Pods ---"
kubectl get pods -n "$NS" | grep -v Running | grep -v Completed
echo ""
echo "--- Recent Events ---"
kubectl get events -n "$NS" --sort-by='.lastTimestamp' | tail -20
echo ""
echo "--- Resource Usage ---"
kubectl top pods -n "$NS" 2>/dev/null || echo "Metrics not available"
echo ""
echo "--- Services ---"
kubectl get svc -n "$NS"
echo ""
echo "--- Endpoints ---"
kubectl get endpoints -n "$NS"
Common Fixes Quick Reference
| Problem | Quick Fix |
|---|
| CrashLoopBackOff | kubectl logs <pod> --previous then fix app |
| ImagePullBackOff | Verify image name, create imagePullSecret |
| Pending (resources) | Scale down other pods or add nodes |
| Pending (PVC) | Check StorageClass and provisioner |
| OOMKilled | Increase memory limits |
| Readiness failing | Check probe endpoint, increase timeout |
| No endpoints | Fix service selector to match pod labels |
| DNS failure | Check CoreDNS pods in kube-system |
| Permission denied | Check RBAC, ServiceAccount |