In this blog, we will showcase how to use kubectl get logs to check kubernetes logs. When managing pods in Kubernetes, checking logs for troubleshooting or monitoring is a fundamental task. Here are the commands we can use:

Get logs for Pod in a specific namespace

 

kubectl logs POD -n NAMESPACE
e.g. kubectl logs nginx-v1-f8f4f7b5bf -n production

For default namespace, you can omit the namespace parameter e.g.

kubectl logs nginx-v1-f8f4f7b5bf

 

Get streaming logs for a Pod

To stream logs continously, we can use the -f option i.e.

kubectl logs nginx-v1-f8f4f7b5bf -n production -f

 

Get logs for multiple Pods using labels 

 

First, display labels for pods within a specific namespace

kubectl get pods --show-labels -n NAMESPACE 

Then, use a specific label to retrieve logs across multiple pods within that namespace

kubectl logs -l label -n NAMESPACE
e.g. kubectl logs -l app=nginx-v1 -n production

 

Get logs for multiple containers 

We can use Kubectl get logs

kubectl logs nginx-v1 --all-containers=true

 

Get logs of previous terminated container

 

Return logs from previous terminated java container from pod app-1

kubectl logs -p -c java app-1

 

Output only last N lines of code in Kubernetes logs

 

To display only last 10 lines of logs in pod, we can use –tail N

  kubectl logs --tail=10 java

 

Return last X hours of Kubernetes get logs

 

To display only last 1h of logs, we can use the –since Xh

  kubectl logs --since=1h java

 

In this blog, we covered how to use kubectl get logs commands to easily view Kubernetes logs. We also showcased how to pass few arguments to view logs for multiple pods, get logs of previously terminated container, return logs for last 1 hour using since option,  output only specific lines of code using tail option and few other combinations.

For more details on Kubernetes logging, please check this link

Also, please checkout our other blogs here 

Leave a Reply

Your email address will not be published. Required fields are marked *