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:
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.
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!
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
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.
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>
.
Here, we have the following pods running on my Azure Kubernetes Service (AKS) cluster:
Thekubectl logs
command 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.
Example 1: We are interested in the logs for the redis-master pod called redis-master-f46ff57fd-qmrd7.
kubectl logs redis-master-f46ff57fd-qmrd7
As our pod has just started, we can see the logs created when the pod was starting up. This shows all the logs from the pod.
Example 2: Let’s 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 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
Example 4: If we want to follow any new logs we 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).
Example 5: Now, we want to print the logs for a container in redis-master-f46ff57fd-qmrd7. We 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. Let’s examine it:
kubectl logs -c master redis-master-f46ff57fd-qmrd7
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
Here we see an error confirming no previous containers in the pod have been terminated.
Example 8: If we had multiple containers, we 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 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 your containers’ standard output and error streams.
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.