如何通过向上/向下扩展策略实现 Kubernetes 水平 Pod 自动缩放?

Chr*_*s F 7 kubernetes kubernetes-helm amazon-eks hpa

AWS EKS 中的 Kubernetes v1.19

我正在尝试在我的 EKS 集群中实现水平 Pod 自动缩放,并尝试模仿我们现在使用 ECS 所做的事情。对于 ECS,我们执行类似以下操作

  • 在 3 个连续 1 分钟采样周期后,当 CPU >= 90% 时进行扩展
  • 5 个连续 1 分钟采样周期后,当 CPU <= 60% 时缩小
  • 在 3 个连续 1 分钟采样周期后,当内存 >= 85% 时进行扩展
  • 在 5 个连续 1 分钟采样周期后,当内存 <= 70% 时缩小规模

我正在尝试使用那种HorizontalPodAutoscaler,并helm create给了我这个模板。(请注意,我对其进行了修改以满足我的需要,但该metrics节仍然保留。)

{- if .Values.autoscaling.enabled }}
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: {{ include "microserviceChart.Name" . }}
  labels:
    {{- include "microserviceChart.Name" . | nindent 4 }}
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: {{ include "microserviceChart.Name" . }}
  minReplicas: {{ include "microserviceChart.minReplicas" . }}
  maxReplicas: {{ include "microserviceChart.maxReplicas" . }}
  metrics:
    {{- if .Values.autoscaling.targetCPUUtilizationPercentage }}
    - type: Resource
      resource:
        name: cpu
        targetAverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }}
    {{- end }}
    {{- if .Values.autoscaling.targetMemoryUtilizationPercentage }}
    - type: Resource
      resource:
        name: memory
        targetAverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }}
    {{- end }}
{{- end }}
Run Code Online (Sandbox Code Playgroud)

但是,如何适应上述模板中Horizo​​ntal Pod Autoscaling中显示的放大/缩小信息,以匹配我想要的行为?

Daw*_*ruk 9

Horizo​​ntal Pod Autoscaler 根据观察到的指标(例如CPUMemory)自动缩放复制控制器、部署、副本集或有状态集中的 Pod 数量。

有一个官方演练重点关注HPA它的扩展:


缩放副本数量的算法如下:

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

YAML可以使用如下所示的清单来实现(已渲染的)自动缩放的示例:

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: HPA-NAME
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: DEPLOYMENT-NAME
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 75
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 75
Run Code Online (Sandbox Code Playgroud)

附注!

HPA将使用计算这两个指标并选择较大的一个desiredReplicas

针对我在该问题下写的评论:

我想我们都误解了彼此。“当 CPU >= 90 时扩大规模”是完全可以的,但由于公式背后的逻辑,我认为不可能说“当 CPU <= 70 时缩小规模”。根据公式,它会处于中间状态:当 CPU >= 90 时向上扩展,当 CPU =< 45 时向下扩展。

此示例可能会产生误导,并且在所有情况下并非 100% 真实。看一下下面的例子:

  • HPA设置为averageUtilization75%

具有一定程度的近似值的快速计算(默认公差为HPA0.1

  • 2复制品:
    • scale-up(by 1) 应该发生在:currentMetricValueis >=时80%
      • x = ceil[2 * (80/75)], x = ceil[2,1(3)],x = 3
    • scale-down(by ) 应该在is <=1时发生: currentMetricValue33%
      • x = ceil[2 * (33/75)], x = ceil[0,88],x = 1
  • 8复制品:
    • scale-up(by ) 应该在is >=1时发生: currentMetricValue76%
      • x = ceil[8 * (76/75)], x = ceil[8,10(6)],x = 9
    • scale-down(by ) 应该在is <=1时发生: currentMetricValue64%
      • x = ceil[8 * (64/75)], x = ceil[6,82(6)],x = 7

按照此示例,具有at (设置为)8的副本应该为副本。currentMetricValue55desiredMetricValue75scale-down6

可以通过运行以下命令找到描述决策制定的更多信息HPA例如为什么它无法扩展):

  • $ kubectl describe hpa HPA-NAME
Name:                                                     nginx-scaler
Namespace:                                                default
Labels:                                                   <none>
Annotations:                                              <none>
CreationTimestamp:                                        Sun, 07 Mar 2021 22:48:58 +0100
Reference:                                                Deployment/nginx-scaling
Metrics:                                                  ( current / target )
  resource memory on pods  (as a percentage of request):  5% (61903667200m) / 75%
  resource cpu on pods  (as a percentage of request):     79% (199m) / 75%
Min replicas:                                             1
Max replicas:                                             10
Deployment pods:                                          5 current / 5 desired
Conditions:
  Type            Status  Reason              Message
  ----            ------  ------              -------
  AbleToScale     True    ReadyForNewScale    recommended size matches current size
  ScalingActive   True    ValidMetricFound    the HPA was able to successfully calculate a replica count from cpu resource utilization (percentage of request)
  ScalingLimited  False   DesiredWithinRange  the desired count is within the acceptable range
Events:
  Type     Reason                   Age                   From                       Message
  ----     ------                   ----                  ----                       -------
  Warning  FailedGetResourceMetric  4m48s (x4 over 5m3s)  horizontal-pod-autoscaler  did not receive metrics for any ready pods
  Normal   SuccessfulRescale        103s                  horizontal-pod-autoscaler  New size: 2; reason: cpu resource utilization (percentage of request) above target
  Normal   SuccessfulRescale        71s                   horizontal-pod-autoscaler  New size: 4; reason: cpu resource utilization (percentage of request) above target
  Normal   SuccessfulRescale        71s                   horizontal-pod-autoscaler  New size: 5; reason: cpu resource utilization (percentage of request) above target
Run Code Online (Sandbox Code Playgroud)

HPA扩展程序可以通过 Kubernetes 版本及更新版本中引入的更改进行修改,1.18其中:

支持可配置的缩放行为

从v1.18开始,APIv2beta2允许通过 HPA 字段配置扩展行为behaviorscaleUp分别指定在字段中或scaleDown部分中按比例放大和缩小的行为behavior。可以为两个方向指定稳定窗口,以防止缩放目标中副本数量的波动。同样,指定扩展策略可以控制扩展时副本的变化率。

Kubernetes.io:文档:任务:运行应用程序:水平 Pod 自动缩放:支持可配置的缩放行为

我认为您可以使用新引入的字段,如behaviorstabilizationWindowSeconds来调整您的工作量以满足您的特定需求。

我还建议您查阅EKS文档以获取更多参考、对指标和示例的支持。