Kubernetes系列之Kubernetes的弹性伸缩(HPA)
2021-04-10 12:26
标签:最小值 dep ofo uber cti clust port walk sum Pod因为不明原因挂掉,导致服务不可用 如果我们人工监控pods,人工进行调整副本那么这个工作量无疑是巨大的,但kubernetes已经有了相应的机制来应对了。 ###HPA全称Horizontal Pod Autoscaler控制器工作流程(V1版本) 更详细的介绍参考官方文档Horizontal Pod Autoscaler 流程 ###配置HPA实现应用横向扩展 apiVersion: apps/v1 cat hpa-svc.yaml apiVersion: v1 cat hpa-hpa.yaml deployment.apps/php-apache configured NAME READY STATUS RESTARTS AGE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE NAME READY UP-TO-DATE AVAILABLE AGE NAME DESIRED CURRENT READY AGE NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE kubectl run --generator=run-pod/v1 -i --tty load-generator --image=busybox /bin/sh OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK! kubectl get hpa kubectl get hpa kubectl get hpa kubectl get hpa kubectl get hpa kubectl get deployment php-apache while true; do wget -q -O- http://php-apache-svc.default.svc.cluster.local; done kubectl get hpa #等待几分钟之后(默认5分钟),原因: #再次等待几分钟后(默认5分钟) #再次等待几分钟后(默认5分钟),稳定在最小副本数量 apiVersion: autoscaling/v2beta2 Kubernetes系列之Kubernetes的弹性伸缩(HPA) 标签:最小值 dep ofo uber cti clust port walk sum 原文地址:https://blog.51cto.com/79076431/2475855
在kubernetes中,我们使用pod对外提供服务。这时候,我们需要以下两种情形需要关注:
Pod在高负荷的情况下,不能支撑我们的服务
前提:pod一定要设置资源限制,参数request,HPA才会工作。
--horizontal-pod-autoscaler-downscale-delay
--horizontal-pod-autoscaler-upscale-delay
本记录是对kubernetes HPA功能的验证,参考kubernetes官方文档,使用的是官方文档提供的镜像php-apache进行测试。
kubernetes集群需要配置好metrics server,配置参考文档Kubernetes部署metrics-server
cat hpa-deployment.ymal
kind: Deployment
metadata:
name: php-apache
labels:
app: hpa-test
spec:
replicas: 1
selector:
matchLabels:
name: php-apache
app: hpa-test
template:
metadata:
labels:
name: php-apache
app: hpa-test
spec:
containers:
image: mirrorgooglecontainers/hpa-example:latest
ports:
name: http
protocol: TCP
resources:
requests:
cpu: 0.005
memory: 64Mi
limits:
cpu: 0.05
memory: 128Mi
2. 配置service: php-apache-svc
kind: Service
metadata:
name: php-apache-svc
labels:
app: hpa-test
spec:
selector:
name: php-apache
app: hpa-test
ports:
port: 80
protocol: TCP
3. 配置hpa:php-apache-hpa
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: php-apache-hpa
labels:
app: hpa-test
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-apache
minReplicas: 1
maxReplicas: 10
targetCPUUtilizationPercentage: 504. 启动deployment,service,hpa,并验证
kubectl apply -f ./
service/php-apache-svc unchanged
horizontalpodautoscaler.autoscaling/php-apache-hpa unchangedkubectl get all
pod/php-apache-6b9f498dc4-vwlfr 1/1 Running 0 3h14m
service/kubernetes ClusterIP 10.96.0.1
service/php-apache-svc ClusterIP 10.104.34.168
deployment.apps/php-apache 1/1 1 1 3h14m
replicaset.apps/php-apache-6b9f498dc4 1 1 1 3h14m
horizontalpodautoscaler.autoscaling/php-apache-hpa Deployment/php-apache 20%/50% 1 10 1 3h14m###压力测试,观察HPA效果
>1.生成一个压测客户端,持续压力测试
while true; do wget -q -O- http://php-apache-svc.default.svc.cluster.local; done
>2.压测一下,观察结果
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache-hpa Deployment/php-apache 800%/50% 1 10 1 27m
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache-hpa Deployment/php-apache 1000%/50% 1 10 2 27m
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache-hpa Deployment/php-apache 1000%/50% 1 10 4 27m
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache-hpa Deployment/php-apache 1000%/50% 1 10 8 27m
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache-hpa Deployment/php-apache 120%/50% 1 10 10 27m
NAME READY UP-TO-DATE AVAILABLE AGE
php-apache 10/10 10 10 28m#####结论:随着压力测试进行,deployment下pod的CPU使用率增加,超过HPA设定的百分比50%,之后逐次翻倍扩容replicaset。达到上限停止扩容。根据replicaset设置的request QoS逐渐稳定资源的使用率。
>3.停止压测
OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!OK!wget: can‘t connect to remote host (10.104.63.73): Connection refused
OK!OK!OK!OK!OK!OK!........OK!OK!OK! ^C
/ # exit
/ # Session ended, resume using ‘kubectl attach load-generator -c load-generator -i -t‘ command when the pod is running#####CPU使用率恢复到最初值20%,controller会周期观测,逐次缩容到最小值。
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache-hpa Deployment/php-apache 20%/50% 1 10 10 36m
kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache-hpa Deployment/php-apache 20%/50% 1 10 4 41m
kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache-hpa Deployment/php-apache 20%/50% 1 10 2 46m
kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache-hpa Deployment/php-apache 20%/50% 1 10 1 53m###其他
以上测试验证了HPA功能,使用的API版本是autoscaling/v1。通过kubectl api-versions可以查看到存在3个版本。v1版本只支持CPU,v2beta2版本支持多metrics(CPU,memory)以及自定义metrics。基于autoscaling/v2beta2的hpa yaml文件写法
kind: HorizontalPodAutoscaler
metadata:
name: php-apache-hpa
labels:
app: hpa-test
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-apache
minReplicas: 1
maxReplicas: 10
metrics:
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
文章标题:Kubernetes系列之Kubernetes的弹性伸缩(HPA)
文章链接:http://soscw.com/index.php/essay/73787.html