When you have multiple pods running in a Kubernetes cluster โ and you definitly should for highly available services, it's important to be able to view logs from all of them in one place. This can help you troubleshoot issues, monitor performance, and gain insights into how your applications are behaving.
In this blog post, we'll explore how to view live logs from multiple pods in Kubernetes. We'll cover two different approaches, including using Aptakube (Desktop GUI for Kubernetes) and kubectl (CLI). Let's get started!
Using Aptakube
As Aptakube is a graphical tool, it's better to show than tell. Here's a quick video of how to view logs from multiple nginx pods.
In a nutshell, the process is as follows:
- Find the deployment that manages the pods you want to view logs from, in this case, it is the
nginx
deployment; - Right click and select
Logs
; - Aptakube will automatically find all pods that are part of the deployment and start streaming logs from all of them;
How to use Aptakube's Log Viewer
Logs are aggregated based on all pods owned by the resource. You can find the Logs tab on the Resource View of Pods, Deployments, ReplicaSets, DaemonSets, StatefulSets, and Services.
While Services don't own pods, they often target pods using their selector, so we show logs from all selected pods.
- Filter: Enter text or regex to filter the logs. Clear the input to remove the filter and view all logs.
- Pods & Containers: Select which pods and containers would you like to view the logs from. Deselecting a pod or container will not remove its logs from the view, but it'll stop streaming new logs from them.
- Time Since: Defines how far back to look for logs.
Live Tail
means that only new logs are shown. Useful to view real-time logs.Terminated
will show logs from previously terminated containers (if any). Useful to view logs from crashed/errored containers.{X}h ago or {X}d ago
will show logs from the last X hours or days.
- Expand: Removes the sidebar and expands the log viewer to a full window. Combine this with your Operating System's fullscreen mode to get the most out of the logs view.
- Timestamp: Toggles the timestamp generated by Kubernetes for each log.
- Download: Download all visible logs as a CSV file so you can analyze them offline with your favorite tool.
Interested in learning more about what you can do with Aptakube? Take a look at all the other features.
Using kubectl
With kubectl you can use the kubectl logs
command. This command can be used to view logs from a single pod, but you can also use it to view logs from multiple simulataneously.
For a single pod you can use the following command:
kubectl logs {target} -f
{target}
is the name of the pod you want to view logs from;-f
flags tells kubectl to keep the connection open and stream logs as they are generated;
While most people are probably using this command to view logs from a single pod, very few actually now it also works for multiple pods.
To view logs from all pods in a deployment, you can use the following command:
kubectl logs deployment/{deployment-name} -f
Easy right? You can also use this command to view logs from pods managed by replica sets, stateful sets, and daemon sets.
A few other flags you might want to use are:
--previous=true
will show logs from terminated pods, useful if you want to view logs from containers that have crashed/errored--all-containers=true
useful if you have multiple containers in a pod, or you can use-c
to select a specific container--timestamps=true
include timestamps on each line in the log output--since=10s
only return logs newer than a relative duration like 5s, 2m, or 3h.
Which one to use?
It comes down to preference between a GUI or a CLI, and what you're more comfortable with.
Aptakube is pretty much batteries included for all you need in relation to logs, while in kubectl you'll need to pipe other commands/instructions to get things like filtering and exporting to files.
Aptakube also distinguish between logs from different pods and containers by using different colors, while kubectl will just show you all logs in one stream.