Kubernetes: Container and Pod Logging

Claire Lee
3 min readMar 13, 2023

--

In Kubernetes, we can use the kubectl logs command to retrieve and monitor the logs generated by the application running inside a container in a pod. By default, this command retrieves logs from the first container running in the specified pod. Therefore, if a pod has a single container, you can use the kubectl logs <pod_name> to retrieve its logs. However, if the pod has multiple containers, you can use the kubectl logs <pod_name> <container_name> to retrieve the logs of a specific container. Additionally, you can use the -f flag to stream logs in real-time.

Kubernetes: Container and Pod Logging

kubectl logs command allows us to retrieve the logs of a container or pod. -f flag with this command allows us to monitor the application's behavior, troubleshoot issues, and get real-time insights into how the application is performing.

kubectl logs --previous command helps us retrieve the logs of a previous instance of a container or pod. It's an essential tool for troubleshooting past issues by inspecting the logs to identify the root cause of the problem.

Logging for Pod with a Single Container

Logging for Pod with a Single Container
  1. Retrieve the logs of a single container running in a pod
$ kubectl logs <pod_name>

2. Stream the logs from a pod continuously

$ kubectl logs -f <pod_name>

3. Retrieve the logs of the previous instance of a pod

$ kubectl logs --previous <pod_name>

Logging for Pod with Multiple Containers

Logging for Pod with Multiple Containers
  1. Retrieve the logs of a specific container running in a pod
$ kubectl logs <pod_name> <container_name>

2. Stream the logs from a specific container running in a pod continuously

$ kubectl logs -f <pod_name> <container_name>

3. Retrieve the logs of the previous instance of a specific container in a pod

$ $ kubectl logs --previous <pod_name> <container_name>

These are my personal notes for CKA exam preparation on Kubernetes. Please feel free to correct me if you notice any errors. 😊

Reference:

--

--

Claire Lee
Claire Lee

No responses yet