Context
A pod whose container keeps exiting with an error (here exit code 3, on Kubernetes v1.37 with containerd) doesn’t sit in CrashLoopBackOff all the time. Its STATUS alternates between two states:
Error:state.terminated, the instance that just finishedCrashLoopBackOff:state.waiting, the kubelet in back-off before the next start
Insight
kubectl logs <pod> --previous only works in the CrashLoopBackOff window. While the pod shows Error, the finished instance is still the current container, so there is no previous one, and the command fails:
unable to retrieve container logs for containerd://…Plain kubectl logs <pod> returns the crashed instance’s output in both windows. So an error from --previous at seemingly random moments is a timing issue, not a missing log.
In scripts
Check STATUS first, or just use plain logs:
status=$(kubectl get pod crasher -o jsonpath='{.status.containerStatuses[0].state.waiting.reason}')if [ "$status" = "CrashLoopBackOff" ]; then kubectl logs crasher --previouselse kubectl logs crasher # Error window: the finished instance is currentfikubectl get pod crasher -o jsonpath='{.status.containerStatuses[0].lastState.terminated.exitCode}'