Kubernetes

Essential Kubernetes Skills: How to Get Logs from Pods Using kubectl

Learn to use kubectl for Kubernetes: Get logs from specific pods, understand namespaces, and handle logging tasks efficiently.
December 22, 2023

kubectl get logs Essential Commands

When managing applications on Kubernetes, retrieving logs from pods is a fundamental task. This article provides a step-by-step guide on how to obtain logs from pods using the kubectl command-line tool.

#1 Retrieve a list of pods within the current namespace

kubectl get pods

#2 Retrieve a list of pods within the specified namespace

kubectl get pods -n "namespace_name"
Figure 1 - kubectl get logs | Retrieve a list of pods within the specified namespace
Figure 1 - kubectl get logs | Retrieve a list of pods within the specified namespace

In Kubernetes, a pod is the smallest deployable unit that can be created and managed. It's often a representation of a single instance of an application in your cluster. To retrieve a list of pods within the current namespace, Kubernetes administrators typically use the kubectl get pods command. This command provides vital information about each pod's status, including whether they're running, pending, or in an error state. In use cases where microservices architecture is implemented, this command is indispensable for tracking the health and status of each service. For instance, in a financial services application where different microservices handle transactions, user authentication, and data processing, quickly retrieving pod status can be crucial for uptime and rapid troubleshooting. This functionality is also pivotal in CI/CD pipelines to ensure that new deployments do not disrupt existing services.

#3 Retrieve the logs of a pod located in the current namespace

kubectl logs “pod_name”

#4 Retrieve the logs of a pod located in the specified namespace

kubectl logs “pod_name” -n "namespace_name"
Figure 2 - kubectl get logs | Retrieve the logs of a pod located in the specified namespace
Figure 2 - kubectl get logs | Retrieve the logs of a pod located in the specified namespace

Kubernetes provides a straightforward method to access the logs of a pod through the kubectl logs [POD_NAME] -n [NAMESPACE] command. This functionality is crucial for developers and system administrators to troubleshoot issues within their applications. For instance, in a complex e-commerce platform, if a specific service related to payment processing is failing, retrieving the logs of the corresponding pod can provide insights into errors or exceptions that are occurring.

The command's utility is further enhanced in scenarios involving multiple namespaces, which are often used in Kubernetes to separate different environments (like development, staging, and production) or different tenants in a multi-tenant architecture. By specifying the namespace, administrators can directly access the logs of a pod in the intended environment without navigating through other unrelated pods. This capability is particularly useful in continuous deployment scenarios, where developers need to verify the behavior of newly deployed code in a specific namespace. The ability to retrieve logs at the pod level allows for a granular view of the system, enabling precise diagnosis and quicker resolution of issues.

#5 Retrieve the most recent 50 lines of logs of a pod

kubectl logs --tail=50 "pod_name" -n "namespace_name"
Figure 3 - kubectl get logs | Retrieve the most recent 50 lines of logs of a pod
Figure 3 - kubectl get logs | Retrieve the most recent 50 lines of logs of a pod

In Kubernetes, the kubectl logs [POD_NAME] --tail=50 command is used to fetch the last 50 lines of logs from a specified pod. This command is particularly useful in environments where real-time monitoring and rapid diagnostics are essential.

For example, in high-traffic web applications, such as online retail platforms, where thousands of requests are processed every minute, it's vital to quickly access the most recent log entries to diagnose issues like service downtime or performance degradation. Similarly, in financial services applications dealing with real-time transaction processing, immediate access to the latest logs can help in identifying and resolving issues like transaction failures or delays.

This command is also a valuable tool in DevOps practices, particularly when implementing continuous integration and continuous deployment (CI/CD) pipelines. Developers and operations teams can use this command to instantly check the impact of recent code deployments on application behavior. In a microservices architecture, where different components of an application are deployed in separate pods, the ability to quickly retrieve the latest logs from a specific pod enables faster and more targeted troubleshooting, enhancing the overall efficiency of the development and maintenance process.

#6 Retrieve logs for the last 3 hours for the specified pod

kubectl logs --since=3h "pod_name" -n "namespace_name"
Figure 4 - kubectl get logs | Retrieve logs for the last 3 hours for the specified pod
Figure 4 - kubectl get logs | Retrieve logs for the last 3 hours for the specified pod

In Kubernetes, this can be achieved by using the kubectl logs [POD_NAME] --since=3h command. This command filters the logs to show only the entries from the last three hours, providing a focused view of the pod's recent activities.

This functionality is especially important in scenarios where applications are subject to periodic spikes in usage or when debugging issues that have developed over time. For example, in a media streaming service, understanding the performance and behavior of the service during peak viewing times is crucial. By retrieving logs from the last few hours, engineers can analyze patterns or issues related to high traffic periods.

Additionally, in a continuous delivery environment, where new updates are frequently rolled out, it's essential to monitor how recent changes affect the application's performance and stability. The ability to retrieve logs from a specific timeframe allows developers to correlate the timing of code deployments or configuration changes with any arising issues. This targeted log retrieval is also invaluable in incident response, enabling teams to quickly identify when a problem started and what changes or events might have triggered it. Thus, this command is a vital tool for maintaining the reliability and high performance of applications running in Kubernetes clusters.

#7 Subscribe to the latest logs of a specific pod

kubectl logs -f “pod_name” -n "namespace_name"
Figure 5 - kubectl get logs | Subscribe to the latest logs of a specific pod
Figure 5 - kubectl get logs | Subscribe to the latest logs of a specific pod

To exit the auto-updating logs, use the command ctrl+C.

To achieve this, Kubernetes provides the kubectl logs -f [POD_NAME] command, where the -f flag stands for "follow". This command streams the logs from the specified pod to the console in real-time, allowing administrators and developers to observe the current operations of the pod as they happen.

This live log tailing is particularly useful in scenarios where immediate feedback is required. For example, in a live production environment, if a web application suddenly starts experiencing issues, being able to subscribe to the pod's logs in real-time allows engineers to witness firsthand the application's responses to user requests and any emerging errors.

Similarly, in a development or staging environment, when deploying a new feature or service, developers can use this command to instantly observe the pod's behavior and verify the correct execution of their code. This is especially relevant in microservices architectures, where the interaction between different services can be complex and issues may only surface under specific conditions.

Real-time log monitoring is also a critical component of proactive incident management strategies. By continually observing the logs, DevOps teams can quickly detect and respond to anomalies, unusual patterns, or error messages, often resolving issues before they impact the end users. This capability is an integral part of maintaining the high availability and reliability of services in Kubernetes clusters.

#8 Store the logs of a specified pod in a file

kubectl logs "pod_name" logfile.log -n "namespace_name"
Figure 6 - kubectl get logs | Store the logs of a specified pod in a file
Figure 6 - kubectl get logs | Store the logs of a specified pod in a file
Figure 7 - kubectl get logs | Store the logs of a specified pod in a file
Figure 7 - kubectl get logs | Store the logs of a specified pod in a file

In Kubernetes, this can be accomplished by redirecting the output of the kubectl logs [POD_NAME] command to a file. This is done using the command kubectl logs [POD_NAME] > [FILE_NAME].log. This command captures the logs of the specified pod and saves them into a file, which can be named as per the user's requirement.

This practice is crucial in scenarios where long-term log storage is necessary for auditing or analysis purposes. For instance, in financial applications or healthcare systems where regulatory compliance mandates extensive logging and record-keeping, storing pod logs in files is a part of meeting these compliance requirements.

Additionally, in a scenario where a critical issue has occurred, and a detailed investigation is required, storing logs in a file allows for thorough analysis without the time constraints imposed by the live log retention policies. This is particularly important in complex systems where logs need to be correlated with other data sources or where detailed forensic analysis is necessary post an incident.

Moreover, storing logs in files facilitates the use of log analysis tools or integration with log management systems. Organizations often use such tools for monitoring, alerting, and analyzing log data for insights into application performance and behavior. By saving Kubernetes pod logs in a file, it becomes easier to import these logs into such systems for more advanced analysis. This practice is a key part of maintaining operational excellence in a Kubernetes environment.

#9 Retrieve the logs of a pod that recently failed

kubectl logs –previous "pod_name" -n "namespace_name"

Note that this command will only work on a pod that has previously been terminated.

When a pod fails in Kubernetes, it's crucial to quickly gather its logs to understand what went wrong. The standard approach to retrieve logs from a failed pod is to use the kubectl logs [POD_NAME] command. However, when a pod has crashed and restarted, you'll need to use an additional flag to access the logs from the previous instance of the pod.

The command kubectl logs [POD_NAME] --previous is used in such cases. This command fetches the logs from the most recently terminated container of the specified pod. This is particularly useful when dealing with crash looping pods, where the pod repeatedly starts and fails.

For example, in a cloud-based application, if a service suddenly becomes unavailable, and the corresponding pod is found to be in a crash loop, retrieving the logs from the failed instance of the pod can provide insights into issues like configuration errors, unhandled exceptions in the code, or resource constraints.

This method of log retrieval is crucial in post-mortem analysis, where understanding why a pod failed is necessary for preventing future occurrences of the same issue. It is also an integral part of a robust monitoring and alerting strategy, where immediate insights into failures can lead to quicker resolutions and reduced downtime. Thus, this command is a valuable tool for maintaining the stability and reliability of applications running on Kubernetes.

#10 Retrieve the logs of a container in a pod

kubectl logs -c "container_name" "pod_name" -n "namespace_name"

In Kubernetes, a pod can contain more than one container, each performing its own specific function. To retrieve logs from a specific container within a pod, the command kubectl logs [POD_NAME] -c [CONTAINER_NAME] is used. The -c flag specifies the container from which the logs should be retrieved.

This functionality is crucial in multi-container pods, where different containers might be responsible for different aspects of the application's functionality. For instance, in a web application, one container might be running the main application, while another could be dedicated to logging or monitoring. If an issue arises, it's important to be able to isolate the logs of the specific container that might be affected.

For example, in a scenario where a pod contains a main application container and a sidecar container (such as a logging agent or a service mesh proxy), if there's a network communication issue, retrieving the logs from the service mesh proxy container can provide insights into the network errors or connection issues.

This targeted approach to log retrieval is also valuable in debugging complex applications, where different containers might have different roles or responsibilities. By accessing logs from a specific container, developers and administrators can gain a clearer understanding of the state and behavior of each component of the application, facilitating a more effective troubleshooting process. This capability is an integral part of maintaining and managing robust, distributed applications in a Kubernetes environment.

#11 Retrieve the logs of all containers in a pod

kubectl logs "pod_name" –all-containers -n "namespace_name"

Conclusion on Getting Logs from Pods in Kubernetes using kubectl

In conclusion, the ability to efficiently manage and monitor pods in a Kubernetes environment is essential for maintaining the reliability and performance of applications. Each command discussed:

  1. Retrieving a List of Pods within the Current Namespace: This is crucial for understanding the overall state and health of the applications running in the cluster, enabling quick identification of operational issues.
  2. Retrieving Logs of a Pod in a Specified Namespace: This capability is key for diagnosing and resolving issues in a targeted manner, especially in multi-tenant environments or complex systems with multiple namespaces.
  3. Retrieving the Most Recent 50 Lines of Logs of a Pod: This provides immediate insights into the recent activities of the application, aiding in rapid troubleshooting and real-time monitoring.
  4. Retrieving Logs for the Last 3 Hours for a Specified Pod: This is vital for analyzing trends and issues over a specific timeframe, especially useful in post-deployment reviews or incident analysis.
  5. Subscribing to the Latest Logs of a Specific Pod: This real-time log monitoring is essential for proactive incident management and immediate feedback during development or deployment phases.
  6. Storing the Logs of a Specified Pod in a File: This is important for long-term log storage, compliance, and detailed analysis, allowing logs to be reviewed at a later time or used with external log management tools.
  7. Retrieving the Logs of a Pod that Recently Failed: This is critical for understanding the reasons behind pod failures, crucial for preventing future issues and maintaining application stability.
  8. Retrieving the Logs of a Container in a Pod: This targeted log retrieval is essential in multi-container pods, enabling precise diagnostics and understanding of individual container roles and behaviors.

Together, these capabilities form a comprehensive toolkit for Kubernetes administrators and developers, facilitating robust management, monitoring, and troubleshooting of applications in a Kubernetes environment. This toolkit is not just about maintaining operational efficiency; it's about ensuring that Kubernetes-based applications are resilient, responsive, and capable of meeting the demands of modern digital ecosystems.