Using Kail for viewing Kubernetes logs

July 9, 2019

When debugging your applications on Kubernetes, getting the right information from the different pods isn’t trivial. Especially if you are using deployments which use multiple replicas, checking which pod is the one you need is tricky.

Before Kubernetes, it was easy to SSH into a server and use the tail command to view the logfile(s). You can still do this, but it’s quite cumbersome.

First, you need to figure out the correct name of the pod using kubectl:

$ kubectl get pods
NAME                                                       READY   STATUS             RESTARTS   AGE
babette-6f7bc7ff8c-pmkzv                                   1/1     Running            0          25h
download-server-8d674d785-ppbfz                            1/1     Running            0          4d21h
job-server-5db7f9468c-wcncp                                1/1     Running            0          26h

Once you have the name, you need to use kubectl to dump the logfile:

$ kubectl log babette-6f7bc7ff8c-pmkzv
<log content goes here>

The log command is really limited and doesn’t e.g. offer the option to follow the logfile.

In these cases, a utility such as kail (the abbreviation of Kubernetes tail) is there to help you.

If you are on a mac, you can install it as follows:

$ brew tap boz/repo
$ brew install boz/repo/kail

For other platforms, you can just download the binaries.

You can then use it to monitor multiple pods as once in the same way as you use tail. I prefer to add proper labels to my pods so that I can select on them. If you label all your pods for the staging environment with a label environment set staging, you can follow the logs of all these pods with this simple command:

$ kail -l environment=staging

It will prepend each logline with the name of the pod as soon as it comes in.

You can find all the options and the source code for kail here.