1. 程式人生 > >水平Pod自動伸縮_Kubernetes中文社群

水平Pod自動伸縮_Kubernetes中文社群

在當前的版本中,Pod水平自動伸縮支援基於CPU使用情況對Pod進行自動伸縮。Pod水平自動伸縮通過Kubernetes API資源和控制器進行實現。控制器會根據CPU的使用情況週期性的調整Pod的副本數量。

1、水平Pod自動伸縮是如何工作的?

Horizontal Pod Autoscaler diagram

水平Pod自動伸縮被實現為控制週期迴圈,其週期由控制器管理器的–horizontal-pod-autoscaler-sync-period標誌進行控制(預設值為30秒)。在每個週期間,控制器管理器會根據每個Horizo​​ntalPodAutoscaler定義中指定的度量標準查詢資源利用率。控制器管理器從資源指標的API(針對每個窗格的資源指標)或自定義指標的API(針對所有其他指標)獲取度量。

  • 對於每個Pod資源指標(如CPU),控制器為HorizontalPodAutoscaler的目標Pod從資源指標API中獲取指標。如果設定了目標利用率的值,則控制器將在會計算使用率,作為每個Pod中容器的等效資源請求的百分比。如果設定了目標原始值,則直接使用原始指標標準值。然後,控制器在所有目標Pod中獲取利用率的平均值或原始值(取決於指定的目標型別),併產生用於縮放所需副本數量的比率。

需要注意的,如果某些Pod的容器沒有設定相關的資源請求,則不會定義Pod的CPU利用率,並且autoscaler不會對該指標標準採取任何操作。

  • 對於每個Pod自定義的指標,與每個pod資源指標的工作機理類似;不同之處在於它使用初始值,而不是使用率值。
  • 對於物件指標和外部指標,將獲取單個指標,該指標將描述相關物件。該指標將與目標值進行比較,以產生如上所述的比率。在autoscaling/v2beta2API版本中,可以選擇在進行比較之前將該值除以pod的數量。

Horizo​​ntalPodAutoscaler通常從一系列的API聚集中獲取指標(metrics.k8s.io, custom.metrics.k8s.io和external.metrics.k8s.io)。該metrics.k8s.io API通常由指標伺服器提供,其需要單獨啟動。自動縮放器使用伸縮子資源訪問相應的可伸縮控制器(例如複製控制器,部署和副本集)。Scale是一個介面,允許您動態設定副本數並檢查每個當前狀態。

2、演算法

從最基本的角度來看,Horizo​​ntalPodAutoscaler控制器根據所需指標與當前指標之間的比率進行操作:

desiredReplicas = ceil[currentReplicas * ( currentMetricValue / desiredMetricValue )]

例如,如果當前的指標值為200m,而期望的指標值為100m,則Pod的副本數量將會翻倍,即200/100=2;如果當前的指標值為50m,則會將Pod的副本數減半。如果比率足夠接近1.0(在全域性可配置的容差範圍內,從–horizontal-pod-autoscaler-tolerance標誌,預設為0.1),將會忽略縮放。當指定targetAverageValuetargetAverageUtilization時,currentMetricValue會通過所有Pod(Horizo​​ntalPodAutoscaler伸縮目標)獲取給定指標的平均值來計算。當基於CPU進行伸縮時,如果任意Pod還未準備就緒,則此Pod放棄。currentMetricValue / desiredMetricValue伸縮比率基於保留的Pod進行計算,而不是放棄的Pod進行計算。如果在HorizontalPodAutoscaler中指定了多個指標,則會計算每一個指標,並選擇期望副本數最大的指標。

3、在kubectl中使用水平Pod自動伸縮

與所有API資源一樣,kubectl以標準方式支援水平Pod自動伸縮。可以使用kubectl create命令建立一個新的自動縮放器。通過kubectl get hpa可以列出自動伸縮器,通過kubectl describe hpa能夠獲得自動伸縮器的詳細說明。最後,可以使用kubectl delete hpa刪除自動縮放器。此外,還有一個特殊kubectl autoscale命令可以輕鬆建立Horizo​​ntalPodAutoscaler

1)建立autoscale

$ kubectl autoscale deployment netdisk --min=2 --max=5 --cpu-percent=80 

為部署netdisk建立一個自動伸縮器,目標CPU利用率設定為80%,副本數量介於2和5之間。

2)獲取autoscale

獲取default名稱空間下的hpa

$ kubectl get hpa

3)檢視autoscale

檢視netdisk hpa的詳細資訊。

$ kubectl describe hpa netdisk

4)刪除autoscale

$ kubectl delete hpa netdisk

4、kubectl autoscale命令

kubectl autoscale命令的使用方法

$ autoscale (-f FILENAME | TYPE NAME | TYPE/NAME) [--min=MINPODS] --max=MAXPODS [--cpu-percent=CPU]

欄位說明

名稱 Shorthand 預設值 使用說明
allow-missing-template-keys true 如果值為真,則忽略模板中的任何錯誤。僅僅能夠應用到golang和jsonpath輸出格式。
cpu-percent -1 The target average CPU utilization (represented as a percent of requested CPU) over all the pods. If it’s not specified or negative, a default autoscaling policy will be used.
dry-run false If true, only print the object that would be sent, without sending it.
filename f [] Filename, directory, or URL to files identifying the resource to autoscale.
generator horizontalpodautoscaler/v1 The name of the API generator to use. Currently there is only 1 generator.
max -1 The upper limit for the number of pods that can be set by the autoscaler. Required.
min -1 The lower limit for the number of pods that can be set by the autoscaler. If it’s not specified or negative, the server will apply a default value.
name The name for the newly created object. If not specified, the name of the input resource will be used.
output o Output format. One of: json|yaml|name|template|go-template|go-template-file|templatefile|jsonpath|jsonpath-file.
record false Record current kubectl command in the resource annotation. If set to false, do not record the command. If set to true, record the command. If not set, default to updating the existing annotation value only if one already exists.
recursive R false Process the directory used in -f, –filename recursively. Useful when you want to manage related manifests organized within the same directory.
save-config false If true, the configuration of current object will be saved in its annotation. Otherwise, the annotation will be unchanged. This flag is useful when you want to perform kubectl apply on this object in the future.
template Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].

參考材料

1.《Horizo​​ntal Pod Autoscaler》地址:https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/

2.《autoscale》地址:https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#autoscale