Saturday, 6 August 2022

Kubernetes Pod Logs - View Latest Logs Faster

 We have discussed about enabling detailed request, resposnse logs in ASP.NET Core APIs in the post "Log All Requests in ASP.NET Core API". In a contianerinzed API running in a Kubernetes cluster, say AKS, you would be interested in reading these logs to debug some error or for quick monitoring on the requests made. Implementing a proper monitoring solution using ELK for Kubernetes is more useful, however, doing a quick look at logs of contianers/pods will help as well at times. When you run  kubectl logs podname-xxxxxxxxxx-xxxxxx -n k8snamesapce you will get the entire log of a particular pod. But depending on pod age it may be a long log and you might only be interested in couple of minutes or last hour details to have a quick look. Let's see how we can limit and read pod logs for a time frame or number of log lines.

Limit log view for last few seconds, minutes or hours is possible by passing the --since argument. TO get on last 30 seconds log you can run command as below. We can use --timestamps=true to see the timestamp of logs.

kubectl logs podname-xxxxxxxxxx-xxxxxx -n k8snamesapce --timestamps=true --since 30s

For --since you can give seconds (s), minutes (m) or hours (h) as shown below.

kubectl logs podname-xxxxxxxxxx-xxxxxx -n k8snamesapce --since 30s

kubectl logs podname-xxxxxxxxxx-xxxxxx -n k8snamesapce --since 5m

kubectl logs podname-xxxxxxxxxx-xxxxxx -n k8snamesapce --since 2h

You can use --since-time with rfc3339 date format. It will give logs from the defined date time. You cannot use --since and --since-time together. You need to use either one at a time.

kubectl logs podname-xxxxxxxxxx-xxxxxx -n k8snamesapce --timestamps=true --since-time 2022-08-19T14:48:00.00Z

Another option is to use --tail . You can specify the number of latest log lines you need. 

kubectl logs podname-xxxxxxxxxx-xxxxxx -n k8snamesapce --timestamps=true --tail nooflines

You can use --tail with --since or --since-time as well. By default --tail value is -1 for returning all logs.

No comments:

Popular Posts