Kubernetes is a powerful container management platform that allows you to create and manage your own containers. You can use Kubernetes to run applications, services, and data on your own servers or clusters of servers. Kubernetes also lets you create and manage Pods, which are small containers that run inside of other Pods. To view the logs of a Kubernetes pod, you first need to create a new pod and add it to the cluster. Then, use the kubectl get logs command to get the logs for the pod. The following example shows how to get the logs for a pod named “test” in a cluster named “my-cluster”. kubectl get logs test


Viewing Pod logs is often the first step in diagnosing a problem with your cluster’s workloads. Here’s how to use Kubectl to live stream logs to your terminal, letting you inspect the output from your application.

Getting Started

Make sure you’ve got Kubectl installed and connected to your cluster. You can specify a Kubeconfig file by setting the KUBECONFIG environment variable in your shell:

Then use Kubectl to list your Pods:

Remember to add the –namespace flag when your Pods live outside the default namespace:

Adding a temporary alias to your shell is a good way to shorten this step, helping you run several commands against the same namespace:

Accessing Pod Logs

The kubectl logs command lets you inspect the logs produced by a named Pod:

The Pod’s existing logs will be emitted to your terminal. When the Pod’s formed from more than one container, you must also specify the name of the contaienr you want to inspect:

Alternatively, set the –all-containers flag to include log lines produced by any of the containers in the Pod. Beware that you could see verbose and repetitive output when this flag is used against a busy Pod:

You can also get the logs from a set of Pods with a given label. This lets you aggregate logs from different Pods, provided they all share the same label:

Continually Streaming Logs

The plain logs command emits the currently stored Pod logs and then exits. Add the -f (–follow) flag to the command to follow the logs and live stream them to your terminal.

Kubectl will emit each new log line into your terminal until you stop the command with Ctrl+C. This is equivalent to using tail -f with a local log file in a non-containerized environment.

Viewing Older Logs

kubectl logs won’t include log lines produced by old containers that were once Pod members but have since been replaced. These logs can be accessed by adding the -p (–previous) flag.

Kubectl will then surface the entirety of the stored log for the Pod, including lines that were emitted by containers that have since been terminated.

Getting Recent Logs

Sometimes you don’t need to see the entire log stream. Kubectl supports a –since flag which surfaces log lines emitted after a given time:

This command will show the log output from pod-name that was produced within the past two hours. Another variant, –since-time, supports an RFC3339-compliant timestamp string instead of the relative time expression shown above.

The –tail flag is another option for condensing logs. This limits the number of lines that are displayed, avoiding a full terminal when you only need to see very recent output:

Kubectl doesn’t show line timestamps by default as many applications already include them in their log output. Add the –timestamps flag to have Kubectl add timestamps to the start of lines when your workload doesn’t provide them.

You can prepend the inspected pod and container names to log lines too. This functionality is activated with the –prefix flag. It can be combined with –timestamps to display the time each line was created and the source it originated from.

Accessing Logs From Other Resource Types

kubectl logs works with deployment and job resources in addition to pods:

You’ll get the logs from the first container within the job or deployment. Use the –all-containers flag to surface logs created by any of the matching containers. You can use all the flags described above whether you’re viewing a pod, deployment, or job.

More Advanced Log Management

Kubectl doesn’t include a way to filter, search, or transform your logs. It’s best to pipe the kubectl logs output into established terminal tools like awk, grep or sed for this purpose.

Similarly, use the existing redirection features in your shell to save logs to a file:

Summary

Kubectl lets you access logs from your resources either on a per-container basis or in aggregate. You can view a snapshot of currently collected logs, continually stream new lines to your terminal, and access historical lines emitted by terminated containers.

The command comes with some limited customization options including a line count limiter and simplistic date filtering. When more demanding parsing is needed, pipe the output into Unix terminal commands to rapidly analyze your logs and find the causes of errors in your applications.

Kubectl collects logs from the standard output and error streams of your containers. It’s important to ensure you write output to these streams correctly as a misconfigured container will result in empty output when you run kubectl logs.