thiagowfx's avatar

¬ just serendipity 🍀 (not just serendipity)

Kubernetes: tail logs from pods with stern

• 178 words • 1 min • updated

⚠️ This post is over one year old. It may no longer be up to date or relevant. Opinions may have changed.

You can always use kubectl logs -n {namespace} {pod} [-c {container}] -f to inspect logs from a specific pod1.

Nonetheless that doesn’t scale when you don’t know which pod you want in the first place.

You could start with deployments, dive into replica sets, and then into individual pods, one by one, but… that is tedious and slow.

We can do better with stern:

⎈ Multi pod and container log tailing for Kubernetes

Usage:

stern pod-query [flags]

The pod-query is a regular expression or a Kubernetes resource in the form <resource>/<name>.

Example:

shell
% stern -n argocd argocd

We can also grep for specific strings in log lines with --include:

shell
% stern -n argocd argocd --include level=error

Or grep -v with --exclude.

It’s better to use --include / --exclude than to | grep, because it preserves color attributes (each pod has a different color in the aggregated log output).

The logs stream by default (like tail -f, or kubectl logs -f).


  1. -c to specify a container within it, and -f to stream logs à la tail -f↩︎