
- Containerized applications should write logs to standard output (
stdout) and standard error (stderr). Container runtimes capture these streams and expose them via CLI tools. - Example: run a sample container that prints simulated web-server events to stdout:
- If you start the container in detached/background mode (
-d), logs won’t appear on the terminal. Usedocker logsto view or tail logs:
- Kubernetes relies on the same best practice: write logs to
stdout/stderr. The kubelet (via the container runtime) captures those streams and exposes them usingkubectl logs. - To test the same image in Kubernetes, create a Pod and stream its logs.
- A minimal Pod manifest that runs the
event-simulatorimage:
- Pods can host multiple containers. When more than one container exists,
kubectl logsneeds the specific container name. - Use the
-c/--containerflag to indicate which container to fetch logs from:
- If you omit the container name in a multi-container Pod, kubectl returns an error and asks you to specify which container’s logs you want.
Best practice: Applications running in containers should write logs to stdout/stderr. This makes them compatible with the container runtime and Kubernetes log collection tools.
Wrap-up
- For development and basic troubleshooting,
docker logsandkubectl logsare often sufficient to inspect application output. - In production environments, teams typically deploy cluster-wide log collectors and aggregators (for example Fluentd / Fluent Bit forwarding to Elasticsearch + Kibana, or hosted SaaS log platforms) to collect, index, and retain logs centrally for search, alerting, and long-term analysis.
- Next steps: explore structured logging in your application (JSON logs), sidecar log processors, and Fluentd/Fluent Bit integrations to ship logs to central storage.