Debugging Pods
Kubernetes pods and deployments can fail due to misconfigurations, resource constraints, or runtime errors. Effective debugging requires leveraging kubectl commands to inspect pod states, retrieve logs, and interact with containers. This section covers tools and workflows to diagnose and resolve common issues.
Checking Pod Status and Logs¶
Use kubectl describe and kubectl logs to identify root causes of pod failures.
1. Describe the Pod¶
Look for events under the "Events" section, which often reveal errors like:- "Failed to pull image" (image pull issues)
- "CrashLoopBackOff" (container exited unexpectedly)
- "InvalidImageName" (syntax errors in image names)
2. View Container Logs¶
If the container isn’t running, use--previous to check logs from the last terminated instance:3. Check Pod Phase and Conditions¶
A phase ofError or Failed indicates a critical issue. Use kubectl get pod <pod-name> --output=jsonpath='{.status.conditions}' to inspect detailed conditions like Ready, Initialized, and ContainersReady.
Debugging Running or Stuck Pods¶
1. Exec into a Running Pod¶
If the pod is in Running state but not responding, interact with the container:
Use this to check file systems, verify configurations, or run diagnostic commands like
curl or netstat.
2. Debug a Non-Running Pod¶
If the pod is in Error or CrashLoopBackOff state, use kubectl debug to add a temporary shell container:
Troubleshooting Deployments¶
1. Check Deployment Status¶
Look for rollout status under "Events" and "Replicas" in the "Status" section. A mismatch betweenDesired and Current replicas may indicate deployment failures.
2. Rollback Failed Deployments¶
If a deployment fails, roll back to a previous version:
3. Check Pod Template Configuration¶
Ensure the deployment’s pod template is correctly configured:
Common Issues and Workarounds¶
- Network Issues: Use
kubectl get endpointsto verify service endpoints. Test connectivity withcurlortelnetfrom within a pod. - Resource Limits: Check if pods are terminated due to OOM (Out-Of-Memory) errors:
- Configuration Errors: Validate ConfigMaps and Secrets:
Key takeaways¶
- Use
kubectl describeandkubectl logsto diagnose pod failures quickly. kubectl execandkubectl debugallow interactive troubleshooting of running or stuck pods.- Monitor deployment rollouts and use rollbacks to recover from failed updates.
- Validate configurations for ConfigMaps, Secrets, and resource limits to prevent common errors.
- Combine log analysis with network and resource checks for comprehensive debugging.