OOMKilled containers in Kubernetes - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When Kubernetes kills containers due to out-of-memory (OOM) errors, it's important to understand how the system checks and reacts as workload size changes.
We want to see how the time to detect and handle OOMKilled containers grows as the number of containers increases.
Analyze the time complexity of the following Kubernetes event watcher snippet.
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: app
image: busybox
resources:
limits:
memory: "100Mi"
command: ["sh", "-c", "sleep 3600"]
This pod runs a container with a memory limit. Kubernetes monitors containers and may OOMKill if they exceed this limit.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Kubernetes node agent checks memory usage of each container periodically.
- How many times: Once per container per check interval.
As the number of containers grows, the system checks each container's memory usage one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 containers | 10 memory checks |
| 100 containers | 100 memory checks |
| 1000 containers | 1000 memory checks |
Pattern observation: The number of checks grows directly with the number of containers.
Time Complexity: O(n)
This means the time to detect OOMKilled containers grows linearly with the number of containers running.
[X] Wrong: "Kubernetes checks all containers at once instantly, so time doesn't grow with more containers."
[OK] Correct: Each container's memory usage is checked individually in a loop, so more containers mean more checks and more time.
Understanding how Kubernetes monitors container resources helps you explain system behavior and scaling in real environments.
"What if Kubernetes used parallel checks for container memory usage? How would the time complexity change?"
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
