Essential Methods for Viewing Pod Logs in Kubernetes

Sushvina AS
KPMG UK Engineering
5 min readJun 11, 2024
Image from Pixabay

Errors and unexpected behaviours in Kubernetes can be frustrating.
Do you agree?šŸ’­

But with the right approach to logs, you can transform these challenges into learning opportunities. Dive into this blog to find out how!šŸ˜Š

In the dynamic world of Kubernetes, effective troubleshooting is crucial for maintaining robust and reliable applications. Logs play a pivotal role in this process, providing invaluable insights into the inner workings of your applications and the Kubernetes platform itself.

By leveraging logs, developers and system administrators can quickly diagnose and resolve issues, ensuring smooth operation and minimising downtime.

This blog will guide you through the essentials of Kubernetes logging and demonstrate how to use the kubectl logs command to efficiently troubleshoot and resolve issues in your Kubernetes clusters. Whether you're dealing with application errors, performance bottlenecks, or unexpected behaviours, understanding how to harness the power of logs will enhance your ability to manage and maintain your Kubernetes deployments effectively.

Logs in Kubernetes are generated by various components, including:

  1. Application Logs: These logs are produced by the applications running inside your containers. They provide details about the applicationā€™s behaviour, errors, and other runtime information.
  2. Node Logs: These are logs from the Kubernetes nodes themselves, capturing system-level events and issues.
  3. Cluster Logs: These logs encompass activities related to the entire Kubernetes cluster, including events generated by the Kubernetes control plane components like the API server, scheduler, and controller manager.

Pod logs in Kubernetes contain valuable information about the operation and behaviour of applications running within pods. Hereā€™s a description of what pod logs typically entail:

  1. Application Output: Pod logs primarily consist of the standard output (stdout) and standard error (stderr) streams generated by the containers running within the pod. This includes any messages, warnings, errors, or debug information produced by the application.
  2. Timestamps: Each log entry is usually timestamped to provide a chronological record of when events occurred. Timestamps help in understanding the sequence of events and diagnosing issues over time.
  3. Container Identity: Pod logs often include metadata such as the container name or ID, allowing you to identify which container within the pod generated a particular log entry. This is especially useful in multi-container pods where multiple applications may be running simultaneously.
  4. Lifecycle Events: Pod logs may capture lifecycle events such as container startup, shutdown, or restart. These events provide insights into the health and status of the containers and can help in diagnosing issues related to container initialisation or termination.
  5. Error Messages: Error logs within pod logs highlight any unexpected or abnormal conditions encountered by the application. These error messages provide crucial clues for troubleshooting and resolving issues impacting the applicationā€™s functionality.
  6. Debug Information: In addition to errors, pod logs may contain debug information, diagnostic messages, or performance metrics emitted by the application. Analysing this information can help in optimising application performance and identifying potential bottlenecks.
  7. External Dependencies: Pod logs may also include logs related to interactions with external dependencies such as databases, APIs, or other micro services. Monitoring these logs can help in understanding the overall system behaviour and diagnosing issues arising from external dependencies.

Here is the step by step process to start with šŸ‘‡šŸ»

Step 1: Accessing the Kubernetes Cluster :

Before we can start checking logs, we need to ensure that we have access to the Kubernetes cluster. This typically involves having the Kubernetes command-line tool, kubectl, installed on your local machine. Additionally, youā€™ll need appropriate permissions to access the cluster.

Step 1.1: Verifying Cluster Access: Firstly, ensure that you have the necessary permissions to access the Kubernetes cluster. If youā€™re working in a team or an organisation, your Kubernetes administrator can provide you with the required credentials or permissions.

Step 1.2: Installing kubectl: kubectl is the primary tool for interacting with Kubernetes clusters. Hereā€™s how you can install it:

On macOS:

  1. Using Homebrew
brew install kubectl

On Windows:

  1. Using Chocolatey:
choco install kubernetes-cli

After installing kubectl, verify that itā€™s installed correctly by running:

kubectl version --client
or
kubectl version --short
client version of kubectl installed on your machine

Step 2: Understanding Kubernetes Pods: In Kubernetes, applications run within pods, which are the smallest deployable units. Each pod consists of one or more containers that share resources, including networking and storage. To check logs, weā€™ll need to identify the pod containing the container whose logs we want to examine.

Step 3: Listing Pods: To list all pods in the cluster or a specific namespace, we can use the following command:

kubectl get pods            # List all pods in the namespace
kubectl get pods -n <namespace> # To list pods in a specific namespace

Replace <namespace> with the name of the namespace you want to list pods from. For example, if you want to list pods in the my-namespace namespace, the command would be:

kubectl get pods -n my-namespace

This command provides information about the pods running in the cluster, including their names, statuses, and other relevant details.

Step 4: Selecting a Pod: Once weā€™ve identified the pod weā€™re interested in, we can use its name to retrieve its logs. If your application consists of multiple containers within a single pod, youā€™ll need to specify the container name as well.

Step 5: Retrieving Logs: To retrieve logs from a pod, we use the kubectl logs command followed by the pod name. If there's only one container in the pod, we can simply execute:

kubectl logs <pod_name>

This command retrieves the logs of the pod named <pod_name>. If the pod has multiple containers, you need to specify the container name using the -c flag.

kubectl logs <pod_name> -c <container_name>

Step 6: Following Logs in Real-Time Sometimes, we may need to monitor logs in real-time to debug issues or track application behaviour. To achieve this, we can use the -f flag with the kubectl logs command:

kubectl logs -f <pod_name>

This command continuously streams logs from the specified pod, allowing us to observe changes as they occur.

Few additional commands:

kubectl logs ā€” tail: This command is used to fetch a specific number of lines from the end of the logs.

kubectl logs --tail=<number_of_lines> <pod_name>

kubectl logs ā€” since-time: This command is used to fetch logs since a specific timestamp.

kubectl logs --since-time=<timestamp> <pod_name>

timestamp should be in RFC3339 format (e.g., ā€œ2024ā€“06ā€“09T12:00:00Zā€).

Redirecting logs to a file

kubectl logs <pod-name> > logs.txt

Conclusion:

Effectively monitoring and analysing logs is crucial for maintaining the health and performance of Kubernetes-based applications. By leveraging kubectl, we can easily retrieve and analyse logs from individual pods, enabling us to diagnose issues and troubleshoot problems effectively. With the step-by-step process outlined in this guide, youā€™ll be equipped to navigate Kubernetes logs with confidence, ensuring the smooth operation of your containerised applications.

Until next time, happy writing and happy reading šŸ˜Š

Your feedback is important. Feel free to comment below or connect with me on LinkedIn to share your thoughts.ā€

https://www.linkedin.com/in/sushvina-a-s-7a360417b/

--

--