当前位置: 移动技术网 > 科技>操作系统>Linux > 集群管理中资源限制和资源分配来实现资源的使用量

集群管理中资源限制和资源分配来实现资源的使用量

2020年04月08日  | 移动技术网科技  | 我要评论

一 规划

1.1 需求

本实验通过资源配额和资源配置范围的配合来控制一个命名空间的资源使用。
集群管理员根据集群用户的数量来调整集群配置,以达到这个目的:能控制特定命名空间中的资源使用量,最终实现集群的公平使用和成本控制。
需要实现的功能如下:
  • 限制运行状态的pod的计算资源用量。
  • 限制持久存储卷的数量以控制对存储的访问。
  • 限制负载均衡器的数量以控制成本。
  • 防止滥用网络端口这类稀缺资源。
  • 提供默认的计算资源requests以便于系统做出更优化的调度。

二 步骤

2.1 创建命名空间

[root@k8smaster01 study]# vi namespace.yaml
  1 apiversion: v1
  2 kind: namespace
  3 metadata:
  4   name: quota-example
  5 
[root@k8smaster01 study]# kubectl create -f namespace.yaml
[root@k8smaster01 study]# kubectl get namespaces
name status age
……
quota-example active 14s

2.2 设置对象数目的资源配额

通过设置限定对象的数量的资源配额,可以控制以下资源的数量:
  • 持久存储卷;
  • 负载均衡器;
  • nodeport。
[root@k8smaster01 study]# vi object-counts.yaml
  1 apiversion: v1
  2 kind: resourcequota
  3 metadata:
  4   name: object-counts
  5 spec:
  6   hard:
  7     persistentvolumeclaims: "2"
  8     services.loadbalancers: "2"
  9     services.nodeports: "0"
 10 
[root@k8smaster01 study]# kubectl create -f object-counts.yaml --namespace=quto-example
[root@k8smaster01 study]# kubectl describe quota object-counts --namespace=quota-example
name: object-counts
namespace: quota-example
resource used hard
-------- ---- ----
persistentvolumeclaims 0 2
services.loadbalancers 0 2
services.nodeports 0 0
提示:配额系统会检测到资源项配额的创建,并且会统计和限制该命名空间中的资源消耗,创建完毕后,配额系统会自动阻止那些使资源用量超过资源配额限定值的请求。

2.3 设置计算资源的资源配额

通过设置限定计算资源的资源配额,限制该命名空间中的计算资源的使用总量。
[root@k8smaster01 study]# vi compute-resources.yaml
  1 apiversion: v1
  2 kind: resourcequota
  3 metadata:
  4   name: compute-resources
  5 spec:
  6   hard:
  7     pods: "4"
  8     requests.cpu: "1"
  9     requests.memory: 1gi
 10     limits.cpu: "2"
 11     limits.memory: 2gi
 12 
[root@k8smaster01 study]# kubectl create -f compute-resources.yaml --namespace=quota-example
[root@k8smaster01 study]# kubectl describe quota compute-resources --namespace=quota-example
name: compute-resources
namespace: quota-example
resource used hard
-------- ---- ----
limits.cpu 0 2
limits.memory 0 2gi
pods 0 4
requests.cpu 0 1
requests.memory 0 1gi
解读:配额系统会自动防止在该命名空间中同时拥有超过4个非“终止态”的pod。此外,由于该项资源配额限制了cpu和内存的limits和requests的总量,因此会强制要求该命名空间下的所有容器都显式定义cpu和内存的limits和requests(可使用默认值 requests默认等于limits)。

2.4 配置默认requests和limits

在命名空间已经配置了限定计算资源的资源配额的情况下,如果尝试在该命名空间下创建一个不指定requests和limits的pod,由于pod中没有指定cpu和内存的requests和limits,那么pod的创建会失败。为了避免这种失败,可以使用limitrange来为这个命名空间下的所有pod都提供一个资源配置的默认值。
[root@k8smaster01 study]# vi limits.yaml
  1 apiversion: v1
  2 kind: limitrange
  3 metadata:
  4   name: limits
  5 spec:
  6   limits:
  7   - default:
  8       cpu: 200m
  9       memory: 512mi
 10     defaultrequest:
 11       cpu: 100m
 12       memory: 256mi
 13     type: container
 14 
[root@k8smaster01 study]# kubectl create -f limits.yaml --namespace=quota-example
[root@k8smaster01 study]# kubectl describe limitranges limits --namespace=quota-example
name: limits
namespace: quota-example
type resource min max default request default limit max limit/request ratio
---- -------- --- --- --------------- ------------- -----------------------
container cpu - - 100m 200m -
container memory - - 256mi 512mi -
解读:在limitrange创建成功后,用户在该命名空间下创建未指定资源限制的pod的请求时,系统会自动为该pod设置默认的资源限制。

2.5 触发limits

[root@k8smaster01 study]# kubectl run nginx --image=nginx --replicas=1 --namespace=quota-example
[root@k8smaster01 study]# kubectl run nginx \
--image=nginx \
--replicas=1 \
--requests=cpu=100m,memory=256mi \
--limits=cpu=200m,memory=512mi \
--namespace=quota-example
[root@k8smaster01 study]# kubectl get pods --namespace=quota-example
name ready status restarts age
nginx-78df7bdbcf-mxcql 1/1 running 0 21s
[root@k8smaster01 study]# kubectl describe quota --namespace=quota-example
clipboard
解读:每个pod在创建时都会消耗指定的资源量,而这些使用量都会被kubernetes准确跟踪、监控和管理。

三 指定作用域

3.1 作用域场景

假设并不想为某个命名空间配置默认的计算资源配额,而是希望限定在命名空间内运行的qos为besteffort的pod总数,例如让集群中的部分资源运行qos为非besteffort的服务,而让闲置的资源运行qos为besteffort的服务,即可避免集群的所有资源仅被大量的besteffortpod耗尽。此需求可通过创建两个资源配额来实现。

3.2 创建命名空间

[root@k8smaster01 study]# kubectl create namespace quota-scopes

3.3 创建两个resourcequota

[root@k8smaster01 study]# vi best-effort.yaml
  1 apiversion: v1
  2 kind: resourcequota
  3 metadata:
  4   name: best-effort
  5 spec:
  6   hard:
  7     pods: "10"
  8   scopes:
  9   - besteffort
 10 
[root@k8smaster01 study]# kubectl create -f best-effort.yaml --namespace=quota-scopes
[root@k8smaster01 study]# vi not-best-effort.yaml
  1 apiversion: v1
  2 kind: resourcequota
  3 metadata:
  4   name: not-best-effort
  5 spec:
  6   hard:
  7     pods: "4"
  8     requests.cpu: "1"
  9     requests.memory: 1gi
 10     limits.cpu: "2"
 11     limits.memory: 2gi
 12   scopes:
 13   - notbesteffort
 14 
[root@k8smaster01 study]# kubectl create -f not-best-effort.yaml --namespace=quota-scopes
[root@k8smaster01 study]# kubectl describe quota --namespace=quota-scopes
name: best-effort
namespace: quota-scopes
scopes: besteffort
* matches all pods that do not have resource requirements set. these pods have a best effort quality of service.
resource used hard
-------- ---- ----
pods 0 10


name: not-best-effort
namespace: quota-scopes
scopes: notbesteffort
* matches all pods that have at least one resource requirement set. these pods have a burstable or guaranteed quality of service.
resource used hard
-------- ---- ----
limits.cpu 0 2
limits.memory 0 2gi
pods 0 4
requests.cpu 0 1
requests.memory 0 1gi
提示:创建完成后,对于没有配置requests的pod将会被名为best-effort的resourcequota限制;而配置了requests的pod会被名为not-best-effort的resourcequota限制。

3.4 创建pod

[root@k8smaster01 study]# kubectl run best-effort-nginx --image=nginx --replicas=8 --namespace=quota-scopes
[root@k8smaster01 study]# kubectl run not-best-effort-nginx \
--image=nginx \
--replicas=2 \
--requests=cpu=100m,memory=256mi \
--limits=cpu=200m,memory=512mi \
--namespace=quota-scopes
[root@k8smaster01 study]# kubectl get pods --namespace=quota-scopes
clipboard
解读:名为best-effort-nginx的deployment因为没有配置requests和limits,所以它的qos级别为besteffort,因此它的创建过程由best-effort资源配额项来限制,而not-best-effort资源配额项不会对它进行限制。best-effort资源配额项没有限制requests和limits,因此best-effort-nginx deployment可以成功创建8个pod。
名为not-best-effort-nginx的deployment因为配置了requests和limits,且二者不相等,所以它的qos级别为burstable,因此它的创建过程由not-best-effort资源配额项限制,而best-effort资源配额项不会对它进行限制。not-best-effort资源配额项限制了pod的requests和limits的总上限,not-best-effort-nginx deployment并没有超过这个上限,所以可以成功创建两个pod。

3.5 触发资源配额

[root@k8smaster01 study]# kubectl describe quota --namespace=quota-scopes
name: best-effort
namespace: quota-scopes
scopes: besteffort
* matches all pods that do not have resource requirements set. these pods have a best effort quality of service.
resource used hard
-------- ---- ----
pods 8 10


name: not-best-effort
namespace: quota-scopes
scopes: notbesteffort
* matches all pods that have at least one resource requirement set. these pods have a burstable or guaranteed quality of service.
resource used hard
-------- ---- ----
limits.cpu 400m 2
limits.memory 1gi 2gi
pods 2 4
requests.cpu 200m 1
requests.memory 512mi 1gi
解读:如上所示best-effort资源配额项已经统计了在best-effort-nginx deployment中创建的8个pod的资源使用信息,not-best-effort资源配额项也已经统计了在not-best-effort-nginx deployment中创建的两个pod的资源使用信息。
结论:资源配额的作用域(scopes)提供了一种将资源集合分割的机制,可以使集群管理员更加方便地监控和限制不同类型的对象对各类资源的使用情况,同时为资源分配和限制提供更大的灵活度和便利性。

四 资源管理总结

kubernetes中资源管理的基础是容器和pod的资源配置(requests和limits)。容器的资源配置指定了容器请求的资源和容器能使用的资源上限, pod的资源配置则是pod中所有容器的资源配置总和上限。
通过资源配额机制,可以对命名空间中所有pod使用资源的总量进行限制,也可以对这个命名空间中指定类型的对象的数量进行限制。
使用作用域可以让资源配额只对符合特定范围的对象加以限制,因此作用域机制可以使资源配额的策略更加丰富、灵活。
如果需要对用户的pod或容器的资源配置做更多的限制,则可以使用资源配置范围(limitrange)来达到这个目的。limitrange可以有效地限制pod和容器的资源配置的最大、最小范围,也可以限制pod和容器的limits与requests的最大比例上限,此外limitrange还可以为pod中的容器提供默认的资源配置。
kubernetes基于pod的资源配置实现了资源服务质量(qos)。不同qos级别的pod在系统中拥有不同的优先级:高优先级的pod有更高的可靠性,可以用于运行可靠性要求较高的服务;低优先级的pod可以实现集群资源的超售,可以有效地提高集群资源利用率。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网