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
Not sure if this is a new thing, but with deployments it is possible to do it like this:
kubectl logs deployment/app1
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.
One option is to set up cluster logging via Fluentd/ElasticSearch as described at https://kubernetes.io/docs/user-guide/logging/elasticsearch/. Once logs are in ES, it's easy to apply filters in Kibana to view logs from certain containers.
I use this simple script to get a log from the pods of a deployment:
#!/usr/bin/env bash
DEPLOYMENT=$1
for p in $(kubectl get pods | grep ^${DEPLOYMENT}- | cut -f 1 -d ' '); do
echo ---------------------------
echo $p
echo ---------------------------
kubectl logs $p
done
Gist of the script
Usage: log_deployment.sh "deployment-name".
Script will then show log of all pods that start with that "deployment-name".
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
You can get help from kubectl logs -h
and according the info,
kubectl logs -f deployment/myapp -c myapp --tail 100
-c
is the container name and --tail
will show the latest num lines,but this will choose one pod of the deployment, not all pods. This is something you have to bear in mind.
kubectl logs -l app=myapp -c myapp --tail 100
If you want to show logs of all pods, you can use -l
and specify a lable, but at the same time -f
won't be used.