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 logscommand line tool.
What is kubectl?
kubectl is a command line tool that allows you to run commands against K8S clusters. You can use kubectlto deploy applications, inspect and manage cluster resources, and view logs.
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
- Print the logs for a pod
kubectl logs
2. Print the logs for the last 6 hours for a pod
kubectl logs --since=6h
3. Get the most recent 50 lines of logs for a pod
kubectl logs --tail=50
4. Print the logs for a pod and follow new logs
kubectl logs -f
5. Print the logs for a container in a pod
kubectl logs -c
6. Output the logs for a pod into a file named ‘pod.log’
kubectl logs> pod.log
7. View the logs for a previously failed pod
kubectl logs --previous
8. View the logs for all containers in a pod
kubectl logs--all-containers
Kubectl logs command references with examples
You can view the pods on your cluster using the kubectl get pods command. Add the–namespace
I have the following pods running on my Azure Kubernetes Service (AKS) cluster:
kubectl logsshows 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.
- 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 describecommand:
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--all-containers
Summary
Thekubectl logs command enables you to access logs from your resources. In this article, we have focused on some examples for 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.
Want more Kubernetes content? Check out my other articles on K8S here!
Cheers! 🍻
Jack Roper is Blogging on Azure, Azure DevOps, Terraform, Kubernetes and Cloud tech!
Leave a Reply