How do I get logs from all pods of a Kubernetes replication controller?
Running kubectl logs
shows me the stderr/stdout of one Kubernetes container.
How can I get the aggregated stderr/stdout of a set of pods, preferably those created by a certain replication controller?
Solution 1:
You can use labels
kubectl logs -l app=elasticsearch
Solution 2:
I've created a small bash script called kubetail
that makes this possible. For example to tail all logs for pods named "app1" you can do:
kubetail app1
You can find the script here.
Solution 3:
You can get the logs from multiple containers using labels as Adrian Ng suggested:
kubectl logs --selector app=yourappname
In case you have a pod with multiple containers, the above command is going to fail and you'll need to specify the container name:
kubectl logs --selector app=yourappname --container yourcontainername
Note: If you want to see which labels are available to you, the following command will list them all:
kubectl get pod <one of your pods> -o template --template='{{.metadata.labels}}'
...where the output will look something like
map[app:yourappname controller-revision-hash:598302898 pod-template-generation:1]
Note that some of the labels may not be shared by other pods - picking "app" seems like the easiest one