Kubernetes is renowned for its robustness and scalability as a container orchestration platform. However, managing applications within Kubernetes can be challenging, particularly when it comes to debugging and monitoring.
A common hurdle for developers and operators is handling logs from pods running in Kubernetes clusters. Keeping track of logs across multiple pods, containers, and namespaces can quickly become overwhelming. This is where Stern proves invaluable. In this blog post, we’ll delve into how Stern simplifies Kubernetes log management, making it easy to follow logs from multiple pods, containers, and namespaces.
Stern allows you to tail logs from multiple pods and containers in Kubernetes, streamlining the debugging process with color-coded outputs for quicker identification and troubleshooting. The tool supports filtering based on regular expressions or Kubernetes resources, so you can easily target specific pods without needing to specify exact IDs (e.g., omitting deployment IDs). Additionally, Stern dynamically adapts to changes—automatically removing logs from deleted pods and seamlessly adding logs from newly created ones—ensuring you always have the most up-to-date information.
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
Leave a Reply