How to Check Container Logs in Kubernetes Quickly
Use the
kubectl logs command followed by the pod name to see container logs in Kubernetes. For pods with multiple containers, add the -c flag with the container name to specify which container's logs to view.Syntax
The basic syntax to check logs of a container in Kubernetes is:
kubectl logs <pod-name>: Shows logs of the single container in the pod.kubectl logs <pod-name> -c <container-name>: Shows logs of a specific container in a multi-container pod.kubectl logs -f <pod-name>: Follows the logs live (like tail -f).
bash
kubectl logs <pod-name> kubectl logs <pod-name> -c <container-name> kubectl logs -f <pod-name>
Example
This example shows how to get logs from a pod named myapp-pod with a single container, and how to follow logs live.
bash
kubectl logs myapp-pod kubectl logs -f myapp-pod
Output
2024-06-01T12:00:00Z Starting application...
2024-06-01T12:00:05Z Application running
... (logs continue when following)
Common Pitfalls
Common mistakes when checking logs include:
- Not specifying the container name in pods with multiple containers, which causes an error.
- Trying to get logs from a pod that has already terminated or restarted, which may show no logs.
- Not using
-fto follow logs live when needed.
Correct usage for multi-container pods:
bash
# Wrong (missing container name) kubectl logs multi-container-pod # Right (specify container) kubectl logs multi-container-pod -c container1
Quick Reference
| Command | Description |
|---|---|
| kubectl logs | Show logs of single-container pod |
| kubectl logs | Show logs of specific container in multi-container pod |
| kubectl logs -f | Follow logs live |
| kubectl logs --previous | Show logs from previous container instance after restart |
Key Takeaways
Use
kubectl logs <pod-name> to see logs of a pod's container.Add
-c <container-name> for pods with multiple containers.Use
-f to follow logs live as they are generated.Check if the pod is running; terminated pods may have limited logs.
Use
--previous to see logs from a crashed or restarted container.