Container Logging Fundamentals
What You'll Learn
You'll understand how containers emit logs and how Kubernetes automatically collects them for observability.
How Containers Write Logs
In traditional applications, you might write logs to files on disk. Containers follow a different pattern: they write logs directly to standard output (stdout) and standard error (stderr) streams.
Think of stdout and stderr like two megaphones your application uses to announce information. Stdout is for normal messages ("User logged in"), while stderr is for errors and warnings ("Database connection failed").
Why This Matters in Containers
When your application prints to stdout/stderr inside a container, the container runtime (like Docker or containerd) automatically captures these streams and writes them to files on the node. You don't need to configure log files, paths, or rotation—it happens automatically.
apiVersion: v1
kind: Pod
metadata:
name: logging-example
spec:
containers:
- name: app
image: busybox
command: ["/bin/sh", "-c", "echo 'Normal message'; echo 'Error message' >&2"]
In this example, the container echoes to stdout and stderr. Kubernetes captures both streams seamlessly.
Accessing Container Logs
Once Kubernetes captures your logs, you can retrieve them using kubectl logs. The logs are stored on the node's filesystem (typically /var/log/containers/), making them accessible even if your container restarts.
This stdout/stderr pattern is the foundation of the "12-factor app" logging methodology, where applications treat logs as event streams rather than managing files.
Key Takeaway: Containers should write logs to stdout and stderr, not files; Kubernetes automatically captures these streams and makes them accessible through kubectl and other observability tools.