← Whiteboard questions

A container restarts again and again after a release

Pods end after 40 seconds and restart

Troubleshooting, Easy

You are on call for a Kubernetes cluster. report-worker 2.4.0 (3 pods) was released at 14:05 and the pods restart in a loop. At 14:25 you see: - All pods show CrashLoopBackOff, 9 restarts each. Before, 0 for months. - Each pod lives about 40 seconds. Customer report jobs stay "queued". - kubectl describe pod shows OOMKilled, exit code 137, memory limit 256Mi. - The last log line before each restart is loading customer export into memory (412 MB). No error follows. - Version 2.3.0 used about 180Mi. The nodes have free memory. Give your first 15 minutes in order, with reason and command. Then explain the cause, the proof, and the prevention.

Examples

Input kubectl get pods -w
Output Each pod goes Running, then Error, then CrashLoopBackOff. The age of each run is about 40 seconds and the restart count is 9.
Why Shows the pattern and how long each run lasts. It does not give the reason.
Input kubectl logs report-worker-xxxx --previous | tail -3
Output The last line is "loading customer export into memory (412 MB)". No error line follows.
Why Shows where the previous run stopped. A missing error line is a fact the answer must explain.
Input Decoy: kubectl top nodes
Output Every node is below 40% memory.
Why The nodes have free memory. This does not say whether one container ran into a limit of its own.

Constraints

Your answer must contain

A strong answer covers A strong answer rolls back first, reads OOMKilled and exit code 137, ties the 412 MB load to the 256Mi limit, fixes it by streaming or a measured limit, and tests with production limits.

Hints

Try the problem first. Open one hint at a time.

Hint 1 a nudge

Customers are waiting. Before you hunt for the cause, what is the quickest safe step? The release is the only change in 7 days.

Hint 2 the approach

Read the state of the dead container, not only the log. The reason and the exit code say how it ended, and why no error line appears.

Hint 3 the key step

Compare the 412 MB that the worker loads with the 256Mi limit. Then choose between streaming the export and a limit based on a measured peak.

Walkthrough

Full walkthrough brute force to best

First minutes, in order

The first minutes, in order. 0 to 2 minutes: tell the incident channel that you are on call, the impact (all 3 pods down, report jobs queued) and the time line (release at 14:05). 2 to 5 minutes: roll back with kubectl rollout undo deployment/report-worker, because a rollback is quick and safe and the release is the only change. 5 to 8 minutes: run kubectl get pods -w until the pods run and the restart count stops growing, and watch the queue depth fall. Hold for several minutes before you call it fixed.

Narrow down the cause

8 to 15 minutes: read the state of the dead container. kubectl describe pod shows the last state, the reason and the limits, and kubectl logs --previous shows the last lines. Reason OOMKilled with exit code 137 means the container crossed its memory limit. The log has no error line, because the kernel ends the process with no chance to log. A liveness probe kill also gives 137, but then the reason is Error and the events show the failed probe. A startup crash gives exit code 1 and an error in the log.

Cause and fix

Cause: version 2.4.0 loads a 412 MB export into memory at start, and the limit is 256Mi, so the kernel kills it within about 40 seconds. Kubernetes restarts it with a growing back-off delay, which is CrashLoopBackOff. Version 2.3.0 used about 180Mi because it did not load the whole export. Free memory on the node does not matter, because the limit is per container. Fix: the rollback came first. Then change the code to stream the export in chunks, so memory does not grow with the export. Raising the limit to a measured peak plus headroom, with the request equal to it, is the second choice.

Read the evidence

Read the evidence. kubectl get pods shows each pod running about 40 seconds and then ending, with 9 restarts each. kubectl logs --previous ends at the line that loads the 412 MB export. kubectl describe pod shows Reason OOMKilled, exit code 137 and a limit of 256Mi. The node check shows every node below 40 percent memory, so the nodes are not short of memory. The 412 MB load is bigger than the 256Mi limit, which explains the kill.

Commands

kubectl get pods -w
kubectl rollout undo deployment/report-worker
kubectl describe pod report-worker-xxxx
kubectl logs report-worker-xxxx --previous | tail -3
kubectl top pod --containers

Confirm and prevent

Prevention: test a release with the production memory limit and a production-size export. Alert on more than 3 restarts in 10 minutes and on memory above 80 percent of the limit. Roll out one pod first with a small maxUnavailable. Record the peak memory in the release notes.

Common mistakes

  • Deleting pods or restarting by hand again and again. The loop continues.
  • Raising the limit by guess, with no measured peak.
  • Reading code for an hour before any rollback.
  • Blaming the node. The limit is per container, and the nodes have free memory.

Related problems

A container restarts again and again after a release