Please enable Javascript to view the contents

Kubernetes 自动扩缩容

 ·  ☕ 1 分钟

本文状态:草稿

image-20210323153341945

配置例子

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: php-apache
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: php-apache
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50

算法

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

currentMetricValue 为相关 pod 的 metric 平均数。

If multiple metrics are specified in a HorizontalPodAutoscaler, this calculation is done for each metric, and then the largest of the desired replica counts is chosen.

Just before HPA scales the target, the scale recommendation is recorded. The controller considers all recommendations within a configurable window choosing the highest recommendation from within that window.

参数

  • --horizontal-pod-autoscaler-sync-period flag (with a default value of 15 seconds) : control loop, with a period controlled by the controller manager
  • --horizontal-pod-autoscaler-tolerance : We’ll skip scaling if the ratio is sufficiently close to 1.0 (within a globally-configurable tolerance, defaults to 0.1
  • --horizontal-pod-autoscaler-initial-readiness-delay : Due to technical constraints, the HorizontalPodAutoscaler controller cannot exactly determine the first time a pod becomes ready when determining whether to set aside certain CPU metrics. Instead, it considers a Pod “not yet ready” if it’s unready and transitioned to unready within a short, configurable window of time since it started. This value is configured with the --horizontal-pod-autoscaler-initial-readiness-delay flag, and its default is 30 seconds
  • --horizontal-pod-autoscaler-cpu-initialization-period : Once a pod has become ready, it considers any transition to ready to be the first if it occurred within a longer, configurable time since it started. This value is configured with the --horizontal-pod-autoscaler-cpu-initialization-period flag, and its default is 5 minutes.
  • --horizontal-pod-autoscaler-downscale-stabilization : just before HPA scales the target, the scale recommendation is recorded. The controller considers all recommendations within a configurable window choosing the highest recommendation from within that window. This value can be configured using the --horizontal-pod-autoscaler-downscale-stabilization flag, which defaults to 5 minutes. This means that scaledowns will occur gradually, smoothing out the impact of rapidly fluctuating metric values.

CRD

targetAverageValue
targetAverageUtilization

支持的指标

POD 指标

1
2
3
4
5
6
type: Resource
resource:
  name: cpu
  target:
    type: Utilization
    averageUtilization: 60

Container 指标

Kubernetes v1.20 的支持。

1
2
3
4
5
6
7
type: ContainerResource
containerResource:
  name: cpu
  container: application
  target:
    type: Utilization
    averageUtilization: 60

查询状态

1
2
3
$ kubectl get hpa
NAME       REFERENCE          TARGET    MINPODS MAXPODS REPLICAS AGE
busybox-1  Deployment/busybox 0% / 80%  3       4       1        11m

参考

分享

Mark Zhu
作者
Mark Zhu
An old developer