K8s HPA
目錄
- 一、Horizontal Pod Autoscaler
- 二、Algorithm Details
- 1、計算公式
- 2、默認行為
- 三、示例
參考:
K8S - Horizontal Pod Autoscaler
K8S - horizontal-pod-autoscale-walkthrough
簡書 - 探索Kubernetes HPA
Github - K8S metrics Implementations
Github - metrics-server
一、Horizontal Pod Autoscaler
The Horizontal Pod Autoscaler automatically scales the number of pods in a replication controller, deployment, replica set or stateful set based on observed CPU utilization (or, with custom metrics support, on some other application-provided metrics). Note that Horizontal Pod Autoscaling does not apply to objects that can’t be scaled, for example, DaemonSets.
The Horizontal Pod Autoscaler is implemented as a Kubernetes API resource and a controller. The resource determines the behavior of the controller. The controller periodically adjusts the number of replicas in a replication controller or deployment to match the observed average CPU utilization to the target specified by user.
The Horizontal Pod Autoscaler is implemented as a control loop, with a period controlled by the controller manager’s --horizontal-pod-autoscaler-sync-period flag (with a default value of 15 seconds).
二、Algorithm Details
From the most basic perspective, the Horizontal Pod Autoscaler controller operates on the ratio between desired metric value and current metric value:
1、計算公式
desiredReplicas = ceil[currentReplicas * ( currentMetricValue / desiredMetricValue )]
期望副本數=ceil[當前副本數*(當前指標值/期望指標值)]
期望副本數/ 當前副本數 = 當前指標值/期望指標值
其中期望副本數則為autoscale后的待設置的副本數
當前副本數則為被監控target pod的當前實例個數
當前指標值則為HPA指定metric的當前值
期望指標值則為HPA中指定的averageValue, averageUtilizationValue
When a targetAverageValue or targetAverageUtilization is specified, the currentMetricValue is computed by taking the average of the given metric across all Pods in the HorizontalPodAutoscaler’s scale target. Before checking the tolerance and deciding on the final values, we take pod readiness and missing metrics into consideration, however.
targetAverageValue: 所有目標pod的metric的平均值
targetAverageUtilization: 所有目標pod的metric的使用率(百分比)的平均值,例如limit.cpu=1000m,實際使用500m,則utilization=50%,例如deployment.replica=3, limit.cpu=1000m,則pod1實際使用cpu=500m, pod2=300m, pod=600m,則averageUtilization=(500/1000+300/1000+600/1000)/3 = (500 + 300 + 600)/(3*1000))
2、默認行為
縮容策率:
穩定窗口 5分鐘(或–horizontal-pod-autoscaler-downscale-stabilization)
允許同時停止 當前運行副本數*100% 的pod數量,即可以一次性將縮放目標縮小到最小允許的副本
擴容策略:
無穩定窗口(立即觸發擴容)
策略1:允許 每隔15秒 增加 當前運行副本數*100% 的pod數量
策略2:允許 每隔15秒 增加 4 個pod數量
且取兩個策略中的最大改變值the highest amount of change
三、示例
apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata:name: php-apachenamespace: default spec:# HPA的伸縮對象描述,HPA會動態修改該對象的pod數量scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: php-apache# HPA的最小pod數量和最大pod數量minReplicas: 1maxReplicas: 10# 監控的指標數組,支持多種類型的指標共存metrics:# 【Resource】類型的指標,該類型指標依賴metric-server(目前僅支持cpu和memory)- type: Resourceresource:# 支持cpu | memoryname: cpu# Resource類型的target只支持Utilization和AverageValue類型的目標值target:# Utilization類型的目標值(type:Utilization, averageUtilization: 具體百分值)# AverageValue類型的目標值(type:AverageValue, averageValue: 具體值)type: UtilizationaverageUtilization: 50# Pods類型的指標- type: Podspods:metric:name: packets-per-second# AverageValue類型的目標值,Pods指標類型下只支持AverageValue類型的目標值target:type: AverageValueaverageValue: 1k# Object類型的指標- type: Objectobject:metric:# 指標名稱name: requests-per-second# 監控指標的對象描述,指標數據來源于該對象describedObject:apiVersion: networking.k8s.io/v1beta1kind: Ingressname: main-route# Value類型的目標值,Object類型的指標只支持Value和AverageValue類型的目標值target:type: Valuevalue: 10k# External類型的指標- type: Externalexternal:metric:name: queue_messages_ready# 該字段與第三方的指標標簽相關聯selector:matchLabels:env: "stage"app: "myapp"# External指標類型下只支持Value和AverageValue類型的目標值target:type: AverageValueaverageValue: 30autoscaling/v1版本將metrics字段放在了annotation中進行處理。
若同時存在多個metrics塊,則HPA依次計算每個metrics塊對應的期望副本數,然后選擇maxValue為最后的期望副本數。
metrics.{type}.target.type共有3種類型
metrics.type字段有四種類型
若需簡單使用,可結合metrics-server、resource類型,通過對pod的cpu或memory進行監控來進行自動伸縮pod副本數,在程序負載低時釋放服務器資源,在程序負載突然升高時自動擴充pod副本數,以此應對流量訪問高峰。
在使用前需確保:
例如根據cpu自動伸縮
apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata:name: luo-vp-hpanamespace: tsp spec:# HPA的伸縮對象描述,HPA會動態修改該對象的pod數量scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: mx-vehicle-parts-management# HPA的最小pod數量和最大pod數量minReplicas: 1maxReplicas: 10# 監控的指標數組,支持多種類型的指標共存metrics:# 【Resource】類型的指標,該類型指標依賴metric-server(目前僅支持cpu和memory)- type: Resourceresource:# 支持cpuname: cpu# Resource類型的target只支持Utilization和AverageValue類型的目標值target:# Utilization類型的目標值(type:Utilization, averageUtilization: 具體百分值)type: UtilizationaverageUtilization: 50針對memory自動伸縮
apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata:name: luo-vp-hpanamespace: tsp spec:# HPA的伸縮對象描述,HPA會動態修改該對象的pod數量scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: mx-vehicle-parts-management# HPA的最小pod數量和最大pod數量minReplicas: 1maxReplicas: 10# 監控的指標數組,支持多種類型的指標共存metrics:# 【Resource】類型的指標,該類型指標依賴metric-server(目前僅支持cpu和memory)- type: Resourceresource:# 支持memoryname: memory# Resource類型的target只支持Utilization和AverageValue類型的目標值target:# AverageValue類型的目標值(type:AverageValue, averageValue: 具體值)type: AverageValueaverageValue: 1Gi總結
- 上一篇: K8S(5)HPA
- 下一篇: Arduino Uno 实验14——声音