How to get logs of two pods without opening two terminals and using kubectl tail command?

Viewed 4382

I am new to kubernetes and want to follow the logs for two pods at the same time. But to do that i have to open two terminals and use the following command

kubectl logs -f <POD_NAME> -n namespace

I was wondering if there was some better way to get the logs without opening multiple terminals as this creates a serious problem if the number of pods increase.

I am not looking for some logging tool but some easy to set up way which can help me in achieving this.

Thanks in advance.

2 Answers

As already explained in the previous you can use the labels if the pods shares any. If you have multiple containers in a pod you can use this command:

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

If you are looking for some simple tool you have couple of options:

Stern allows you to tail multiple pods on Kubernetes and multiple containers within the pod. Each result is color coded for quicker debugging. The query is a regular expression so the pod name can easily be filtered and you don't need to specify the exact id (for instance omitting the deployment id). If a pod is deleted it gets removed from tail and if a new pod is added it automatically gets tailed.

With a simple command like this:

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

Stern will tail logs from the given namespace for that app name since last 30 minutes.

It`s a bash script that enables you to aggregate (tail/follow) logs from multiple pods into one stream. This is the same as running "kubectl logs -f " but for multiple pods.

Streams logs from all containers of all matched pods. Match pods by service, replicaset, deployment, and others. Adjusts to a changing cluster - pods are added and removed from logging as they fall in or out of the selection.

It`s tail -f experience for Kubernetes Pods

Do they share the same labels? If they do, then you can filter them by using kubectl logs itself. For example:

kubectl logs -n <namespace> -l <label-key>=<label-value>
Related