DEV Community

Shivam Agnihotri
Shivam Agnihotri

Posted on

Exploring Stern : Day 21 of 50 days DevOps Tools Series

Welcome to Day 21 of our "50 Days of DevOps Tools" series! Today, we dive into Stern, an indispensable tool for DevOps engineers working with Kubernetes. Stern simplifies the process of tailing logs from multiple Kubernetes pods and containers, providing a powerful solution for debugging and monitoring applications in a Kubernetes environment.

What is Stern?

Stern is an open-source tool designed to tail multiple Kubernetes pod logs in real time. It allows you to aggregate logs from multiple pods and containers into a single stream, making it easier to monitor and troubleshoot applications. Stern supports advanced filtering options, color-coded output, and the ability to follow logs, making it a highly efficient tool for DevOps engineers.

Key Features of Stern

Multiple Pod Log Tailing: Aggregate logs from multiple pods and containers into a single stream.
Advanced Filtering: Filter logs based on pod name, namespace, container, and labels.
Color-Coded Output: Easily distinguish between logs from different pods and containers with color-coded output.
Real-Time Following: Continuously follow logs as new entries are added.
Compatibility: Works seamlessly with any Kubernetes cluster.

Installing Stern

Stern can be installed using Homebrew on macOS, or you can download the binary directly for other operating systems. Here are the installation steps for different platforms:

Installation Using Homebrew
Install Homebrew (if not already installed):

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Enter fullscreen mode Exit fullscreen mode

Install Stern:

brew install stern
Enter fullscreen mode Exit fullscreen mode

Installation on Windows

Download the Stern Binary: Visit the Stern releases page and download the appropriate binary for Windows.

Add Stern to PATH: Extract the binary and add its location to your system's PATH environment variable.

Installation on Linux

Download the Stern Binary:
Visit the Stern releases page and download the appropriate binary for Linux.

Make the Binary Executable:

chmod +x stern
Enter fullscreen mode Exit fullscreen mode

Move the Binary to a Directory in PATH:

sudo mv stern /usr/local/bin/
Enter fullscreen mode Exit fullscreen mode

Using Stern

Stern is straightforward to use. Here are some common commands and usage examples:

Tailing Logs from All Pods in a Namespace
To tail logs from all pods in the default namespace, simply run:

stern .
Enter fullscreen mode Exit fullscreen mode

Filtering Logs by Pod Name
To tail logs from pods with names matching a pattern, use the following command:

stern nginx
Enter fullscreen mode Exit fullscreen mode

Filtering Logs by Namespace
To tail logs from pods in a specific namespace, use the -n or --namespace flag:

stern <pod-name-pattern> -n <namespace>
Enter fullscreen mode Exit fullscreen mode

Filtering Logs by Container Name
To tail logs from specific containers within the pods, use the -c or --container flag:

stern <pod-name-pattern> -l <label-selector>
Enter fullscreen mode Exit fullscreen mode

Configuring Stern

Stern offers various configuration options to customize log output, such as time formatting, log output colorization, and more.

Color-Coded Output
Stern automatically color-codes logs from different pods and containers to make it easier to distinguish between them. You can customize the colors using the --color flag:

stern <pod-name-pattern> --color <always|never|auto>
Enter fullscreen mode Exit fullscreen mode

Customizing Time Format
You can customize the time format for log entries using the --timestamps and --time-format flags:

stern <pod-name-pattern> --timestamps --time-format "2024-08-02T15:04:05Z07:00"
Enter fullscreen mode Exit fullscreen mode

Ignoring Older Logs
To ignore older logs and display only new log entries, use the --since flag:

stern <pod-name-pattern> --since 1h
Enter fullscreen mode Exit fullscreen mode

Benefits of Using Stern

Simplified Log Management: Aggregate logs from multiple pods and containers into a single stream, making it easier to monitor and troubleshoot applications.
Advanced Filtering: Filter logs based on pod name, namespace, container, and labels, providing fine-grained control over log output.
Real-Time Monitoring: Follow logs in real-time to stay updated on the latest events and issues.
Color-Coded Output: Easily distinguish between logs from different pods and containers with color-coded output.
Compatibility: Works seamlessly with any Kubernetes cluster, providing a flexible and efficient log management solution.

Limitations of Stern

Resource Intensive: Tailing logs from multiple pods and containers can be resource-intensive, especially in large clusters.
Limited Historical Logs: Stern is primarily designed for real-time log monitoring, so accessing historical logs can be limited.
No Built-In Storage: Stern does not provide built-in log storage, so you'll need to use additional tools for log persistence and analysis.

Conclusion
Stern is an essential tool for DevOps engineers working with Kubernetes, providing a powerful and efficient solution for tailing logs from multiple pods and containers. Its advanced filtering options, color-coded output, and real-time monitoring capabilities make it a valuable addition to any DevOps toolkit. By integrating Stern into your workflow, you can streamline log management, improve troubleshooting, and enhance the overall reliability of your Kubernetes applications.

Stay tuned for tomorrow's post, where we'll dive into another exciting tool to enhance Kubernetes and DevOps practices!

👉 Make sure to follow me on LinkedIn for the latest updates: Shiivam Agnihotri

Top comments (0)