Marcio Cunha

Container crashing? How to diagnose OOMKill in Kubernetes

Did your Kubernetes pod suddenly stop? Learn how to identify the OOMKill error and discover why the Linux kernel terminated your process due to low RAM.

Marcio Cunha•2 min
Also available in:PortuguêsEspañol
Summary
  • The OOMKill error occurs when the Linux kernel terminates processes that exceed the memory limit set in the pod manifest.
  • The kubectl describe command is the primary tool for checking the container status and identifying the 137 exit code.
  • Accurate analysis of Kubernetes events prevents guesswork and allows for proper and predictable resource scaling.
  • Monitoring real memory usage before setting limits helps balance infrastructure costs with application stability.
  • Understanding the distinction between requests and limits is the first step to prevent arbitrary eviction by the orchestrator.

Why does Kubernetes kill my container?

When a container stops working without warning and Kubernetes restarts it, the primary suspect should be memory exhaustion. This phenomenon is known as OOMKill, short for Out Of Memory Kill. In practice, this means the Linux kernel, the brain of the operating system, noticed that the pod was consuming more memory than allowed and, to save the rest of the node from a total system freeze, it simply terminates the offending process.

How to identify OOMKill with kubectl

The first step to confirm this suspicion is to check the pod status. Kubernetes is quite transparent if you know where to look. Use the command kubectl describe pod <pod-name> and focus on the Last State section. If you find a Reason: OOMKilled or an Exit Code: 137, you have found the root cause. Exit code 137 is the classic signature of a process that received a forceful termination signal from the system.

Understanding memory limits

In Kubernetes, we control consumption through Limits, which is the hard ceiling of RAM the container can reach. Beyond understanding this mechanism, it is essential to master how the system manages resources, which we have covered in detail when we limit Docker container memory with Cgroups v2. If your process exceeds the limit, the kernel does not ask if it needs more time; it kills it instantly.

How to analyze pod events for diagnostics

In addition to the describe command, pod events provide a timeline of what happened. Run kubectl get events --sort-by='.lastTimestamp' to list recent occurrences. Look for Killing messages. Combining this view with logs is crucial; if the container is in an infinite failure loop, see how to handle this CrashLoopBackOff error and debug on Linux to extract valuable information before the pod disappears.

How to prevent further crashes

  1. Check monitoring metrics (like Prometheus/Grafana) to see the usage behavior before the crash.
  2. Review the pod manifest and compare the memory limit value with the peak usage.
  3. Consider increasing the limit only if the application truly requires it; otherwise, optimizing code might be a cheaper solution.
  4. If the pod keeps restarting and you need a quick manual intervention, learn how to restart stuck pods in Kubernetes with kubectl.

Conclusion

OOMKill is not a system error, but a protection mechanism to maintain the health of your cluster. Diagnosing memory issues requires looking beyond surface symptoms and understanding the resource guarantees you have configured.

When adjusting your limits, always remember to observe the application behavior under real-world load. Balancing performance and stability is an ongoing task for any systems engineer.