Memory requests and limits in Kubernetes
Tags: kubernetes, devops
Published: 2023-03-27 12:23:51

In Kubernetes, what should I use as memory requests and limits?

And what happens when you don't set them?

Let's dive into it.

In Kubernetes, you have two ways to specify how much memory a pod can use:

1. "Requests" are usually used to determine the average consumption.
1. "Limits" set the max number of resources allowed.

**The Kubernetes scheduler uses requests to determine where the pod should be allocated in the cluster.**

Since the scheduler doesn't know the consumption (the pod hasn't started yet), it needs a hint.

![The Kubernetes scheduler works best with requests](https://res.cloudinary.com/learnk8s/image/upload/v1679895579/threads/memory-allocations-3_ofejy7.png)

**The kubelet uses limits to stop the process when it uses more memory than is allowed.**

It's worth noting that the process could spike in memory usage before it's terminated.

![The kubelet terminates the container when it goes over the memory limit](https://res.cloudinary.com/learnk8s/image/upload/v1679895579/threads/memory-allocations-4_imhxyr.png)

**The kubelet is also in charge of monitoring the total memory utilization of the node.**

If memory is running low, the kubelet evicts low-priority pods.

_But how does it decide what's low priority?_

![The kubelet evict pods if the node is running low on resources](https://res.cloudinary.com/learnk8s/image/upload/v1679895579/threads/memory-allocations-5_o7dcqg.png)

When Kubernetes creates a Pod, it assigns one of these QoS classes to the Pod:

1. Guaranteed
2. Burstable
3. BestEffort

**Pods that are "Guaranteed" have CPU and memory requests and limits and are least likely to face eviction.**

Also, memory request = memory limit AND CPU request = CPU limit.

This class is best suited for stateful applications like databases.

![Guaranteed Quality of Service for Pods](https://res.cloudinary.com/learnk8s/image/upload/v1679895580/threads/memory-allocations-7_jqgkxh.png)

**Pods with a "Burstable" class have memory and CPU requests but not limits.**

This allows the Pods to flexibly increase their resources if available (but they could also use any amount of resources).

![burstable Quality of Service for Pods](https://res.cloudinary.com/learnk8s/image/upload/v1679895580/threads/memory-allocations-8_qm5gbk.png)

**A Pod is "BestEffort" only if none of its containers has a memory or CPU limit or request.**

Those Pods are the first to be evicted in the event of Node resource pressure.

![Burstable Quality of Service for Pods](https://res.cloudinary.com/learnk8s/image/upload/v1679895580/threads/memory-allocations-9_prcfkt.png)

Most of your pods are likely to be "Burstable" (i.e. requests, but fewer limits), and a very selected few should be "Guaranteed".

Burstable pods are good because they use resources dynamically and are cheaper.

![With Burstable pods you can dymically allocate resources as the container needs them](https://res.cloudinary.com/learnk8s/image/upload/v1679895580/threads/memory-allocations-10_uakjog.png)

With Guaranteed pods, you allocate all resources up to the limit upfront, which could result in more expensive (but safer) deployments.

![With Guaranteed pods, resources are allocated upfront and can't be freed even if the process isn't using them](https://res.cloudinary.com/learnk8s/image/upload/v1679895580/threads/memory-allocations-11_jxm1sk.png)

BestEffort pods are generally something you should avoid.

The Kubernetes scheduler doesn't know how much memory or CPU the process needs, so it could end up scheduling an impractical amount of pods in the existing nodes.

![You can fit as many BestEffort pods in a node as you wish](https://res.cloudinary.com/learnk8s/image/upload/v1679895580/threads/memory-allocations-12_xgfmtn.png)

_But if you stick only to Burstable pods, how does the kubelet know which pod to evict first?_

**Pods can have PriorityClass that indicates the importance of a Pod relative to other Pods.**

![Pod PriorityClass](https://res.cloudinary.com/learnk8s/image/upload/v1679895580/threads/memory-allocations-13_ryrcbh.png)

The scheduler also leverages the Pod PriorityClass to evict pods when the cluster is full.

For example, if you have low-priority batch jobs (e.g. reports), you could assign a low priority, and they will be evicted first.

![Pods with lower priority are evicted to make space for higher priority pods](https://res.cloudinary.com/learnk8s/image/upload/v1679895580/threads/memory-allocations-14_qkyzz9.png)

_How should you choose the memory and request of a pod?_

A simple way is to calculate the smallest memory unit as:

```
REQ = NODE_MEM / MAX_PODS_PER_NODE
```

For a 4GB node and a limit of 10 Pods, that's a 400Mb request.

Assign the smallest unit or a multiplier to your containers.

![Assigning requests for your pods](https://res.cloudinary.com/learnk8s/image/upload/v1679895580/threads/memory-allocations-15_favjl5.png)

**A better approach is to monitor the app and derive the memory utilization.**

You can do this with your existing monitoring infrastructure or use the Vertical Pod Autoscaler to monitor and report the average request value.

![Measuring memory consumption with the Vertical Pod Autoscaler](https://res.cloudinary.com/learnk8s/image/upload/v1678078846/threads/demo_agsihk.gif)

_How should I set the limits?_

Limits trigger eviction, so you should definitely set a value lower than the available memory.

[Here's a handy calculator for that.](https://learnk8s.io/kubernetes-instance-calculator)

Also, if you want to dig in more a few relevant links:

- [Pod priorities](https://kubernetes.io/docs/concepts/scheduling-eviction/pod-priority-preemption/)
- [Node pressure](https://kubernetes.io/docs/concepts/scheduling-eviction/node-pressure-eviction/)
- [Pod Quality of Service](https://kubernetes.io/docs/tasks/configure-pod-container/quality-service-pod)

And finally, if you've enjoyed this thread, you might also like:

- The Kubernetes workshops that we run at Learnk8s https://learnk8s.io/training
- This collection of past threads https://twitter.com/danielepolencic/status/1298543151901155330
- The Kubernetes newsletter I publish every week https://learnk8s.io/learn-kubernetes-weekly