Viewing Logs

Difficulty: Beginner

Question

How do you view and stream the logs of a Pod in Kubernetes, including logs from a crashed container or from many Pods at once?

Answer

Use kubectl logs <pod> to print a container's stdout and stderr, and add -f to stream new lines as they arrive. Add -c <container> for multi-container Pods, --previous to see logs from the last crashed instance, and -l app=<name> to read from all Pods matching a label. For anything beyond quick debugging, ship logs to a central system, because node-level logs are rotated and disappear with the Pod.

Where the logs come from

Kubernetes does not collect application log files. The container runtime captures whatever the process writes to stdout and stderr and stores it on the node under /var/log/pods/. kubectl logs asks the API server, which proxies the request to the kubelet on that node. So:

  • Apps should log to stdout/stderr, not to files inside the container.
  • The kubelet rotates these files (by default around 10Mi per file, keeping 5 files), so old lines are lost on busy Pods.
  • When a Pod is deleted or rescheduled, its logs go with it.

Useful flags

FlagWhat it does
-fFollow the stream
--tail=100Only the last 100 lines
--since=15m or --since-time=...Only recent lines
-c sidecarPick a container in a multi-container Pod
--all-containersLogs from every container in the Pod
-p / --previousLogs from the previous, crashed container instance
-l app=apiLogs from all Pods with that label
--prefixPrefix each line with the Pod and container name
--timestampsAdd the runtime timestamp to each line

When you use a label selector, kubectl defaults to the last 10 lines per Pod and opens at most 5 concurrent streams (--max-log-requests raises that). You can also target a controller, for example kubectl logs deploy/api, which picks one of its Pods.

Debugging a crash loop

If a Pod is in CrashLoopBackOff, kubectl logs <pod> may show nothing because the new container just started. The stack trace is in the previous instance: kubectl logs <pod> --previous. Init container failures need -c <init-container-name>.

Logs in production

On a real system, such as order services at a food delivery company with hundreds of Pods, nobody tails Pods one by one. A DaemonSet agent (Fluent Bit, Vector, the OpenTelemetry Collector) reads /var/log/pods on every node and ships logs to Loki, Elasticsearch/OpenSearch, CloudWatch or a vendor. Structured JSON logs with a request ID make it possible to follow one UPI payment across services. For quick multi-Pod tailing at the terminal, tools like stern colour-code output by Pod.

Path of a kubectl logs request. Logs are read from files on the node, proxied through the API server and kubelet.
  1. App writes to stdout: Plain or JSON log lines from the process.
  2. Runtime writes node file: containerd stores them under /var/log/pods.
  3. kubelet serves logs: Reads and streams the file on request.
  4. API server proxies: Checks RBAC for the pods/log subresource.
  5. kubectl prints output: Follows the stream when -f is set.

Code examples

Common kubectl logs commands

# Stream the last 50 lines and follow
kubectl logs -f --tail=50 api-7d9f8c6b5-x2k4q -n orders

# A specific container in a multi-container Pod
kubectl logs api-7d9f8c6b5-x2k4q -c envoy

# Why did it crash? Read the previous instance
kubectl logs api-7d9f8c6b5-x2k4q --previous

# All Pods of a Deployment, last 15 minutes, labelled by Pod
kubectl logs -l app=api --since=15m --prefix --max-log-requests=10

# Let kubectl pick a Pod from the Deployment
kubectl logs deploy/api -f

Combine --tail or --since with -f to avoid dumping huge histories, and use --previous for crash loops.

Key points

  • kubectl logs reads container stdout/stderr stored on the node.
  • -f streams, --tail and --since limit output, -c selects a container.
  • --previous shows logs from the last crashed instance, essential for CrashLoopBackOff.
  • Label selector logs default to 10 lines per Pod and 5 concurrent streams.
  • Node logs rotate and vanish with the Pod; use a DaemonSet log shipper in production.

Common follow-up questions

  • Why does kubectl logs show nothing for a crash-looping Pod?
  • How would you centralise logs for a cluster with hundreds of Pods?
  • What should an application do so its logs work well in Kubernetes?
  • What RBAC permission does a user need to read Pod logs?

Interview tips

  • Mention --previous unprompted; it is the flag that separates beginners from people who have debugged real crashes.
  • Explain that logs come from stdout/stderr and are not persistent.
  • Close with how production logging works (DaemonSet agent plus a central store).

Concepts covered

kubectl logs, Streaming, Previous Container, Multi-container Pods, Log Aggregation

More Kubernetes & Orchestration questions