Kubernetes Debugging Commands
Debugging applications in Kubernetes can be challenging due to the distributed nature of the environment. kubectl provides several powerful commands to inspect the state of your resources and application logs, helping you diagnose and resolve issues.
1. kubectl describe: Detailed Resource Information
The kubectl describe command provides a detailed view of a specific Kubernetes resource. It includes information about its current state, events, labels, annotations, resource limits, and more. This is often the first step in troubleshooting a resource.
Usage:
# Describe a pod in a specific namespace
kubectl describe pod <pod-name> -n <namespace>
# Example:
kubectl describe pod homarr -n uat
# You can describe other resource types as well:
kubectl describe deployment <deployment-name> -n <namespace>
kubectl describe service <service-name> -n <namespace>
kubectl describe node <node-name>Key Information from describe:
- Events: Crucial for understanding why a pod might not be scheduling, starting, or running correctly (e.g., ImagePullBackOff, CrashLoopBackOff).
- Status: Current phase of the resource (e.g., Pod’s status, Container statuses).
- Resource Limits/Requests: Shows configured CPU and memory limits.
- Volumes: Information about attached volumes.
2. kubectl logs: Inspecting Container Output
The kubectl logs command allows you to view the standard output and standard error of containers running in a pod. This is analogous to docker logs for individual containers.
Usage:
# View logs from a specific pod and container (if multiple containers exist in the pod)
kubectl logs <pod-name> -n <namespace> -c <container-name>
# Example:
kubectl logs homarr -n uat
# Continuously stream logs (like `tail -f`)
kubectl logs -f <pod-name> -n <namespace>
# View logs from a previous instance of a crashed container
kubectl logs -p <pod-name> -n <namespace>
# View logs for all containers in a pod (if only one container, no -c needed)
kubectl logs <pod-name> -n <namespace> --all-containers=true3. kubectl exec: Running Commands Inside Containers
For interactive debugging, you can execute commands directly within a running container, similar to docker exec.
Usage:
# Get an interactive shell in a container
kubectl exec -it <pod-name> -n <namespace> -- /bin/bash
# Often, -- /bin/sh is used as a more common shell in minimal container images
# Example:
kubectl exec -it homarr -n uat -- /bin/sh
# Run a single command in a container
kubectl exec <pod-name> -n <namespace> -- ls -l /app4. kubectl get events: Cluster-wide Event Stream
While kubectl describe shows events for a specific resource, kubectl get events provides a cluster-wide stream of events. This can be useful for seeing what’s happening across the entire cluster, especially for transient issues.
Usage:
# Get the most recent events from the entire cluster
kubectl get eventsYou can also filter events by namespace, resource type, or specific event reasons.