Kubernetes

How to View Kubernetes Pod Logs Files With Kubectl

How to View Kubernetes Pod Logs With Kubectl

Viewing logs in Kubernetes (K8S) is important for troubleshooting and understanding what is happening in the various components of your cluster.

In this article, we will focus on how to view pod logs using the kubectl logs command line tool.

What is Kubectl?

kubectl is a command line tool that allows you to run commands against K8S clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs.

Check out our Kubernetes Cheat Sheet with 15 Kubectl Commands & Objects.

Setting a Kubectl Alias

When using kubectl it can quickly become annoying to type many times. To shorten this, you can set an alias (Something that is highly recommended to save time if you are thinking of taking the Certified Kubernetes Administrator (CKA) exam, as time is short!).

The example below sets an alias called k so all future commands are run against the namespace called ‘my-namespace’.

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

Kubectl Logs Command Quick Reference

1. Print the logs for a pod

kubectl logs <pod_name>

2. Print the logs for the last 6 hours for a pod

kubectl logs --since=6h <pod_name>

3. Get the most recent 50 lines of logs for a pod

kubectl logs --tail=50 <pod_name>

4. Print the logs for a pod and follow new logs

kubectl logs -f <pod_name>

5. Print the logs for a container in a pod

kubectl logs -c <container_name> <pod_name>

6. Output the logs for a pod into a file named ‘pod.log’

kubectl logs <pod_name> > pod.log

7. View the logs for a previously failed pod

kubectl logs --previous <pod_name>

8. View the logs for all containers in a pod

kubectl logs <pod_name> --all-containers

Kubectl Logs Command References With Examples

1. I am interested in the logs for the redis-master pod called redis-master-f46ff57fd-qmrd7.

kubectl logs redis-master-f46ff57fd-qmrd7
kubectl logs --since=10m redis-master-f46ff57fd-qmrd7

Nothing is shown, as no logs have been generated in the last 10 minutes! Changing the time to 15m shows me all the logs.

kubectl logs --since=15m redis-master-f46ff57fd-qmrd7

3. To see the last 5 lines of the logs only:

kubectl logs redis-master-f46ff57fd-qmrd7 --tail 5
view pods log lines

4. I want to follow any new logs I add -f.

kubectl logs -f redis-master-f46ff57fd-qmrd7
kubectl describe pod redis-master-f46ff57fd-qmrd7

This shows one container called master.

kubectl logs -c master redis-master-f46ff57fd-qmrd7

6. Now lets output the logs for a pod into a file named ‘pod.log’.

kubectl logs redis-master-f46ff57fd-qmrd7 >> pod.log

View the logs using cat.

cat .\pod.log

7. View the logs for a previously failed pod

kubectl logs --previous redis-master-f46ff57fd-qmrd7
view pods failed pod
kubectl logs <pod_name> --all-containers

Key Points

We encourage you to also check out how Spacelift helps you manage the complexities and compliance challenges of using Kubernetes. Anything that can be run via kubectl can be run within a Spacelift stack. Find out more about how Spacelift works with Kubernetes, and get started on your journey by creating a free trial account or booking a demo with one of our engineers.

The most Flexible CI/CD Automation Tool

Spacelift is an alternative to using homegrown solutions on top of a generic CI. It helps overcome common state management issues and adds several must-have capabilities s for infrastructure management.

Start free trial