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:
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 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:
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:
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 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.
Of course, there are other options to tail logs from multiple containers. Some of them are below:
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.