Difficulty: Beginner
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?
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.
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:
| Flag | What it does |
|---|---|
-f | Follow the stream |
--tail=100 | Only the last 100 lines |
--since=15m or --since-time=... | Only recent lines |
-c sidecar | Pick a container in a multi-container Pod |
--all-containers | Logs from every container in the Pod |
-p / --previous | Logs from the previous, crashed container instance |
-l app=api | Logs from all Pods with that label |
--prefix | Prefix each line with the Pod and container name |
--timestamps | Add 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.
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>.
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.
# 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.
kubectl logs, Streaming, Previous Container, Multi-container Pods, Log Aggregation