DEV Community

Erick Okal
Erick Okal

Posted on

Tail Kubernetes Logs Efficiently with Stern

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/
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Tail logs from a specific container in a pod
  stern meetings-summarizer -c backend
Enter fullscreen mode Exit fullscreen mode
  • Follow logs from the deployment pods across all namespaces
  stern meetings-summarizer --all-namespaces
Enter fullscreen mode Exit fullscreen mode
  • Filter logs using regex
  stern meetings-summarizer | grep "ERROR"
Enter fullscreen mode Exit fullscreen mode
  • Exclude unwanted log patterns
  stern meetings-summarizer --exclude "healthcheck"
Enter fullscreen mode Exit fullscreen mode
  • Enable colorized output for better readability
  stern meetings-summarizer --color=always
Enter fullscreen mode Exit fullscreen mode

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)