2 min read | by Jordi Prats
When troubleshooting applications in Kubernetes, you often need to monitor logs from multiple pods simultaneously. The kubectl logs command provides powerful options to stream logs from several pods at once using label selectors.
In this post, we'll explore how to use kubectl logs with the selector flag to monitor multiple pods in real-time.
The kubectl logs command with the -l
(selector) flag allows us to stream logs from all pods that match specific labels. Here's how to stream logs from all pods with the label app=webapp
:
kubectl logs -l app=webapp -f
This command will:
- Select all pods with the label app=webapp
- Stream logs in follow mode (-f
) to see new log entries in real-time
One drawback of this approach is that we won't see the pod names in the output, which can make it difficult to identify which pod the logs are coming from. To include pod names in the logs, we can use the --prefix
option. It will prepend the pod name to each log line, making it easier to distinguish logs from different pods:
$ kubectl logs -l app=webapp -f --prefix
[pod/eso-external-secrets-79d8477b9b-8w672/external-secrets] {"level":"info","ts":1710592198.2775939,"logger":"provider.aws","msg":"using aws session","region":"us-east-1","external id":"","credentials":null}
[pod/eso-external-secrets-79d8477b9b-8w672/external-secrets] {"level":"info","ts":1710592198.584481,"logger":"provider.aws","msg":"using aws session","region":"us-west-2","external id":"","credentials":null}
[pod/eso-external-secrets-79d8477b9b-8w672/external-secrets] {"level":"info","ts":1710592198.6353924,"logger":"provider.aws","msg":"using aws session","region":"us-west-2","external id":"","credentials":null}
For a more user-friendly output, you can take a look at stern: It provides a colored output with pod names and supports multiple label selectors, making it a great tool for monitoring logs from multiple pods simultaneously.
Posted on 15/05/2025