Managing container CPU limits machanism in Kubernetes
Kubernetes control limits of container CPU by `cpu.cfs_period_us` and `cpu.cfs_quota_us`.
`cpu.cfs_period_us` represents the time period over which CPU usage is measured and controlled.
`cpu.cfs_quota_us` represents the maximum amount of CPU time that processes in the container can use during each period.
Let's assume a senario to help understanding. In this senario assume that `cpu.cfs_period_us` is set to 100ms, `spec.containers[].resources.limits.cpu` in the container setting is set to 1000m.
It means the container can use 1 CPU core during each 100ms period. When we convert this CPU limit to its equvalent quota time(cpu.cfs_quota_us), it results in 100ms. When `spec.containers[].resources.limits.cpu` is set to 500m, quota time can be translated to 50ms.
But, It may not behave as expected.
Suppose the next situation.
- Kubernetes node has 4 CPU core
- Application in container designed multi-threaded model and use 4 CPU core
In this senario, the application can use CPU for only 25ms and experiences CPU throttling for the remaining 75ms of the 100ms period.
Managing Container CPU requests mechanism in Kubernetes
In this section, Let’s find out how manages Kubernetes container CPU requests.
When the spec.containers[].resources.requests.cpu property is set, Kubernetes traslates it to a correspending unit called a "CPU share". This allows Kubernetes to fairly schedule CPU resources, even when containers request more than the actual CPU capacity.
Likewise, consider the following scenario for easier understanding.
- Kubernetes node has 4 CPU core
- Node has four containers (pods), each requesting 2 CPU cores (2000m), and all of them want to use the full amount they requested
In this scenario, Kubernetes actually orchestrates scheduling CPU resource fairly across containers.
As a result, each container can use up to 1 CPU core in period time, based on the CPU share ratio.
Also, if a container that requests a lot of CPU doesn’t use much CPU compared to its request, Kubernetes effeciently schedules the unused CPU for other containers.
Conclusion
We can know that following knowledge.
- Kubernetes schedules CPU even when containers request CPU more than the actual available CPU capacity
- Setting CPU limits can cause CPU throttling for your application
Therefore, if application performance is important, it may be better not to set the CPU limit on the container.
But if your application prioritizes stabillity, you should set the CPU limit appropriately.