Kubernetes Troubleshooting: A Practical Guide
Introduction Troubleshooting Kubernetes can be challenging due to its distributed nature. This guide covers practical techniques I’ve used to debug production issues across dozens of clusters. The Troubleshooting Mindset When debugging Kubernetes, follow this systematic approach: Define the problem - What’s the expected vs actual behavior? Gather information - Collect logs, events, and metrics Form hypotheses - What could cause this behavior? Test hypotheses - Verify or eliminate causes Implement fix - Apply and validate the solution Essential kubectl Commands Pod Status and Events 1 2 3 4 5 6 7 8 9 10 11 # Get pod status with more details kubectl get pods -o wide # Describe pod for events and conditions kubectl describe pod <pod-name> # Get pods with specific status kubectl get pods --field-selector=status.phase=Pending # Watch pod status changes kubectl get pods -w Container Logs 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 # Get logs from a pod kubectl logs <pod-name> # Get logs from a specific container in multi-container pod kubectl logs <pod-name> -c <container-name> # Follow logs in real-time kubectl logs -f <pod-name> # Get logs from previous container instance (after restart) kubectl logs <pod-name> --previous # Get last N lines kubectl logs --tail=100 <pod-name> # Get logs with timestamps kubectl logs --timestamps <pod-name> Executing Commands in Containers 1 2 3 4 5 6 7 8 # Open a shell in a running container kubectl exec -it <pod-name> -- /bin/bash # Run a specific command kubectl exec <pod-name> -- cat /etc/config/app.yaml # Exec into a specific container kubectl exec -it <pod-name> -c <container-name> -- /bin/sh Common Issues and Solutions 1. Pod Stuck in Pending Symptoms: Pod stays in Pending state indefinitely ...