DEV Community

Nadim Tuhin
Nadim Tuhin

Posted on

How to Check Logs of a Kubernetes Pod: A Comprehensive Guide

When running applications within a Kubernetes cluster, having access to and understanding logs is crucial for both troubleshooting and monitoring the health of your applications. In Kubernetes, each pod's logs provide a window into the behavior and activity of the applications running within that pod. Here's a comprehensive guide on how to check the logs of a Kubernetes pod.

1. Basics of Accessing Pod Logs

To view the logs for a pod:

kubectl logs <pod-name>
Enter fullscreen mode Exit fullscreen mode

This will display the logs of the default container within the specified pod.

2. Accessing Logs from Specific Containers

If a pod has multiple containers, specify which container's logs you want to access:

kubectl logs <pod-name> -c <container-name>
Enter fullscreen mode Exit fullscreen mode

3. Streaming Pod Logs

To stream logs in real-time:

kubectl logs -f <pod-name>
Enter fullscreen mode Exit fullscreen mode

The -f or --follow flag allows you to view new log messages as they appear.

4. Viewing Previous Logs of a Pod

If a container has crashed and restarted, you might want to view the logs from the previous instance of the container:

kubectl logs <pod-name> -c <container-name> --previous
Enter fullscreen mode Exit fullscreen mode

5. Accessing Logs from Pods in a Specific Namespace

By default, kubectl logs searches for pods in the default namespace. To access logs from a pod in a different namespace:

kubectl logs <pod-name> -n <namespace-name>
Enter fullscreen mode Exit fullscreen mode

6. Using Labels to Filter Pods

If you've labeled your pods, you can use label selectors to filter which pod logs you want to view. For example, if you have a label app=myApp:

kubectl logs -l app=myApp
Enter fullscreen mode Exit fullscreen mode

This will show logs from the default container of all pods with the label app=myApp.

7. Accessing Logs from Completed Pods (Jobs and CronJobs)

For pods that run to completion (like Jobs), you can still access their logs even after they've completed:

kubectl logs <job-pod-name>
Enter fullscreen mode Exit fullscreen mode

8. Using Stern for Advanced Log Tailoring

Stern is a tool that allows for tailing Kubernetes logs from multiple pods and containers with added features like color coding and pattern matching. Once installed, you can tail logs like:

stern -n <namespace-name> <pod-name-pattern>
Enter fullscreen mode Exit fullscreen mode

9. Storing and Accessing Logs Outside of Kubernetes

For long-term log storage, analysis, and more advanced querying, consider integrating Kubernetes with a centralized logging solution like ELK Stack (Elasticsearch, Logstash, Kibana), Loki, or Fluentd.

Best Practices:

  1. Centralized Logging: Always consider having a centralized logging solution in place. It aids in long-term storage, querying, and analysis.
  2. Log Rotation: Ensure that your applications and nodes handle log rotation to prevent them from consuming all available storage.
  3. Structured Logging: Whenever possible, write logs in a structured format like JSON. It makes parsing and querying logs much easier in centralized logging systems.
  4. Logging Levels: Make use of logging levels (INFO, WARN, ERROR, etc.) to categorize the importance and severity of log messages.

In conclusion, logs are an indispensable tool in the toolkit of anyone managing or debugging applications in Kubernetes. Whether you're diagnosing a tricky issue or monitoring routine operations, understanding how to access and interpret pod logs is essential.

Top comments (0)