DEV Community

Erick Okal
Erick Okal

Posted on

2

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.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay