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] QUERYOptions 14
Most used
Options
Nothing matches this filter.
Examples 11
$ run as a normal user# run as root (or with sudo)
1All pods of a deployment
stern -n shop deployment/shop2Pods by name regex in all namespaces
stern -A '^ingress-nginx'3Only errors from the last 30 minutes
stern -n shop shop --since 30m -i 'ERROR|panic'4Pods with a label
stern -n shop -l app=shop,tier=api5Only the app container, not the sidecar
stern -n shop shop -c app6Hide health check lines
stern -n shop shop -e 'GET /health'7With timestamps, last 50 lines, no follow
stern -n shop shop --tail 50 -t --no-follow8JSON logs to jq
stern -n shop shop -o raw | jq -r 'select(.level=="error") | .msg'9Watch a rollout in a second terminal
kubectl rollout restart deploy/shop -n shop stern -n shop deployment/shop --since 1m10Logs of a CronJob's pods
stern -n jobs '^backup-' --since 2h11Another cluster
stern --context prod -n shop shop
Nothing matches this filter.
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
kubectl logs -f -l app=shop --all-containers --prefixis the built-in, simpler version.
Exit codes
0 | OK. |
1 | Error. |