DEV Community

Cover image for Tail logs from multiple Kubernetes pods the easy way
Geshan Manandhar
Geshan Manandhar

Posted on • Originally published at theiconic.tech

Tail logs from multiple Kubernetes pods the easy way

Kubernetes (a.k.a K8s) is the de-facto standard of container orchestration software backed by Google and one of the most active open source projects. If you are using Docker it is very likely that you are using Kubernetes or at least have heard about it.

When using Kubernetes and kubectl 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
Enter fullscreen mode Exit fullscreen mode

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 containersKubectl 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 when you want to get logs from multiple Kubernetes objects like Service, Deployment or Job/CronJob.

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
Enter fullscreen mode Exit fullscreen mode

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 containersStern 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 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 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 — 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 — 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 — 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

Top comments (0)