Container Restarts and lastState
A container crashed and restarted on its own, and now it runs as if nothing had ever gone wrong. Where can you still find out what killed it the last time around, and for how many restarts does that answer last?
Play the animation 6 steps, animated, plays on a computer or a tablet
Container Restarts and lastState, step by step
-
The process hits its memory limit and the kernel kills it. Kubelet spots the dead container and writes state as Terminated: a record carrying exitCode 137, reason OOMKilled, startedAt and finishedAt. The record holds the live slot only until Kubelet turns to the restart, not until the new container runs.
Play from step 1 -
A container can leave a note. Whatever it writes to terminationMessagePath, by default /dev/termination-log, goes into the message field of that Terminated record. The terminationMessagePolicy defaults to File, and FallbackToLogsOnError takes the log tail instead when the file is empty after a failed exit.
Play from step 2 -
Kubelet starts a fresh container in the same Pod and reports it. The records roll over. The state reads Running again, the Terminated record moves into lastState, and what lastState held before is dropped. The restartCount becomes 2. The dead instance stays on the Node, because container GC keeps one per container by default.
Play from step 3 -
The live state reads Running, so the container is fine right now and the answer is not here. Running kubectl describe pod prints both records, State and Last State, and the answer is in Last State. It holds a Terminated record with exitCode 137 and reason OOMKilled, which explains why the previous instance died.
Play from step 4 -
The exitCode narrows the cause, and the reason beside it names that cause. 0 is Completed, a clean exit, and 1 is a generic application Error. Codes above 128 usually carry a signal number. So 137 is 128 plus 9 for SIGKILL, and if the kernel killed the process, the reason is OOMKilled. 143 is 128 plus 15 for SIGTERM.
Play from step 5 -
The record has no log. Running kubectl logs --previous asks for the log of the previous instance, and Kubelet reads it off the dead container on the Node. It works for one generation only: the next restart makes this instance the older one, container GC removes it, and its lastState record is overwritten too.
Play from step 6