Viewing Logs In Kubernetes

Fonte: https://medium.com/faun/viewing-logs-in-kubernetes-e055f936e187 Em: 29-01-2020

This article contains how to see logs based on various options available in Kubernetes.

  1. Viewing logs of a pod running a single container inside it. This can be achieved via running command:-

#kubectl -n kube-system logs podname

2. Viewing full logs of a pod running a single container inside it. This will also show the appending logs at run time. This can be achieved via running command:-

#kubectl -n kube-system logs -f podname

3. Viewing logs of a particular container inside a pod running multiple container. Like in below example, i have searched for the pods via label and then viewing logs of one of the containers available. This can be achieved via below commands:-

#kubectl -n kube-system get po -l k8s-app=kube-state-metric

#kubectl -n kube-system logs podname ## This will help you to see the containers available in a multi-container pod and based on that you can view the logs of a particular container using below command.

#kubectl -n kube-system logs podname -c container-name

4. Viewing logs of a pod based on number of lines you want to see. This can be achieved via below command:-

# kubectl -n kube-system logs — tail=10 podname( It’s double hyphen symbol which is being used in front of tail).

5. Viewing logs of a pod based on time. This can be achieved via below command:-

#kubectl -n kube-system logs — since=1h podname(It’s double hyphen symbol which is being used in front of since).

In my case output is blank as there are no logs for last one hour.

6. Viewing logs of a pod based on date-time value. This can be achieved via below command:-

#kubectl -n kube-system log — since-time=“2019–02–06T03:59:40.417Z”

However only since or since-time can be used at a time.

7. Viewing logs of a pod based on labels available for the pod. This can be achieved via bellow command:-

#kubectl -n kube-system logs -l k8s-app=kube-dns

This is valid for pods running with single container inside it.

For pods with multiple container, to view logs via label, we need to provide the container name as well as an argument. This can be achieved via below command:-

#kubectl -n kube-system logs -l k8s-app=kube-state-metrics -c kube-state-metrics

8. We can also see the logs of a previous container of a pod but this only works in case the container exists. This can be achieved via below command:-

#kubectl -n kube-system logs -l k8s-app=kube-dns -p

I think this article will help my follow Kubernetes mates to perform their day to day role in a more subtle way. Keep reading, keep learning. :)