[英]How to implement Kubernetes horizontal pod autoscaling with scale up/down policies?
AWS EKS 中的 Kubernetes v1.19
我正在嘗試在我的 EKS 集群中實現水平 pod 自動縮放,並嘗試模仿我們現在使用 ECS 所做的事情。 使用 ECS,我們執行類似於以下的操作
我正在嘗試使用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 根據觀察到的指標(如CPU
或Memory
)自動擴展復制 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
個副本,其currentMetricValue
為55
( desiredMetricValue
設置為75
)應該scale-down
到6
副本。
可以通過運行找到更多描述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 允許通過 HPAbehavior
字段配置縮放行為。 在behavior
字段下的scaleUp
或scaleDown
部分中分別指定放大和縮小的行為。 可以為兩個方向指定穩定 window,以防止縮放目標中副本數量的波動。 同樣,指定擴展策略可以控制擴展時副本的變化率。
我認為您可以使用新引入的字段(例如behavior
和stabilizationWindowSeconds
窗口秒)來調整您的工作負載以滿足您的特定需求。
我還建議您訪問EKS
文檔以獲取更多參考、對指標和示例的支持。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.