How To View Pod Logs in Kubernetes via the CLI

Overview:

This guide explains how to view or follow logs of a single pod or all pods in a deployment in Kubernetes, via the command-line interface.

Preface

When an application is running, it usually emits logs that contain a wide variety of information such as client actions, warnings, errors, etc. These logs can help you understand what is happening inside your application. The logs are particularly useful for monitoring, troubleshooting, and debugging purposes.

Most modern applications have some kind of logging mechanism enabled in them. If your applications are packaged in a container system like Docker and are deployed within a Kubernetes cluster, Kubernetes will capture logs from each container in a running Pod. You can use the kubectl command to view pod logs as explained below.

View Pod Logs in Kubernetes via the Command-Line Interface
View Logs for Single Container Pods

Before you proceed, you may want to identify the pods you will work with. The following command is used to get a list of all pods in the specified namespace (which must exist within the cluster). Alternatively, you can get pods defined with the label test-api-service:

$kubectl get pods -n  test
OR
$kubectl get pod -l app=test-api-service -n  test
Get all pods in a namespace or those defined by a label

To view snapshot logs for a single pod that has only one container in it, use the following kubectl command below. Here the pod is test-api-service-6c9dc8f4b7-24sk9 within the test namespace:

$kubectl logs test-api-service-6c9dc8f4b7-24sk9  -n  test

To display only the most recent 20 lines of output in the specified pod, use the –tail option, like this:

$kubectl logs --tail=20  test-api-service-6c9dc8f4b7-24sk9  -n  test

To show all logs from the specified pod written in the last hour use the –since option as shown:

$kubectl logs --since=1h test-api-service-6c9dc8f4b7-24sk9  -n  test

To enable following or streaming of logs as they are generated in real-time, use the -f option like this:

$kubectl logs -f test-api-service-6c9dc8f4b7-24sk9  -n  test

To view logs of all pods in a deployment or those defined by a certain label, use the -l option and specify the selector (label query) to filter on. This option supports ‘=’, ‘==’, and ‘!=’.(e.g. -l key1=value1,key2=value2). Also, matching objects must satisfy all of the specified label constraints.

This example shows how to follow the logs of all pods defined by the label test-api-service:

$kubectl logs -f -l app=test-api-service -n test

By default when using kubectl logs command with a selector like in the previous command, you can only view a maximum of 5 concurrent logs. To specify a number more than 5, use the –max-log-requests option. Below is an example:

$kubectl logs  -f -l app=test-api-service --max-log-requests=10 -n test

To prefix each log line with the log source (pod name and container name), add the –prefix like this:

$kubectl logs -f -l app=test-api-service --prefix --max-log-requests=10 -n test

Additionally, you can include timestamps on each line in the log output by adding the –timestamps option:

$kubectl logs -f -l app=test-api-service --prefix --timestamps --max-log-requests=10 -n test
View Logs for Pods with Multi Containers

If you have pods that run multiple containers that need to work together, then you can retrieve the logs of a single or all containers in that pod. To view snapshot logs from a pod with multiple containers, use the –all-containers option. In this example, the pod name is test-api-service:

$kubectl logs test-api-service --all-containers=true

To view snapshot logs from a single container in a multi-container pod, use the -c flag to specify the container, like this:

$kubectl logs test-api-service -c test-api-service-1

To view snapshot logs from all containers in pods defined by the label app=test-api-service, run this command:

$kubectl logs -l app=test-api-service --all-containers=true

For more usage options, see:

$kubectl logs -h
Conclusion

In this guide, I have explained several example commands and options of how to view and follow logs of a single pod or all pods defined with a certain in Kubernetes, via the command-line interface. I hope you have learned one or two things from this guide. Your feedback is welcome via the comment form below. Remember to stay with FOSSguides!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Page Contents