Senior
How to limit CPU and memory for a container in Kubernetes? What happens if the limits are exceeded?
sobes.tech AI
Answer from AI
In Kubernetes, the fields resources.requests and resources.limits are used in the Pod or Deployment manifest to restrict container resources.
- requests — guarantees the minimum amount of resources allocated to the container.
- limits — the maximum amount of resources the container can use.
Example of CPU and memory restrictions:
containers:
- name: my-container
image: my-image
resources:
requests:
memory: "256Mi"
cpu: "500m"
limits:
memory: "512Mi"
cpu: "1"
What happens when limits are exceeded:
- CPU: If the container tries to use more CPU than specified in the limit, Kubernetes will throttle its usage, but the container will not be killed.
- Memory: If the container exceeds the memory limit, it will be terminated (OOMKilled) by the operating system kernel, and Kubernetes may restart the container depending on the restart policy.
Thus, limits help control resource usage and prevent one container from negatively affecting others in the cluster.