The kubectl logs
command in Kubernetes is used to retrieve the logs of containers running inside pods. It is particularly useful for debugging and monitoring applications deployed in Kubernetes clusters. Let’s explore how to effectively use kubectl logs
to fetch container logs.
Example 1: Basic usage
To fetch logs from a container named my-container
in a pod named my-pod
, use the following command:
kubectl logs my-pod -c my-container
This command retrieves and displays the logs of the specified container within the specified pod.
Example 2: Follow logs in real-time
To continuously stream the logs of a container, use the -f
flag:
kubectl logs -f my-pod -c my-container
This is useful for real-time monitoring of logs as they are generated.
Example 3: Previous terminated container
To fetch logs from a previous terminated container, add the --previous
flag:
kubectl logs --previous my-pod -c my-container
This retrieves logs from the last terminated instance of the container.
Example 4: Display timestamps
To include timestamps with the log entries, use the --timestamps
or -t
flag:
kubectl logs --timestamps my-pod -c my-container
Timestamps help in correlating events across different logs.
Example 5: Retrieve logs from the first container
If a pod has multiple containers, logs from the first container can be retrieved without specifying its name:
kubectl logs my-pod
This assumes the first container defined in the pod’s configuration.
Example 6: Retrieve all containers’ logs
To fetch logs from all containers within a pod, use the --all-containers
or -a
flag:
kubectl logs my-pod --all-containers
This command is useful when troubleshooting interactions between containers in the same pod.
Example 7: Limit number of lines
To limit the number of lines of logs fetched, use the --tail
flag:
kubectl logs my-pod --tail=100
This fetches only the last 100 lines of logs, helpful for quick overviews.
Example 8: Export logs to a file
To export logs to a local file, redirect the output of kubectl logs
:
kubectl logs my-pod -c my-container > my-container.log
This saves the logs of the specified container to a file named my-container.log
in the current directory.
Example 9: Display logs from all namespaces
To fetch logs from a pod in a specific namespace, specify the namespace using the -n
flag:
kubectl logs my-pod -n my-namespace
This is useful when working with multiple namespaces in a Kubernetes cluster.
Example 10: Use custom formatting
To format the output of logs, use the --output
or -o
flag with options like json
or wide
:
kubectl logs my-pod -c my-container -o json
This formats the log output in JSON format, which can be parsed programmatically.
To verify if the kubectl logs
command executed successfully, check the output in your terminal or command prompt where the command was executed. Ensure that the logs retrieved match the expected behavior based on the options and flags used. For commands that save logs to a file, verify the presence and contents of the file created.
Also check similar articles.
Detailed Resource Inspection with kubectl describe
Update Node Taints with kubectl taint
Prepare Nodes for Maintenance with kubectl drain
Scheduling Nodes in Kubernetes with kubectl uncordon
How to Cordon Kubernetes Nodes with kubectl cordon
Discussion about this post