Monitoring logs in Kubernetes is essential for debugging and troubleshooting. While kubectl logs -f pod-name
works for individual pods, it becomes difficult when dealing with multiple pods in a deployment. Sometimes we end up opening multiple terminal tabs to follow logs. Well, stern can be a better alternative.
Why Use Stern?
- Allows tailing logs from multiple pods simultaneously
- Supports regex filtering for better log analysis
- Works with multi-container pods
- Provides color-coded output for better readability
Installing Stern
-
macOS:
brew install stern
- Linux:
curl -Lo stern
https://github.com/wercker/stern/releases/download/{VERSION}/stern_linux_amd64
chmod +x stern
sudo mv stern /usr/local/bin/
Note:
To illustrate some of the commands, I'll use meetings-summarizer
as my deployment.
Using Stern for Log Tailing
- Tail logs from all pods in a deployment
stern meetings-summarizer
- Tail logs from a specific container in a pod
stern meetings-summarizer -c backend
- Follow logs from the deployment pods across all namespaces
stern meetings-summarizer --all-namespaces
- Filter logs using regex
stern meetings-summarizer | grep "ERROR"
- Exclude unwanted log patterns
stern meetings-summarizer --exclude "healthcheck"
- Enable colorized output for better readability
stern meetings-summarizer --color=always
When to Use Stern?
- Debugging microservices that generate logs across multiple pods
- Monitoring real-time logs for deployments
- Filtering logs for specific containers or error messages
- Analyzing logs in large-scale Kubernetes environments
Stern simplifies log management in Kubernetes, making it easier to diagnose and resolve issues efficiently. I hope you find this article useful.
Top comments (0)