Monday 24 May 2021

Kubernetes - How to check logs of a running and crashed pods

 This is the most common issue that every DevOps guy faces, while working on Kubernetes. One day suddenly you will find that one of the pod you are working is in bad state. And you dont know what went wrong. So as a DevOps engineer you need to be aware of how to view logs of running pod or crashed pod.


First you need to know which pod you want see logs for, to do that

Get the pods

kubectl get pods -A      To list all pods irrespective of namespace

kubectl get pods -n <namespace>  To get list of pods from a specific namespace

kubectl get pods  To get list of pods from default namespace


[devopsvm@dev ~]$ kubectl get pods 

NAMESPACE     NAME                                                    READY   STATUS      RESTARTS   AGE

default       jenkins-master-wed66fcfc-kasdas                      1/1     Running           1               18d

default       nfs-client-provisioner-6756-75vc5q                 1/1     Running         0               86d


Get logs of running pods

If pod running in default namespace

kubectl log jenkins-master-wed66fcfc-kasdas  


if pod running in a different namespace

kubectl log jenkins-master-wed66fcfc-kasdas  -n <namespace>


To see logs in realtime (use -f option)

-f option gives you logs in real time on your screen, to exit press CTRL+C or CTRL+Z

kubectl logs -f jenkins-master-wed66fcfc-kasdas  


Get logs of crashed pods (use -p option)

-p or --previous option give option to see logs of a pod which got restarted or crashed. In above case you see jenkins is restarted 1 time..so if you want to see like why it got restarted, use either of the option.


kubectl logs jenkins-master-wed66fcfc-kasdas -p 

This will show logs of pod before it got crashed, best usage in realtime to troubleshoot why a pod is restarted.


Get logs a specific container inside a pod having multiple containers

When you have a pod having multiple containers running in it, then this way we can find logs of that specific container.

[devopsvm@dev ~]$ kubectl get pods 

NAMESPACE     NAME                                                    READY   STATUS      RESTARTS   AGE

default       jenkins-master-wed66fcfc-kasdas                      2/2     Running           1               18d


If you see 2/2 in under READY , it means that pod is having two containers inside it.

so if you try to run just kubectl logs jenkins-master-wed66fcfc-kasdas it will throw you an error saying "container name must be specified"

error: a container name must be specified for pod  jenkins-master-wed66fcfc-kasdas 
choose one of: [master slave]

From above error you got to know two containers are running master and slave.

use -c option to pass container name like

kubectl logs  jenkins-master-wed66fcfc-kasdas  -c slave

kubectl logs  jenkins-master-wed66fcfc-kasdas  -c slave -n <namespace>

kubectl logs  jenkins-master-wed66fcfc-kasdas  -c slave -p


you can pass namespace and -p options as well to get previous logs.


Above commands are mostly used in troubleshooting pod while running and crashed state.


No comments:

Post a Comment

Tricks and Tips