Kubernetes Troubleshooting Guide¶
Comprehensive guide to diagnosing and resolving common Kubernetes issues.
Troubleshooting Workflow¶
1. Identify the Problem
↓
2. Check Pod Status
↓
3. View Logs
↓
4. Describe Resources
↓
5. Check Events
↓
6. Verify Configuration
↓
7. Test Connectivity
↓
8. Fix and Verify
Pod Issues¶
Pod Stuck in Pending¶
Symptoms:
Diagnosis:
# Describe pod
kubectl describe pod myapp
# Check events
kubectl get events --sort-by='.lastTimestamp'
# Check node resources
kubectl top nodes
kubectl describe nodes
Common Causes: 1. Insufficient Resources
Solution: Add nodes or reduce resource requests-
Node Selector Mismatch
Solution: Fix node selector or add label to nodes -
PVC Not Bound
Solution: Check PVC status and storage class
Pod Stuck in ImagePullBackOff¶
Symptoms:
Diagnosis:
Common Causes: 1. Image Doesn't Exist
Solution: Verify image name and tag-
Authentication Required
Solution: Add imagePullSecrets -
Network Issues
Pod Stuck in CrashLoopBackOff¶
Symptoms:
Diagnosis:
# View logs
kubectl logs myapp
kubectl logs myapp --previous
# Describe pod
kubectl describe pod myapp
# Check exit code
kubectl get pod myapp -o jsonpath='{.status.containerStatuses[0].lastState.terminated.exitCode}'
Common Causes: 1. Application Error
-
Missing Dependencies
Solution: Verify ConfigMap/Secret values -
Liveness Probe Failing
Pod in Error State¶
Diagnosis:
Common Causes: 1. Init Container Failed
-
Volume Mount Issues
-
Security Context Issues
Service Issues¶
Service Not Accessible¶
Diagnosis:
# Check service
kubectl get svc myapp
kubectl describe svc myapp
# Check endpoints
kubectl get endpoints myapp
# Test from within cluster
kubectl run test --rm -it --image=busybox --restart=Never -- wget -O- http://myapp
Common Causes: 1. No Endpoints
Solution: Check pod selector matches-
Wrong Port
-
Readiness Probe Failing
LoadBalancer Pending¶
Symptoms:
Diagnosis:
Common Causes: 1. Cloud Provider Not Configured - Verify cloud controller manager is running - Check cloud provider credentials
- Quota Exceeded
- Check cloud provider quotas
-
Verify billing is active
-
Network Configuration
- Check VPC/subnet configuration
- Verify security groups
Networking Issues¶
Pod-to-Pod Communication¶
Test Connectivity:
# Get pod IPs
kubectl get pods -o wide
# Test from one pod to another
kubectl exec -it pod1 -- ping <pod2-ip>
kubectl exec -it pod1 -- curl http://<pod2-ip>:8080
Common Issues: 1. Network Policy Blocking
- CNI Plugin Issues
DNS Issues¶
Test DNS:
# Test DNS resolution
kubectl run test --rm -it --image=busybox --restart=Never -- nslookup kubernetes.default
# Test service DNS
kubectl run test --rm -it --image=busybox --restart=Never -- nslookup myapp.default.svc.cluster.local
# Check CoreDNS
kubectl get pods -n kube-system -l k8s-app=kube-dns
kubectl logs -n kube-system -l k8s-app=kube-dns
Common Issues: 1. CoreDNS Not Running
-
DNS Configuration
-
Pod DNS Policy
Storage Issues¶
PVC Stuck in Pending¶
Diagnosis:
Common Causes: 1. No Storage Class
Solution: Create or specify storage class-
No Available PV
Solution: Create PV or use dynamic provisioning -
Access Mode Mismatch
Volume Mount Failures¶
Diagnosis:
Common Issues: 1. Permission Denied
- Volume Not Found
Node Issues¶
Node NotReady¶
Diagnosis:
kubectl get nodes
kubectl describe node <node-name>
# Check kubelet
ssh <node>
systemctl status kubelet
journalctl -u kubelet -f
Common Causes: 1. Kubelet Not Running
-
Disk Pressure
-
Memory Pressure
Node Disk Pressure¶
Diagnosis:
Solution:
# Clean up images
docker system prune -a
# Clean up logs
journalctl --vacuum-time=3d
# Clean up pods
kubectl delete pods --field-selector=status.phase==Succeeded
Resource Issues¶
Out of Memory (OOM)¶
Symptoms:
Diagnosis:
Solution:
CPU Throttling¶
Diagnosis:
kubectl top pods
kubectl top nodes
# Check metrics
kubectl get --raw /apis/metrics.k8s.io/v1beta1/pods
Solution:
Configuration Issues¶
ConfigMap/Secret Not Found¶
Diagnosis:
Solution:
# Create missing ConfigMap
kubectl create configmap myapp-config --from-literal=key=value
# Verify reference
kubectl get pod myapp -o yaml | grep -A 5 configMap
Environment Variable Issues¶
Diagnosis:
# Check environment variables
kubectl exec myapp -- env
# Check pod spec
kubectl get pod myapp -o yaml | grep -A 10 env
RBAC Issues¶
Permission Denied¶
Diagnosis:
# Check permissions
kubectl auth can-i create pods
kubectl auth can-i create pods --as=system:serviceaccount:default:myapp-sa
# Check role bindings
kubectl get rolebindings
kubectl describe rolebinding <binding-name>
Solution:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: myapp-binding
subjects:
- kind: ServiceAccount
name: myapp-sa
roleRef:
kind: Role
name: myapp-role
apiGroup: rbac.authorization.k8s.io
Debugging Tools¶
Debug Container¶
# Add ephemeral debug container
kubectl debug myapp -it --image=busybox --target=myapp
# Debug node
kubectl debug node/<node-name> -it --image=ubuntu
Network Debug Pod¶
kubectl run netshoot --rm -it --image=nicolaka/netshoot --restart=Never -- bash
# Inside pod
ping <ip>
nslookup <service>
curl http://<service>
traceroute <ip>
Port Forward for Debugging¶
# Forward pod port
kubectl port-forward myapp 8080:80
# Forward service port
kubectl port-forward svc/myapp 8080:80
# Test locally
curl http://localhost:8080
Useful Commands¶
Get Events¶
# All events
kubectl get events --sort-by='.lastTimestamp'
# Events for specific resource
kubectl get events --field-selector involvedObject.name=myapp
# Watch events
kubectl get events --watch
Check Resource Usage¶
# Node resources
kubectl top nodes
# Pod resources
kubectl top pods
# Pod resources in namespace
kubectl top pods -n production
View Logs¶
# Current logs
kubectl logs myapp
# Previous container logs
kubectl logs myapp --previous
# Follow logs
kubectl logs -f myapp
# Logs from specific container
kubectl logs myapp -c container-name
# Logs with timestamps
kubectl logs myapp --timestamps
# Tail logs
kubectl logs myapp --tail=100
Describe Resources¶
kubectl describe pod myapp
kubectl describe node <node-name>
kubectl describe svc myapp
kubectl describe pvc myapp-pvc
Common Error Messages¶
"ImagePullBackOff"¶
- Image doesn't exist
- Wrong image name/tag
- Authentication required
- Network issues
"CrashLoopBackOff"¶
- Application error
- Missing dependencies
- Liveness probe failing
- Wrong command/args
"Pending"¶
- Insufficient resources
- Node selector mismatch
- PVC not bound
- Taints/tolerations
"Error"¶
- Init container failed
- Volume mount issues
- Security context issues
- Command failed
"OOMKilled"¶
- Memory limit too low
- Memory leak in application
- Increase memory limits
Best Practices¶
- Enable Logging
- Centralized logging
- Structured logs
-
Appropriate log levels
-
Monitor Resources
- Set up metrics collection
- Create alerts
-
Regular reviews
-
Use Health Checks
- Liveness probes
- Readiness probes
-
Startup probes
-
Document Issues
- Keep runbooks
- Document solutions
-
Share knowledge
-
Test Changes
- Test in non-production
- Use dry-run
- Gradual rollouts