This guide explores the kubectl logs command for retrieving logs from Kubernetes pods to aid in troubleshooting applications.
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.
To view the logs for a specific pod, use the following command:
Copy
Ask AI
kubectl logs
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:
Copy
Ask AI
controlplane ~ ➜ k logs -n uat notes-app-deployment-d4fcc5ccd-5fl7z>[email protected] start /app> node app.jsApp is running on port 3000controlplane ~ ➜
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.
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:
Copy
Ask AI
controlplane ~ → k logs -n uat notes-app-deployment-d4fcc5ccd-5fl7z>[email protected] start /app> node app.jsApp is running on port 3000controlplane ~ → k logs -n uat notes-app-deployment-d4fcc5ccd-5fl7z --all-containers>[email protected] start /app> node app.jsApp is running on port 3000controlplane ~ → k get podsNAME READY STATUS RESTARTS AGElogs-generator 1/1 Running 1 (6m25s ago) 16mmulti-container-pod 2/2 Running 0 7m54scontrolplane ~ →
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.
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:
Copy
Ask AI
k get pod multi-container-pod -o jsonpath='{.spec.containers}'
For a more readable output, pipe the result to jq:
Copy
Ask AI
k get pod multi-container-pod -o jsonpath='{.spec.containers}' | jq
This command will list containers such as nginx-container and cron-logger.
If you need logs for the cron-logger container, execute:
Copy
Ask AI
controlplane ~ ✗ k logs multi-container-pod -c cron-loggerCron logger started. Logging messages every 10 seconds.Sat Apr 6 22:42:21 UTC 2024 - Regular log message.Sat Apr 6 22:42:22 UTC 2024 - Regular log message.Sat Apr 6 22:42:23 UTC 2024 - Regular log message.Sat Apr 6 22:42:24 UTC 2024 - Regular log message.Sat Apr 6 22:42:25 UTC 2024 - Regular log message.Sat Apr 6 22:42:26 UTC 2024 - Regular log message.Sat Apr 6 22:42:27 UTC 2024 - Regular log message.Sat Apr 6 22:42:28 UTC 2024 - Regular log message.
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.
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!