Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

stern

Follow the logs of many pods and containers at once, with colours per pod — great during a rollout or an incident.

Level
DevOps tool
Group
Containers and Kubernetes
Needs root
No
Package
stern

What it does

kubectl logs shows one pod. stern takes a pod query (a regex, a deployment, a label selector) and follows the logs of every matching pod and container, also of new pods that start later. Each pod gets its own colour and a prefix.

How to write it

stern [OPTIONS] QUERY

Options 14

Most used

Options

QUERY
Pod name regex, or deployment/NAME, statefulset/NAME.
-n, --namespace NS
Namespace (can repeat).
-A, --all-namespaces
All namespaces.
-l, --selector LABELS
Pods with these labels.
-c, --container REGEX
Only matching containers.
-s, --since 15m
Logs from the last 15 minutes.
--tail N
Start with the last N lines.
-i, --include REGEX
Only lines that match.
-e, --exclude REGEX
Hide lines that match.
-o, --output raw|json|extjson
Output format.
-t, --timestamps
Show timestamps.
--context NAME
Use another kube context.
--no-follow
Print and exit (do not follow).
--max-log-requests N
Limit parallel log streams.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1All pods of a deployment

    stern -n shop deployment/shop
  2. 2Pods by name regex in all namespaces

    stern -A '^ingress-nginx'
  3. 3Only errors from the last 30 minutes

    stern -n shop shop --since 30m -i 'ERROR|panic'
  4. 4Pods with a label

    stern -n shop -l app=shop,tier=api
  5. 5Only the app container, not the sidecar

    stern -n shop shop -c app
  6. 6Hide health check lines

    stern -n shop shop -e 'GET /health'
  7. 7With timestamps, last 50 lines, no follow

    stern -n shop shop --tail 50 -t --no-follow
  8. 8JSON logs to jq

    stern -n shop shop -o raw | jq -r 'select(.level=="error") | .msg'
  9. 9Watch a rollout in a second terminal

    kubectl rollout restart deploy/shop -n shop
    stern -n shop deployment/shop --since 1m
  10. 10Logs of a CronJob's pods

    stern -n jobs '^backup-' --since 2h
  11. 11Another cluster

    stern --context prod -n shop shop

Watch out

  • Many pods mean many log streams to the API server; filter with a namespace and a selector.
  • stern shows only what the kubelet still has; old logs need a log system (Loki, Elasticsearch).

Tips to remember

Exit codes

0OK.
1Error.

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man stern