stern
Stern allows you to tail multiple pods on Kubernetes and multiple containers within the pod. Install via x-cmd for instant access and enhanced productivity.
| Language | Go |
| Homepage | https://github.com/stern/stern |
- Using docker module to automatically pull the base image and, on first execution, reconstruct it as a fully runnable software image as needed.
- For subsequent runs, the command quickly launches containers based on the pre-built image, achieving near-instant startup.
- All operations are executed inside the container, completely isolated from the host system, requiring no local dependencies—secure, clean, and leaving no traces.
- When a rebuild is required due to software version or dependency changes, the Docker image build cache is fully leveraged to maximize reuse of downloaded dependencies and build artifacts, significantly reducing rebuild time.
x docker alpine sternxd alp sternx docker ubuntu sternxd ubu sternx docker kali sternxd kal sternx docker debian sternxd deb sternx docker fedora sternxd fed sternx install stern
| /go | sh
|
| darwin/brew | sh
|
| /krew | sh
|
| /asdf | sh
|
stern - Multi-Pod Log Aggregation for Kubernetes
Viewing logs is a fundamental skill when debugging Kubernetes applications, but kubectl logs has clear limitations: it can only tail one Pod at a time, requires re-running after Pod restarts, and mixes logs from multiple containers without distinction. When you have dozens of Pods rolling out, checking them one by one is painfully inefficient.
stern was created to solve exactly this problem. It can tail logs from multiple Pods simultaneously, supports regex matching for Pod names, automatically tracks Pod lifecycles (new Pods are automatically added, deleted Pods are removed), and uses different colors to distinguish between Pods and containers, making log reading immediately clear.
Core Capability: Simultaneous Multi-Pod, Multi-Container Log Tailing
Regex-Based Pod Matching
stern's query parameter supports regular expressions—you don't need to know the full Pod name. If your Deployment is named web-backend, the Pod might be called web-backend-7c8f9d6b5-x2v4p. You simply run:
stern web-backendstern automatically matches all Pods whose names contain web-backend. More flexibly, you can use regex to precisely control the scope:
# Only track web-backend and web-frontend, not web-123
stern "web-\w+"Kubernetes Resource Queries
In addition to regex, stern supports the <resource>/<name> format to directly specify workloads:
# Tail all Pods under deployment/nginx
stern deployment/nginx
# Tail Pods associated with a specific service
stern service/my-serviceSupported resource types include: pod, replicationcontroller, service, daemonset, deployment, replicaset, statefulset, job.
Automatic Multi-Container Aggregation
If a Pod has multiple containers (e.g., main app + sidecar), stern tails logs from all containers by default. You can also use the --container flag to specify viewing only particular containers:
# Only view gateway container logs
stern web-backend --container gateway
# Exclude istio-proxy sidecar logs
stern . --exclude-container istio-proxyAutomatic Pod Lifecycle Tracking
This is a practical feature of stern: while running, if a new Pod is created, stern automatically starts tailing its logs; if a Pod is deleted, it's automatically removed from the tail list. For rolling update scenarios, you can see logs from new Pods without manually re-executing commands.
Color Coding: Distinguish Log Sources at a Glance
stern assigns different colors to different Pods and containers. The default output format is:
[namespace] [pod-name] [container-name] log messageEach Pod has its own color, and different containers within the same Pod are displayed with variations of that color (such as underline or different brightness). This is especially useful when simultaneously tailing dozens of Pods—you can visually locate logs from specific services quickly.
You can control color output with the --color flag: auto (default, decides based on terminal support), always, or never.
Common Commands and Flags
Basic Usage
| Command | Description |
|---|---|
stern pod-query | Tail logs from all Pods matching pod-query |
stern . --all-namespaces | Tail all Pods from all namespaces |
stern app -n production | Tail Pods matching "app" in production namespace |
stern . -n kube-system --tail 0 | Only show new logs from kube-system, no history |
Filtering and Selection
| Flag | Function |
|---|---|
-n, --namespace | Specify namespace, can be used multiple times for multiple namespaces |
-A, --all-namespaces | All namespaces |
-c, --container | Container name filter (regex) |
-E, --exclude-container | Exclude specific containers |
--exclude-pod | Exclude specific Pods |
-e, --exclude | Exclude log lines containing specific content |
-i, --include | Only show log lines containing specific content |
-l, --selector | Filter using label selector |
--field-selector | Filter using field selector (e.g., spec.nodeName=node-1) |
--since | Only show logs within duration, e.g., 15m, 1h |
--tail | Show last N lines, default -1 (all) |
Output Control
| Flag | Function |
|---|---|
-t, --timestamps | Show timestamps (default or short format) |
--timezone | Set timestamp timezone, e.g., Asia/Tokyo |
-o, --output | Output format: default, raw, json, extjson, ppextjson |
--only-log-lines | Only output log content itself |
--no-follow | Exit after displaying existing logs (non-continuous tail) |
--max-log-requests | Limit concurrent log requests, default 50 (follow mode) / 5 (non-follow mode) |
Template Output
stern supports Go templates for custom output formats. Available variables include Message, NodeName, Namespace, PodName, ContainerName, Labels, Annotations, and color functions like color, colorRed, parseJSON, prettyJSON, etc.:
# Custom format: message (node/namespace/pod/container)
stern --template '{{printf "%s (%s/%s/%s/%s)\n" .Message .NodeName .Namespace .PodName .ContainerName}}' backend
# Custom format with colors
stern --template '{{.Message}} ({{.Namespace}}/{{color .PodColor .PodName}}/{{color .ContainerColor .ContainerName}}){{"\n"}}' backend
# JSON output piped to jq
stern backend -o json | jq .Practical Usage Scenarios
Monitoring New Version Logs During Rolling Updates
# Tail all Pods under deployment, new Pods automatically added
stern deployment/my-appTroubleshooting Issues on Specific Nodes
# Only view Pods running on a specific node
stern --all-namespaces --field-selector spec.nodeName=kind-control-planeMulti-Environment Comparison Debugging
Open two terminal windows, tailing staging and production separately:
# Terminal 1
stern my-app --context staging -n default
# Terminal 2
stern my-app --context production -n defaultLogs from different environments are displayed in different colors, making them easy to distinguish visually.
Log Export and Post-Processing
# Export all logs from last 5 minutes, sorted by time
stern --since=5m --no-follow --only-log-lines -A -t . | sort -k4
# JSON format export for programmatic processing
stern my-app -o json --no-follow > logs.jsonViewing Only Not-Ready Pod Logs
# Useful for troubleshooting Pod startup failures
stern . --condition=ready=false --tail=0Configuration File Support
stern supports setting default values via configuration file, default path is ~/.config/stern/config.yaml:
# Default to showing only last 10 lines
tail: 10
# Increase concurrency limit
max-log-requests: 999
# Default to short timestamp format
timestamps: short
# Custom Pod colors (SGR sequences)
pod-colors: "32,33,34,35,36,37"
# Container colors (omitting uses pod-colors)
container-colors: "32;4,33;4,34;4,35;4,36;4,37;4"You can specify a different config file path with the --config flag or STERNCONFIG environment variable.
Comparison with kubectl logs
| Feature | kubectl logs | stern |
|---|---|---|
| Multi-Pod tailing | Not supported | Supported (core feature) |
| Regex Pod name matching | Not supported | Supported |
| Auto-tracking new Pods | Not supported | Supported |
| Color differentiation | Not supported | Supported |
| Multi-container aggregation | Manual specification required | Automatic aggregation |
| Log filtering | Basic support | Rich include/exclude options |
| Output format | Plain text | Supports JSON, custom templates |
Interactive Selection
stern provides the -p flag to launch an interactive prompt, allowing you to select from app.kubernetes.io/instance label values:
stern -p
# Lists all available instance label values for selectionThis is convenient for Helm-managed applications, where you can filter by release name directly.
Running stern in Kubernetes Pods
If you want to run stern from within a Pod in the cluster, you need to create appropriate RBAC permissions:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: stern
rules:
- apiGroups: [""]
resources: ["pods", "pods/log"]
verbs: ["get", "watch", "list"]Then bind this ClusterRole to the ServiceAccount running stern.
stern fills the capability gap of kubectl in log viewing, especially suited for scenarios requiring simultaneous observation of multiple Pod logs. For microservices architectures, rolling update debugging, and multi-environment comparative analysis, it can significantly improve troubleshooting efficiency.
Source:
Help us make these docs great!
All X-CMD docs are generated from command help and multiple data sources. See something that's wrong or unclear? Feel free to let us know through any of these ways~