Master Real-Time Kubernetes Logs with Kubetail and Stern
Learn how to efficiently monitor Kubernetes workloads by deploying log agents and using two powerful real‑time log aggregation tools—Kubetail and Stern—including installation steps, usage examples, and detailed command‑line options for filtering, coloring, and exporting logs across multiple pods and containers.
Usually when deploying K8S services, a log system is integrated to monitor service operation. To collect logs, a log collection agent is deployed on each node and sends logs to a centralized storage.
Container log storage methods include:
Standard output (stdout/stderr) – the simplest method but lacks classification and filtering.
Container log files – can be tailed in real time, but requires managing file size and quantity.
Log collectors (Fluentd, Logstash) – aggregate logs from multiple containers, adding configuration complexity.
Log drivers (json-file, syslog) – provided by Docker/Kubernetes to forward logs to remote destinations, also with configuration overhead.
Two highly useful real‑time multi‑container log viewing tools are introduced: Kubetail and Stern .
1. Kubetail
Bash script to tail Kubernetes logs from multiple pods at the same time
Kubetail is a simple shell script that aggregates logs from multiple pods, supports colored output and conditional filtering.
1.1 Installation
Homebrew:
<code># install kubetail using brew
$ brew tap johanhaleby/kubetail && brew install kubetail</code>Linux:
<code># download and install
$ wget https://raw.githubusercontent.com/johanhaleby/kubetail/master/kubetail
$ chmod +x kubetail
$ cp kubetail /usr/local/bin</code>zsh plugin:
<code># oh‑my‑zsh
$ cd ~/.oh-my-zsh/custom/plugins/
$ git clone https://github.com/johanhaleby/kubetail.git kubetail
$ vim ~/.zshrc # add kubetail to plugins
$ source ~/.zshrc</code>1.2 Usage
Example commands:
<code># show all pods
$ kubectl get pods -n test
# tail logs from two pods
$ kubetail app2
$ kubetail app1,app2
# specify container
$ kubetail app2 -c container1
# use regex
$ kubetail "^app1|.*my-demo.*" --regex
# color options
$ kubetail app2 -k pod
$ kubetail app2 -k line
$ kubetail app2 -k false</code>Common command‑line flags:
Flag
Meaning
-nSpecify namespace name
-cSpecify container name in a multi‑container pod
-kEnable colored output (pod, line, false)
-bUse line‑buffered mode (default false)
-lLabel filter to ignore pod names
-tSince time (e.g., 5s, 2m, 3h, default 10s)
2. Stern
Multi pod and container log tailing for Kubernetes
Stern, written in Go, aggregates logs from multiple pods and containers with colored output and filtering. It is no longer actively maintained, so use with caution.
2.1 Installation
Homebrew:
<code># install stern
$ brew install stern</code>Linux:
<code># download and install
$ wget https://github.com/wercker/stern/releases/download/1.11.0/stern_linux_amd64
$ chmod +x stern_linux_amd64
$ mv stern_linux_amd64 /usr/local/bin</code>zsh plugin:
<code># bash completion
$ brew install bash-completion
$ source <(brew --prefix)/etc/bash-completion
$ source <(stern --completion=bash)
# zsh completion
$ source <(stern --completion=zsh)</code>2.2 Usage
Example commands:
<code># tail all pods in default namespace
$ stern .
# tail specific container
$ stern app2 --container container1
# specify namespace
$ stern app2 --namespace namespace1
# exclude a container
$ stern --namespace namespace1 --exclude-container container1 .
# tail logs from last 15 minutes
$ stern app2 -t --since 15m
# filter by label across all namespaces
$ stern --all-namespaces -l run=nginx
# select pods by label
$ stern frontend --selector release=canary
# output as JSON and pipe to jq
$ stern backend -o json | jq .
# raw output
$ stern backend -o raw
# custom template
$ stern --template '{{.Message}} ({{.Namespace}}/{{.PodName}}/{{.ContainerName}})' backend
# colored template
$ stern --template '{{.Message}} ({{.Namespace}}/{{color .PodColor .PodName}}/{{color .ContainerColor .ContainerName}})' backend</code>Common flags:
Flag
Default
Purpose
--container .*Container name (regex) when multiple containers in pod
--exclude-containerContainer name to exclude (regex)
--container-state runningTail containers with given state
--timestampsPrint timestamps
--sinceReturn logs newer than given duration
--contextKubernetes context to use
--excludeLog lines to exclude (regex)
--namespaceKubernetes namespace to use
--kubeconfig ~/.kube/configPath to kubeconfig file
--all-namespacesTail across all namespaces
--selectorLabel selector to filter pods
--tail -1Number of lines from end of logs to show (default all)
--color autoForce color output (auto, always, never)
--output defaultPredefined output format (default, raw, json)
templateCustom template for log lines
3. References
johanhaleby/kubetail
wercker/stern
Two highly useful Kubernetes real‑time log viewing tools
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.