Debugging CrashLoopBackOff and OOMKilled pods: finding the real memory limit
I got pulled into this at about 2am on a Thursday. The payments-api deployment had been restarting for forty minutes, nobody had shipped anything, and the on-call person had already rolled back once with no effect. EKS 1.28, managed node group on m5.large, nothing exotic in the chart.
## What kubectl actually showed
$ kubectl get pods -n payments
NAME READY STATUS RESTARTS AGE
payments-api-7d8f9c45bd-k2m4p 0/1 CrashLoopBackOff 9 (65s ago) 18m
payments-api-7d8f9c45bd-q7n9w 0/1 Running 1 (14m ago) 18m$ kubectl describe pod payments-api-7d8f9c45bd-k2m4p -n payments | tail -20
Last State: Terminated
Reason: OOMKilled
Exit Code: 137
Started: Thu, 14 Mar 2024 01:58:04 +0000
Finished: Thu, 14 Mar 2024 02:01:31 +0000
Ready: False
Restart Count: 9
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning BackOff 3m (x22 over 17m) kubelet Back-off restarting failed container
Exit 137 is the giveaway: 128 plus SIGKILL. The kernel killed it, the app did not crash on its own. kubectl logs --previous ended abruptly with nothing after the last request line, which is what you get when the process never gets to write an error.
## Reading the resource block
The deployment had this:
resources:
requests:
cpu: 100m
memory: 256Mi
limits:
memory: 512Mi
One replica sat at about 180Mi and was fine. The other reached 512Mi and died. Same image, same config, so my first guess was a slow memory leak that only showed up on the pod taking production traffic.
The trap here is that a memory limit is not a soft target. Go and Node runtimes look at cgroup limits, but older JVMs and some native libs do not, and they allocate until the OOM killer arrives. We were on Node 20 with no explicit heap cap, so V8 was happy to grow toward whatever the cgroup said was available.
## Confirming with metrics
$ kubectl top pod -n payments
NAME CPU(cores) MEMORY(bytes)
payments-api-7d8f9c45bd-k2m4p 31m 509Mi
payments-api-7d8f9c45bd-q7n9w 28m 241Mi
509Mi against a 512Mi limit, minutes before the restart. That confirmed it. There was no leak in the usual sense; a new endpoint that buffered a full response body had pushed the baseline up by roughly 300Mi.
## The change that stuck
We raised the limit to 1Gi and, more importantly, set the request to 768Mi so the scheduler would stop cramming too many replicas onto a 2Gi node. Then we capped the runtime:
env:
- name: NODE_OPTIONS
value: "--max-old-space-size=640"
resources:
requests:
cpu: 200m
memory: 768Mi
limits:
memory: 1Gi
Setting a heap cap below the cgroup limit means Node throws an allocation error we can log instead of getting killed silently. That was the part I should have done from the start.
## What I would do differently
We had no alert on container_memory_working_set_bytes creeping up, only on restarts. By the time a restart alert fires the damage is done. I added a warning at 80% of the limit the next morning. If you know a cleaner way to catch this without paging someone at 2am, I am all ears.