Skip to content
beginner Phase 9 · Observability

Logging Fundamentals

Learn Kubernetes logging patterns and kubectl logs for container debugging.

45m
0 problems
Topic Progress 0%

The Container Logging Model

The Container Logging Model

Kubernetes treats stdout and stderr as the log streams: the container runtime writes them to files on the node, and the kubelet manages them. Nothing is shipped anywhere by default — logs exist only as long as the pod (and node) do.

Kubelet rotation flags keep disks bounded:

--container-log-max-size=10Mi   # rotate after this size
--container-log-max-files=5     # keep this many rotated files

Applications may also write to files inside the container, but then nothing rotates them and kubectl logs cannot see them — prefer stdout unless a framework demands a file.

Reading Logs with kubectl

Reading Logs with kubectl

kubectl logs deploy/api -f --tail=200        # stream the latest lines
kubectl logs api-7d9f -c sidecar             # pick a container
kubectl logs api-7d9f -p                     # previous (crashed) container
kubectl logs api-7d9f --since=15m --timestamps

On the node, raw files sit under /var/log/pods/<namespace>_<pod>_<uid>/<container>/0.log with symlinks in /var/log/containers/. kubectl logs does not query etcd or the API server — the request is proxied to the kubelet, which tails those files. Multi-container pods require -c; without it you get an error whenever the pod has more than one container.

Pitfalls and Interview Angles

Pitfalls and Interview Angles

Node loss = log loss. Logs are node-local. When a pod is evicted or the node dies, its logs go with it unless something shipped them first. That is the entire rationale for agents like Fluent Bit running as DaemonSets.

Crashed pods need -p. Once the container restarts, live logs start over; --previous shows the output before the crash.

Timestamps missing. Application timestamps may be absent or in another timezone — use --timestamps and rely on kubelet-prefixed RFC3339 lines when correlating.

Interview angle: name the full path (runtime writes file → kubelet serves kubectl logs), explain why logs are not stored in etcd, and say what you add to survive node failure.

Quiz

1. Where does kubectl logs read from?

Question 1 options

2. A pod shows RestartCount climbing. Which command exposes the crashing container's last output?

Question 2 options

3. What happens to logs when a node is permanently lost?

Question 3 options

Flashcards

Question

Where container logs live

Answer

Runtime writes stdout/stderr to node files under /var/log/pods (symlinked from /var/log/containers); kubelet serves them.

Question

kubectl logs -p

Answer

Prints logs from the previous (terminated) container — the first stop for CrashLoopBackOff.

Question

Log rotation flags

Answer

kubelet --container-log-max-size (10Mi default) and --container-log-max-files (5 default) bound disk use.

Question

Why logs are not in etcd

Answer

High write volume would destroy etcd; the API server proxies to the kubelet instead.

Revision Notes

Key Takeaways

  • 1. stdout/stderr is the model; kubelet writes node-local files and serves kubectl logs
  • 2. Use -c, -p, --since, and --timestamps as the standard debugging set
  • 3. Node-local storage means you need a DaemonSet shipper before the node disappears

Interview Tips

  • • Walk the full log path: runtime → node file → kubelet → kubectl, and state what etcd is not involved in
  • • Explain the trade-off between writing files in-app (rotation burden) and using stdout (central tooling)

Cheat Sheet

stdout/stderr → /var/log/pods → kubelet serves kubectl logs. Flags: -f, -c, -p, --since, --timestamps. Rotate via --container-log-max-size/max-files. Ship centrally or lose logs.

Continue Learning

Before You Start

  • No prerequisites listed.

Next Topics