在《研发工程师玩转Kubernetes——使用Deployment进行多副本维护》一文中,我们通过Deployment实现了多副本维护——即维持在一个确定数量的副本个数。而在现实场景中,我们往往需要根据服务的压力,采用水平(横向)扩容的方式——即增加多个副本,来分担压力。当服务压力降低时,又需要减少副本数来节约成本。本节我们将介绍如何根据服务压力进行自动的扩缩容。
部署
我们借用《研发工程师玩转Kubernetes——CPU配额》中的代码。它是一个Http服务,接受请求后执行一段消耗CPU的操作。
推送镜像
docker build -t simple_http:v5 .
docker tag simple_http:v5 localhost:32000/simple_http:v5
docker push localhost:32000/simple_http:v5
Deployment清单
Deployment可以通过spec.replicas指定需要维护的副本个数。但是本节我们需要动态扩缩容,副本个数根据服务压力动态改变,而不会固定在一个固定的副本个数上,所以spec.replicas字段不需要指定。
HorizontalPodAutoscaler清单
HorizontalPodAutoscaler是用来设置动态扩缩容条件的组件。我们可以通过它来设置诸如:最少副本数量,最多副本数量和触发扩容的条件(如CPU达到80%时开始扩容)。
# simple_http_deployment.yaml部分内容
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: simple-http-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: simple-http-deployment
minReplicas: 2
maxReplicas: 10
targetCPUUtilizationPercentage: 80
上面的例子意思是:针对名字(name)叫simple-http-deployment、类型(kind)是Deployment的Pod集合,最低维持(minReplicas)2个副本。如果Pod的CPU利用率超过80%,则开始扩容,最大(maxReplicas)不超过10个副本。
完整的yaml如下:
# simple_http_deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: simple-http-deployment
spec:
replicas: 2
selector:
matchLabels:
app: simple_http
template:
metadata:
labels:
app: simple_http
spec:
containers:
- name: simple-http-container
image: localhost:32000/simple_http:v4
ports:
- containerPort: 8888
command: ["python","main.py","-port","$(SERVER_PORT)"]
env:
- name: SERVER_PORT
value: "8888"
resources:
limits:
cpu: 100m
---
apiVersion: v1
kind: Service
metadata:
name: simple-http-service
spec:
type: NodePort
selector:
app: simple_http
ports:
- port: 80
targetPort: 8888
nodePort: 30000
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: simple-http-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: simple-http-deployment
minReplicas: 2
maxReplicas: 10
targetCPUUtilizationPercentage: 80
执行部署
上述清单我们都放在simple_http_deployment.yaml文件中,只要执行create指令就行
kubectl create -f simple_http_deployment.yaml
deployment.apps/simple-http-deployment created
service/simple-http-service created
horizontalpodautoscaler.autoscaling/simple-http-hpa created
测试
查看HorizontalPodAutoscaler副本数量
kubectl get horizontalpodautoscalers.autoscaling simple-http-hpa
此时只有2个副本。
我们使用《研发工程师玩转Kubernetes——CPU配额》中的wrk进行压力测试。
wrk -t10 -c10 -d300 http://192.168.137.248:30000
在压测过程中,我们使用下面命令查看CPU负载。
kubectl get horizontalpodautoscalers.autoscaling simple-http-hpa
NAME | REFERENCE | TARGETS | MINPODS | MAXPODS | REPLICAS | AGE |
---|---|---|---|---|---|---|
simple-http-hpa | Deployment/simple-http-deployment | 1%/80% | 2 | 10 | 2 | 4m48s |
simple-http-hpa | Deployment/simple-http-deployment | 10%/80%(CPU负载开始增加) | 2 | 10 | 2 | 5m1s(开始压测) |
simple-http-hpa | Deployment/simple-http-deployment | 95%/80%(CPU负载超过阈值80%) | 2 | 10 | 2 | 5m16s |
simple-http-hpa | Deployment/simple-http-deployment | 97%/80% | 2 | 10 | 3(开始动态扩容) | 5m33s |
simple-http-hpa | Deployment/simple-http-deployment | 89%/80%(CPU负载开始下降) | 2 | 10 | 4 | 6m53s |
simple-http-hpa | Deployment/simple-http-deployment | 89%/80% | 2 | 10 | 6 | 9m17s |
simple-http-hpa | Deployment/simple-http-deployment | 85%/80% | 2 | 10 | 7 | 9m43s |
simple-http-hpa | Deployment/simple-http-deployment | 79%/80%(CPU负载开始低于阈值80%) | 2 | 10 | 7 | 10m(压测结束) |
simple-http-hpa | Deployment/simple-http-deployment | 1%/80% | 2 | 10 | 7 | 10m |
simple-http-hpa | Deployment/simple-http-deployment | 1%/80% | 2 | 10 | 2(自动缩容) | 15m |
我们使用wrk压测了5分钟。这5分钟容器的CPU负载一直很高,Pod也一直在自动扩容。
5分钟后wrk停止,CPU利用率降低到1%。大概又过了5分钟,Pod自动缩容到最低的副本数2。
Kubernetes HorizontalPodAutosc
参考资料
- https://kubernetes.io/zh-cn/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/