Checking Kubernetes Log With kubectl logs Command
The kubectl logs is a powerful tool for monitoring and troubleshooting Kubernetes applications
β LHB Community
In Kubernetes, viewing logs is essential for troubleshooting and monitoring the health of applications. The kubectl logs
command is primarily used to fetch the logs from a particular pod.
This guide provides various practical examples of checking logs for pods and other resources in Kubernetes.
kubectl logs command
Kubernetes logs give insights into the behavior of your applications running within pods. Each container in a pod generates logs, and kubectl logs
helps you fetch those logs.
By default, this command retrieves logs from the first container of a pod, but you can specify other containers when necessary.
Basic syntax:
kubectl logs <pod_name> [container_name] [flags]
Viewing logs of a single container pod
To view logs of a single container pod, simply specify the pod name. Hereβs an example of fetching logs from a pod named dev-app-pod
:
kubectl logs dev-app-pod
Output
2024-09-13T12:35:47.123Z Starting application...
2024-09-13T12:35:47.456Z Connecting to database...
2024-09-13T12:35:48.789Z Listening on port 8080...
2024-09-13T12:35:50.123Z Application started successfully.
Viewing logs of a multi-container pod
In a multi-container pod, each container generates its own log. To view logs from a specific container, you can add the container name to the kubectl logs
command.
kubectl logs my-multi-container-pod -c my-container
Output.
2024-09-13T12:36:00.123Z Initializing container `my-container`.
2024-09-13T12:36:00.789Z Processing incoming request...
2024-09-13T12:36:02.567Z Sending response...
Viewing logs with timestamps
By default, logs do not show timestamps. To view logs with timestamps, use the --timestamps
flag.
kubectl logs dev-app-pod --timestamps
Output.
2024-09-13T12:35:47.123Z Starting application...
2024-09-13T12:35:47.456Z Connecting to database...
2024-09-13T12:35:48.789Z Listening on port 8080...
2024-09-13T12:35:50.123Z Application started successfully.
Streaming logs (Real-time logs)
To stream logs in real time as they are being generated, use the -f
(follow) option.
kubectl logs dev-app-pod -f
Output.
2024-09-13T12:35:50.123Z Application started successfully.
2024-09-13T12:36:00.456Z Received request from 192.168.1.10
2024-09-13T12:36:00.789Z Processing request...
2024-09-13T12:36:01.234Z Sending response...
This command will continuously stream new log entries until you stop it with Ctrl+C.
Viewing logs of previous instances of pods
When a pod is restarted, you can view the logs from its previous instance using the --previous
flag.
kubectl logs dev-app-pod --previous
Output.
2024-09-13T12:30:40.123Z Shutting down application...
2024-09-13T12:30:41.567Z Application stopped.
Filtering logs by tail count
If you're only interested in the most recent log entries, you can use the --tail
flag to display a limited number of logs.
kubectl logs dev-app-pod --tail=5
Output.
2024-09-13T12:35:48.789Z Listening on port 8080...
2024-09-13T12:35:50.123Z Application started successfully.
2024-09-13T12:36:00.456Z Received request from 192.168.1.10
2024-09-13T12:36:00.789Z Processing request...
2024-09-13T12:36:01.234Z Sending response...
Using labels to fetch logs
Instead of fetching logs by specifying pod names, you can fetch logs by labels. This is particularly useful when you have many pods with the same label.
kubectl logs -l app=dev-app
Output.
2024-09-13T12:35:47.123Z Starting application...
2024-09-13T12:35:47.456Z Connecting to database...
2024-09-13T12:35:48.789Z Listening on port 8080...
2024-09-13T12:35:50.123Z Application started successfully.
This will retrieve logs from all pods with the label app=dev-app
.
Viewing logs from a specific node
If you need to troubleshoot logs from pods running on a particular node, use the following command to list logs from that node.
kubectl logs -l app=dev-app --field-selector spec.nodeName=node-1
Output.
2024-09-13T12:40:22.123Z Pod on node-1 initializing...
2024-09-13T12:40:23.456Z Starting the service on node-1.
This retrieves logs from pods on a node labeled node-1
.
Conclusion
The kubectl logs command is a powerful tool for monitoring and troubleshooting Kubernetes applications. By leveraging various options and flags, you can gain deep insights into your pod's behavior, filter logs effectively, and even stream logs in real time for immediate feedback.
LHB Community is made of readers like you who like to contribute to the portal by writing helpful Linux tutorials.