opyeb logscrub

Kubernetes and container log collection

2026-11-29 · 2 min read

One-line answer: a Kubernetes node stores each container's standard output and error as CRI-format files, and kubectl logs reads those files through the kubelet.

Where the logs actually live

The container runtime writes each container's stdout and stderr under /var/log/pods on the node. The path contains the namespace, pod name, pod UID, container name and restart count. /var/log/containers holds friendlier symlinks.

kubectl logs reaches the file through the API server and kubelet. When a pod is deleted the local file goes too, so long-term retention needs a collector such as Fluent Bit, Loki or Elasticsearch.

/var/log/pods/<namespace>_<pod>_<uid>/<container>/0.log
/var/log/containers/<pod>_<namespace>_<container>-<container-id>.log

One CRI log line

containerd and CRI-O write each line as a timestamp, a stream, a flag and the message. The timestamp is RFC 3339 with nanoseconds, the stream is stdout or stderr, and the flag is F for a complete line or P for part of a multi-line entry.

If the message is JSON you have a structured log; otherwise it is whatever the application printed. When a library writes to both stdout and stderr, the two streams interleave in the same file.

2026-11-29T08:12:44.123456789Z stdout F {"level":"info","msg":"request","status":200,"duration_ms":184}

Using kubectl logs well

The basic command prints recent logs once. Add -f to follow, --since=1h to limit the window, --tail=200 for the last lines and --timestamps to prefix time. Use --previous for the run before a restart, -c for one container and -l for every pod with a label.

In production, pair --since with --tail. Downloading a whole log tends to pull in personal data and tokens and produces a large file.

kubectl logs deploy/api --since=30m --tail=500 --timestamps
kubectl logs api-7d9f8c6b5-4xk2p -c sidecar --previous
kubectl logs -l app=api --since=10m --max-log-requests=5

Why structured logs pay off

A JSON message lets an aggregator read status, path and duration as fields instead of interpreting a sentence with regular expressions. The result is more accurate and survives a change of wording.

Multi-line stack traces, by contrast, are split into several CRI lines with the P flag. Emitting one event per line, or joining the stack into one string field, makes collection and masking simpler.

Recommended fields
FieldTypeExample
timestampRFC 33392026-11-29T08:12:44Z
levelstringinfo, warn, error
msgshort stringrequest completed
http.method, http.urlstringGET /api/v1/orders/4821
http.status_codeinteger200
duration_msnumber184
request_idstringreq-EXAMPLE-0001

Retention and cost

Node-local logs rotate so they cannot fill the disk. kubelet limits them with containerLogMaxSize and containerLogMaxFiles, while a collector ships them to central storage.

The longer central storage keeps a log, the longer personal data stays exposed. Set a period per purpose — 30 to 90 days for access logs, six to twelve months for security audit logs — and mask on the way in rather than at read time.

kubelet:
  containerLogMaxSize: 50Mi
  containerLogMaxFiles: 5

Questions

Where do logs go after a container restart?

A restarted container in the same pod leaves its previous output in a rotated file, reachable with kubectl logs --previous. Deleting the pod removes the local files, which is why central collection matters.

Do I need to keep stdout and stderr separate?

It helps when reading an incident but rarely matters for aggregation. A structured level field is a better way to separate errors from routine output.

Sources

Every example here can be pasted into the workbench as it is.

Open the workbench