How to View Kubernetes Pod Logs With Kubectl – CloudSavvy IT

Must read

Bengaluru singer Saahas Patil’s single ‘California’ is about his life in Los Angeles

Five years ago, Saahas Patil was awaiting his turn at an open-mic night for singer-songwriters at State Social House, a chic gastropub on...
Bhawani Singh
I am a blogger who believes in delivering latest tech news from around the world to my viewers.

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

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.

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.

Source link

More articles

Leave a Reply

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.

- Advertisement -

Latest article

What you should know about treating Covid-19

The recommendation comes on the heels of a new variant emerging, the B.1.1.529 variant, also known as the Omicron variant. The...

Make Twitter less toxic by fixing your notifications

Essentials Week spotlights unexpected items that make our daily lives just a little bit better.I’m in a love-hate relationship with Twitter. I love the...

SSD Slowing Down? Windows 11 Might Be To Blame

Christian Wiediger/Shutterstock.comUsers are reporting issues with Windows 11 and NVMe SSDs slowing down. If you’ve noticed that your drive isn’t as blazing-fast as it...

Forza Motorsport car list, news and rumors

Forza Motorsport is set to be the eighth entry in Microsoft's flagship racing franchise and while we’re still waiting for confirmation of its...

Apple Planning Redesigned iPad Pro, New Entry-Level MacBook Pro, Apple Watch Lineup for 2022: Mark Gurman

Apple is working on a “wider-range” of new devices in 2022 that will include the redesigned iPad Pro as well as new...
- Advertisement -