K8s HPA
目錄
- 一、Horizontal Pod Autoscaler
- 二、Algorithm Details
- 1、計(jì)算公式
- 2、默認(rèn)行為
- 三、示例
參考:
K8S - Horizontal Pod Autoscaler
K8S - horizontal-pod-autoscale-walkthrough
簡(jiǎn)書 - 探索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、計(jì)算公式
desiredReplicas = ceil[currentReplicas * ( currentMetricValue / desiredMetricValue )]
期望副本數(shù)=ceil[當(dāng)前副本數(shù)*(當(dāng)前指標(biāo)值/期望指標(biāo)值)]
期望副本數(shù)/ 當(dāng)前副本數(shù) = 當(dāng)前指標(biāo)值/期望指標(biāo)值
其中期望副本數(shù)則為autoscale后的待設(shè)置的副本數(shù)
當(dāng)前副本數(shù)則為被監(jiān)控target pod的當(dāng)前實(shí)例個(gè)數(shù)
當(dāng)前指標(biāo)值則為HPA指定metric的當(dāng)前值
期望指標(biāo)值則為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: 所有目標(biāo)pod的metric的平均值
targetAverageUtilization: 所有目標(biāo)pod的metric的使用率(百分比)的平均值,例如limit.cpu=1000m,實(shí)際使用500m,則utilization=50%,例如deployment.replica=3, limit.cpu=1000m,則pod1實(shí)際使用cpu=500m, pod2=300m, pod=600m,則averageUtilization=(500/1000+300/1000+600/1000)/3 = (500 + 300 + 600)/(3*1000))
2、默認(rèn)行為
縮容策率:
穩(wěn)定窗口 5分鐘(或–horizontal-pod-autoscaler-downscale-stabilization)
允許同時(shí)停止 當(dāng)前運(yùn)行副本數(shù)*100% 的pod數(shù)量,即可以一次性將縮放目標(biāo)縮小到最小允許的副本
擴(kuò)容策略:
無(wú)穩(wěn)定窗口(立即觸發(fā)擴(kuò)容)
策略1:允許 每隔15秒 增加 當(dāng)前運(yùn)行副本數(shù)*100% 的pod數(shù)量
策略2:允許 每隔15秒 增加 4 個(gè)pod數(shù)量
且取兩個(gè)策略中的最大改變值the highest amount of change
三、示例
apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata:name: php-apachenamespace: default spec:# HPA的伸縮對(duì)象描述,HPA會(huì)動(dòng)態(tài)修改該對(duì)象的pod數(shù)量scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: php-apache# HPA的最小pod數(shù)量和最大pod數(shù)量minReplicas: 1maxReplicas: 10# 監(jiān)控的指標(biāo)數(shù)組,支持多種類型的指標(biāo)共存metrics:# 【Resource】類型的指標(biāo),該類型指標(biāo)依賴metric-server(目前僅支持cpu和memory)- type: Resourceresource:# 支持cpu | memoryname: cpu# Resource類型的target只支持Utilization和AverageValue類型的目標(biāo)值target:# Utilization類型的目標(biāo)值(type:Utilization, averageUtilization: 具體百分值)# AverageValue類型的目標(biāo)值(type:AverageValue, averageValue: 具體值)type: UtilizationaverageUtilization: 50# Pods類型的指標(biāo)- type: Podspods:metric:name: packets-per-second# AverageValue類型的目標(biāo)值,Pods指標(biāo)類型下只支持AverageValue類型的目標(biāo)值target:type: AverageValueaverageValue: 1k# Object類型的指標(biāo)- type: Objectobject:metric:# 指標(biāo)名稱name: requests-per-second# 監(jiān)控指標(biāo)的對(duì)象描述,指標(biāo)數(shù)據(jù)來(lái)源于該對(duì)象describedObject:apiVersion: networking.k8s.io/v1beta1kind: Ingressname: main-route# Value類型的目標(biāo)值,Object類型的指標(biāo)只支持Value和AverageValue類型的目標(biāo)值target:type: Valuevalue: 10k# External類型的指標(biāo)- type: Externalexternal:metric:name: queue_messages_ready# 該字段與第三方的指標(biāo)標(biāo)簽相關(guān)聯(lián)selector:matchLabels:env: "stage"app: "myapp"# External指標(biāo)類型下只支持Value和AverageValue類型的目標(biāo)值target:type: AverageValueaverageValue: 30autoscaling/v1版本將metrics字段放在了annotation中進(jìn)行處理。
若同時(shí)存在多個(gè)metrics塊,則HPA依次計(jì)算每個(gè)metrics塊對(duì)應(yīng)的期望副本數(shù),然后選擇maxValue為最后的期望副本數(shù)。
metrics.{type}.target.type共有3種類型
metrics.type字段有四種類型
若需簡(jiǎn)單使用,可結(jié)合metrics-server、resource類型,通過(guò)對(duì)pod的cpu或memory進(jìn)行監(jiān)控來(lái)進(jìn)行自動(dòng)伸縮pod副本數(shù),在程序負(fù)載低時(shí)釋放服務(wù)器資源,在程序負(fù)載突然升高時(shí)自動(dòng)擴(kuò)充pod副本數(shù),以此應(yīng)對(duì)流量訪問(wèn)高峰。
在使用前需確保:
例如根據(jù)cpu自動(dòng)伸縮
apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata:name: luo-vp-hpanamespace: tsp spec:# HPA的伸縮對(duì)象描述,HPA會(huì)動(dòng)態(tài)修改該對(duì)象的pod數(shù)量scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: mx-vehicle-parts-management# HPA的最小pod數(shù)量和最大pod數(shù)量minReplicas: 1maxReplicas: 10# 監(jiān)控的指標(biāo)數(shù)組,支持多種類型的指標(biāo)共存metrics:# 【Resource】類型的指標(biāo),該類型指標(biāo)依賴metric-server(目前僅支持cpu和memory)- type: Resourceresource:# 支持cpuname: cpu# Resource類型的target只支持Utilization和AverageValue類型的目標(biāo)值target:# Utilization類型的目標(biāo)值(type:Utilization, averageUtilization: 具體百分值)type: UtilizationaverageUtilization: 50針對(duì)memory自動(dòng)伸縮
apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata:name: luo-vp-hpanamespace: tsp spec:# HPA的伸縮對(duì)象描述,HPA會(huì)動(dòng)態(tài)修改該對(duì)象的pod數(shù)量scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: mx-vehicle-parts-management# HPA的最小pod數(shù)量和最大pod數(shù)量minReplicas: 1maxReplicas: 10# 監(jiān)控的指標(biāo)數(shù)組,支持多種類型的指標(biāo)共存metrics:# 【Resource】類型的指標(biāo),該類型指標(biāo)依賴metric-server(目前僅支持cpu和memory)- type: Resourceresource:# 支持memoryname: memory# Resource類型的target只支持Utilization和AverageValue類型的目標(biāo)值target:# AverageValue類型的目標(biāo)值(type:AverageValue, averageValue: 具體值)type: AverageValueaverageValue: 1Gi總結(jié)
- 上一篇: K8S(5)HPA
- 下一篇: Arduino Uno 实验14——声音