Various ways to tail Kubernetes pod logs
I’ve been using Kubernetes as the main container orchestration software for almost two years and during that period I’ve tried different ways to tail container logs, some of which were even third party cloud logging services like Google Logging, but still the fastest way is using a CLI tool and thankfully there are a couple to choose from.
Kubectl
Platforms supported: Linux, MacOS, Windows
Kubectl remains the default go to tool for tailing containers logs.
Usage:
kubectl logs [-f] [-p] (POD | TYPE/NAME) [-c CONTAINER]
Using it with -f
will allow you to stream the logs, -p
is for printing the logs of the previous pod/container instance and -c
is used to specify the container name if you have a pod in which resides multiple containers.
More about this robust kubectl
command can be found at Kubernetes homepage.
Stern
Platforms supported: Linux, MacOS
My all time favorite although I started using it really late, stern
is a tool made by Wercker which recently were acquired by Oracle and it allows you to tail multiple pods/containers. The results are colored coded to improve readability and debugging and it supports regular expressions use in query so you can easily filter K8s pods/containers without the need to specify the exact id.
Usage:
stern pod-query [flags]
For the whole list of flags and other features you can checkout their Github page.
Kubetail
Platforms supported: Linux, MacOS
If the need arises to tail multiple logs from multiple pods Kubetail is an option that you surely can consider. It’s a robust bash script that does kubectl logs -f
but for multiple pods.
A few noteworthy features that it supports:
- Regular expressions:
kubetail "^app1|.*my-demo.*" --regex
- Tail multiple pods:
kubetail app1,app2
- Tail multiple specific containers:
kubetail app2 -c container1 -c container2
Check their Github page for more.
Kail
Platforms supported: Linux, MacOS, Windows
Kail is another worthy mention, it does the same job as stern and kubetail. Their Github page contains detailed information about the features it supports.
EndPost
Basically these are some of the tools out there that are used to stream pod logs. I mostly go with Kubectl but started recently to use Stern as it offers more flexibility and features.