Requests and Limits in Kubernetes. What happens when memory limit and CPU limit are exceeded?
sobes.tech AI
Answer from AI
In Kubernetes, two resource parameters can be set for containers: requests and limits.
- Requests — the minimum amount of resources guaranteed to be allocated to the container. The scheduler uses these values to place pods on nodes.
- Limits — the maximum amount of resources the container can use.
What happens when limits are exceeded:
-
CPU limit: If the container tries to use more CPU than specified in the limit, Kubernetes (via cgroups) will restrict its CPU usage. The container will not be killed, but its processes will be allocated less CPU than they want, which may lead to reduced performance.
-
Memory limit: If the container exceeds the memory limit, the Linux kernel (via OOM Killer) will kill the container's process, as memory is allocated strictly. As a result, the container will restart (if RestartPolicy is set).
Example in a manifest:
resources:
requests:
memory: "128Mi"
cpu: "250m"
limits:
memory: "256Mi"
cpu: "500m"
Here, the container is guaranteed 128Mi of memory and 250m CPU but cannot use more than 256Mi memory and 500m CPU.