Kubernetes扩缩容介绍
首先来介绍一下什么是扩缩容。
扩缩容,可以分成两部分,扩容和缩容,都是指通过调整系统资源以适应负载需求变化的过程。
具体来讲,扩容即是在负载增大时增加系统资源,以应对更大的业务需求,负载压力过大导致系统故障;而缩容恰恰相反,是在负载减少时减少系统资源,以用较少的资源来承接负载,节约成本。
扩缩容又可以划分为水平和垂直两个方面:
水平扩缩容是指通过增加或者减少服务实例数量的方式来实现,单个服务实例的资源不变。
垂直扩缩容是指通过增加或者减少单个服务实例的资源的方式来实现,总的服务实例的数量不变。
具体到Kubernetes的实现中,包括了三种:
- 
HPA(Horizontal Pod Autoscaler): - HPA从名字可以看出此种方式是水平自动扩缩,具体原理就是通过监控 CPU 使用率或其他自定义指标来自动调整 Pod 的数量。在负载增加时,动态增加Pod的数量;在负载减少时,动态减少Pod的数量。
 
- 
VPA (Vertical Pod Autoscaler): - 而VPA则是垂直自动扩缩容,具体原理是通过监控监控 CPU 使用率或内存指标来动态调整 Pod 所需的 CPU 和内存资源。VPA是在不改变 Pod 数量的情况下动态的增加或减少分配给每个 Pod 的资源,以应对业务负载的变化。
 
- 
Cluster Autoscaler: - 集群自动扩缩,在Kubernetes执行HPA或者VPA的时候,如果集群中的资源不足,Cluster Autoscaler就会起作用了。它可以根据资源申请情况,在资源缺乏的时候,自动增加节点(虚拟机)数量,在资源过剩时,则自动减少节点数量。
 
本文主要先针对Kubernetes水平扩缩容进行介绍。
Kubernetes水平扩缩容详解
安装组件
Kubernetes安装默认是带有HorizontalPodAutoscaler水平扩缩容组件的。
水平扩缩容示例
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler  
metadata:  
  name: hpa-demo
spec:  
  scaleTargetRef:  
    apiVersion: apps/v1  
    kind: Deployment  
    name: hpa-demo
  minReplicas: 1     #进行缩容的最小副本数
  maxReplicas: 20  #进行扩容的最大副本数
  metrics:  
  - type: Resource
    resource:  
      name: cpu  
      target:  
        type: Utilization  
        averageUtilization: 60  # 当CPU利用率大于60%的时候,执行扩容操作
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 70  # 当内存利用率大于60%的时候,执行扩容操作上述yaml创建了一个HorizontalPodAutoscaler类型资源:
scaleTargetRef: 定义了该HorizontalPodAutoscaler作用的资源类型,示例中表示该HorizontalPodAutoscaler作用于同一命名空间的名字叫demo的deployment。
minReplicas:定义了该demo deployment进行缩容的最小副本数
maxReplicas:定义了该demo deployment进行扩容的最大副本数
behavior:该字段定义了达到指标阈值后执行的行为,取值包括:scaleDown和scaleUp,分别表示缩容和扩容,可选字段,不设置就表示扩缩容都会生效。
metrics:该字段定义了该HorizontalPodAutoscaler针对demo这个deployment进行扩缩容时要监控的指标。主要包括了CPU和内存指标:
- type字段:取值为如下值:ContainerResource、 External、Object、Pods、Resource,分别表示容器资源、外部资源、kubernetes对象(deployment、ingress等)、多个Pod资源或者单个pod的资源。
- containerResource:指定pod内部特定容器的资源指标
- external:非kubernetes对象的资源指标
- object:kubernetes对象的资源指标
- pods:针对选择器内的pod的资源的加权平均指标来进行扩缩容
- resource:pod执行的resource中的资源指标
创建后,如下图所示:
kubectl get hpa -n demo
NAME       REFERENCE             TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
hpa-demo   Deployment/hpa-demo   10%/20%   1         20        1          6h16m关联的Pod:
kubectl get po -n demo   
NAME                        READY   STATUS    RESTARTS   AGE
hpa-demo-5c8b8d4497-c68wq   1/1     Running   0          6h16m通过wrk进行压测,命令如下:
Running 2m test @ xxxxx
  2 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    15.06ms   15.90ms 704.16ms   98.12%
    Req/Sec     3.60k   679.08     8.79k    80.65%
  716889 requests in 1.67m, 581.09MB read
Requests/sec:   7166.88
Transfer/sec:      5.81MB查看HPA日志:
  Normal  SuccessfulRescale  8m43s  horizontal-pod-autoscaler  New size: 4; reason: cpu resource utilization (percentage of request) above target
  Normal  SuccessfulRescale  8m28s  horizontal-pod-autoscaler  New size: 8; reason: cpu resource utilization (percentage of request) above target
  Normal  SuccessfulRescale  8m13s  horizontal-pod-autoscaler  New size: 16; reason: cpu resource utilization (percentage of request) above target
  Normal  SuccessfulRescale  7m58s  horizontal-pod-autoscaler  New size: 20; reason: cpu resource utilization (percentage of request) above target查看Pod数量:
kubectl get po -n demo
NAME                        READY   STATUS    RESTARTS   AGE
hpa-demo-5c8b8d4497-488lx   1/1     Running   0          6m3s
hpa-demo-5c8b8d4497-4w2bj   1/1     Running   0          5m33s
hpa-demo-5c8b8d4497-4x247   1/1     Running   0          5m18s
hpa-demo-5c8b8d4497-629wg   1/1     Running   0          5m33s
hpa-demo-5c8b8d4497-8n966   1/1     Running   0          14m
hpa-demo-5c8b8d4497-bvfml   1/1     Running   0          5m48s
hpa-demo-5c8b8d4497-c68wq   1/1     Running   0          5m48s
hpa-demo-5c8b8d4497-d9hz5   1/1     Running   0          5m18s
hpa-demo-5c8b8d4497-f8rh5   1/1     Running   0          6m3s
hpa-demo-5c8b8d4497-fm5c5   1/1     Running   0          5m33s
hpa-demo-5c8b8d4497-gtv2s   1/1     Running   0          5m33s
hpa-demo-5c8b8d4497-j6w98   1/1     Running   0          5m33s
hpa-demo-5c8b8d4497-kps8c   1/1     Running   0          5m18s
hpa-demo-5c8b8d4497-mrtfp   1/1     Running   0          5m48s
hpa-demo-5c8b8d4497-qpxvj   1/1     Running   0          5m18s
hpa-demo-5c8b8d4497-sj624   1/1     Running   0          5m33s
hpa-demo-5c8b8d4497-tl4cz   1/1     Running   0          5m33s
hpa-demo-5c8b8d4497-wcx5h   1/1     Running   0          5m33s
hpa-demo-5c8b8d4497-wm2nm   1/1     Running   0          5m48s
hpa-demo-5c8b8d4497-z2c4l   1/1     Running   0          6m3s最终扩容到了20个pod
停止压测后,开始缩容:
  Normal  SuccessfulRescale  18m    horizontal-pod-autoscaler  New size: 10; reason: All metrics below target
  Normal  SuccessfulRescale  13m    horizontal-pod-autoscaler  New size: 5; reason: All metrics below target
  Normal  SuccessfulRescale  8m18s  horizontal-pod-autoscaler  New size: 3; reason: All metrics below target
  Normal  SuccessfulRescale  3m18s  horizontal-pod-autoscaler  New size: 2; reason: All metrics below targetkubectl get po -n demo     
NAME                        READY   STATUS    RESTARTS   AGE
hpa-demo-5c8b8d4497-c68wq   1/1     Running   0          6h19m那么HPA扩缩容的数量是如何计算的呢?公式如下:
desiredReplicas = ceil[currentReplicas * ( currentMetricValue / desiredMetricValue )]即最终的副本数 等于向上取整( (当前指标值 ➗ 设计指标值) ✖️ 当前Pod数 )。
总结
本文针对Kubernetes的扩缩容概念进行了简单的介绍,随后又通过实践的方式对水平扩容进行了详解,让大家对Kubernetes的扩缩容有初步的了解,后续还会针对垂直扩缩容进行实践详解。