Viewing Logs in Kubernetes
Kubernetes allows you to inspect logs from containers running in Pods using the kubectl logs
command. This is essential for debugging and monitoring.
Basic Log Command
kubectl logs <pod-name>
This shows logs from the main container in a pod.
Common Use Cases
Command | Description |
---|---|
kubectl logs my-pod | Logs from the default container |
kubectl logs my-pod -c my-container | Logs from a specific container in the pod |
kubectl logs -n my-namespace my-pod | Logs from a pod in a specific namespace |
kubectl logs -f my-pod | Follow logs (live streaming like tail -f ) |
kubectl logs --previous my-pod | Logs from the previous instance of a container (after crash or restart) |
Live Log Monitoring
kubectl logs -f my-pod
To stop the live view, press Ctrl+C
.
Get Logs for All Pods in a Deployment
You can combine commands to fetch logs from all pods with a specific label:
kubectl get pods -l app=my-app -o name | xargs -I {} kubectl logs {}
For aggregated logs across multiple pods, third-party tools like stern
or kubetail
are often used.
Limitations of kubectl logs
- Only works for currently running or recently crashed containers.
- You won’t get logs if a pod has been evicted or deleted.
- Logs are lost after pod termination unless a logging solution is in place.
Advanced Logging in Production
To persist and manage logs across your cluster, use centralized logging solutions such as:
- Elastic Stack (Filebeat + Logstash + Elasticsearch + Kibana)
- Fluentd or Fluent Bit with Elasticsearch and Kibana
- Loki with Grafana
- Cloud-native tools like AWS CloudWatch, GCP Cloud Logging, or Azure Monitor
These tools allow for log aggregation, persistent storage, and advanced search and visualization.
Last updated on