# Kubernetes Troubleshooting Guide ## Common Issues and Solutions ### Pod Issues #### Pod Stuck in Pending ```bash # Check events kubectl describe pod -n # Common causes: # - Insufficient resources kubectl describe nodes | grep -A 5 "Allocated resources" # - No matching nodes (taints/tolerations) kubectl get nodes -o json | jq '.items[].spec.taints' # - PVC not bound kubectl get pvc -n ``` #### Pod in CrashLoopBackOff ```bash # Check logs kubectl logs -n --previous # Check container exit code kubectl get pod -n -o jsonpath='{.status.containerStatuses[0].lastState.terminated.exitCode}' # Common exit codes: # 0 - Success (check livenessProbe) # 1 - Application error # 137 - OOMKilled (increase memory) # 139 - Segmentation fault # 143 - SIGTERM received ``` #### Pod in ImagePullBackOff ```bash # Check image name kubectl get pod -n -o jsonpath='{.spec.containers[0].image}' # Verify image exists docker pull # Check imagePullSecrets kubectl get pod -n -o jsonpath='{.spec.imagePullSecrets}' kubectl get secret -n -o jsonpath='{.data.\.dockerconfigjson}' | base64 -d ``` ### Service Issues #### Service Not Accessible ```bash # Verify endpoints exist kubectl get endpoints -n # Check selector matches pod labels kubectl get svc -n -o jsonpath='{.spec.selector}' kubectl get pods -n --show-labels # Test from within cluster kubectl run debug --rm -it --image=busybox -- wget -qO- http://..svc.cluster.local ``` #### DNS Resolution Issues ```bash # Test DNS from pod kubectl run dns-test --rm -it --image=busybox -- nslookup kubernetes.default # Check CoreDNS pods kubectl get pods -n kube-system -l k8s-app=kube-dns # Check CoreDNS logs kubectl logs -n kube-system -l k8s-app=kube-dns ``` ### Node Issues #### Node NotReady ```bash # Check node conditions kubectl describe node | grep -A 20 Conditions # Check kubelet status systemctl status kubelet # Check kubelet logs journalctl -u kubelet -f # Common causes: # - Disk pressure # - Memory pressure # - Network issues # - Container runtime issues ``` #### Node Disk Pressure ```bash # Check disk usage kubectl describe node | grep -A 3 "Allocated resources" # Cleanup unused images docker system prune -af # Check for large logs du -sh /var/log/containers/* ``` ### Networking Issues #### Pod-to-Pod Communication Fails ```bash # Test connectivity kubectl exec -- ping # Check network policies kubectl get networkpolicies -n # Verify CNI plugin kubectl get pods -n kube-system | grep -E "calico|weave|flannel|cilium" ``` ### Storage Issues #### PVC Stuck in Pending ```bash # Check PVC events kubectl describe pvc -n # Verify StorageClass exists kubectl get storageclass # Check provisioner pods kubectl get pods -n kube-system | grep provisioner ``` ## Diagnostic Commands Cheat Sheet ```bash # Cluster overview kubectl cluster-info kubectl get componentstatuses # Resource usage kubectl top nodes kubectl top pods -n # Events kubectl get events -n --sort-by='.lastTimestamp' # Logs kubectl logs -f -n kubectl logs -f -n --all-containers # Exec into pod kubectl exec -it -n -- /bin/sh # Port forward kubectl port-forward 8080:80 -n # Copy files kubectl cp /:/path/to/file ./local-file ```