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 <pod-name> with the name of the pod and <container-name> with the name of the container whose logs we want to view.
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:
$ kubectl logs <pod-name> -c <container-name> --tail=<number-of-lines>
In this command, we replace <pod-name> with the name of the pod, <container-name> with the name of the container, and <number-of-lines> with the number of lines of logs that we want to retrieve.
$ 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:
$ kubectl logs <pod-name> -c <container-name> --follow
Where <pod-name> is the name of the pod and <container-name> is the name of the container whose logs we want to stream.
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
After running the previous command, we can navigate to http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/ in our web browser.
Once we are in the Kubernetes Dashboard, we can find the specific pod that we want to monitor the logs for in the namespace. We can click on the name of the pod to access its details. If the pod has multiple containers, we can select the container whose logs we want to monitor from the “Containers” list.
Finally, to monitor the logs in real-time, we can click on the “Logs” tab, and the logs will be displayed in a console-like interface, which will update as new logs are generated.
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.
In conclusion, monitoring the logs of our Kubernetes pods is essential for ensuring the health and performance of our applications. By using the Kubernetes Dashboard or the kubectl logs command, we can quickly troubleshoot issues, identify errors, and ensure the availability and reliability of our applications.
The steps outlined in this tutorial provide a straightforward and easy-to-follow guide for viewing logs using the Kubernetes Dashboard, which can be an ideal tool for Kubernetes administrators and developers. Whether we are a Kubernetes expert or just getting started with containerization, monitoring our logs is a critical part of maintaining the health and performance of our applications.
res – REST with Spring (eBook) (everywhere)