Cloud Native 10 min read

Master Real-Time Multi-Pod Log Viewing in Kubernetes with Kubetail & Stern

This guide introduces two lightweight Kubernetes log‑tailing tools, Kubetail and Stern, explains their installation on various platforms, demonstrates common usage patterns and command‑line options, and provides practical examples for aggregating and filtering logs across multiple pods and containers.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Real-Time Multi-Pod Log Viewing in Kubernetes with Kubetail & Stern

Kubetail

Kubetail is a lightweight Bash script that aggregates logs from multiple Kubernetes pods in real time. It wraps kubectl logs, adds optional colored output, regular‑expression filtering, and supports multi‑container pods.

Installation

Homebrew (macOS)

# install kubetail using brew
brew tap johanhaleby/kubetail && brew install kubetail

Linux (binary)

# download the script
wget https://raw.githubusercontent.com/johanhaleby/kubetail/master/kubetail
chmod +x kubetail
sudo cp kubetail /usr/local/bin/

Zsh plugin (oh‑my‑zsh)

# clone into custom plugins directory
cd ~/.oh-my-zsh/custom/plugins/
git clone https://github.com/johanhaleby/kubetail.git kubetail
# enable in .zshrc
vim ~/.zshrc   # add kubetail to plugins array
plugins=( ... kubetail )
source ~/.zshrc

Basic usage

List pods in a namespace: kubectl get pods -n myns Tail logs from one or more pods:

kubetail app1
kubetail app1,app2

Specify container(s) in a multi‑container pod: kubetail app1 -c containerA -c containerB Use a regular expression to match pod names: kubetail "^frontend|.*demo.*" --regex Control coloring:

# only pod names colored
kubetail app1 -k pod
# whole line colored (default)
kubetail app1 -k line
# disable coloring
kubetail app1 -k false

Common flags

-n – namespace

-c – container name(s) (comma‑separated or repeated)

-k – coloring mode (pod, line, false)

-b – enable line‑buffered mode (default false)

-l – label filter to ignore pods matching a pattern

-t – kubeconfig context

-s – show logs newer than a relative time (e.g., 5s, 2m, 3h; default 10s)

Stern

Stern is a Go‑based command‑line tool that streams logs from multiple pods and containers, offering similar coloring, filtering, and templating capabilities. It is distributed as a single binary.

Installation

Homebrew (macOS)

# install stern using brew
brew install stern

Linux (binary)

# download the latest release
wget https://github.com/wercker/stern/releases/download/1.11.0/stern_linux_amd64
chmod +x stern_linux_amd64
sudo mv stern_linux_amd64 /usr/local/bin/stern

Bash/Zsh completion

# bash completion
brew install bash-completion
source <(brew --prefix)/etc/bash-completion
source <(stern --completion=bash)
# zsh completion
source <(stern --completion=zsh)

Basic usage

Tail all pods in the current namespace: stern . Tail a specific container: stern app1 --container containerA Tail logs in a given namespace: stern app1 --namespace myns Exclude a container:

stern --namespace myns --exclude-container sidecar .

Show logs from the last 15 minutes: stern app1 --since 15m Filter by label across all namespaces: stern --all-namespaces -l app=web Use a label selector to match pods: stern frontend --selector release=canary Output as JSON and pipe to jq: stern backend -o json | jq . Output raw log messages only: stern backend -o raw Custom Go template example:

stern --template '{{.Message}} ({{.Namespace}}/{{.PodName}}/{{.ContainerName}})' backend

Template with built‑in colors:

stern --template '{{.Message}} ({{.Namespace}}/{{color .PodColor .PodName}}/{{color .ContainerColor .ContainerName}})' backend

Common flags

--container – container name regex (default .*)

--exclude-container – container name regex to exclude

--container-state – filter by container state (default running)

--timestamps – include timestamps

--since – show logs newer than a relative duration

--context – Kubernetes context to use

--exclude – regex to exclude log lines (repeatable)

--namespace – target namespace (default from current context)

--kubeconfig – path to kubeconfig file (default ~/.kube/config)

--all-namespaces – tail across all namespaces

--selector – label selector for pod filtering

--tail – number of lines from the end to show (default -1 for all)

--color – force color output ( auto, always, never)

--output – output format ( default, raw, json)

--template – custom Go template for log lines

References

Kubetail repository: https://github.com/johanhaleby/kubetail

Stern repository: https://github.com/wercker/stern

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Cloud NativeKubernetesDevOpslog aggregationkubetailstern
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.