Application logs can be retrieved from a Pod in Kubernetes using kubectl
command.
In this note i will show how to get logs from a running Pod (including all replicas) and a previously crashed Pod.
I will also show how to tail and follow logs from a Pod using kubectl
command.
Cool Tip: List Pods in Kubernetes cluster! Read more →
Get Pod Logs using Kubectl
To get logs from a Pod in Kubernetes, firstly it’s required to find out the name of the Pod or the label associated with the Pod:
$ kubectl get pods --show-labels
Get logs from a Pod:
$ kubectl logs <podName>
If a Pod has previously crashed, you can access logs from the previous Pod with:
$ kubectl logs --previous <podName>
If there are several replicas of a Pod, that has an associated label (e.g. app=my-app
), you can use it to view logs from all Pods with that label:
$ kubectl logs -l app=my-app
Cool Tip: Increase kubectl
verbosity for better debugging! Read more →
Tail Logs
The commands above show all logs that have been collected during a lifetime of a Pod, so it may take some time to display them all.
There is a way to tail logs using the kubectl
command, e.g. to tail the last 100 lines of logs from a Pod, execute:
$ kubectl logs --tail=100 <podName>
To show logs from a Pod written in the last hour:
$ kubectl logs --since=1h <podName>
In the last 15 minutes:
$ kubectl logs --since=15m <podName>
Follow Logs
Cool Tip: Login to a Pod using kubectl
command! Read more →
Logs from a Pod can also be followed in a real-time:
$ kubectl logs -f <podName>
To tail the last 100 lines of logs from a Pod and follow them in a real-time:
$ kubectl logs --tail=100 -f <podName>