Sometimes, the user may face some pod states such as error, failed, or unknown state. To debug the pod, or to monitor the application running inside the pod, the user may be required to view the logs of the pod.
This post will explain:
- How to Get Logs of a Single Pod?
- How to Get Logs of Specific Containers of Pod?
- How to Get Logs of All Containers of Pod?
- How to Get Logs of Deployment in Kubernetes?
- How to Get Logs of All Pods of Kubernetes Deployment?
- Conclusion
How to Get Logs of a Single Pod?
The pods can execute as a separate component in the Kubernetes cluster or be managed and operated by Kubernetes deployment. For pods that are running separate containerized applications and not part of any deployment, the logs of these pods can be checked individually only. To check the logs of a single pod, follow the below steps.
Step 1: Get Pods
To list down Kubernetes pods, use the “kubectl get pods” commands:
From the below output, you can see currently five pods are executing, the “demo-pod” is running and has only one container. The next three pods are part of “html-deployment”, and the last “web-app” pod is executing two containers:
Step 2: View Logs of Single Pod
To retrieve the logs of a single pod, utilize the “kubectl logs <pod-name>” command:
How to Get Logs of Pod From End?
Usually, the length of logs is longer and may contain hundreds of lines. Sometimes, users want to view pods from specific points or up to specific numbers of logs. To view a specific number of logs from the end, use the below command:
Here, the “–tail” option is used to view the log from the end.
How to Get Logs of Specific Containers of Pod?
The Kubernetes single pod can execute one or more than one container. To access the log of a pod container, follow the below instructions.
Step 1: Get Pods
To list down the pods of the Kubernetes cluster, utilize the “kubectl get pods” command:
Here, the “web-app” is executing more than one container:
Note: Sometimes, the user may not remember the names of containers running in the pod. To check the container details inside the pod, inspect the pod through the “kubectl describe pod <pod-name>” command:
Step 2: View Logs of Container
To get the logs of the specific pod container, use the “kubectl logs <pod-name> -c <container-name>” command. Here “-c” option is used to embed the container name:
How to Get Logs of All Containers of Pod?
To view logs of all containers of the Kubernetes pod, set the “–all-containers” value as “true” in the “kubectl logs” command:
How to Get Logs of Deployment in Kubernetes?
Deployments are another core Kubernetes resources that run the containerized application inside the pods. The deployment manages and operates the running pods with the help of replicas. To view the logs of deployment, follow the below-listed steps.
Step 1: Get All Kubernetes Resources
To list down all resources of Kubernetes, use the “kubectl get all” command:
Here, you can see three pods are executing under “html-deployment” deployment:
Step 2: View Logs of Deployment
To retrieve the logs of Kubernetes deployment, use the “kubectl logs deployment/<deployment-name>” command:
The output shows that three pods are found in “html-deployment” and currently viewing the logs of the first pod only:
How to Get Logs of All Pods of Kubernetes Deployment?
The above case does not show the logs of all deployment pods. To view the logs of all pods of a Kubernetes deployment, first, find out the label of pods. Then access the logs of all pods by specifying the pods label. For demonstration, go through the below instructions.
Step 1: Get Pods
Access the Kubernetes pods along with their labels using the “–show-label” option in the “kubectl get pods” command:
Here, the below result shows the pods along with labels. These pods have the same label because they are running under the same deployment:
Step 2: View Logs of All Deployment Pods
Now, view the logs of all pods of deployment using the pods label. For this purpose, use the “kubectl logs -l <label>” command.
Live View the Logs of Pods
To live view the logs while executing the Kubernetes pods, use the “-f” option along with the “kubectl logs” command:
Here, the “-l” option is used to specify the pod label, and the “–all-containers” option will show the logs of all containers running under the pods:
That is all about viewing logs of all pods using kubectl.
Conclusion
In Kubernetes, the user can view the logs of all pods that are running under the deployment. To retrieve the logs of a single pod, utilize the “kubectl logs <pod-name>” command. To retrieve the logs of a specific pod container, use the “kubectl logs <pod-name> -c <container-name>” command. To view the logs of all pods of Kubernetes deployment, use the “kubectl logs -l <label-name>” command. This blog has illustrated the techniques to get logs from all pods using kubectl.