Home > Enterprise >  Is it possible to enforce a hard memory limit on pods?
Is it possible to enforce a hard memory limit on pods?

Time:12-16

A Container can exceed its memory request [...]. If a Container allocates more memory than its limit, the Container becomes a candidate for termination.

From the documentation I understand setting resource limits can't limit the pod's memory usage.

I have a container that will always use up all the memory available to it, but will function also with smaller amounts of memory. Is there any way I can set a hard limit to the amount of memory available to the pod running the container?

Since it is possible in Docker I hope there is also a way to achieve this in Kubernetes, but I could not find a way to do this.

CodePudding user response:

It is possible with standard request and limits. First see the official documentation page:

When you specify a Pod, you can optionally specify how much of each resource a Container needs. The most common resources to specify are CPU and memory (RAM); there are others.

When you specify the resource request for Containers in a Pod, the scheduler uses this information to decide which node to place the Pod on. When you specify a resource limit for a Container, the kubelet enforces those limits so that the running container is not allowed to use more of that resource than the limit you set. The kubelet also reserves at least the request amount of that system resource specifically for that container to use.

In kubernetes, if you want to reserve a minimum amount of memory specifically for a given container, you should use request. If, on the other hand, you want to force the container to not be able to use more memory than you specify, you must use the limit.

Here is an example:

resources:
  limits:
    cpu: 500m
    memory: 1Gi
  requests:
    cpu: 500m
    memory: 2Gi

See also this great article about setting the right requests and limits in Kubernetes.

CodePudding user response:

Normal requests and limits should work. Documentation might be misleading. Pods will not go beyond the memory limit, if they try to it will cause an OOM and crash(restarted).

  • Related