簡體   English   中英

如何使用擴展/縮減策略實現 Kubernetes 水平 pod 自動擴展?

[英]How to implement Kubernetes horizontal pod autoscaling with scale up/down policies?

AWS EKS 中的 Kubernetes v1.19

我正在嘗試在我的 EKS 集群中實現水平 pod 自動縮放,並嘗試模仿我們現在使用 ECS 所做的事情。 使用 ECS,我們執行類似於以下的操作

  • 在連續 3 個 1 分鍾的采樣周期后 CPU >= 90% 時按比例放大
  • 在 5 個連續 1 分鍾的采樣周期后 CPU <= 60% 時按比例縮小
  • 當 memory >= 85% 在連續 3 個 1 分鍾的采樣周期后放大
  • 當 memory <= 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 }}

但是,我如何適應上述模板中Horizontal Pod Autoscaling中顯示的放大/縮小信息,以匹配我想要的行為?

Horizontal Pod Autoscaler 根據觀察到的指標(如CPUMemory )自動擴展復制 controller、部署、副本集或有狀態集的 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

旁注!

HPA將使用計算這兩個指標並選擇具有更大desiredReplicas的指標!

解決我在問題下寫的評論:

我想我們互相誤解了。 “當 CPU >= 90 時按比例放大”是完全可以的,但由於公式背后的邏輯,我認為不可能說“當 CPU <=70 時按比例縮小”。 根據公式,它將處於中間狀態:當 CPU >= 90 時放大,當 CPU =< 45 時縮小。

此示例可能具有誤導性,並且並非在所有情況下都 100% 正確。 看看下面的例子:

  • HPA設置為75%averageUtilization

具有某種程度的近似值的快速計算( HPA的默認容差為0.1 ):

  • 2副本:
    • scale-up (按1 )應該在以下情況下發生: currentMetricValue >= 80%
      • x = ceil[2 * (80/75)] , x = ceil[2,1(3)] , x = 3
    • currentMetricValue <= 33%時應該發生按scale-down (按1 ):
      • x = ceil[2 * (33/75)] , x = ceil[0,88] , x = 1
  • 8個副本:
    • currentMetricValue >= 76%時,應該發生scale-up (按1 ):
      • x = ceil[8 * (76/75)] , x = ceil[8,10(6)] , x = 9
    • currentMetricValue <= 64%時應該發生按scale-down (按1 ):
      • x = ceil[8 * (64/75)] , x = ceil[6,82(6)] , x = 7

按照這個例子,有8個副本,其currentMetricValue55desiredMetricValue設置為75 )應該scale-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

HPA縮放過程可以通過 Kubernetes 1.18版和更新版本中引入的更改進行修改,其中:

支持可配置的縮放行為

v1.18開始, v2beta2 API 允許通過 HPA behavior字段配置縮放行為。 behavior字段下的scaleUpscaleDown部分中分別指定放大和縮小的行為。 可以為兩個方向指定穩定 window,以防止縮放目標中副本數量的波動。 同樣,指定擴展策略可以控制擴展時副本的變化率。

Kubernetes.io:文檔:任務:運行應用程序:水平 pod 自動縮放:支持可配置的縮放行為

我認為您可以使用新引入的字段(例如behaviorstabilizationWindowSeconds窗口秒)來調整您的工作負載以滿足您的特定需求。

我還建議您訪問EKS文檔以獲取更多參考、對指標和示例的支持。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM