1. Overview
Kubernetes is a powerful container orchestration platform that allows us to manage and deploy containerized applications at scale.
One of the most critical aspects of ensuring the reliability and availability of our applications is monitoring the health and status of running pods. Reviewing logs is an essential part of this process, as they provide a record of events and actions performed by our application or system.
In this article, we’ll discover various techniques for viewing pod logs in Kubernetes. These techniques include using the kubectl logs command, the Kubernetes dashboard, and streaming logs in real time.
2. Using kubectl logs Command
The kubectl logs command is the most straightforward way to view the logs of a pod in Kubernetes. We can use this command to retrieve logs of a specific container running in a pod.
Here are some examples of how to use the kubectl logs command:
Firstly, to retrieve logs from a running pod with only one container, we use the following:
$ kubectl logs <pod-name> -c <container-name>
In this command, we’ll replace
When troubleshooting a pod with multiple containers, it can be useful to retrieve logs for a specific container by using the –container or -c option. This option allows you to focus on the container of interest and can simplify the troubleshooting process.
To view the logs of a specific container named httpd-server running in a pod named my-pod, we’ll use this command:
$ kubectl logs my-pod -c httpd-server
[Wed Apr 27 10:12:29.000000 2022] [core:notice] [pid 1:tid 140028840174080] AH00094: Command line: 'httpd -D FOREGROUND'
[Wed Apr 27 10:12:29.000000 2022] [mpm_prefork:notice][pid 1:tid 140028840174080] AH00163: Apache/2.4.41 (Unix) configured -- resuming normal operations
The output of this command displays the logs of the httpd-server container that is running in the my-pod.
2.1. Using the tail Option
By default, the kubectl logs command retrieves all logs from the start of the container’s output. However, we can use the –tail option to fetch only the latest logs. Here’s an example of how to use this option:
In this command, we replace
$ kubectl logs my-pod -c httpd-server --tail=100
This command fetches the most recent 100 lines of logs of the container running in our pod.
2.2. Using the follow Option
The kubectl logs command provides an easy way to access the logs, but sometimes we need to monitor them in real-time to catch errors or issues as they happen. That’s where the –follow option comes in.
To use the –follow option with the kubectl logs command, we simply use the following:
Where
Using the —follow option allows us to see logs in real-time as they are generated, making it especially useful when troubleshooting an issue and needing to view logs as soon as they are created.
3. Monitoring Logs of Kubernetes Pods with the Kubernetes Dashboard
Kubernetes provides a web-based dashboard that we can use to view logs of pods. To view logs using the Kubernetes dashboard, we need to follow these steps:
To start monitoring logs of Kubernetes pods, we can access the Kubernetes Dashboard by running the command kubectl proxy in our terminal:
$ kubectl proxy
Starting to serve on 127.0.0.1:8001
4. Best Practices for Logging in Kubernetes
In addition to various techniques for viewing pod logs in Kubernetes, it’s important to follow best practices for logging in order to ensure that logs are structured, secure, and easily accessible.
Some best practices for logging in Kubernetes include:
- Using structured logging
- Logging in JSON format
- Avoiding logging sensitive information
- Setting log levels
Initially, structured logging makes it easier to search and analyze logs, while logging in JSON format facilitates parsing and integration with other systems.
Nevertheless, it’s important to avoid logging sensitive information, and instead, we use secure methods such as environment variables. Finally, setting log levels helps filter out the noise and focus on relevant information.
By following these best practices, we can ensure that our logs are reliable and useful for troubleshooting and analysis.