Marcio Cunha

How to stream logs from multiple Kubernetes pods at once

Learn how to use the stern tool to filter, track, and inspect logs from multiple pods and containers in Kubernetes simultaneously right from your terminal.

Marcio Cunha•4 min
Also available in:PortuguêsEspañol
Summary
  • The native kubectl logs command usually limits viewing to a single container at a time, making distributed system analysis harder.
  • The stern tool solves this bottleneck by allowing you to follow multiple pods and containers simultaneously using regular expressions.
  • Namespace filters and label selectors prevent visual clutter and bring only relevant data for troubleshooting.
  • Automatic per-container color coding helps visually distinguish which service generated each log line in the terminal.
  • Simple integrations with other DevOps ecosystem tools speed up the identification of failures in production environments.

The challenge of tracking logs in distributed environments

Managing modern container-based systems means dealing with dozens or hundreds of components running simultaneously. When an error occurs in production, any engineer's first instinct is to rush to the terminal and check what the programs are saying. In the Kubernetes ecosystem, the standard command for this task is kubectl logs, a fantastic utility for simple scenarios that shows its limitations as soon as you need to scale your investigation. Because it requires you to specify the exact name of a pod and a specific container, tracking a continuous stream of events across multiple services becomes a frustrating and fragmented task.

In practice, this means you end up opening multiple tabs in the terminal, pasting different commands, and trying to cross-reference information visually to figure out where the application failed. This manual process consumes precious time and increases the chance of missing a crucial clue during a critical outage. To make matters worse, when pods are automatically recreated by the orchestrator, their names change, breaking the commands you were using moments before. If you have ever experienced managing pods stuck in Terminating, you know very well how much operational instability demands agility in diagnostic tools.

Meet stern and its value proposition

To solve this daily headache, the development community created stern, a small command-line tool that takes Kubernetes log inspection to a new level of efficiency. The name comes from a nautical expression, but in practice it acts as a Swiss Army knife to unify the output stream of multiple pods and containers into a single screen. Unlike the traditional method, stern allows you to use regular expressions to select which pods you want to monitor, automatically capturing new pods that spin up and discarding those that have already terminated.

Besides bringing everything together in one place, the program automatically colors each log line according to the source container. This instant visual identity lets you glance at the screen and know exactly whether the message came from the authentication service, the main API, or the database. Before moving on to practice, it is worth noting that just as how to inspect environment variables in Kubernetes pods helps understand the internal behavior of the application, mastering log streaming is the next natural step for complete observability in the cluster.

Quick installation and first steps in the terminal

Installing stern is a straightforward process since it is distributed as a single binary that can be placed in any directory listed in your operating system's PATH. If you use Linux or macOS systems, package managers like Homebrew or directly copying the executable to the /usr/local/bin folder solves the issue in a few seconds. Once installed, the basic command only requires you to provide a pattern to match the names of the pods you want to follow, integrating natively with your kubeconfig configuration file.

Below is a practical example of how to start basic monitoring by filtering for part of the service name:

stern web-frontend

This simple command is already able to connect to all pods whose name contains the word web-frontend, displaying the combined logs in real time. If a new pod is scaled up to handle an access spike, stern detects it immediately and starts displaying its output on the same screen without requiring you to restart the command.

Real Kubernetes environments are usually divided into multiple namespaces, which act as virtual fences separating teams or staging and production environments. To prevent stern from listening to the entire cluster and generating an absurd amount of noise on your screen, you can direct the search to a specific namespace using direct parameters. Combining this environment choice with label selectors is the best strategy to isolate precisely the component exhibiting anomalous behavior.

Below is an example of a command that restricts the search to a dedicated namespace and filters by a specific application label:

stern --namespace production -l app=payment-service

Another indispensable feature for daily work is the ability to exclude irrelevant lines using the exclusion flag. If your service generates a lot of health check logs that clutter the terminal, you can simply ignore specific terms using a negative regular expression, keeping the focus only on errors and warnings relevant to the ongoing investigation. To explore these architectural concepts further in production, see our related technical analysis on How to Audit Vulnerabilities in Docker Images with Trivy.

Final considerations

Mastering terminal-focused productivity tools completely changes how you interact with cloud infrastructure and resolve complex incidents. stern eliminates the repetitive friction of opening multiple windows and dealing with ephemeral pod names, allowing the engineering team to focus their energy on actually solving the technical problem. By integrating the use of smart filters, label selectors, and visual coloring, the debugging experience in distributed environments becomes much faster and less stressful. Adopting this kind of utility in your daily development and support workflow elevates operational resilience and ensures much faster diagnostics when every second of downtime counts.