K8s memory request handling for 2 and more pods

4/9/2020

I am trying understand memory requests in k8s. I have observed that when I set memory request for pod, e.g. nginx, equals 1Gi, it actually consume only 1Mi (I have checked it with kubectl top pods). My question. I have 2Gi RAM on node and set memory requests for pod1 and pod2 equal 1.5Gi, but they actually consume only 1Mi of memory. I start pod1 and it should be started, cause node has 2Gi memory and pod1 requests only 1.5Gi. But what happens If I try to start pod2 after that? Would it be started? I am not sure, cause pod1 consumes only 1Mi of memory but has request for 1.5Gi. Do memory request of pod1 influences on execution of pod2? How k8s will rule this situation?

-- Kirill Bugaev
kubernetes

3 Answers

4/9/2020

Memory request is the ammount of memory that kubernetes holds for pod. If pod requests some ammout of memory, there is a strong guarantee that it will get it. This is why you can't create pod1 with 1.5Gi and pod2 with 1.5Gi request on 2Gi node beacause if kubernetes would allow it and these pods start using this memory kubernetes won't be able to satisfy the requirements and this is unaccpetable.

This is why sum of all pod requests running an specific node cannot exceed this specific node's memory.

"But what happens If I try to start pod2 after that? [...] How k8s will rule this situation?"

If you have only one node with 2Gi of memory then pod2 won't start. You would see that this pod is in Pending state, waiting for resources. If you have spare resources on different node then kubernetes would schedule pod2 to this node.

Let me know if something is not clear and needs more explaination.

-- HelloWorld
Source: StackOverflow

4/9/2020

In Kubernetes you decide on Pod/Container memory using two parameters:

  • spec.containers[].resources.requests.memory: Kubernetes scheduler will not schedule your Pod if there is not enough memory, this memory is also reserved for you container
  • spec.containers[].resources.limits.memory: Container cannot exceed this memory

If you want to be precise about the memory for you container, then you'd better set the same value for both parameters.

This is a very good article explaining by example. And here's the official doc.

-- RafaƂ Leszko
Source: StackOverflow

4/9/2020

Request is reserved resource for a container, Limit is maximum allowed for the container to use. If you try to start two pods with 1.5Gi on a machine with 2Gi the 2nd one will not start due to the lack of resources it needs to reserve. You need to set requests lower - to the average expected consumption of the pod and some reasonable Limit (max allowed memory). It's better to get familiar with these concepts

-- Anton Matsiuk
Source: StackOverflow