Logging in Docker
Imagine a Docker container named “event simulator” that produces random events to mimic a web server. This container sends these event logs to its standard output. For instance, executing the container in the foreground using the command below:If you run the Docker container in detached mode using the
-d option, the logs won’t display on the console immediately. Instead, you can inspect the logs later by using the docker logs command along with the container ID.Logging in Kubernetes
Transitioning to Kubernetes, the process is similar but integrated into a managed cluster environment. You can create a pod using the same Docker image defined in a pod specification file. Once the pod is running, you can stream its logs with thekubectl logs command using the -f flag.
Start by creating the pod:
event-simulator.yaml):
Multiple Containers in a Pod
Kubernetes allows you to create pods with multiple containers. Suppose you update your pod specification to include an additional container called “image-processor” as follows:When a pod contains more than one container, running the command:results in an error because Kubernetes cannot determine which container’s logs to display. To view logs for a specific container, explicitly specify the container name. For example, to view logs for the “event-simulator” container: