How to read the logs inside a pod?

To read logs from a pod in Kubernetes, we can use the kubectl logs command. Here’s how we can do it:

Get Pod Name:

First, identify the name of the pod you want to read logs from. You can list all the pods in a namespace using:

$ kubectl get pods -n <namespace>

Read Logs:

Once you have the pod name, use the following command to read the logs:

$ kubectl logs <podName> -n <namespace>

The above command retrieves logs from the primary container in the pod. If there are multiple containers in the pod, we can specify the container name with -c <containerName>.

$ kubectl logs <podName> -c <containerName> -n <namespace>

To continuously stream logs as they are generated (similar to tail -f command), we can add the -f flag

$ kubectl logs <podName> -f -n<namespace>