Graphic showing the Kubernetes logo

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:

export KUBECONFIG=~/.kube/my-cluster.yaml

Then use Kubectl to list your Pods:

kubectl get pods

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

kubectl --namespace my-namespace get pods

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

alias k="kubectl --namespace my-namespace"

k get pods

Accessing Pod Logs

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

kubectl logs pod-name

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:

kubectl logs pod-name container-name

image of viewing Kubernetes pod logs with Kubectl

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:

kubectl logs pod-name --all-containers

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:

kubectl logs -l my-label=my-value --all-containers

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:

kubectl logs pod-name --since=2h

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:

# Shows the last 10 lines of the log
kubectl logs pod-name --tail=10

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.

image of viewing Kubectl pod logs with pod name and timestamp prefixed

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:

kubectl logs job/my-job
kubectl logs deployment/my-deployment

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.

kubectl logs my-pod | grep search-expression

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

kubectl logs my-pod > my-pod-logs.txt


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.

Profile Photo for James Walker James Walker
James Walker is a contributor to How-To Geek DevOps. He is the founder of Heron Web, a UK-based digital agency providing bespoke software development services to SMEs. He has experience managing complete end-to-end web development workflows, using technologies including Linux, GitLab, Docker, and Kubernetes.
Read Full Bio »