Cron HPA動態(tài)擴縮容實踐

Kubernetes 默認 HPA 功能可實現(xiàn)基于Metric CPU 與 MEM 的使用率來進行動態(tài)擴縮容败去,在實現(xiàn)工作中針對如果已知的突峰流量的場景(如指定時間段的業(yè)務推廣等)可能存在擴容置后不及時的問題,為了實現(xiàn)此類場景可以通過類似 Crontab 定時機制來動態(tài)擴縮容爷绘。本次實踐是使用了一個開源的 FinOps 項目Crane ,項目在 k8s 默認的 HPA基礎上封裝了更智能的 ehpa、 Analytics政鼠、tsp 等功能逮栅,如果有興趣可以進一步了解。今天主要實踐了基于 cron 方式的ehpa 功能胸私。

一、 安裝

k8s 集群內(nèi)安裝 Crane (推薦采用自定義安裝方式)

  1. clone 最新代碼并切到最新分支內(nèi)容
git clone https://github.com/gocrane/crane.git
cd crane

CRANE_LATEST_VERSION=$(curl -s https://api.github.com/repos/gocrane/crane/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")')
git checkout $CRANE_LATEST_VERSION
  1. 應用 CRD 資源對象的創(chuàng)建
kubectl apply -f deploy/manifests
  1. 修改 craned 連接的 prometheus server 的地址配置
  vi deploy/craned/deployment.yaml 
  command:
    - /craned
    - --prometheus-address=http://<ipaddr>:<port>
  1. 應用核心服務部署
kubectl apply -f deploy/craned 
kubectl apply -f deploy/metric-adapter

二鳖谈、 配置

定義定時擴縮調(diào)度策略配置
#配置實例岁疼,創(chuàng)建 cron-scale.yaml 
apiVersion: autoscaling.crane.io/v1alpha1
kind: EffectiveHorizontalPodAutoscaler
metadata:
  name: ehpa-cron-test
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment               
    name: nginx                       #指定scale deployment對象名稱
  minReplicas: 1                      #最大復本數(shù)
  maxReplicas: 5                      #最小復本數(shù)
  scaleStrategy: Auto   
  crons:
    - name: "biz-push-cron-1"
      timezone: "Asia/Shanghai"       #時區(qū)定義
      description: "scale up"         #擴容至3個POD復本
      start: "36 17 ? * *"            ##17:36~17:37 執(zhí)行scale up復本數(shù)為3
      end: "37 17 ? * *"
      targetReplicas: 3
    - name: "biz-push-cron-2"         #縮容至1個POD復本 
      timezone: "Asia/Shanghai"
      description: "scale down"
      start: "37 17 ? * *"
      end: "45 17 ? * *"              ##scale down復本數(shù)為1,注意start-end須>5m
      targetReplicas: 1      
      
# 應用策略
kubectl  apply -f cron-scale.yaml      

三缆娃、測試驗證

  • 查看應用策略狀態(tài)
[crane]# kubectl get hpa
NAME                  REFERENCE          TARGETS     MINPODS   MAXPODS   REPLICAS   AGE
ehpa-ehpa-cron-test   Deployment/nginx   1/1 (avg)   1         5         1          3m
[root@itg-k8s-master01 crane]# kubectl get ehpa
NAME             STRATEGY   MINPODS   MAXPODS   SPECIFICPODS   REPLICAS   AGE
ehpa-cron-test   Auto       1         5                        1          3m3s
  • 時間到達觸發(fā)后狀態(tài)查看捷绒,擴容到達三個
[crane]# kubectl get hpa
NAME                  REFERENCE          TARGETS     MINPODS   MAXPODS   REPLICAS   AGE
ehpa-ehpa-cron-test   Deployment/nginx   1/1 (avg)   1         5         3          5m29s

[crane]# kubectl get ehpa
NAME             STRATEGY   MINPODS   MAXPODS   SPECIFICPODS   REPLICAS   AGE
ehpa-cron-test   Auto       1         5                        3          5m58s
  • 查看 pod craned 日志
I0624  1 effective_hpa_controller.go:40] Got ehpa default/ehpa-cron-test
I0624  1 effective_hpa_controller.go:139] Update EffectiveHorizontalPodAutoscaler status successful, ehpa default/ehpa-cron-test
  • 時間到達觸發(fā)后查看 ehpa 實例的狀態(tài)信息
# 第一次cron調(diào)度(biz-push-cron-1)
# kubectl get ehpa ehpa-cron-test -oyaml
Status:
  Conditions:
    Last Transition Time:  2022-06-29T09:36:07Z
    Message:               Effective HPA is ready
    Reason:                EffectiveHorizontalPodAutoscalerReady
    Status:                True
    Type:                  Ready
    Last Transition Time:  2022-06-29T09:36:07Z
    Message:               the HPA controller was able to update the target scale to 3
    Reason:                SucceededRescale
    Status:                True
    Type:                  AbleToScale
    Last Transition Time:  2022-06-29T09:36:07Z
    Message:               the HPA was able to successfully calculate a replica count from external metric ehpa-cron-test(&LabelSelector{MatchLabels:map[string]string{autoscaling.crane.io/effective-hpa-uid: f2525179-a3bf-44b7-babe-11a71c73a252,},MatchExpressions:[]LabelSelectorRequirement{},})
    Reason:                ValidMetricFound
    Status:                True
    Type:                  ScalingActive
    Last Transition Time:  2022-06-29T09:36:07Z
    Message:               the desired count is within the acceptable range
    Reason:                DesiredWithinRange
    Status:                False
    Type:                  ScalingLimited
  Current Replicas:        1
  Expect Replicas:         3
---------------  
Status:
  Conditions:
    Last Transition Time:  2022-06-29T09:37:07Z
    Message:               Effective HPA is ready
    Reason:                EffectiveHorizontalPodAutoscalerReady
    Status:                True
    Type:                  Ready
    Last Transition Time:  2022-06-29T09:37:07Z
    Message:               recent recommendations were higher than current one, applying the highest recent recommendation
    Reason:                ScaleDownStabilized
    Status:                True
    Type:                  AbleToScale
    Last Transition Time:  2022-06-29T09:37:07Z
    Message:               the HPA was able to successfully calculate a replica count from external metric ehpa-cron-test(&LabelSelector{MatchLabels:map[string]string{autoscaling.crane.io/effective-hpa-uid: f2525179-a3bf-44b7-babe-11a71c73a252,},MatchExpressions:[]LabelSelectorRequirement{},})
    Reason:                ValidMetricFound
    Status:                True
    Type:                  ScalingActive
    Last Transition Time:  2022-06-29T09:37:07Z
    Message:               the desired count is within the acceptable range
    Reason:                DesiredWithinRange
    Status:                False
    Type:                  ScalingLimited
  Current Replicas:        3
  Expect Replicas:         3
  

# (上次結(jié)束時間+5分鐘)第二次 cron 調(diào)度 (biz-push-cron-2)
Status:
  Conditions:
    Last Transition Time:  2022-06-29T09:41:53Z
    Message:               Effective HPA is ready
    Reason:                EffectiveHorizontalPodAutoscalerReady
    Status:                True
    Type:                  Ready
    Last Transition Time:  2022-06-29T09:41:53Z
    Message:               the HPA controller was able to update the target scale to 1
    Reason:                SucceededRescale
    Status:                True
    Type:                  AbleToScale
    Last Transition Time:  2022-06-29T09:41:53Z
    Message:               the HPA was able to successfully calculate a replica count from external metric ehpa-cron-test(&LabelSelector{MatchLabels:map[string]string{autoscaling.crane.io/effective-hpa-uid: f2525179-a3bf-44b7-babe-11a71c73a252,},MatchExpressions:[]LabelSelectorRequirement{},})
    Reason:                ValidMetricFound
    Status:                True
    Type:                  ScalingActive
    Last Transition Time:  2022-06-29T09:41:53Z
    Message:               the desired count is within the acceptable range
    Reason:                DesiredWithinRange
    Status:                False
    Type:                  ScalingLimited
  Current Replicas:        3                         
  Expect Replicas:         1                             #觸發(fā)期望復本數(shù)

---------------  
Status:
  Conditions:
    Last Transition Time:  2022-06-29T09:42:08Z
    Message:               Effective HPA is ready
    Reason:                EffectiveHorizontalPodAutoscalerReady
    Status:                True
    Type:                  Ready
    Last Transition Time:  2022-06-29T09:42:08Z
    Message:               recommended size matches current size
    Reason:                ReadyForNewScale
    Status:                True
    Type:                  AbleToScale
    Last Transition Time:  2022-06-29T09:42:08Z
    Message:               the HPA was able to successfully calculate a replica count from external metric ehpa-cron-test(&LabelSelector{MatchLabels:map[string]string{autoscaling.crane.io/effective-hpa-uid: f2525179-a3bf-44b7-babe-11a71c73a252,},MatchExpressions:[]LabelSelectorRequirement{},})
    Reason:                ValidMetricFound
    Status:                True
    Type:                  ScalingActive
    Last Transition Time:  2022-06-29T09:42:08Z
    Message:               the desired count is within the acceptable range
    Reason:                DesiredWithinRange
    Status:                False
    Type:                  ScalingLimited
  Current Replicas:        1                                   # HPA 達成所期望復本數(shù)
  Expect Replicas:         1

~~~ Finish ~~~

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市贯要,隨后出現(xiàn)的幾起案子暖侨,更是在濱河造成了極大的恐慌,老刑警劉巖崇渗,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件字逗,死亡現(xiàn)場離奇詭異京郑,居然都是意外死亡,警方通過查閱死者的電腦和手機葫掉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門些举,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人俭厚,你說我怎么就攤上這事户魏。” “怎么了挪挤?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵叼丑,是天一觀的道長。 經(jīng)常有香客問我扛门,道長鸠信,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任尖飞,我火速辦了婚禮症副,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘政基。我一直安慰自己,他們只是感情好闹啦,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布沮明。 她就那樣靜靜地躺著,像睡著了一般窍奋。 火紅的嫁衣襯著肌膚如雪荐健。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天琳袄,我揣著相機與錄音江场,去河邊找鬼。 笑死窖逗,一個胖子當著我的面吹牛址否,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播碎紊,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼佑附,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了仗考?” 一聲冷哼從身側(cè)響起音同,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎秃嗜,沒想到半個月后权均,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體顿膨,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年叽赊,在試婚紗的時候發(fā)現(xiàn)自己被綠了恋沃。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡蛇尚,死狀恐怖芽唇,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情取劫,我是刑警寧澤匆笤,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站谱邪,受9級特大地震影響炮捧,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜惦银,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一咆课、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧扯俱,春花似錦书蚪、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至读存,卻和暖如春为流,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背让簿。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工敬察, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人尔当。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓莲祸,卻偏偏與公主長得像,于是被迫代替她去往敵國和親居凶。 傳聞我的和親對象是個殘疾皇子虫给,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容