【发布时间】:2019-09-26 00:08:50
【问题描述】:
如何控制在特定 k8s 命名空间中运行的所有内容(在给定时刻)使用的最大资源。 (最大内存,最大 CPU)?
【问题讨论】:
标签: kubernetes kubernetes-namespace
如何控制在特定 k8s 命名空间中运行的所有内容(在给定时刻)使用的最大资源。 (最大内存,最大 CPU)?
【问题讨论】:
标签: kubernetes kubernetes-namespace
这可以通过给定命名空间上的 ResourceQuota 来完成。
来自docs:
由 ResourceQuota 对象定义的资源配额提供限制每个命名空间的聚合资源消耗的约束。它可以按类型限制命名空间中可以创建的对象数量,以及该项目中资源可能消耗的计算资源总量。
资源配额是这样定义的(来自k8s admin docs):
apiVersion: v1
kind: ResourceQuota
metadata:
name: mem-cpu-demo
spec:
hard:
requests.cpu: "1"
requests.memory: 1Gi
limits.cpu: "2"
limits.memory: 2Gi
注意:此信息来自 k8s v1.16 文档
【讨论】:
为每个命名空间定义资源配额对象。 您可以设置每个命名空间的最大计算资源,还可以定义每个命名空间要部署的对象数量。按照以下示例进行操作
apiVersion: v1
kind: ResourceQuota
metadata:
name: mem-cpu-demo
namespace: quota-mem-cpu-example
spec:
hard:
requests.cpu: "1"
requests.memory: 1Gi
limits.cpu: "2"
limits.memory: 2Gi
----
The ResourceQuota places these requirements on the quota-mem-cpu-example namespace:
Every Container must have a memory request, memory limit, cpu request, and cpu limit.
The memory request total for all Containers must not exceed 1 GiB.
The memory limit total for all Containers must not exceed 2 GiB.
The CPU request total for all Containers must not exceed 1 cpu.
The CPU limit total for all Containers must not exceed 2 cpu.
similarly define object quota
apiVersion: v1
kind: ResourceQuota
metadata:
name: object-quota-demo
namespace: quota-object-example
spec:
hard:
persistentvolumeclaims: "1"
services.loadbalancers: "2"
services.nodeports: "0"
Which implies that there can be at most one PersistentVolumeClaim, at most two Services of type LoadBalancer, and no Services of type NodePort.
reference: https://kubernetes.io/docs/tasks/administer-cluster/quota-api-object/
【讨论】: