K8S动态扩缩容-HPA

kubernetes Horizontal Pod Autoscaling

  1. 自动扩缩ReplicationController、Deployment、ReplicaSet 和 StatefulSet 中的 Pod 数量
  2. 度量指标可以是:CPU指标 和 自定义
HPA 控制过程
  1. 实现自动扩缩容的原理官方文档

    1. --horizontal-pod-autoscaler-sync-period=15s 控制器的自动检测周期。
    2. metrics-server 提供 metrics.k8s.io API 为pod资源的使用提供支持。
    3. 15s/周期 -> 查询metrics.k8s.io API -> 算法计算 -> 调用scale 调度 -> 特定的扩缩容策略执行。
  2. HPA扩缩容算法 期望副本数 = ceil[当前副本数 * (度量指标 / 期望指标)]

    1. 扩容
      1. 如果计算出的扩缩比例接近 1.0, 将会放弃本次扩缩, 度量指标 / 期望指标接近1.0。
    2. 缩容
      1. 冷却/延迟: 如果延迟(冷却)时间设置的太短,那么副本数量有可能跟以前一样出现抖动。 默认值是 5 分钟(5m0s)--horizontal-pod-autoscaler-downscale-stabilization
    3. 特殊处理
      1. 丢失度量值:缩小时假设这些 Pod 消耗了目标值的 100%, 在需要放大时假设这些 Pod 消耗了 0% 目标值。 这可以在一定程度上抑制扩缩的幅度。
      2. 存在未就绪的pod的时候:我们保守地假设尚未就绪的 Pod 消耗了期望指标的 0%,从而进一步降低了扩缩的幅度。
      3. 未就绪的 Pod 和缺少指标的 Pod 考虑进来再次计算使用率。 如果新的比率与扩缩方向相反,或者在容忍范围内,则跳过扩缩。 否则,我们使用新的扩缩比例。
      4. 指定了多个指标, 那么会按照每个指标分别计算扩缩副本数,取最大值进行扩缩。

HPA 对象定义

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: nginx
spec:
  behavior:
  scaleDown:
    policies:
    - type: Pods
      value: 4
      periodSeconds: 60
    - type: Percent
      value: 10
      periodSeconds: 60
    stabilizationWindowSeconds: 300
  
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: nginx
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50

HPA对象默认行为

behavior:
  scaleDown:
    stabilizationWindowSeconds: 300
    policies:
    - type: Percent
      value: 100
      periodSeconds: 15
  scaleUp:
    stabilizationWindowSeconds: 0
    policies:
    - type: Percent
      value: 100
      periodSeconds: 15
    - type: Pods
      value: 4
      periodSeconds: 15
    selectPolicy: Max

metrics-server api github

为集群提供监控性能参考是必须的。

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

遇到问题请参考 issue

一个nginx的演示实例

---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: nginx
  namespace: lidj
spec:
  maxReplicas: 10
  metrics:
  - resource:
      name: cpu
      target:
        averageUtilization: 40
        type: Utilization
    type: Resource
  minReplicas: 1
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: nginx
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: lidj
spec:
  type: NodePort
  ports:
    - name: "http"
      port: 80
      targetPort: 80

  selector:
    service: nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: lidj
spec:
  replicas: 1
  selector:
    matchLabels:
      service: nginx
  template:
    metadata:
      labels:
        service: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:latest
          resources:
            requests:
              cpu: 100m
              memory: 100Mi
            limits:
              cpu: 200m
              memory: 200Mi

使用压测工具ab进行

ab -n 100000 -c 800 http://10.22.112.223:43434

查看现象

[root@shifeinode5 ~]# kubectl get pods,hpa -n lidj | grep nginx
pod/nginx-866f647cbc-97qp2            0/1     ContainerCreating   0          11s
pod/nginx-866f647cbc-hf65d            0/1     ContainerCreating   0          11s
pod/nginx-866f647cbc-hq6b7            1/1     Running             0          19h
pod/nginx-866f647cbc-kp6jq            1/1     Running             0          23h
horizontalpodautoscaler.autoscaling/nginx   Deployment/nginx   97%/40%   2         20        2          23h
[root@shifeinode5 ~]# kubectl get pods,hpa -n lidj | grep nginx
pod/nginx-866f647cbc-5tlft            1/1     Running             0          22s
pod/nginx-866f647cbc-97qp2            1/1     Running             0          37s
pod/nginx-866f647cbc-bfc5f            0/1     ContainerCreating   0          22s
pod/nginx-866f647cbc-chl48            0/1     ContainerCreating   0          7s
pod/nginx-866f647cbc-dl4v8            1/1     Running             0          22s
pod/nginx-866f647cbc-hf65d            1/1     Running             0          37s
pod/nginx-866f647cbc-hq6b7            1/1     Running             0          19h
pod/nginx-866f647cbc-j5275            1/1     Running             0          22s
pod/nginx-866f647cbc-kp6jq            1/1     Running             0          23h
pod/nginx-866f647cbc-v5gpc            0/1     ContainerCreating   0          7s
horizontalpodautoscaler.autoscaling/nginx   Deployment/nginx   91%/40%   2         20        8          23h

你可能感兴趣的:(K8S动态扩缩容-HPA)