Kubernetes, the popular container orchestration platform, is known for its robustness and scalability. However, managing applications in Kubernetes can be a daunting task, especially when it comes to debugging and monitoring.
One common issue developers and operators face is dealing with logs from pods running in Kubernetes clusters. It can be challenging to keep track of logs across multiple pods, containers, and namespaces. That’s where Stern comes to the rescue. In this blog post, we will explore how Stern simplifies Kubernetes log tailing by allowing you to follow logs from multiple pods, containers, and namespaces with ease.
Stern allows you to tail multiple pods on Kubernetes and multiple containers within the pod. Each result is color-coded for quicker debugging. The query is a regular expression or a Kubernetes resource in the form / so the pod name can easily be filtered and you don’t need to specify the exact id (for instance omitting the deployment id). If a pod is deleted it gets removed from tail and if a new pod is added it automatically gets tailed.
Installation
Before diving into the details, you need to install Stern. There are multiple ways to do this depending on your preference and environment.
Downloading the Binary
The simplest way to get started is to download the binary directly from the releases on GitHub. Stern provides pre-built binaries for Linux, macOS, and Windows. For example, to install Stern on Linux:
curl -Lo stern
https://github.com/wercker/stern/releases/download/{VERSION}/stern_linux_amd64
chmod +x stern
sudo mv stern /usr/local/bin/
Using Go
If you have Go installed, you can install Stern using the go
command:
go install github.com/stern/stern@latest
Package Managers
You can also install Stern using popular package managers like Homebrew (for macOS and Linux) or Krew (for kubectl plugins).
Usage
Once Stern is installed, it’s time to put it to work. Stern makes it simple to tail logs from multiple pods and containers.
Basic Usage
To get started, simply run Stern followed by the pod query:
stern <pod-query>
The pod query is a regular expression or a Kubernetes resource in the form <resource>/<name>
. This allows you to easily filter logs without specifying the exact ID. If a
pod is deleted, Stern will automatically remove it from tail, and new pods are automatically added.
For example, to tail logs from all pods in the my-namespace
namespace:
stern -n my-namespace .
To tail logs from a specific container within a pod, use the --container
flag:
stern -n my-namespace my-pod --container my-container
Advanced Usage
Stern provides a wide range of flags and options to customize your log-tailing experience. Here are a few useful ones:
--all-namespaces
: Tails logs across all namespaces.--exclude
: Allows you to exclude log lines based on regular expressions.--since
: Shows logs newer than a relative duration (e.g., 5s for 5 seconds).--tail
: Specifies the number of lines from the end of the logs to show.--template
: Provides a custom template for log lines.
Real-Time Example
Let’s consider a real-world example where you want to monitor logs from all pods that belong to
a specific deployment. You can use Stern to achieve this. Here’s how you can do it:
stern deployment/my-deployment
This command will tail logs from all pods managed by the my-deployment
deployment. It’s a handy way to monitor the logs of an application that scales up or down based on load. Stern also makes it easy to work with logs in JSON format. For example, if your application logs in JSON and you want to filter logs by a specific key, you can do this:
stern deployment/my-app -i '.*"status":"error".*'
In this command, Stern only displays logs with the "status":"error"
JSON field, making debugging a breeze.
Conclusion
Stern is a powerful and flexible tool for simplifying log tailing in Kubernetes. Whether you’re debugging applications, monitoring containers, or troubleshooting issues in a complex microservices environment, Stern can save you time and effort.
With easy installation and a variety of customization options, Stern is a must-have for anyone working with Kubernetes. Try it out in your Kubernetes clusters and make log management less of a hassle.
Give Stern a try and streamline your Kubernetes log tailing experience today!
usage, advanced features, and a real-time example. By using Stern, you can significantly improve your log-tailing workflow in Kubernetes, making it a valuable addition to your toolkit