Step-by-Step Guide to Access Application Logs in Kubernetes Pods
In a Kubernetes environment monitoring application logs is essential for debugging performance tuning and maintaining the overall health of our application. The Kubernetes pods which are the smallest deployable units in Kubernetes can generate logs that provide valuable insights inside them. In this article we explain about process of accessing these logs, explaining key terminologies and provide a step-by-step process with examples and outputs for your reference.
Primary Terminologies
- Kubernetes Pod: A Kubernetes pod is a group of one or more containers with shared storage and network resources. It is the smallest deployable unit in Kubernetes.
- Logs: Logs are record of event that occur within a system, application or network. They are essential for monitoring and debugging applications.
- kubectl: kubectl is the command line tool for interacting with Kubernetes API server. It allows users to deploy and manage applications on Kubernetes.
- Namespace: A namespace in Kubernetes is a virtual cluster that provides a way to divide cluster resources between multiple users.
- Container: A container is lightweight, Standalone and executable software package that includes everything needed to run a piece of software, including the code, runtime, system tools, libraries and settings.
Prerequisites
- kubernetes Commands
- Kubernetes Cluster
- kubectl
- Access Permissions
- Namespace Knowledge
- Kubeconfig File
Step By Step Process Get Application Logs From Kubernetes Pod
Here we explain about how to get application logs from kubernetes pod by using kubectl commands with step by step process with related examples with outputs.
Step 1 : Get All Pods
First we need to know what are the available pods for this use below kubectl command.
kubectl get po
Example:


Step 2 : Identify the Pod
Before you can retrieve logs from a pod you need to identify the pod. After that follow below commands. Now I want see the Mongod -2 logs.
kubectl get pods -n <namespace>
Example:

Step 3 : Access the Logs
Once we have identified the pod, Use the following command to retrieve the logs
kubectl logs <pod-name> -n <namespace>
Example:

Step 4 : Stream Logs
To stream logs in real time use the -f flag.
kubectl logs -f <pod-name> -n <namespace>
Example:


Step 5 : Access Previous Logs
If a pod has crashed and restarted you can access the logs from the previous instances using --previous flag.
kubectl logs <pod-name> --previous -n <namespace>
Example:


View Pod Logs
Viewing and managing logs of Kubernetes pods can be done by using kubectl. The Kubernetes command line tool. Below we explain how to view the logs with different various.
1. View Pod Logs
To view the logs of a pod by use below kubectl command.
kubectl logs <pod-name>

2. View pod logs in Real Time
View pod logs in Real Time means to stream the logs of pod in real time use the -f flag. And you can use tail -f for files.
kubectl logs -f <pod-name>

3. Fetch a Specific Number of Lines of Pod Logs
To fetch a specific number of lines from the end of the logs use the --tail flag.
kubectl logs --tail=<number-of-lines> <pod-name>
4. View Logs of an Exited Container
To view the logs of a previous exited container within a pod use -p flag.
kubectl logs -p <pod-name>

5. Fetch Pod Logs from a Specific Time Period
To fetch logs from a specific time period use the --since or --since-time flag.
kubectl logs --since=<duration> <pod-name>
kubectl logs --since-time=<timestamp> <pod-name>

Fetch the Logs of a Specific Container in a Multi-Container Pod.T fetch logs of a specific container within a multi container pod use the -c flag.kubectl logs -c <container-name> <pod-name>
Conclusion
Accessing and managing application logs from Kubernetes pods is a critical task for ensuring the health and performance of your application. Having a solid understanding of key Kubernetes concepts and ensuring that all prerequisites are met will enable you to navigate your Kubernetes environment with confidence. Additionally, leveraging advanced log management tools can further enhance your ability to monitor and analyze logs, leading to more robust and reliable applications.