Kubernetes: cpu request and total resources doubts

5/19/2021

For better understand my doubts, I will put an example

Example:

We have one worker node with 3 allocatable cpus and kubernetes has scheduled three pods on it:

  • pod_1 with 500m cpu request
  • pod_2 with 700m cpu request
  • pod_3 with 300m cpu request

In this worker node I can't schedule other pods.

But if I check the real usage:

  • pod_1 cpu usage is 300m
  • pod_2: cpu usage is 600m

My question is:

Can pod_3 have a real usage of 500m or the request of other pods will limit the cpu usage?

Thanks

Pietro

-- P3pp3r
kubernetes

2 Answers

5/19/2021

It doesn't matter what the real usage is - the "request" means how much resources are guaranteed to be available for the pod. Your workload might be using only a fraction of the requested resources - but what will really count is the "request" itself.

Example - Let's say you have a node with 1CPU core.

Pod A - 100m Request

Pod B - 200m Request

Pod C - 700m Request

Now, no pod can be allocated in the node - because the whole 1 CPU resource is already requested by 3 pods. It doesn't really matter which fraction of the allocated resources each pod is using at any given time.


Another point worth noting is the "Limit". A requested resource usage could be surpassed by a workload - but it cannot surpass the "Limit". This is a very important mechanism to be understood.

-- Charlie
Source: StackOverflow

5/19/2021

Kubernetes will schedule the pods based on the request that you configure for the container(s) of pod (via the specs for the respective Deployment or other kinds).

Here's an example:

For simplicity, let's assume only one container for the pod.

  containers:
  - name: "foobar"
    resources:
      requests:
        cpu: "300m"
        memory: "256Mi"
      limits:
        cpu: "500m"
        memory: "512Mi"

If you ask for 300 millicpus as your request, Kubernetes will place the pod on a node that has at least 300 millicpus allocatable to that pod. If a node has less allocatable CPU available, the pod will not be placed on that node. Similarly, you can also set the value for memory request as well.

The limit works to limit the resource use by the container. In the example above, Kubernetes will evict the pod if the container ends up using more than 512MiB of memory; once evicted, the pod will be placed on a node that has at least 300 millicpus available (and if no such node exists, the pod will remain in Pending state with FailedScheduling as the reason, until a node with sufficient capacity is available).

Do note, that the resource request works only at the time of pod scheduling, and not at runtime (meaning, the actual consumption of the resources will not trigger a re-scheduling of the pod even if the container used more resources than what it requested as long as it remains below the limit, if specified).

https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#how-pods-with-resource-requests-are-scheduled

So, in summary,

  • The total of all your requests is used as the what can be allocated regardless of the actual runtime utilization of your pod (as long as the limit is not crossed)
  • You can request for 300 millicpus, but only use 100 millicpus, or 400 millicpus; Kubernetes will still show the "allocated" value as 300
  • If your container crosses the limit, it will get evicted by Kubernetes
-- Bloodysock
Source: StackOverflow