k8s部署-33-k8s的集群资源管理cpu、memory(下)

网友投稿 846 2022-11-05

本站部分文章、图片属于网络上可搜索到的公开信息,均用于学习和交流用途,不能代表睿象云的观点、立场或意见。我们接受网民的监督,如发现任何违法内容或侵犯了您的权益,请第一时间联系小编邮箱jiasou666@gmail.com 处理。

k8s部署-33-k8s的集群资源管理cpu、memory(下)

服务的“可靠性”

这个最好还是设置下,因为万一某些本该特别稳固的服务,因为没有配置而被down了,就得不偿失了。

limit-range

设想一下,当我们有服务器只有10个G的内存,但是有个人他创建了一个pod,配置的request为8G,但是实际上他运行的pod只占用了1个G的内存,我们有没有什么方式避免这种情况呢?还是说我们只能画个圈圈诅咒他?

亦或者他的requests配置的1G,limit配置的10G,这种情况是不是也不是很合适,而且光从配置上看,你无法确认这个服务到底要占用多少的资源。

所以k8s引入了一个概念,就叫做limitrange,资源范围限制。

[root@node1 ~]# cd namespace/[root@node1 namespace]# mkdir limitrange[root@node1 namespace]# cd limitrange/[root@node1 limitrange]# vim limits-test.yaml apiVersion: v1kind: LimitRangemetadata: name: test-limitsspec: limits: - max: cpu: 2000m memory: 1Gi min: cpu: 100m memory: 100Mi maxLimitRequestRatio: cpu: 3 memory: 2 type: Pod - default: cpu: 300m memory: 200Mi defaultRequest: cpu: 200m memory: 100Mi max: cpu: 2000m memory: 700Mi min: cpu: 100m memory: 100Mi maxLimitRequestRatio: cpu: 5 memory: 4 type: Container[root@node1 limitrange]# kubectl create ns test # 新建一个命名空间namespace/test created[root@node1 limitrange]# kubectl apply -f limits-test.yaml -n testlimitrange/test-limits created[root@node1 limitrange]# kubectl describe limits -n testName: test-limitsNamespace: testType Resource Min Max Default Request Default Limit Max Limit/Request Ratio---- -------- --- --- --------------- ------------- -----------------------Pod cpu 100m 2 - - 3Pod memory 100Mi 1Gi - - 2Container cpu 100m 2 200m 300m 5Container memory 100Mi 700Mi 100Mi 200Mi 4[root@node1 limitrange]#

从上面可以看到我们对于这个命名空间的限制时生效了,那么接下来我们做个测试看看呢。

1、创建一个不设置资源限额的服务。

[root@node1 limitrange]# vim web-test.yaml #deployapiVersion: apps/v1kind: Deploymentmetadata: name: web-demo namespace: testspec: selector: matchLabels: app: web-demo replicas: 1 template: metadata: labels: app: web-demo spec: containers: - name: web-demo image: registry.cn-beijing.aliyuncs.com/yunweijia0909/springboot-web:v1 ports: - containerPort: 8080[root@node1 limitrange]# kubectl apply -f web-test.yaml [root@node1 limitrange]# kubectl get pod web-demo-774f645c57-fn45g -n test -o yaml# 省略部分信息输出containers: - image: registry.cn-beijing.aliyuncs.com/yunweijia0909/springboot-web:v1 imagePullPolicy: IfNotPresent name: web-demo ports: - containerPort: 8080 protocol: TCP resources: limits: cpu: 300m memory: 200Mi requests: cpu: 200m memory: 100Mi[root@node1 limitrange]# # 可以看到就算不配置内存和cpu,也会按照我们默认的来配置# 且这个配置是针对的pod,并不是deployment

2、创建下超过我们限制的内存和cpu比例的看看呢。

[root@node1 limitrange]# vim web-test.yaml #deployapiVersion: apps/v1kind: Deploymentmetadata: name: web-demo namespace: testspec: selector: matchLabels: app: web-demo replicas: 1 template: metadata: labels: app: web-demo spec: containers: - name: web-demo image: registry.cn-beijing.aliyuncs.com/yunweijia0909/springboot-web:v1 ports: - containerPort: 8080 resources: requests: memory: 100Mi cpu: 100m limits: memory: 1000Mi cpu: 2000m[root@node1 limitrange]# [root@node1 limitrange]# kubectl apply -f web-test.yaml deployment.apps/web-demo configured[root@node1 limitrange]# [root@node1 limitrange]# kubectl get deploy -n testNAME READY UP-TO-DATE AVAILABLE AGEweb-demo 1/1 0 1 20m[root@node1 limitrange]# # 可以看到他的状态不对哈,我们看下他的yaml[root@node1 limitrange]# kubectl get deploy -n test -o yaml# 省略部分信息 message: 'pods "web-demo-75d76bbf6f-zv9kp" is forbidden: [cpu max limit to request ratio per Pod is 3, but provided ratio is 20.000000, memory max limit to request ratio per Pod is 2, but provided ratio is 10.000000, maximum memory usage per Container is 700Mi, but limit is 1000Mi, cpu max limit to request ratio per Container is 5, but provided ratio is 20.000000, memory max limit to request ratio per Container is 4, but provided ratio is 10.000000]' reason: FailedCreate# 可以看到有个message,提示我们比例不符合,说明比例也是验证过了。

3、测试一下配置超限。

[root@node1 limitrange]# vim web-test.yaml #deployapiVersion: apps/v1kind: Deploymentmetadata: name: web-demo namespace: testspec: selector: matchLabels: app: web-demo replicas: 1 template: metadata: labels: app: web-demo spec: containers: - name: web-demo image: registry.cn-beijing.aliyuncs.com/yunweijia0909/springboot-web:v1 ports: - containerPort: 8080 resources: requests: memory: 1000Mi cpu: 2000m limits: memory: 1000Mi cpu: 2000m[root@node1 limitrange]# kubectl apply -f web-test.yaml deployment.apps/web-demo configured[root@node1 limitrange]# [root@node1 limitrange]# kubectl get deploy -n testNAME READY UP-TO-DATE AVAILABLE AGEweb-demo 1/1 0 1 27m[root@node1 limitrange]# [root@node1 limitrange]# kubectl get deploy -n test -o yamlmessage: 'pods "web-demo-7f68c7bd89-2hf9z" is forbidden: maximum memory usage per Container is 700Mi, but limit is 1000Mi' reason: FailedCreate# 提示我们允许的最大是700M,但是我们创建了1000M,就不合适[root@node1 limitrange]#

总结下对于limitrange验证结果。

ResourceQuota

还有一种情况是这个集群被很多team所使用,但是有一个team不讲武德,运行了很多服务,这个时候会不会把其他team的资源占用完了呢,这个时候k8s就引入了一个ResourceQuota(资源配额)的概念来避免这种情况。

[root@node1 ~]# cd namespace/[root@node1 namespace]# mkdir resourcequota[root@node1 namespace]# cd resourcequota/[root@node1 resourcequota]# vim compute-resource.yaml apiVersion: v1kind: ResourceQuotametadata: name: resource-quota namespace: testspec: hard: pods: 4 requests.cpu: 2000m requests.memory: 4Gi limits.cpu: 4000m limits.memory: 8Gi[root@node1 resourcequota]# [root@node1 resourcequota]# vim object-count.yaml apiVersion: v1kind: ResourceQuotametadata: name: object-counts namespace: testspec: hard: configmaps: 10 persistentvolumeclaims: 4 replicationcontrollers: 20 secrets: 10 services: 10[root@node1 resourcequota]# [root@node1 resourcequota]# kubectl apply -f compute-resource.yaml resourcequota/resource-quota created[root@node1 resourcequota]# kubectl apply -f object-count.yaml resourcequota/object-counts created[root@node1 resourcequota]# [root@node1 resourcequota]# kubectl get quota -n testNAME AGE REQUEST LIMITobject-counts 27s configmaps: 1/10, persistentvolumeclaims: 0/4, replicationcontrollers: 0/20, secrets: 1/10, services: 0/10 resource-quota 34s pods: 1/4, requests.cpu: 200m/2, requests.memory: 100Mi/4Gi limits.cpu: 300m/4, limits.memory: 200Mi/8Gi[root@node1 resourcequota]#  # 从上面可以看到配置生效[root@node1 resourcequota]# kubectl describe quota object-counts -n testName: object-countsNamespace: testResource Used Hard-------- ---- ----configmaps 1 10persistentvolumeclaims 0 4replicationcontrollers 0 20secrets 1 10services 0 10[root@node1 resourcequota]# kubectl describe quota resource-quota -n testName: resource-quotaNamespace: testResource Used Hard-------- ---- ----limits.cpu 300m 4limits.memory 200Mi 8Gipods 1 4requests.cpu 200m 2requests.memory 100Mi 4Gi[root@node1 resourcequota]#

验证下?

不行,服务器性能跟不上,做不了验证,但是结果肯定是符合预期的,这个配置绝对没问题。

至此,本文结束,有关于k8s的资源配额和相关限制就介绍完了,那么是不是运行就万无一失了呢?不会的,总有意外发生,那么k8s中就又引入了一个概念,Pod驱逐,这个我们下一节专门介绍这个。

往期推荐k8s部署-21-ingress-nginx实现原理和部署,并简单使用必备技能之python正则表达式(一)12-docker系列-docker之compose如何上手使用

上一篇:软件测试培训之如何设计模块测试需求
下一篇:软件测试培训之如何划分用例等级
相关文章

 发表评论

暂时没有评论,来抢沙发吧~