How to Retrieve Crash Logs of a Restarted Pod Using kubectl --previous
When a pod crashes and continuously restarts, standard kubelet logs may miss the previous container's output, but using kubectl logs with the --previous flag lets you access the logs of the last terminated instance, as explained with commands, file locations, and practical verification steps.
Scenario
When a pod is in a crash state and the container keeps restarting, using kubelet logs may fail to capture any logs.
Solution
Use the kubectl logs command with the --previous flag, which prints logs for the previous instance of the container in a pod if it exists. kubectl logs pod-name --previous For a multi‑container pod:
kubectl logs pod-name --previous -c container-namePrinciple
Kubelet retains the logs of the last few failed containers, which is a prerequisite for using --previous.
Core Mechanism
The --previous option works because kubelet stores pod logs under /var/log/pods/podname as symlinked files that point to Docker container log files. A separate symlink points to the log file of the previously crashed container, and --previous reads that file.
Practice
Example: list pods and view logs.
ubuntu@~$ kubelet get pod
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 2394 99d
nginx-deployment 1/1 Running 0 79d
redis 1/1 Running 0 49dOn the node, inspect the log files:
ls /var/log/pods/default_busybox_f72ab71a-5b3b-4ecf-940d-28a5c3b30683/busybox
2393.log 2394.logThe numbers indicate the restart count (e.g., 2393 is the log after the 2393rd restart).
Both files are symlinks to Docker’s JSON log files. Reading logs without --previous accesses the current container’s file; adding --previous reads the previous container’s file.
Verification by editing the log files shows that kubectl logs busybox --previous returns the content of the previous log, while kubectl logs busybox returns the current log.
Conclusion
Kubelet reads log files from /var/log/pods/. The --previous flag also reads from this directory, using a dedicated symlink that points to the log of the last exited container, allowing you to retrieve crash logs of a pod before it restarts.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
