CPU Requests and Limits in Kubernetes

Reddeppa S
2 min readMar 10, 2023

In Kubernetes what should we use for CPU requests and Limits?

There is more than one combination of requests and limits. In some projects, only CPU requests are defined, in other projects, both requests and limits are being used and some teams don't define CPU requests and limits at all in their pod definition.

But, Do we really need to define the CPU limits and requests? Let us dig in...

What are the Requests and Limits for CPU?

Requests: are usually used to determine the average consumption

Limits: set the max number of resources allowed

Note: 1 CPU unit is equivalent to 1 physical CPU core, or 1 virtual core, depending on whether the node is a physical host or a virtual machine.

How does the scheduler decide pod allocation to node?

Kubernetes scheduler decides where to allocate the pod in the Kubernetes cluster based on the CPU requests. Since the pod has not started yet, Kubernetes does not know the CPU consumption of the pod. So CPU requests will help in scheduling the pod to the right Node.

If more than one container runs in the pod, the CPU will be divided into both containers.

Example:

container A requests: 0.1 vCPU

container B requests: 0.2 vCPU

If both containers use 100% CPU it overloads the system and overall Kubernetes will throttle CPU usage for both containers and the application will observe the performance degradation.

So setting Limits will save us from overstepping containers one another.

CPU Limits:

When you set a CPU limit, you define a period and quota

Example:

— period: 100000 microseconds (0.1s)

— quota: 10000 microseconds (0.01s)

I can only use the CPU for 0.01 seconds every 0.1 seconds That’s also abbreviated as “100m”

So if your pod has a limit defined and consumes the max CPU, the process will be throttled. It has to wait for the next period.

How to identify CPU limits?

The best approach is to monitor the application using Grafana. Alternatively, you can VPA(vertical pod autoscaler), this will determine the CPU usage and adjusts the CPU requests accordingly. Also, some applications have hard limits for CPU usage.

So Should you use CPU requests and Limits?

Yes, You should use CPU requests and Limits. If don't know the usage of your application use VPA or a monitoring tool to identify these values.

--

--