Sub topic 4
Container states: Waiting, Running, Terminated
This chapter assumes you can run kubectl get and kubectl describe, read YAML, and know that a Pod holds one or more containers. It builds on the Pod phase, which gives only a coarse summary. The details are in each container's state.
Three states per container
Every container in a Pod has its own state, reported under status.containerStatuses. A container is in exactly one of three states:
| State | Meaning | Typical fields |
|---|---|---|
| Waiting | The container is not running yet, or is between attempts. | `reason`, `message` |
| Running | The container process has started. | `startedAt` |
| Terminated | The process ran and exited. | `exitCode`, `reason`, `startedAt`, `finishedAt` |
Waiting is the default state. A container that is pulling its image, waiting for a volume, or backing off after a crash is Waiting, and the reason field says which.
Phase can hide a failing container
A Pod's phase is a summary. A Pod stays in phase Running as long as at least one container is running or restarting. This means a crashing container can sit under a Pod that still reports Running.
status:
phase: Running
conditions:
- type: Ready
status: "False"
reason: ContainersNotReady
containerStatuses:
- name: app
ready: false
restartCount: 5
state:
waiting:
reason: CrashLoopBackOff
lastState:
terminated:
reason: Error
exitCode: 1Read this excerpt from the bottom up:
lastState.terminatedshows the previous run exited with code 1 and reasonError.state.waitingwithCrashLoopBackOffshows the kubelet is delaying the next restart.restartCount: 5shows this has happened repeatedly.ready: falsefeeds the Pod'sReadycondition, which is also false.
state and lastState
state is what the container is doing now. lastState holds the previous state, and it is mainly useful after a restart. When a container is Waiting in backoff, the current state has no exit information. The reason it died lives in lastState.terminated.
You can read it directly:
kubectl get pod web-0 -o jsonpath='{.status.containerStatuses[0].lastState.terminated.reason}'A result of OOMKilled means the kernel killed the process for exceeding its memory limit. A result of Error means the process exited with a non-zero code, so check exitCode.
Exit codes
Terminated containers report an exitCode. These values come up most often:
0: the process exited normally. For a Job this is success.1or another small number: the application failed. Check its logs.137: the process received SIGKILL (128 + 9). This is common withOOMKilled.143: the process received SIGTERM (128 + 15), the normal result of a graceful shutdown.
Restart information
restartCount counts how many times the kubelet restarted the container. Whether it restarts at all depends on the Pod's restartPolicy: Always (the default), OnFailure, or Never. Between restarts the kubelet waits with an exponential backoff that grows up to five minutes. That wait is what you see as CrashLoopBackOff.
Because the previous instance is gone from the live state, its output is available only through the logs of that earlier run:
kubectl logs web-0 --previousInspecting states with kubectl
kubectl describe prints the same data in a readable form. Look for the State, Last State, Ready, and Restart Count lines under each container, and the Events section at the bottom.
kubectl get pod web-0 -o wide
kubectl describe pod web-0In describe, a crashing container looks like this:
State: Waiting
Reason: CrashLoopBackOff
Last State: Terminated
Reason: Error
Exit Code: 1
Ready: False
Restart Count: 5Common mistakes
- Treating phase
Runningas healthy. Checkreadyandstatefor each container. - Reading only
stateafter a restart. The cause of death is inlastState. - Checking only the first container. Pods with sidecars or init containers have several entries, and init containers appear under
initContainerStatuses.
Exercise
A Pod shows phase Running, but one container has state.waiting.reason: CrashLoopBackOff and lastState.terminated.exitCode: 137. What does this suggest, and where do you look next?
Show answer
Exit code 137 means the process was killed with SIGKILL, often because it exceeded its memory limit. Check lastState.terminated.reason for OOMKilled, then compare the container's memory limit with its actual use. Use kubectl logs web-0 --previous to see the output before the kill.