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.
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.
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
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
You can view the pods on your cluster using the kubectl get pods
 command. Add the--namespace <namespace name>
flag if your pods are running outside of the default namespace. You can also use labels to filter the results as required by adding <my-label>=<my-value>
.
I have the following pods running on my Azure Kubernetes Service (AKS) cluster:
kubectl logs
shows logs from the standard output and error streams of your containers. If you are configuring logging for a container, you will need to ensure the output is written to these outputs for it to be displayed correctly by the following commands.
1. I am interested in the logs for the redis-master pod called redis-master-f46ff57fd-qmrd7.
kubectl logs redis-master-f46ff57fd-qmrd7
As my pod has just started I can see the logs created when the pod was starting up. This shows all the logs from the pod.
2. Print the logs for the last 10 minutes for the pod kubectl logs — since=10m 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
4. I want to follow any new logs I add -f
.
kubectl logs -f redis-master-f46ff57fd-qmrd7
The logs are shown as in example 1, any newly generated logs will be continually streamed and displayed automatically in my terminal until I exit (e.g., using CTRL+C in Windows PowerShell).
5. Now, I want to print the logs for a container in redis-master-f46ff57fd-qmrd7. I first need to see which containers are running in the pod using the kubectl describe
command:
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
Here I see an error, confirming no previous containers in the pod have been terminated.
8. If I did have multiple containers, I could view logs for all of them using the --all-containers
 flag.
kubectl logs <pod_name> --all-containers
See also: How to Restart Kubernetes Pods With Kubectl.
The kubectl logs
command enables you to access logs from your resources. In this article, we have focused on some examples of obtaining logs from your pods.
As shown, you can view current logs, continually stream new pod logs to your terminal, and access logs from previously terminated pods using different command options. Kubectl collects logs from the standard output and error streams of your containers.
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.
Cheers!
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.