13.深入k8s:Pod 水平自动扩缩HPA及其源码分析

源码版本是1.19

Pod 水平自动扩缩 Pod 水平自动扩缩工作原理

Pod 水平自动扩缩全名是Horizontal Pod Autoscaler简称HPA。它可以基于 CPU 利用率或其他指标自动扩缩 ReplicationController、Deployment 和 ReplicaSet 中的 Pod 数量。

image-20200928160203334

Pod 水平自动扩缩器由--horizontal-pod-autoscaler-sync-period 参数指定周期(默认值为 15 秒)。每个周期内,控制器管理器根据每个 HorizontalPodAutoscaler 定义中指定的指标查询资源利用率。

Pod 水平自动扩缩控制器跟据当前指标和期望指标来计算扩缩比例,公式为:

desiredReplicas = ceil[currentReplicas * ( currentMetricValue / desiredMetricValue )]

currentReplicas表示当前度量值,desiredMetricValue表示期望度量值,desiredReplicas表示期望副本数。例如,当前度量值为 200m,目标设定值为 100m,那么由于 200.0/100.0 == 2.0, 副本数量将会翻倍。 如果当前指标为 50m,副本数量将会减半,因为50.0/100.0 == 0.5。

我们可以通过使用kubectl来创建HPA。如通过 kubectl create 命令创建一个 HPA 对象, 通过 kubectl get hpa 命令来获取所有 HPA 对象, 通过 kubectl describe hpa 命令来查看 HPA 对象的详细信息。 最后,可以使用 kubectl delete hpa 命令删除对象。

也可以通过kubectl autoscale来创建 HPA 对象。 例如,命令 kubectl autoscale rs foo --min=2 --max=5 --cpu-percent=80 将会为名 为 foo 的 ReplicationSet 创建一个 HPA 对象, 目标 CPU 使用率为 80%,副本数量配置为 2 到 5 之间。

如果指标变化太频繁,我们也可以使用--horizontal-pod-autoscaler-downscale-stabilization指令设置扩缩容延迟时间,表示的是自从上次缩容执行结束后,多久可以再次执行缩容,默认是5m。

Pod 水平自动扩缩示例

编写用于测试的Deployment:

apiVersion: apps/v1 kind: Deployment metadata: name: hpatest spec: replicas: 1 selector: matchLabels: app: hpatest template: metadata: labels: app: hpatest spec: containers: - name: hpatest image: nginx imagePullPolicy: IfNotPresent command: ["/bin/sh"] args: ["-c","/usr/sbin/nginx; while true;do echo `hostname -I` > /usr/share/nginx/html/index.html; sleep 120;done"] ports: - containerPort: 80 resources: requests: cpu: 1m memory: 100Mi limits: cpu: 3m memory: 400Mi --- apiVersion: v1 kind: Service metadata: name: hpatest-svc spec: selector: app: hpatest ports: - port: 80 targetPort: 80 protocol: TCP

编写HPA,用于水平扩展,当cpu达到50%的利用率的时候开始扩展:

apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler metadata: name: haptest-nginx spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: hpatest minReplicas: 2 maxReplicas: 6 targetCPUUtilizationPercentage: 50

写一个简单的压测脚本:

[root@localhost HPA]# vim hpatest.sh while true do wget -q -O- done

观察一下hpa的TARGETS情况:

[root@localhost ~]# kubectl get hpa -w NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE hpatest Deployment/hpatest 0%/50% 1 5 1 5m47s hpatest Deployment/hpatest 400%/50% 1 5 1 5m49s hpatest Deployment/hpatest 400%/50% 1 5 4 6m4s hpatest Deployment/hpatest 400%/50% 1 5 5 6m19s hpatest Deployment/hpatest 500%/50% 1 5 5 6m49s

观察是否会自动扩容:

[root@localhost ~]# kubectl get pods -o wide -w NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES hpatest-bbb44c476-jv8zr 0/1 ContainerCreating 0 0s <none> 192.168.13.130 <none> <none> hpatest-bbb44c476-sk6qb 0/1 ContainerCreating 0 0s <none> 192.168.13.130 <none> <none> hpatest-bbb44c476-7s5qn 0/1 ContainerCreating 0 0s <none> 192.168.13.130 <none> <none> hpatest-bbb44c476-7s5qn 1/1 Running 0 6s 172.20.0.23 192.168.13.130 <none> <none> hpatest-bbb44c476-sk6qb 1/1 Running 0 6s 172.20.0.22 192.168.13.130 <none> <none> hpatest-bbb44c476-jv8zr 1/1 Running 0 6s 172.20.0.21 192.168.13.130 <none> <none> hpatest-bbb44c476-dstnf 0/1 Pending 0 0s <none> <none> <none> <none> hpatest-bbb44c476-dstnf 0/1 Pending 0 0s <none> 192.168.13.130 <none> <none> hpatest-bbb44c476-dstnf 0/1 ContainerCreating 0 0s <none> 192.168.13.130 <none> <none> hpatest-bbb44c476-dstnf 1/1 Running 0 6s 172.20.0.24 192.168.13.130 <none> <none>

停止压测之后,HPA开始自动缩容:

[root@localhost HPA]# kubectl get pod -w hpatest-bbb44c476-dstnf 0/1 Terminating 0 9m52s hpatest-bbb44c476-jv8zr 0/1 Terminating 0 10m hpatest-bbb44c476-7s5qn 0/1 Terminating 0 10m hpatest-bbb44c476-sk6qb 0/1 Terminating 0 10m hpatest-bbb44c476-sk6qb 0/1 Terminating 0 10m hpatest-bbb44c476-dstnf 0/1 Terminating 0 10m hpatest-bbb44c476-dstnf 0/1 Terminating 0 10m hpatest-bbb44c476-7s5qn 0/1 Terminating 0 10m hpatest-bbb44c476-7s5qn 0/1 Terminating 0 10m hpatest-bbb44c476-jv8zr 0/1 Terminating 0 10m hpatest-bbb44c476-jv8zr 0/1 Terminating 0 10m 源码分析 初始化

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zypgss.html