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 we will cover:

  1. What is kubectl?
  2. How to set a kubectl alias?
  3. What is the kubectl logs command?
  4. Kubectl logs command options quick reference
  5. How to use the kubectl logs command with examples

What is kubectl?

kubectl is a command line tool that allows you to run commands against Kubernetes 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.

How to set a kubectl alias?

When using kubectl it can quickly become annoying to type many times. To shorten this, you can set an alias.

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

Setting a kubectl alias is highly recommended if you are thinking of taking the Certified Kubernetes Administrator (CKA) exam, as time is short!

What is the kubectl logs command?

The kubectl logs command is used in Kubernetes to retrieve and display the logs from a running container in a pod. It is primarily used for debugging and monitoring applications deployed in a Kubernetes cluster.

The basic syntax for retrieving logs from a pod is:

kubectl logs [OPTIONS] POD_NAME [-c CONTAINER_NAME]

If your pod has only one container, you can simply run kubectl logs <pod_name>, replacing <pod_name>  with the name of your pod. If your pod has multiple containers, you can specify which container’s logs you want to view using the -c option.

For example, to retrieve the logs of a specific container in a pod within a specific namespace, you would run:

kubectl logs my-pod -c my-container -n my-namespace

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 N lines of logs for a pod (e.g. 50)

kubectl logs --tail=50 <pod_name>

4. Print the logs for a pod in real-time and follow new logs

kubectl logs -f <pod_name>

5. Retrieve the logs of a specific container within a pod in a Kubernetes cluster

kubectl logs <pod_name> -c <container_name>

6. Specify the namespace if the pod is not in the default namespace

kubectl logs <pod_name> -n <namespace>

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

kubectl logs <pod_name> > pod.log

8. View the logs for a previously failed pod

kubectl logs --previous <pod_name>

9. Include timestamps on each line of the log output

kubectl logs <pod-name> --timestamps

10. View the logs for all containers in a pod

kubectl logs <pod_name> --all-containers

If you don’t see the logs you’re expecting, check if your Pod has restarted using the kubectl describe pod <pod-name> command. You might need to look at logs for the previous container using --previous flag.

Kubectl logs command references with examples

view pods aks cluster
kubectl logs redis-master-f46ff57fd-qmrd7
view pods redis
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 would show us all the logs.

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

Example 3: If we want to see the last five lines of the logs only:

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

Example 4: If we want to follow any new logs we add -f.

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

This shows one container called master. Let’s examine it:

kubectl logs -c master redis-master-f46ff57fd-qmrd7
view pods master container

Example 6: Now, let’s output the logs for a pod into a file named ‘pod.log’.

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

To view the logs using cat, run:

cat .\pod.log

Example 7: To view the logs for a previously failed pod we would run:

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.

Manage Kubernetes Easier and Faster

Spacelift allows you to automate, audit, secure, and continuously deliver your infrastructure. It helps overcome common state management issues and adds several must-have features for infrastructure management.

Learn more

Kubernetes Commands Cheat Sheet

Grab our ultimate cheat sheet PDF

for all the kubectl commands you need.

k8s book
Share your data and download the cheat sheet