Kubernetes中requests的具体用途及与cgroup cpu.shares的关系疑问
Great question—this is one of those spots where Kubernetes abstractions sit on top of raw Linux cgroup mechanics, and it’s super easy to get mixed up. Let’s break this down step by step.
Kubernetes
requests 的核心用途 First, let’s clear up exactly what requests does in the Kubernetes ecosystem:
- 调度决策的硬依据: The kube-scheduler uses
requestsvalues to check if a node has enough "available" capacity to run a new Pod. For example, if a node has 4 vCPUs total, and existing Pods on that node have a combined CPUrequestsof 3 vCPUs, the scheduler will only place new Pods with CPUrequests≤ 1 vCPU here. This prevents overcommitting nodes to the point where critical Pods can’t even start or get the resources they need to run. - 资源预留与QoS基础:
requestsworks withlimitsto define a Pod’s Quality of Service (QoS) class:- Guaranteed: When
requestsequalslimits, the Pod is guaranteed its requested resources (and can’t use more than the limit). - Burstable: When
requestsis less thanlimits, the Pod can use extra resources when available, but will be deprioritized over Guaranteed Pods during resource contention. - BestEffort: No
requestsorlimitsset—these Pods are the first to get evicted when nodes run low on resources.
- Guaranteed: When
- 运行时资源优先级: For CPU specifically,
requeststranslates to a weight that influences how much CPU time the Pod gets when the node is under heavy load (more on this next).
You’re right that cpu.shares in cgroups is a relative weight, not an absolute value. Here’s how Kubernetes bridges these two:
- By default, Kubernetes maps 1 vCPU of
requests.cputo 1024cpu.shares(the Linux default weight). So a Pod withrequests.cpu: 200m(0.2 vCPUs) gets ~205cpu.shares, and one with1500mgets ~1536 shares. - This mapping only applies to Burstable and BestEffort Pods. For Guaranteed Pods (where
requests=limits), Kubernetes uses a different cgroup tool:cpu.cfs_quota_usandcpu.cfs_period_us. These settings effectively cap the Pod’s CPU usage to the limit value, while ensuring it always gets its requested resources. - The key takeaway:
cpu.sharesonly matters when CPU resources are fully saturated. If the node has idle CPU, any Pod can use more than itsrequestsvalue—shares don’t restrict that.
为什么调度时将CPU
request 视为绝对值? Your confusion here comes from mixing up two separate stages of resource management: scheduling (before the Pod starts) and runtime CPU allocation (while the Pod runs).
- Scheduling needs an absolute metric to make logical decisions.
cpu.sharesis relative—1024 shares could mean 100% of a single-core node or 25% of a 4-core node. The scheduler can’t use relative weights to judge if a node has enough capacity for a Pod; it needs a fixed, node-agnostic value to calculate resource availability. - Think of
requestsas a "minimum guarantee" the scheduler promises to the Pod. By using absolute values, the scheduler ensures that the sum of all Podrequestson a node doesn’t exceed the node’s total resources. This stops overcommitment that would leave Pods starving for resources even when the node is under load. - Remember: The absolute
requestdoesn’t mean the Pod is locked to that amount of CPU. It just means the scheduler has reserved that capacity for the Pod. When the node has spare CPU, the Pod can burst above itsrequest(up tolimits, if set). Only when CPU is saturated does the relativecpu.shares(derived fromrequests) kick in to split available time between competing Pods.
内容的提问来源于stack exchange,提问作者BLasan
相关产品推荐
相关产品推荐

