Get logs of Kubernetes pods in a loop

9/4/2020

I am interested in grabbing the logs of multiple pods that contain "test" in name, but using json and jq. The first step was to find out the names that contain that particular string - in my case, "test":

kubectl get pods -n test -o json | jq '.items[] | select(.metadata.name|test("test"))| .metadata.name'

I had this answer with the help of someone on Stackoverflow.

Now I would like to find out the command that grabs the logs of each of these pods that contain "test" name. I would like to have something like the following command, but unfortunately this way doesn't work as I guess the first command return all pods in one go on multiple lines:

kubectl logs ``kubectl get pods -n test -o json | jq '.items[] | select(.metadata.name|test("test"))| .metadata.name'`` -n test

Is any way to get the logs in this way with some extra arguments to make it work?

Thanks.

-- Albert
kubernetes
kubernetes-pod
loops

3 Answers

9/4/2020

Use a loop to get all pod's log, try this:

for pod in `kubectl get po -o json |  jq '.items[] | select(.metadata.name|contains("test"))| .metadata.name' | sed 's/"//g'` ; do
  echo $pod
  kubectl logs $pod 
done
-- Kun Li
Source: StackOverflow

9/5/2020

To summarize all the options you have

@Kun answer is working solution that give you all the logs to output. +1 for that. With his solution you will achieve what you want, but personally I would think about redirecting logs to files for convenience. Logs can be(and most probably will be) huge - you should have an option to view them separately.

For this you can use @Eduardo tip and install kubelogs

It is a bash script that uses your current kubectl context to interactively select namespaces and multiple pods to download logs from. It basically runs kubectl logs in a loop for all containers, redirecting the logs to local files.

Sometimes you may want to watch logs live - in that case kubetail would be good option

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.

Also there is a paid solution for more advanced needs(if for some reason you dont use ELK): SolarWinds Papertrail

SolarWinds® Papertrail™ is a cloud-hosted log management solution that lets you live tail your logs from a central location. Using Papertrail, you can view real-time log events from your entire Kubernetes cluster in a single browser window.

When a log event is sent from Kubernetes to Papertrail, Papertrail records the log’s contents along with its timestamp and origin pod. You can view these logs in a continuous stream in your browser using the Papertrail Event Viewer, as well as the Papertrail CLI client or Papertrail HTTP API. Papertrail shows all logs by default, but you can limit these to a specific pod, node, or deployment using a flexible search syntax.

Here is great article how to use is if some reasons you may want to try it: How to “Live Tail” Kubernetes Logs

-- Vit
Source: StackOverflow

10/1/2021

You can also get logs by app name if you included it in your deployment.yml that will find all the pod logs.

kubectl logs -l app=test -n test

enter image description here

-- Ethan Miller
Source: StackOverflow