Viewing Kubernetes pod logs is an essential task for debugging and troubleshooting issues with applications running in a Kubernetes cluster. In this article, we’ll discuss how to view Kubernetes pod logs using the
kubectl logs command.
Logging in Docker:
Let’s say you are running a docker Image, Which has a web server, and these web-server-related logs and events will be written in the sys out.
Docket run <Image name>
Check here to know more about Docker files and how to create one
The above command will print all the logs from the web server to the console screen,
So Now, If you are running the docker container in the background (detach mode).
Docker run -d <Image name>
Then you will not be seeing the logs in the console, So to view the container logs, We can use the below docker command to view the logs (-f) to get the live logs
Docker logs -f <container id>
Logging in Kubernetes
Viewing Pod Logs
To view Kubernetes pod logs, we use the
kubectl logs command. This command allows us to retrieve logs from a container running inside a pod. Here’s the syntax for the
kubectl logs command:
kubectl logs <pod-name> <container-name>
<pod-name> the parameter is the name of the pod whose logs you want to view, and
<container-name> is the name of the container whose logs you want to view. If the pod has only one container, you can omit the container name.
Live Pod Logs
We can directly run the logs command as below to get the container logs in the console “-f” will help to get the live logs in the console
Kubectl logs -f <pod>
But the above command only works, If your pod has 1 container, If you have a multi-container pod, then we need to explicitly set the container name in the command as below
Kubectl logs -f <pod-name> <conatiner name>
Check here for more posts related to Kubernetes
In this article, we’ve learned how to view Kubernetes pod logs using the
kubectl logs command. We’ve seen how to view logs from a specific container and how to tail logs in real time. Kubernetes pod logs are an essential tool for debugging and troubleshooting issues with applications running in a Kubernetes cluster, and the kubectl logs command is a powerful tool for viewing those logs.
Good Luck with your learning. !!