If you're like me and like to view log events from multiple Kubernetes containers at once in a single terminal, you're in the right place.
In this article, we will see how to view logs of a single container pod with multiple replicas, logs of a particular container from a group of pods, and logs of all containers in a multi-container pod. So, let's dive right in.
Kubernetes Log Commands
1. Stream logs of a single container pod with multiple replicas
In Kubernetes, it's all about labels and selectors. And I'm going to demonstrate commands using labels. In case you are not familiar with how to get label details from a pod or how to add a label to a pod, then jump to the "All About Labels" section of this post.
Note: I prefer using an alias; here, 'k' is an alias for kubectl. set it as
alias k=kubectl
k logs -f -l <pod-label-value> --prefix
Note:
--prefixPrefix each log line with log source (pod and container names)
E.g.
k logs -f -l app-name=example-app --prefix
Note: here
app-name=example-applabel is on the pod. If you would like to stream logs from multiple pods from different application deployments within a namespace, then use a common label thatβs present across all pods, or add such a label.
2. Stream logs of a particular container of a multi-container pod with multiple replicas
k logs -f -l <pod-label-value> --prefix -c <container-name>
E.g.
k logs -f -l app-name=example-app --prefix -c example-conn
Note: here
app-name=example-applabel is on the pod, andexample-connis the container name.
3. Stream logs of all containers of a multi-container pod with multiple replicas
a. Using label
k logs -f -l <pod-label-value> --prefix -all-containers
E.g.
k logs -f -l app-name=example-app --prefix -all-containers
b. Using the deployment name
k logs -f deploy/<deployment-name> --prefix -all-containers
E.g.
k logs -f deply/example-deployment --prefix -all-containers
All About Labels
1. Show labels associated with a pod
k get po <pod-name> --show-labels
2. Add a label on running pod
k label po <pod-name> app-name=example-app
If you have reached here, then I made a satisfactory effort to keep you reading. Please be kind enough to leave any comments or ask for any corrections. Happy Kuberneting!
Top comments (0)