日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

K8s HPA

發布時間:2024/3/12 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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: 30

autoscaling/v1版本將metrics字段放在了annotation中進行處理。
若同時存在多個metrics塊,則HPA依次計算每個metrics塊對應的期望副本數,然后選擇maxValue為最后的期望副本數。
metrics.{type}.target.type共有3種類型

  • Utilization:平均使用率
  • AverageValue:平均值
  • Value:裸值
  • metrics.type字段有四種類型

  • Resource:指的是當前伸縮對象下的pod的cpu和memory指標,只支持Utilization和AverageValue類型的目標值。
  • Object:指定k8s內部對象的指標,數據需要第三方adapter提供,只支持Value和AverageValue類型的目標值。
  • Pods:伸縮對象(statefulSet、replicaController、replicaSet)底下的Pods的指標,數據需要第三方的adapter提供,并且只允許AverageValue類型的目標值。
  • External:k8s外部的指標,數據同樣需要第三方的adapter提供,只支持Value和AverageValue類型的目標值。
  • 若需簡單使用,可結合metrics-server、resource類型,通過對pod的cpumemory進行監控來進行自動伸縮pod副本數,在程序負載低時釋放服務器資源,在程序負載突然升高時自動擴充pod副本數,以此應對流量訪問高峰。
    在使用前需確保:

  • 首先已安裝metrics-server,是否支持kubectl top node|pod -n {yourNamespace}
  • 其次檢查是否支持metrics.api
  • 例如根據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 HPA的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。