Files
container.training/slides/kube/logs-cli.md
Jerome Petazzoni 22fb898267 Show how to install and use Stern
Stern is super cool to stream the logs of multiple
containers.
2018-04-08 16:26:08 -05:00

2.9 KiB

Accessing logs from the CLI

  • The kubectl logs commands has limitations:

    • it cannot stream logs from multiple pods at a time

    • when showing logs from multiple pods, it mixes them all

  • We are going to see how to do better


Doing it manually

  • We could (if we were so inclined), write a program or script that would:

    • take a selector as an argument

    • enumerate all pods matching that selector (with kubectl get -l ...)

    • fork one kubectl logs --follow ... command per container

    • annotate the logs (the output of each kubectl logs ... process) with their origin

    • preserve ordering by using kubectl logs --timestamps ... and merge the output

--

  • We could do it, but thankfully, others did it for us already!

Stern

Stern is an open source project by Wercker.

From the README:

Stern allows you to tail multiple pods on Kubernetes and multiple containers within the pod. Each result is color coded for quicker debugging.

The query is a regular expression so the pod name can easily be filtered and you don't need to specify the exact id (for instance omitting the deployment id). If a pod is deleted it gets removed from tail and if a new is added it automatically gets tailed.

Exactly what we need!


Installing Stern

  • For simplicity, let's just grab a binary release

.exercise[

  • Download a binary release from GitHub:
    sudo curl -L -o /usr/local/bin/stern \
         https://github.com/wercker/stern/releases/download/1.6.0/stern_linux_amd64
    sudo chmod +x /usr/local/bin/stern
    

]

(The installation will differ if you want to run Stern on anything else than a Linux amd64 machine.)


Using Stern

  • There are two ways to specify the pods for which we want to see the logs:

    • -l followed by a selector expression (like with many kubectl commands)

    • with a "pod query", i.e. a regex used to match pod names

  • These two ways can be combined if necessary

.exercise[

  • View the logs for all the rng containers:
    stern rng
    

]


Stern convenient options

  • The --tail N flag shows the last N lines for each container

    (Instead of showing the logs since the creation of the container)

  • The -t / --timestamps flag shows timestamps

  • The --all-namespaces flag is self-explanatory

.exercise[

  • View what's up with the weave system containers:
    stern --tail 1 --timestamps --all-namespaces weave
    

]


Using Stern with a selector

  • When specifying a selector, we can omit the value for a label

  • This will match all objects having that label (regardless of the value)

  • Everything created with kubectl run has a label run

  • We can use that property to view the logs of all the pods created with kubectl run

.exercise[

  • View the logs for all the things started with kubectl run:
    stern -l run
    

]