参考:https://kubernetes.io/docs/concepts/policy/resource-quotas/
当集群有多个用户或者团队时,一个重要的问题就是资源的公平分配。
资源配置就是管理员用来解决类似问题的工具。
通过定义ResourceQuota对象,约束一个名称空间可以消耗的系统资源的总数。它可以限制一个名称空间内可以创建的某种类型对象的数量,也可以限制名称空间所能消耗的低层资源的总数。
资源配额按如下方式工作:
- 不同teams工作在不同namespace下,目前这种个约束需要用户自愿遵守,但支持通过ACLs强制遵守此约束。
- 由管理员为每个namespace创建一个或者多个ResourceQuota。
- 用户在namespace下创建各种资源,配额系统追踪namespace下各种资源的使用量确保不会超过ResourceQuota规定的硬性数量。
- 如果在创建、更新资源时违反了资源配额规定,apiserver返回403应答并在正文中解决原因。
- 如果名称空间中有对计算资源如cpu、memory等的配额限制,则用户在创建pod时必需指明对这些资源的最小需求与最大限制,否则系统创建pod。提示:用LimitRanger admission controller(入口管理器)强制为没有指定对资源需求的pod设定默认值。
你可能会创建如下资源配额规则:
- 集群有32 GiB RAM、16 cores,允许team A使用20 GiB RAM、10 cores,允许B使用10 GiB RAM、4 cores,剩下2GiB RAM、2 cores保留。
- 限制“testing” namespace使用1core、1GiB RAM。让“production” namespace没有配额限制。
发系统中的资源总额少于系统中所有资源配额的总时,会发生争用情况,目前的处理措施是先到先得。
配额对已经创建的资源没有影响。
允许资源配额管理
默认安装,apiserver的标志--enable-admission-plugins的值包含ResourceQuota,资源配额管理默认打开。
如果某个namespace包含资源配额条目,那么无法apiserver标志是否打开,针对这个namespace的资源配额管理都会被打开。
计算资源配额管理
可以为namespace设置计算资源配额,支持如下类型:
Resource Name | Description |
---|---|
cpu |
Across all pods in a non-terminal state, the sum of CPU requests cannot exceed this value. |
limits.cpu |
Across all pods in a non-terminal state, the sum of CPU limits cannot exceed this value. |
limits.memory |
Across all pods in a non-terminal state, the sum of memory limits cannot exceed this value. |
memory |
Across all pods in a non-terminal state, the sum of memory requests cannot exceed this value. |
requests.cpu |
Across all pods in a non-terminal state, the sum of CPU requests cannot exceed this value. |
requests.memory |
Across all pods in a non-terminal state, the sum of memory requests cannot exceed this value. |
扩展资源资源配额
允许对扩展资源使用资源配额管理。方式与系统内置资源类型有所不同。对于扩展资源类型的限制,只需要指定limit值,无需指定request值。例如,以GPU资源为例,资源的名称是nvidia.com/gpu,限制namespace对此资源的配额为4,那么quota的定义如下:
requests.nvidia.com/gpu: 4
存储资源配额
可以为namespace指定存储资源配额,进一步的话可以基于存储资源的种类进行配额管理。
esource Name | Description |
---|---|
requests.storage |
Across all persistent volume claims, the sum of storage requests cannot exceed this value. |