Skip to main content
In this guide, we explore one of the fundamental Kubernetes troubleshooting commands: kubectl logs. Retrieving logs is often the first step when debugging issues with applications running in Kubernetes pods.

Basic Log Retrieval

To view the logs for a specific pod, use the following command:
Typically, you will specify the namespace (if needed) and the pod’s name. For instance, consider a pod named notes-app-deployment-d4fcc5ccd-5fl7z running in the uat namespace. Execute the following command to retrieve its logs:
The output above shows the log messages produced during the startup of the application.
For a complete list of kubectl logs options and other Kubernetes commands, check out the Kubernetes Documentation.

Viewing Logs in Multicontainer Pods

In more advanced scenarios, a pod may run multiple containers (e.g., an init container, a sidecar container, and the main application container). When you need to retrieve logs from all containers simultaneously, use the --all-containers flag:
While this approach consolidates logs from every container in the pod, it may become challenging to determine the source container for each log entry.
Avoid using --all-containers when you need to diagnose issues specific to a single container. Explicitly targeting a container clarifies the troubleshooting process.

Retrieving Container-Specific Logs

To view logs for a particular container in a multicontainer pod, use the -c flag along with the container’s name. First, determine the container names from the pod specification using a JSONPath query:
For a more readable output, pipe the result to jq:
This command will list containers such as nginx-container and cron-logger.

Example: Viewing Logs for nginx-container

To see the logs from the nginx-container, run:

Example: Viewing Logs for cron-logger

If you need logs for the cron-logger container, execute:
By using the -c flag, you can accurately isolate logs for individual containers within a multicontainer pod. This separation is vital for efficient troubleshooting and gaining targeted insights into each container’s behavior.

Conclusion

Using kubectl logs is an integral part of managing and troubleshooting Kubernetes applications. Whether you are working with single-container pods or more complex multicontainer setups, understanding how to access and interpret logs will significantly enhance your ability to diagnose and resolve application issues. For further details and advanced use cases, visit the official Kubernetes Documentation. Happy troubleshooting!

Watch Video