kubernetes调度之资源配额

当多个用户或者开发团队共享一个有固定节点的的kubernetes集群时,一个团队或者一个用户使用的资源超过他应当使用的资源是需要关注的问题,资源配额是管理员用来解决这个问题的一个工具.

资源配额,通过ResourceQuota定义,提供了对某一名称空间使用资源的总体约束.它即可以限制这个名称空间下有多少个对象可以被创建,也可以限制对计算机资源使用量的限制(前面说到过,计算机资源包括cpu,内存,磁盘空间等资源)

资源配额通过以下类似方式工作:

不同的团队在不同的名称空间下工作.当前kubernetes并没有强制这样做,完全是自愿的,但是kubernetes团队计划通过acl授权来达到强制这样做.

管理员对每一个名称空间创建一个ResourceQuota(资源配额)

用户在一个名称空间下创建资源(例如pod,service等),配额系统跟踪资源使用量来保证资源的使用不超过ResourceQuota定义的量.

如果对一个资源的创建或者更新违反了资源配额约束,则请求会返回失败,失败的http状态码是403 FORBIDDEN并且有一条消息来解释哪个约束被违反.

如果一个名称空间下的计算机资源配额,比如CPU和内存被启用,则用户必须指定相应的资源申请或者限制的值,否则配额系统可能会阻止pod的创建.

资源配额在某一名称空间下创建策略示例:

在一个有32G内存,16核cpu的集群,让团队A使用20G内存和10核cpu,让团队B使用10G内存和4核cpu,剩余的2G内存和2核cup预留以备进一步分配

限制测试名称空间使用1核1G,让生产名称空间使用剩下的全部资源

当集群的容量小于所有名称空间下配额总和时,将会出现资源竞争,这种情况下kubernetes将会基于先到先分配的原则进行处理

不论是资源竞争或者是资源配额的修改都不会影响已经创建的资源

启用资源配额

很多kubernetes的发行版中资源配额支持默认是开启的,当ResourceQuota作为apiserver的--enable-admission-plugins=的其中一个值时,资源配额被开启.

当某一名称空间包含ResourceQuota对象时资源配额在这个名称空间下生效.

计算机资源配额

你可以限制一个名称空间下可以被申请的计算机资源的总和

kubernetes支持以下资源类型:

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.  
扩展资源的资源配额

除了上面提到的,在kubernetes 1.10里,添加了对扩展资源的配额支持

存储资源配额

你可以限制某一名称空间下的存储空间总量的申请

此外,你也可以你也可以根据关联的storage-class来限制存储空间资源的使用

Resource Name Description
requests.storage   Across all persistent volume claims, the sum of storage requests cannot exceed this value.  
persistentvolumeclaims   The total number of persistent volume claims that can exist in the namespace.  
.storageclass.storage.k8s.io/requests.storage   Across all persistent volume claims associated with the storage-class-name, the sum of storage requests cannot exceed this value.  
.storageclass.storage.k8s.io/persistentvolumeclaims   Across all persistent volume claims associated with the storage-class-name, the total number of persistent volume claims that can exist in the namespace.  

例如,一个operator想要想要使黄金和青铜单独申请存储空间,那么这个operator可以像如下一样申请配额:

gold.storageclass.storage.k8s.io/requests.storage: 500Gi bronze.storageclass.storage.k8s.io/requests.storage: 100Gi

在1.8版本里,对local ephemeral storage配额的的支持被添加到alpha特征里.

Resource Name Description
requests.ephemeral-storage   Across all pods in the namespace, the sum of local ephemeral storage requests cannot exceed this value.  
limits.ephemeral-storage   Across all pods in the namespace, the sum of local ephemeral storage limits cannot exceed this value.  
对象数量配额

1.9版本通过以下语法加入了对所有标准名称空间资源类型的配额支持

count/<resource>.<group>

以下是用户可能想要设置对象数量配额的例子:

count/persistentvolumeclaims

count/services

count/secrets

count/configmaps

count/replicationcontrollers

count/deployments.apps

count/replicasets.apps

count/statefulsets.apps

count/jobs.batch

count/cronjobs.batch

count/deployments.extensions

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zywjzx.html