## Application Troubleshooting in Kubernetes
Troubleshooting applications in Kubernetes is a critical skill for a CKA. It involves systematically diagnosing issues from the pod level up to service exposure.
## Common Pod Troubleshooting Scenarios
- Pending: A pod stuck in `Pending` state often indicates a scheduling issue. Check resource requests (CPU/memory) against available node capacity using `kubectl describe pod <pod-name>` for events like `FailedScheduling`. Also, verify node selectors, taints and tolerations, or unbound PersistentVolumeClaims (PVCs).
- CrashLoopBackOff: This state means your container is repeatedly starting and crashing. The most common causes are application errors (e.g., incorrect configuration, missing environment variables, database connection issues), an incorrect command or args in the pod definition, or missing dependencies. Use `kubectl logs <pod-name>` to inspect application output, and `kubectl describe pod <pod-name>` for restart counts and events.
- ImagePullBackOff: Occurs when Kubernetes cannot pull the container image. Verify the image name and tag are correct. If using a private registry, ensure imagePullSecrets are correctly configured and have valid credentials. Network connectivity issues to the registry can also cause this.
- Running (but unhealthy): If a pod is `Running` but your application isn't working, check Liveness and Readiness probes. A failing liveness probe will cause the container to restart; a failing readiness probe will remove the pod from service endpoints. Use `kubectl describe pod` to see probe status and events.
## Essential Troubleshooting Commands
- `kubectl describe <resource-type> <resource-name>`: Provides a wealth of information, including status, events, conditions, resource requests/limits, and associated objects. Indispensable for initial diagnosis.
- `kubectl logs <pod-name> [-c <container-name>]`: Retrieves logs from a container, crucial for understanding application behavior and errors. Use `-f` for follow, `--previous` for logs from a previous instance.
- `kubectl exec -it <pod-name> -- bash`: Allows you to execute commands inside a running container, enabling interactive debugging, checking file systems, or running diagnostic tools.
- `kubectl get events`: Shows cluster-level events, which can reveal issues affecting multiple pods or nodes.
- `kubectl top pod <pod-name>` / `kubectl top node <node-name>`: Displays resource usage (CPU/memory) for pods and nodes, helping identify resource bottlenecks.
- `kubectl get pods -o wide`: Shows which node a pod is running on, useful for node-specific issues.
## Service and Network Troubleshooting
If pods are healthy but inaccessible, investigate Services. Use `kubectl describe service <service-name>` to check its selector and ensure it matches the labels of your target pods. Verify that the Service's targetPort matches the container port. Check `kubectl get endpoints <service-name>` to confirm that healthy pods are correctly registered as endpoints. Finally, review any NetworkPolicies that might be inadvertently blocking traffic.
What is the primary command to inspect detailed pod status, events, and conditions?
`kubectl describe pod <pod-name>`
tap to reveal
A pod is in `CrashLoopBackOff` state. What are common causes?
Application errors, incorrect `command`/`args` in the pod definition, or missing configuration/dependencies.
tap to reveal
How do you access logs from a specific container within a pod?
`kubectl logs <pod-name> -c <container-name>`
tap to reveal
What does `ImagePullBackOff` indicate?
Kubernetes failed to pull the container image, often due to a wrong image name/tag, private registry authentication issues, or network problems.
tap to reveal
What is the purpose of a Liveness probe?
To determine if an application within a container is running and healthy; if it fails, the container is restarted.
tap to reveal
How can you interactively debug inside a running container?
`kubectl exec -it <pod-name> -- bash` (or `sh`)
tap to reveal
A pod is stuck in `Pending`. What are potential causes?
Insufficient resources (CPU/memory), node selectors/taints, or an unbound PersistentVolumeClaim (PVC).
tap to reveal
What command helps verify if a Service's selector is correctly matching pods and if endpoints are registered?
`kubectl describe service <service-name>` and `kubectl get endpoints <service-name>`
tap to reveal