Tail logs from multiple Kubernetes pods the easy way
Tags: kubernetes, logs, docker, container
Published: 2019-06-07 07:22:33


[Kubernetes](https://kubernetes.io/) (a.k.a K8s) is the de-facto standard of container orchestration software backed by Google and one of the most active [open source](https://github.com/kubernetes/kubernetes) projects. If you are using [Docker](https://www.docker.com/) it is very likely that you are using Kubernetes or at least have heard about it.
> # When using Kubernetes and [kubectl](https://kubernetes.io/docs/reference/kubectl/overview/) have you ever wished there was a way to tail logs from multiple containers of the same deployment or service.

This post will detail ways to do it for a better developer (or should I say DevOps/SRE) experience:

## Prerequisite

It is assumed that you are aware of concepts like containers, Docker and are used to the Kubernetes and kubectl. You don’t need to be a Kubernetes expert but do need to understand the basics of Kubernetes.

## You can tail logs from multiple pods/containers with kubectl

You can tail logs from multiple pods using the beloved native Kubernetes command line tool kubectl. It is pretty easy to do so like below:

    kubectl -n <namespace> logs -f deployment/<app-name> --all-containers=true --since=10m

The command is self-explaining, it says to follow logs for that deployment from given namespace for all containers since past 10 minutes. You can also use service in place of deployment. Here it is in action, I am using a custom namespace below with the -n parameter:

![Kubectl logs for deployment with all containers](https://cdn-images-1.medium.com/max/2492/1*1fs6pD9BAq7gHlc96s9N3Q.jpeg)*Kubectl logs for deployment with all containers*

This works fine as long as you just have a deployment or service but let’s say if you have a cron job with your deployment this won’t be enough. This is where the next tool becomes useful:

## Meet Stern, kubectl logs on steroids

You can use [Stern](https://github.com/wercker/stern) when you want to get logs from multiple Kubernetes objects like [Service](https://kubernetes.io/docs/concepts/services-networking/service/), [Deployment](https://kubernetes.io/docs/concepts/services-networking/service/) or [Job](https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/)/[CronJob](https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/).
> # Stern lets you get color-coded logs from multiple containers inside the pods from all related Kubernetes objects of your application/microservice.

With a simple command like below, you can tail logs from more relevant containers:

    stern -n <namespace> <app-name> -t --since 10m

The command is pretty simple here, too. Stern tails logs from the given namespace for that app name since last 10 minutes. In the case of Stern, we can see logs not only from one Kubernetes object like deployment or service but all related ones like below:

![Stern logs with color-coded containers](https://cdn-images-1.medium.com/max/2312/1*f9FFFu6wNuqXmPAIlt74Cw.png)*Stern logs with color-coded containers*

Notice here that the containers are color-coded which makes it easy to distinguish the logs. Stern was featured in official Kubenetes [blog](https://kubernetes.io/blog/2016/10/tail-kubernetes-with-stern/) in 2016. Stern is really helpful when you want to get an overall view of the application logs.

If you use a log shipper and log viewer application like [Logentries](https://logentries.com/) it will be a different experience. Still getting live logs on the command line is very helpful when you are debugging or want to know what is happening now on the app.

## Other options to tail logs from multiple containers

Of course, there are other options to tail logs from multiple containers. Some of them are below:

* [Kubetail](https://github.com/johanhaleby/kubetail) — A tiny bash script which is similar to Stern. I did not use it because I didn’t find an easy way to send the kubeconfig file on every run.

* [Kail](https://github.com/boz/kail) — Seems to do a similar job like Stern and Kubetail. Have not used it to comment about it. You could give it a try.

* [K8stail](https://github.com/dtan4/k8stail) — Have not used it but looks like it lacks the control of Kubetail and Stern. If you use it don’t forget to comment about it below :)

## Conclusion

Kubernetes is a great piece of software but it does add an extra layer of complexity. For us software engineers the faster we can see logs the sooner we can solve issues. So if you are using Kubernetes and have access to view logs on your Kubernetes cluster — setup your CLI with some aliases and get going to tail logs from your apps in real-time.

---
First published on [THE ICONIC Tech Blog](https://theiconic.tech/tail-logs-from-multiple-kubernetes-pods-the-easy-way-71401b84d7f)