Kubernetes 1.29 OOMKilled with exit 137 and free node memory: requests vs limits
A Python API in our EKS 1.29 cluster restarted every night around 03:00. The on-call notes said "node ran out of memory", but kubectl describe node showed 11 GiB allocatable and barely 4 GiB used. So that theory was wrong and I needed to look at the container instead.
## Confirming it was the limit
bash
kubectl get pod api-7d9f8c9d4-x2mql -n prod -o jsonpath='{.status.containerStatuses[0].lastState.terminated.reason}{"\n"}'
kubectl describe pod api-7d9f8c9d4-x2mql -n prod | grep -A6 'Last State'
OOMKilled
Last State: Terminated
Reason: OOMKilled
Exit Code: 137
Started: Mon, 19 May 2024 02:41:05 +0000
Finished: Mon, 19 May 2024 03:04:52 +0000
Exit 137 is 128 plus SIGKILL, and the reason field removes any doubt. The container hit its own cgroup limit, not a node-wide shortage. The kernel OOM killer sees memory.max for that cgroup (we're on containerd 1.7.13 with cgroup v2) and kills the process inside it.
## Requests are not limits
The manifest had:
yaml
resources:
requests:
memory: 128Mi
cpu: 100m
limits:
memory: 256Mi
cpu: 500m
I'd assumed increasing requests would stop the killing. It doesn't. Requests are what the scheduler uses to place the pod and what the kubelet uses for QoS classification. Limits are the hard ceiling enforced by the runtime. With requests set much lower than limits, the pod is Burstable, and the scheduler packed six copies onto one node based on 128 Mi each.
kubectl top pod confirmed the real usage:
NAME CPU(cores) MEMORY(bytes)
api-7d9f8c9d4-x2mql 182m 249Mi
It was sitting right on the 256 Mi ceiling. The nightly report endpoint built a large DataFrame in memory, which pushed it over.
I checked the limit from inside the container as well, because that always makes it concrete: cat /sys/fs/cgroup/memory.max returned 268435456. The node's 11 GiB of free memory was completely irrelevant. The runtime enforced that one number, and the spike only had to last a few seconds to trip it.
## The Java version of the same problem
A sibling service had the same symptom with -Xmx2g baked into its JAVA_TOOL_OPTIONS and a 1 GiB limit. It died before the JVM ever OOMed, because the cgroup kill came first. We switched it to -XX:MaxRAMPercentage=70 -XX:+UseContainerSupport so the heap respects the cgroup. The Python service got a streaming parser instead of loading the whole report.
## What we changed
Both services now set requests equal to limits for memory:
yaml
resources:
requests:
memory: 512Mi
cpu: 250m
limits:
memory: 512Mi
cpu: 500m
Equal values give them Guaranteed QoS, which means they're last in line for eviction under node pressure. It also means the scheduler reserves the full 512 Mi, so fewer pods fit per node. That's the trade; we added a node rather than pretending the old packing was safe.
I also added an alert on container_memory_working_set_bytes / container_spec_memory_limit_bytes > 0.8, which would have caught the trend days before the first kill. Checking kubectl describe node was a dead end and cost me an hour I'd rather have back.