Why is my Kubernetes Pod OOMKilled?
Find OOMKilled Pods
An OOMKilled Kubernetes Pod has a status of OOMKilled
. The kubectl get pod
and kubectl describe pod
commands will both display the OOMKilled
status.
$ kubectl -n k8salliance get pod nginx
NAME READY STATUS RESTARTS AGE
nginx 0/1 OOMKilled 1 28s
The OOMKilled
status means that Kubernetes stopped the Pod because the Pod exceeded its memory limits.
How Does Kubernetes Determine Memory and CPU Limits?
Kubernetes allocates memory and CPU resources to a Pod according to the Pod’s resource settings.
A Pod’s resource settings define the CPU and memory available to containers in the Pod.
The request
value specifies the initial amount allocated to the Pod. The limit
value specifies the maximum amount available to the containers in the Pod. Each container in the Pod launches with the request
amount and may consume up to the limit
amount.
View Namespace Memory and CPU Usage Limits
If a Pod does not specify resource settings, it inherits the resource settings of the namespace.
The kubectl describe
command displays the resource limits of a namespace.
$ kubectl describe namespace k8salliance
Name: k8salliance
Labels: <none>
Annotations: <none>
Status: ActiveResource Quotas
Name: gke-resource-quotas
Resource Used Hard
-------- --- ---
count/ingresses.extensions 0 100
count/jobs.batch 0 5k
pods 0 1500
services 0 500No resource limits.
Modify Namespace Memory and CPU Usage Limits
To set the default resource limits of a namespace, create a LimitRange object in the namespace.
Any Pod created in the namespace without resource usages defined will inherit the namespace’s default resource request and limit settings.
kubectl -n k8salliance create -f - <<EOF
apiVersion: v1
kind: LimitRange
metadata:
name: default-resources
spec:
limits:
- default:
memory: 128Mi
defaultRequest:
memory: 64Mi
type: Container
EOF$ kubectl describe namespace k8salliance
Name: k8salliance
Labels: <none>
Annotations: <none>
Status: Active
Resource Quotas
Name: gke-resource-quotas
Resource Used Hard
-------- --- ---
count/ingresses.extensions 0 100
count/jobs.batch 0 5k
pods 0 1500
services 0 500
Resource Limits
Type Resource Min Max Default Request Default Limit Max Limit/Request Ratio
---- -------- --- --- --------------- ------------- -----------------------
Container memory - - 64Mi 128Mi -
Set Pod Resource Limits
To prevent your Kubernetes Pod from being OOMKilled, you need to define the memory and CPU resources required by the Pod.
apiVersion: v1
kind: Pod
metadata:
name: default-mem-demo
spec:
containers:
- name: default-mem-demo-ctr
image: nginx