Which run of the container are you actually reading? A pod that has restarted 7 times has had 8 runs of the same container, and plain kubectl logs shows only the one that is alive now, which may have started 3 seconds ago and printed nothing but a version banner. The run that crashed is a different process with a different log file, and you have to ask for it by name.
That is what --previous (short form -p) does. It is also narrower than most people assume: one earlier run, of one container, from a file on one node, as long as that node still has the file. Knowing exactly what it reads explains every case where it comes back empty.
What --previous actually reads on the node
Container instance: one run of a container inside a pod. Each restart creates a new instance with the same name and a restart count one higher.
lastState: the field in the pod status that describes the most recent terminated instance of a container: exit code, reason, signal, start and finish time. It is what kubectl describe pod prints under Last State.
Termination message: a short text that the container can write to /dev/termination-log before exiting, which the kubelet copies into lastState. It survives even when the log file does not.
The kubelet does not store logs itself. The container runtime, containerd or CRI-O on almost every current cluster, writes each instance's stdout and stderr to a file under /var/log/pods/<namespace>_<pod>_<uid>/<container>/, named after the restart count: 0.log, 1.log and so on. When you run kubectl logs, the API server asks the kubelet on that node to stream the file for the current instance. With --previous, it streams the file for the instance just before.
The kubelet keeps the log of the current instance and of the last terminated one, and garbage collects older ones. So after the seventh restart you can read run 7 and run 8, never run 3.
Each file is also subject to rotation: by default the kubelet rotates a container log at 10 MiB and keeps up to 5 files, both set by containerLogMaxSize and containerLogMaxFiles in the kubelet configuration. A chatty container that writes 2 MiB a second fills its quota in under half a minute, so even the previous run may only show its last few seconds.
If the API path itself fails, for instance because your role lacks the pods/log permission or the kubelet is slow to answer, you can read the same files from the node. kubectl debug node/worker-3 -it --image=busybox starts a pod on that node with the host filesystem mounted at /host, and ls /host/var/log/pods/payments_api-7c9f_*/api/ lists the numbered files that still exist. Sometimes a rotated, compressed segment of the previous run is sitting there even when the stream shows only its tail.
Everything is tied to the pod object and its UID. Delete the pod, let a Deployment replace it, or lose the node, and the directory goes with it. That single fact is behind most failed attempts.
The sequence for a crashed container
Work through these in order. The first three steps cost seconds and often make the log read unnecessary.
- Find the pod and its restart count:
kubectl get pod -n payments -l app=api. A RESTARTS value of 0 means there is no previous instance, and--previouswill fail with "previous terminated container not found". - Read the last state:
kubectl get pod api-7c9f -n payments -o jsonpath='{.status.containerStatuses[?(@.name=="api")].lastState.terminated}'. Note the exit code, the reason andfinishedAt. - Decode the exit code. 137 is 128 plus signal 9, a SIGKILL, and with reason OOMKilled it means the memory limit was hit. 143 is 128 plus 15, a SIGTERM, usually a failed liveness probe or a deliberate stop. 139 is a segfault. 1 or another small number is the application exiting on its own, which is when logs matter most.
- List the containers if the pod has more than one:
kubectl get pod api-7c9f -o jsonpath='{.spec.containers[*].name}'. Sidecars such as an Envoy proxy restart independently, so name the one that crashed. - Read the previous run:
kubectl logs api-7c9f -n payments -c api --previous --timestamps --tail=200. Timestamps let you line up the last lines withfinishedAtfrom step 2. - For an init container that keeps failing, use its name with
-c. Init containers restart in place too, and--previousworks on them the same way. - Check events for causes that never reach the log:
kubectl get events -n payments --field-selector involvedObject.name=api-7c9f. Failed probes, image pull problems and evictions appear here. - If you used a workload shortcut such as
kubectl logs deploy/api --previous, remember that kubectl picks one pod. With 6 replicas, it may pick a pod that never crashed.
An illustrative run shows the arithmetic. A container with a 512 MiB memory limit restarts every 4 minutes. Step 2 returns exit code 137 and reason OOMKilled at 10:41:12. Step 5 shows the last line at 10:41:11, a cache warm-up message reporting 300,000 keys loaded. Nothing in the log says "out of memory", because the kernel killed the process without warning. The log tells you what it was doing; the last state tells you why it stopped.
Why --previous comes back empty or misleading
Each of these mistakes shows up in incident reviews again and again, and each has a quick tell:
- Reading a new pod. A Deployment rollout, an eviction or a manual
kubectl delete podcreates a pod with a new name and restart count 0. The crashed one is gone with its logs. Check the AGE column: a pod 40 seconds old is not the one that crashed an hour ago. - Using --previous on a Job with restartPolicy Never. Each retry is a new pod, so there is never a previous instance in the same pod. List the failed pods with
kubectl get pods -l job-name=nightly-exportand read each one without the flag, while they still exist. - Expecting more than one run back. After 12 restarts,
--previousshows only run 12. If the interesting crash was the first one, an hour ago, that evidence exists only if something shipped it off the node. - Logs buffered inside the process. Python writes to a block buffer when stdout is not a terminal, so a process killed by SIGKILL loses the last few kilobytes. Set
PYTHONUNBUFFERED=1or flush explicitly. Java and Go apps with async appenders lose lines the same way. - Logging to a file instead of stdout. If the app writes to
/var/log/app.loginside the container, the runtime never sees it and the file disappears with the container's writable layer. - A crash before the first line. A missing shared library, a wrong architecture image ("exec format error") or a bad entrypoint fails before the program prints anything. The previous log is empty, and the reason sits in lastState and events.
- Rotation ate the start. With the 10 MiB default and a noisy debug logger, the beginning of the previous run may be rotated away;
--tailonly shows what is left.
The pattern behind all of them: --previous is a convenience for the case where the pod still exists, on a node that still exists, and crashed recently. Treat it as the first look, never as the archive.
Keeping crash evidence for longer than one restart
The cheapest improvement costs one line per container. Set terminationMessagePolicy: FallbackToLogsOnError in the container spec. When a container exits with an error and has not written its own termination message, the kubelet copies the tail of its log into lastState, limited to 2,048 bytes or 80 lines, whichever is smaller. That snippet lives in the pod status, so kubectl describe shows it even after the log file has rotated.
Next comes node-level log shipping. A DaemonSet such as Fluent Bit or the OpenTelemetry Collector tails the files under /var/log/pods and sends them to a central store, for example Loki, Elasticsearch or a cloud logging service. Each line keeps its pod, container and restart labels, so the first crash of the day remains searchable after the pod has been replaced 30 times. Most managed clusters from the big cloud providers offer this as a toggle, usually billed per GB ingested.
For crashes you can reproduce, kubectl debug with --copy-to creates a copy of the pod with a changed command, for example sleep 3600, so you can open a shell in an identical environment and run the entrypoint by hand while watching the output.
The trade-off is cost against certainty. Shipping every container's logs from a 40-node cluster can run to hundreds of GB a month, and teams that keep 30 days of debug-level logs often find logging rivals compute on the bill. Relying on --previous alone is free but loses evidence on exactly the incidents that matter most, the ones where a rollout replaced the broken pods.
The verdict that holds up in most teams: ship warning-level and above for every workload with 7 to 14 days of retention, add FallbackToLogsOnError everywhere, and keep --previous as the 10 second first check rather than the plan. It is also a fair interview answer when a platform team in Lisbon asks how you would investigate a crash that happened overnight.