Kubernetes Troubleshooting — Complete Guide
Learn Kubernetes Troubleshooting — Complete Guide with plain-English explanations and real examples..
20+ years shipping production infrastructure and CI/CD at scale. Notes here come from systems that actually shipped.
- ✓Kubernetes cluster (v1.18+), kubectl installed and configured, basic understanding of pods, services, deployments, and nodes, familiarity with YAML, access to cluster with sufficient permissions (cluster-admin or equivalent), optional: metrics-server, krew plugin manager.
Think of Kubernetes Troubleshooting — Complete Guide like a tool in your DevOps toolkit — once you understand what it does and when to reach for it, managing Kubernetes clusters becomes second nature.
Welcome to Kubernetes Troubleshooting — Complete Guide. We'll break this down from first principles.
1. Understanding the Kubernetes Troubleshooting Hierarchy
Kubernetes troubleshooting follows a layered approach: start from the bottom (pods) and work up to the cluster level. Most issues manifest as pod failures, but the root cause often lies in the control plane, networking, or resource constraints. The hierarchy includes: Pods → Services/Ingress → Nodes → Control Plane → Storage/Networking. Always check pod logs and events first, then examine node health, and finally inspect cluster-level components like kube-apiserver or etcd. This prevents wasted effort on symptoms rather than causes. For example, a CrashLoopBackOff might be due to a missing ConfigMap, which is a pod-level config issue, but if all pods on a node fail, suspect a node problem. Use kubectl get events --all-namespaces to see cluster-wide events quickly.
kubectl get events before diving into logs. Events often contain the exact reason for failures, saving you from guessing.2. Diagnosing Pod Failures: CrashLoopBackOff and ImagePullBackOff
Pod failures are the most common Kubernetes issues. CrashLoopBackOff means the container starts but crashes repeatedly. ImagePullBackOff means the container image cannot be pulled. For CrashLoopBackOff, check logs with kubectl logs <pod> --previous to see the last crash's output. Common causes: missing environment variables, misconfigured secrets, or application bugs. For ImagePullBackOff, verify the image name, tag, and registry credentials. Use kubectl describe pod <pod> to see the exact error. If the image is private, ensure the imagePullSecret is correctly configured. Also check if the node has disk space issues preventing image extraction. A systematic approach: describe the pod, check logs, then inspect the node's disk and network.
--previous to see the last container's output.kubectl describe and kubectl logs --previous to get the error details.3. Debugging Services and Endpoints
When a service is unreachable, the problem often lies in endpoint configuration or network policies. First, verify the service exists and has endpoints: kubectl get svc and kubectl get endpoints. If endpoints are empty, the selector doesn't match any pods. Check pod labels: kubectl get pods --show-labels. Also ensure the service port matches the container port. For headless services, endpoints should list pod IPs. If endpoints exist but the service is unreachable, test connectivity from a pod: kubectl exec -it <pod> -- curl <service-name>:<port>. Network policies might block traffic; check with kubectl describe networkpolicy. Also verify kube-proxy is running on nodes and iptables rules are correct.
kubectl get pods --show-labels and compare labels.4. Node-Level Troubleshooting: Disk, Memory, and Network
Node issues can cause widespread pod failures. Common problems: disk pressure, memory pressure, or network unavailability. Check node status with kubectl get nodes and kubectl describe node <node>. Look for conditions like DiskPressure, MemoryPressure, or PIDPressure. For disk pressure, check disk usage on the node via SSH or use kubectl debug node/<node> -it --image=busybox to run a debug pod. Memory pressure may require adjusting resource limits or adding nodes. Network issues can be diagnosed with kubectl get nodes -o wide to see internal IPs and then ping from another node. Also check kubelet logs: journalctl -u kubelet -n 100 on the node. If the node is NotReady, restart kubelet or check the container runtime.
kubectl get pods --field-selector=status.phase=Failed.5. Control Plane Troubleshooting: API Server, Scheduler, and Controller Manager
Control plane failures affect the entire cluster. Symptoms: kubectl commands hang or return errors, pods not being scheduled, or resources not being reconciled. Check control plane pod status: kubectl get pods -n kube-system. Common issues: etcd leader election failures, API server certificate expiration, or scheduler misconfiguration. For API server issues, check its logs: kubectl logs -n kube-system kube-apiserver-master. If the API server is down, you can't use kubectl; SSH into the master node and check the kube-apiserver process. For scheduler issues, check if pending pods exist: kubectl get pods --field-selector=status.phase=Pending. If pods are pending but nodes are available, the scheduler might be stuck. Restarting the scheduler pod often helps.
kubeadm certs check-expiration or openssl x509 -in /etc/kubernetes/pki/apiserver.crt -noout -dates.6. Networking Troubleshooting: DNS, CNI, and Ingress
Networking issues are notoriously tricky. Common problems: DNS resolution failures, CNI plugin misconfiguration, or Ingress controller not routing traffic. For DNS, check CoreDNS pods: kubectl get pods -n kube-system -l k8s-app=kube-dns. If they are running, test DNS from a pod: kubectl exec -it <pod> -- nslookup kubernetes.default.svc.cluster.local. If DNS fails, check CoreDNS logs and ConfigMap. For CNI issues, check if pods get IPs: kubectl get pods -o wide and see if IPs are assigned. If pods have no IP, the CNI plugin (Calico, Flannel, etc.) might be misconfigured. Check CNI daemonset logs. For Ingress, verify the Ingress resource and controller logs: kubectl logs -n ingress-nginx ingress-nginx-controller-xxxxx.
7. Storage Troubleshooting: PersistentVolumeClaims and Mount Issues
Storage issues cause pods to fail to start or become unresponsive. Common problems: PVC stuck in Pending, pod unable to mount volume, or volume permissions errors. Check PVC status: kubectl get pvc. If Pending, the StorageClass might not exist or the provisioner is unavailable. Verify StorageClass: kubectl get storageclass. If the PVC is Bound but pod can't mount, check the pod events: kubectl describe pod <pod>. Common mount errors include wrong filesystem type or permission denied. For NFS or cloud volumes, ensure the node has the necessary drivers. Also check if the volume is already mounted by another pod (ReadWriteOnce). Use kubectl get pv to see the underlying volume.
8. Using kubectl Debug and Ephemeral Containers
Kubernetes 1.18+ introduced ephemeral containers for debugging running pods without restarting them. This is invaluable for troubleshooting production issues where you can't modify the pod spec. Use kubectl debug to add an ephemeral container to a running pod. For example, to debug a pod with a network issue: kubectl debug -it <pod> --image=nicolaka/netshoot --target=<container>. The ephemeral container shares the same network namespace, so you can run curl, tcpdump, etc. You can also debug nodes: kubectl debug node/<node> -it --image=busybox. This creates a pod with host access. Ephemeral containers are temporary and removed when the pod is deleted.
nicolaka/netshoot image contains curl, tcpdump, dig, and other network tools. It's the Swiss Army knife for network debugging.kubectl debug for network and node issues.9. Logging and Monitoring: Centralized Logs and Metrics
Centralized logging and monitoring are essential for proactive troubleshooting. Use tools like Prometheus for metrics and Loki/Elasticsearch for logs. In Kubernetes, common patterns: Fluentd or Filebeat to ship logs, and Prometheus Operator for metrics. For quick ad-hoc monitoring, use kubectl top to see resource usage: kubectl top pods and kubectl top nodes. If metrics aren't available, ensure metrics-server is installed. For logs, use kubectl logs --tail=100 --follow for real-time streaming. In production, set up alerts for common failure modes: pod restarts, node pressure, and API server latency. Use kubectl get events --watch to monitor cluster events in real-time.
kubectl top requires metrics-server to be installed. If not present, deploy it: kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml.kubectl top and log streaming for quick insights.10. Common Pitfalls and Anti-Patterns
Avoid these common mistakes: 1) Ignoring resource limits — pods without limits can starve other pods. 2) Using latest tag — leads to unpredictable behavior. 3) Not setting pod disruption budgets — causes downtime during node maintenance. 4) Overlooking network policies — default allow-all can be a security risk. 5) Skipping health checks — liveness and readiness probes prevent routing traffic to unhealthy pods. 6) Using hostNetwork without understanding the implications. 7) Not backing up etcd — without backups, cluster recovery is impossible. 8) Manually editing pods — always use deployments. 9) Ignoring pod anti-affinity — can cause all replicas to land on the same node. 10) Not using namespaces — leads to resource conflicts.
latest tag and the image was overwritten with a broken version. Pinning to a specific tag prevented recurrence.11. Advanced Troubleshooting: Using kubectl plugin and krew
Kubectl plugins extend its functionality. Install krew, the plugin manager: kubectl krew install <plugin>. Essential plugins: kubectl-neat to clean up YAML output, kubectl-tree to show resource ownership hierarchy, kubectl-oom to find pods killed by OOM, kubectl-whisper for secret management, and kubectl-sniff to capture network traffic. For example, kubectl sniff <pod> -p <port> starts a remote tcpdump. kubectl oom lists pods that have been OOMKilled. These plugins save time and provide deeper insights. To install krew: kubectl krew install sniff oom tree neat.
kubectl sniff runs tcpdump remotely on the pod and streams the output to Wireshark on your local machine. Great for debugging network issues.kubectl sniff, we captured traffic between a pod and a database and found the app was sending malformed queries, causing high latency.12. Building a Troubleshooting Runbook
A runbook standardizes troubleshooting steps, reducing MTTR. Start with a decision tree: Is the issue pod-level? Node-level? Cluster-level? For each, list commands and expected outputs. Include sections for common scenarios: CrashLoopBackOff, service unreachable, node NotReady, etc. Document escalation paths and contact information. Use tools like Backstage or GitBook to host the runbook. Keep it version-controlled and update it after every incident. Example structure: 1) Initial checks (events, top, describe), 2) Pod issues (logs, previous, debug), 3) Service issues (endpoints, connectivity), 4) Node issues (conditions, disk, kubelet), 5) Control plane (apiserver, etcd), 6) Escalation. Automate common checks with scripts or chatbots.
kubectl describe and return results when you type /k8s describe pod my-pod.| File | Command / Code | Purpose |
|---|---|---|
| check-events.sh | kubectl get events --all-namespaces --sort-by='.lastTimestamp' | tail -20 | 1. Understanding the Kubernetes Troubleshooting Hierarchy |
| debug-pod.sh | kubectl describe pod my-app-7d4f8b6c9-abcde | 2. Diagnosing Pod Failures |
| debug-service.sh | kubectl get svc my-service | 3. Debugging Services and Endpoints |
| debug-node.sh | kubectl get nodes | 4. Node-Level Troubleshooting |
| debug-control-plane.sh | kubectl get pods -n kube-system | grep -E 'apiserver|scheduler|controller|etcd' | 5. Control Plane Troubleshooting |
| debug-networking.sh | kubectl run dns-test --image=busybox --rm -it --restart=Never -- nslookup kubern... | 6. Networking Troubleshooting |
| debug-storage.sh | kubectl get pvc | 7. Storage Troubleshooting |
| debug-ephemeral.sh | kubectl debug -it my-app-7d4f8b6c9-abcde --image=nicolaka/netshoot --target=my-a... | 8. Using kubectl Debug and Ephemeral Containers |
| monitoring.sh | kubectl top pods | 9. Logging and Monitoring |
| resource-limits.yaml | apiVersion: v1 | 10. Common Pitfalls and Anti-Patterns |
| install-plugins.sh | ( | 11. Advanced Troubleshooting |
| runbook-template.yaml | kubectl get events --field-selector involvedObject.kind=Pod --sort-by='.lastTime... | 12. Building a Troubleshooting Runbook |
Key takeaways
kubectl debug with tools like netshoot.Interview Questions on This Topic
What is the first command I should run when a pod is failing?
kubectl describe pod <pod-name> to see events and status, then kubectl logs <pod-name> --previous to get the last crash log.Frequently Asked Questions
20+ years shipping production infrastructure and CI/CD at scale. Notes here come from systems that actually shipped.
That's Kubernetes. Mark it forged?
5 min read · try the examples if you haven't