#devops #kubernetes #tools

Recently, the Stern utility I use for logging in Kubernetes has been updated and got a new maintainer. You can read my original post about this utility here.

After updating to that version, I figured out that there is a difference in the way the line-endings are treated compared to the old version. If you specify a custom template like I do, there is now no longer a line ending after each message, screwing up the whole output:

1$ stern staging --template '{{.PodName}} | {{.Message}}'

To get the output correct again, you need to add the line-ending which can be done as follows:

1$ stern staging --template '{{.PodName}} | {{.Message}}{{"\n"}}'

The commit which changed this behaviour is this one.

Another tip when using Stern, make proper use of labels to get the output you want. By default, the query specified in the command line queries a single pod as you can figure out from the command usage:

 1Tail multiple pods and containers from Kubernetes
 4  stern pod-query [flags]
 7  -A, --all-namespaces             If present, tail across all namespaces. A specific namespace is ignored even if specified with --namespace.
 8      --color string               Color output. Can be 'always', 'never', or 'auto' (default "auto")
 9      --completion string          Outputs stern command-line completion code for the specified shell. Can be 'bash' or 'zsh'
10  -c, --container string           Container name when multiple containers in pod (default ".*")
11      --container-state string     If present, tail containers with status in running, waiting or terminated. Default to running. (default "running")
12      --context string             Kubernetes context to use. Default to current context configured in kubeconfig.
13  -e, --exclude strings            Regex of log lines to exclude
14  -E, --exclude-container string   Exclude a Container name
15  -h, --help                       help for stern
16  -i, --include strings            Regex of log lines to include
17      --init-containers            Include or exclude init containers (default true)
18      --kubeconfig string          Path to kubeconfig file to use
19  -n, --namespace string           Kubernetes namespace to use. Default to namespace configured in Kubernetes context.
20  -o, --output string              Specify predefined template. Currently support: [default, raw, json] (default "default")
21  -l, --selector string            Selector (label query) to filter on. If present, default to ".*" for the pod-query.
22  -s, --since duration             Return logs newer than a relative duration like 5s, 2m, or 3h. Defaults to 48h.
23      --tail int                   The number of lines from the end of the logs to show. Defaults to -1, showing all logs. (default -1)
24      --template string            Template to use for log lines, leave empty to use --output flag
25  -t, --timestamps                 Print timestamps
26  -v, --version                    Print the version and exit

This is anoying if you have multiple replicas of a pod. The easiest solution is to make sure you have a common label for these pods when you deploy them. Doing so enables you to use -l to query on the label instead:

Imagine you use the following deployment description:

 1apiVersion: apps/v1
 2kind: Deployment
 4  name: example-server
 6  replicas: 3
 7  revisionHistoryLimit: 0
 8  selector:
 9    matchLabels:
10      app: example-server
11  template:
12    metadata:
13      labels:
14        app: example-server
15    spec:
16      containers:
17      - name: general-example-environ-server
18        image: pieterclaerhout/example-environ-server:1.7

As we have set the proper labels, we can now query all 3 replicas of this pod using:

1stern -l "app=example-server" --template '{{.PodName}} | {{.Message}}{{"\n"}}'