What if one hungry container could crash your whole app without warning?
Why OOMKilled containers in Kubernetes? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you run a busy restaurant kitchen where chefs have limited counter space. When too many dishes pile up, the kitchen gets overwhelmed and some dishes get dropped or ruined.
Manually tracking memory use of each container is like watching every chef closely all day. It's slow, tiring, and easy to miss when the kitchen runs out of space, causing containers to crash unexpectedly.
Kubernetes automatically monitors container memory use and stops (kills) containers that use too much memory to protect the whole system. This prevents crashes from spreading and helps keep your apps running smoothly.
docker stats container_id
# Manually check memory usage and restart container if neededkubectl describe pod pod-name
# See OOMKilled event and let Kubernetes handle restartsThis lets you run many containers safely without worrying about one using too much memory and crashing everything.
A web app with many users suddenly uses more memory. Kubernetes detects this and stops the heavy container, restarting it cleanly so the app stays available.
OOMKilled means a container used too much memory and was stopped.
Manual memory checks are slow and error-prone.
Kubernetes automates memory limits and container restarts for stability.
Practice
OOMKilled?Solution
Step 1: Understand OOMKilled meaning
OOMKilled means Out Of Memory Killed, which happens when a container uses more memory than its limit.Step 2: Relate to container status
When a container exceeds its memory limit, Kubernetes stops it to protect the system.Final Answer:
The container was stopped because it used more memory than allowed. -> Option DQuick Check:
OOMKilled = Memory limit exceeded [OK]
- Confusing OOMKilled with network errors
- Thinking OOMKilled means container finished normally
- Assuming OOMKilled is a manual stop
kubectl command helps you check why a pod's container was OOMKilled?Solution
Step 1: Identify command to get pod details
kubectl describe pod <pod-name>shows detailed pod info including container status and reasons for restarts.Step 2: Confirm OOMKilled reason visibility
This command shows events and container states, including if a container was OOMKilled.Final Answer:
kubectl describe pod <pod-name> -> Option CQuick Check:
Describe pod shows OOMKilled reason [OK]
- Using 'kubectl get pods' which lacks detailed reason
- Checking logs which may not show OOMKilled cause
- Running exec commands which don't show pod status
State: Terminated Reason: OOMKilled Exit Code: 137
Solution
Step 1: Analyze the Reason field
The Reason is 'OOMKilled', which means the container was killed because it exceeded memory limits.Step 2: Understand Exit Code 137
Exit code 137 means the process was killed by signal 9 (SIGKILL), typical for OOMKilled events.Final Answer:
The container was killed because it used too much memory. -> Option AQuick Check:
Reason OOMKilled + Exit 137 = Memory kill [OK]
- Confusing CPU limits with memory limits
- Assuming manual stop without checking reason
- Thinking software error caused termination
Solution
Step 1: Identify cause of repeated OOMKilled
Repeated OOMKilled means the container needs more memory than allowed.Step 2: Choose proper fix
Increasing the memory limit lets the container use more memory and prevents OOMKilled.Final Answer:
Increase the container's memory limit in the pod spec. -> Option AQuick Check:
More memory limit stops OOMKilled [OK]
- Lowering CPU limit which doesn't affect memory
- Removing memory limit causing instability
- Relying on manual restarts instead of fixing limits
Solution
Step 1: Understand memory limit and OOMKilled
The container hits the 512Mi limit and is killed. Increasing limit is not an option.Step 2: Find alternative to increasing memory
Optimizing the app to use less memory reduces usage below the limit, preventing OOMKilled.Step 3: Evaluate other options
Removing limit risks node stability, increasing CPU doesn't reduce memory, adding replicas doesn't fix memory per container.Final Answer:
Optimize the application to use less memory inside the container. -> Option BQuick Check:
Lower memory use avoids OOMKilled without raising limit [OK]
- Removing memory limits causing node crashes
- Increasing CPU expecting memory fix
- Adding replicas without fixing memory use
