Debug a Restarting Pod Using Previous Container Logs
Scenario
A Pod named log-processor is running in the workloads namespace. The container keeps restarting because its command fails.
Because restartPolicy: Always is set, the container keeps restarting, giving you access to the previous container logs before investigating and fixing the issue.
Task
1. Save the previous container crash logs to /home/laborant/previous-logs.txt.
2. Read the logs to understand why the container is crashing.
3. Fix the container args based on what you find in the logs so the Pod stays Running.
Important: Check the logs carefully to identify the exact failure. Do not remove, modify the existing args commands, but you may add the missing command required to fix the error before the failing command executes.
Hint 1 — Capture previous logs
When a container keeps restarting, the --previous flag lets you read the logs from the last terminated instance before it was restarted. Without it, you only see logs from the currently running container.
kubectl logs <pod> -n <namespace> -c <container> --previous
Save the output to a file so you can refer back to the error message while investigating.
Hint 2 — Understand and resolve the crash
Read the error message carefully — it tells you exactly what failed and why. The error stat: can't stat '/logs/init.d/sas.log' means the file /logs/init.d/sas.log does not exist when the command runs. The fix is to ensure the required directory and log file are created before the failing step executes. Once you understand the error, update the args field of the container using:
kubectl edit pod log-processor -n workloads
Keep all existing steps intact — only add what is missing before the failing command.