Openshift HPA(Horizontal Pod Autoscaler)自動(dòng)伸縮過程及算法

1、HPA介紹

HPA(Horizontal Pod Autoscaler)是Openshift中的一個(gè)非常重要的對象,它定義了系統(tǒng)如何根據(jù)收集對應(yīng)的Pod的狀態(tài)(CPU/Memory)對DeploymentConfig、ReplicationController對象進(jìn)行擴(kuò)容與縮容。

  • HPA依賴于收集到的Pod資源的使用狀態(tài),所以要使HPA生效,Openshift必須安裝好cluster metrics應(yīng)用。
  • 被監(jiān)控的pod必須設(shè)置好了spec.containers.resources.requests屬性,HPA才能正常工作。
  • 僅支持CPU/Memory使用率的判斷,如果自定義監(jiān)控項(xiàng),只能使用經(jīng)驗(yàn)值,不能使用使用率。
  • 支持對象:DeploymentConfig、ReplicationController、Deployment、Replica Set。


    HPA實(shí)現(xiàn)Pod伸縮.JPG

2. HPA伸縮過程及算法

HPA進(jìn)行伸縮過程

  1. 收集該HPA控制下所有Pod最近的cpu使用情況(CPU utilization)
  2. 對比在擴(kuò)容條件里記錄的cpu限額(CPUUtilization)
  3. 調(diào)整實(shí)例數(shù)(必須要滿足不超過最大/最小實(shí)例數(shù))
  4. 每隔30s做一次自動(dòng)擴(kuò)容的判斷
    說明:
  • CPU utilization的計(jì)算方法是用cpu usage(最近一分鐘的平均值,通過heapster可以直接獲取到)除以cpu request(這里cpu request就是我們在創(chuàng)建容器時(shí)制定的cpu使用核心數(shù))得到一個(gè)平均值,這個(gè)平均值可以理解為:平均每個(gè)Pod CPU核心的使用占比。
  • 最重要的步驟為3,這里即為HPA的算法,計(jì)算當(dāng)前需要啟動(dòng)幾個(gè)Pod

HPA進(jìn)行伸縮算法

分為三種情況:

  1. 普通情況下啟動(dòng)Pod數(shù)量計(jì)算方式
TargetNumOfPods = ceil(sum(CurrentPodsCPUUtilization) / Target)

說明:

  • ceil()表示取大于或等于某數(shù)的最近一個(gè)整數(shù)

例子:
我們有一個(gè)集群實(shí)例數(shù)是3 pods,同時(shí)Pod的cpu資源的Request為1.4。cpu限額,即Target是CPU使用率為80%,當(dāng)cpu的使用量CurrentPodsCPUUtilization為1.1,1.4,1.3時(shí),要擴(kuò)容成多少個(gè)呢?

ceil((1.1+1.4+1.3)/1.4/0.8)= 4 

所以擴(kuò)容成四個(gè)實(shí)例。

  1. 實(shí)例剛啟動(dòng)時(shí)及剛完成擴(kuò)容/縮容,會(huì)有一段冷卻時(shí)間
    由于啟動(dòng)實(shí)例時(shí)cpu的使用度會(huì)陡增,所以自動(dòng)擴(kuò)容會(huì)等待一段時(shí)間以收集準(zhǔn)確的運(yùn)行時(shí)監(jiān)控?cái)?shù)據(jù)。每次擴(kuò)容后冷卻3分鐘才能再次進(jìn)行擴(kuò)容,而縮容則要等5分鐘后。這是因?yàn)樽詣?dòng)擴(kuò)容使用保守的方法,盡可能滿足pods業(yè)務(wù)的正常使用,所以擴(kuò)容的優(yōu)先級要大于縮容。

  2. 當(dāng)前Pod Cpu使用率與目標(biāo)使用率接近時(shí),不會(huì)觸發(fā)擴(kuò)容
    當(dāng)滿足以下條件才會(huì)真正觸發(fā)擴(kuò)容/縮容:

avg(CurrentPodsConsumption) / Target >1.1 或 <0.9

這是為了避免出現(xiàn)頻繁的擴(kuò)容縮容。
擴(kuò)容條件的相對與絕對度量
例子:
我們有一個(gè)集群實(shí)例數(shù)是3 pods,同時(shí)Pod的cpu資源的Request為1.5。cpu限額,即Target是CPU使用率為80%,當(dāng)cpu的使用量CurrentPodsCPUUtilization為1.1,1.4,1.3時(shí),會(huì)不會(huì)發(fā)生擴(kuò)容,要擴(kuò)容成多少個(gè)呢?

ceil((1.1+1.4+1.3)/1.5/0.8)= 4 

按照我們1的說法,它再添加一個(gè)pod。但是我們再來算下當(dāng)前Pod使用率與目標(biāo)使用率情況。

(1.1 + 1.4 + 1.3)/3/1.5 = 0.84444 #當(dāng)前Pod CPU平均使用率
0.84444 / 0.8 = 1.055555 < 1.1 #當(dāng)前Pod CPU平均使用率與目標(biāo)CPU使用率比

綜上:1.0555 < 1.1,當(dāng)前HPA并不會(huì)發(fā)生擴(kuò)容,所以最終Pod數(shù)仍然是3個(gè)。

實(shí)戰(zhàn)

為 dc/nginx-demo 創(chuàng)建一個(gè) HPA (最小為1個(gè)pod,最多為3個(gè)pod,cpu使用率目標(biāo)值為80%)

oc autoscale dc/nginx-demo--min=1 --max=3 --cpu-percent=80

查看當(dāng)前hpa狀態(tài)

[root@demo ~]# oc delete hpa hpa-resource-metrics-memory 
horizontalpodautoscaler "hpa-resource-metrics-memory" deleted
[root@demo ~]# oc describe hpa nginx-demo 
Name:                                                  nginx-demo
Namespace:                                             testmysql
Labels:                                                <none>
Annotations:                                           <none>
CreationTimestamp:                                     Wed, 06 Jun 2018 10:36:57 +0800
Reference:                                             DeploymentConfig/nginx-demo
Metrics:                                               ( current / target )
  resource cpu on pods  (as a percentage of request):  0% (0) / 80%
Min replicas:                                          1
Max replicas:                                          3
Conditions:
  Type            Status  Reason            Message
  ----            ------  ------            -------
  AbleToScale     True    ReadyForNewScale  the last scale time was sufficiently old as to warrant a new scale
  ScalingActive   True    ValidMetricFound  the HPA was able to succesfully calculate a replica count from cpu resource utilization (percentage of request)
  ScalingLimited  True    TooFewReplicas    the desired replica count is more than the maximum replica count
Events:           <none>

為dc/nginx-demo創(chuàng)建一個(gè)HPA(最小為1個(gè)pod,最多為3個(gè)pod,memory使用率目標(biāo)值50%)

與CPU使用率作為目標(biāo)值不同,memory使用率不能使用oc autoscale命令來創(chuàng)建,只能通過yaml文件來創(chuàng)建

# hpa-memory.yml
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: hpa-resource-metrics-memory 
spec:
  scaleTargetRef:
    apiVersion: apps.openshift.io/v1
    kind: DeploymentConfig
    name: nginx-demo
  minReplicas: 1 
  maxReplicas: 3 
  metrics:
  - type: Resource
    resource:
      name: memory
      targetAverageUtilization: 50 
oc create -f hpa-memory.yml

查看當(dāng)前hpa狀態(tài)

[root@demo~]# oc describe hpa hpa-resource-metrics-memory 
Name:                                                     hpa-resource-metrics-memory
Namespace:                                                testmysql
Labels:                                                   <none>
Annotations:                                              <none>
CreationTimestamp:                                        Wed, 06 Jun 2018 10:28:59 +0800
Reference:                                                DeploymentConfig/nginx-demo
Metrics:                                                  ( current / target )
  resource memory on pods  (as a percentage of request):  1% (1347584) / 50%
Min replicas:                                             1
Max replicas:                                             3
Conditions:
  Type            Status  Reason              Message
  ----            ------  ------              -------
  AbleToScale     True    ReadyForNewScale    the last scale time was sufficiently old as to warrant a new scale
  ScalingActive   True    ValidMetricFound    the HPA was able to succesfully calculate a replica count from memory resource utilization (percentage of request)
  ScalingLimited  False   DesiredWithinRange  the desired count is within the acceptable range
Events:
  Type     Reason          Age              From                       Message
  ----     ------          ----             ----                       -------
  Warning  FailedGetScale  5m (x6 over 8m)  horizontal-pod-autoscaler  no matches for apps/, Kind=DeploymentConfig
  Warning  FailedGetScale  4m (x3 over 5m)  horizontal-pod-autoscaler  no matches for apps/, Kind=ReplicationController
  Warning  FailedGetScale  3m               horizontal-pod-autoscaler  replicationcontrollers/scale.autoscaling "nginx-demo" not found

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、 K8s 是什么? Kubernetes(k8s)是自動(dòng)化容器操作的開源平臺(tái),這些操作包括部署,調(diào)度和節(jié)點(diǎn)集群...
    loveroot閱讀 6,715評論 1 21
  • 1、基礎(chǔ)架構(gòu) 1.1 Master Master節(jié)點(diǎn)上面主要由四個(gè)模塊組成:APIServer、scheduler...
    阿斯蒂芬2閱讀 11,157評論 0 44
  • Kubernetes |Pod 深入理解與實(shí)踐 這篇文章參考自《Kubernete權(quán)威指南》,對其中的相關(guān)章節(jié)做...
    愛喝咖啡的土撥鼠閱讀 34,888評論 2 15
  • 寫這篇文章主要是為了今后畢業(yè)論文素材上的整理,同時(shí)對docker進(jìn)行鞏固溫習(xí)。大綱: docker簡介docker...
    胡圖仙人閱讀 7,778評論 2 96
  • 昨天講了我公司的情況,今天男友講了他公司那些事。果真是那些理兒,給人家打工的,就是低人一等啊,處處受制,晉升加薪的...
    豐色先生閱讀 128評論 0 0

友情鏈接更多精彩內(nèi)容