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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

HPA控制器介绍以及实战案例

發布時間:2024/3/12 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HPA控制器介绍以及实战案例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

背景:

根據當前pod的負載,動態調整 pod副本數量,業務高峰期自動擴容pod的副本數以盡快響應pod的請求。
在業務低峰期對pod進行縮容,實現降本增效的目的。

動態伸縮控制器類型:

水平pod自動縮放器(HPA):基于pod 資源利用率橫向調整pod副本數量。
垂直pod自動縮放器(VPA):基于pod資源利用率,調整對單個pod的最大資源限制,不能與HPA同時使用。
集群伸縮(Cluster Autoscaler,CA):基于集群中node 資源使用情況,動態伸縮node節點,從而保證有CPU和內存資源用于創建pod。

具體執行過程參照下圖:

HPA控制器簡介:

Horizontal Pod Autoscaling (HPA)控制器,根據預定義好的閾值及pod當前的資源利用率,自動控制在k8s集群中運行的pod數量(自動彈性水平自動伸縮).
指標說明:
horizontal-pod-autoscaler-sync-period #默認每隔15s(可以通過–horizontal-pod-autoscaler-sync-period修改)查詢metrics的資源使用情況。
horizontal-pod-autoscaler-downscale-stabilization #縮容間隔周期,默認5分鐘。
horizontal-pod-autoscaler-sync-period #HPA控制器同步pod副本數的間隔周期
horizontal-pod-autoscaler-cpu-initialization-period #初始化延遲時間,在此時間內 pod的CPU 資源指標將不會生效,默認為5分鐘。
horizontal-pod-autoscaler-initial-readiness-delay #用于設置 pod 準備時間, 在此時間內的 pod 統統被認為未就緒及不采集數據,默認為30秒。

kube-controller-manager --help|grep horizontal-pod-autoscaler-downscale-stabilization #可以查看具體的默認值
注意:
horizontal-pod-autoscaler-tolerance #HPA控制器能容忍的數據差異(浮點數,默認為0.1),即新的指標要與當前的閾值差異在0.1或以上,即要大于1+0.1=1.1,或小于1-0.1=0.9,比如閾值為CPU利用率50%,當前為80%,那么80/50=1.6 > 1.1則會觸發擴容,反之會縮容。即觸發條件:avg(CurrentPodsConsumption) / Target >1.1 或 <0.9=把N個pod的數據相加后根據pod的數量計算出平均數除以閾值,大于1.1就擴容,小于0.9就縮容。

計算公式:TargetNumOfPods = ceil(sum(CurrentPodsCPUUtilization) / Target) #ceil是一個向上取整的目的pod整數。

指標數據需要部署metrics-server,即HPA使用metrics-server作為數據源。

部署metric-server

資源文件下載地址:
https://github.com/kubernetes-sigs/metrics-server
kubectl apply -f metrics-server.yaml #部署metric-server
kubectl top pod xxx -n ns #查看pod指標
kubectl top node xxx #查看node指標

實戰案例【基于k8s1.24.2版本】:

tomcat.yaml

apiVersion: apps/v1 kind: Deployment metadata:name: mytomcat spec:replicas: 5selector:matchLabels:app: mytomcatminReadySeconds: 1progressDeadlineSeconds: 60revisionHistoryLimit: 5strategy:type: RollingUpdaterollingUpdate:maxSurge: 1maxUnavailable: 1template:metadata:name: mytomcatlabels:app: mytomcatspec:containers:- name: mytomcatimage: tomcat:8ports:- containerPort: 8080 --- apiVersion: v1 kind: Service metadata:name: mytomcat spec:#type: NodePortports:- port: 8080#nodePort: 30090selector:app: mytomcat

hap.yaml

apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler metadata:namespace: demoname: demo-tomcat-podautoscalerlabels:app: demo-tomcat spec:scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: mytomcat minReplicas: 2maxReplicas: 8targetCPUUtilizationPercentage: 60 root@ubuntu01:/app# kubectl get pod -n demo NAME READY STATUS RESTARTS AGE myserver-nginx-deployment-56f4ccb9bd-9kjlc 1/1 Running 7 (47m ago) 14d mytomcat-689d96db6f-4t9lh 1/1 Running 0 8m29s mytomcat-689d96db6f-c29sl 1/1 Running 0 8m30s mytomcat-689d96db6f-dhhmm 1/1 Running 0 6s mytomcat-689d96db6f-vsmph 1/1 Running 0 7m35s mytomcat-689d96db6f-w6fwq 1/1 Running 0 7m35sroot@ubuntu01:/app# kubectl describe hpa demo-tomcat-podautoscaler -n demo Warning: autoscaling/v2beta2 HorizontalPodAutoscaler is deprecated in v1.23+, unavailable in v1.26+; use autoscaling/v2 HorizontalPodAutoscaler Name: demo-tomcat-podautoscaler Namespace: demo Labels: app=demo-tomcat Annotations: <none> CreationTimestamp: Fri, 19 Aug 2022 21:32:01 +0800 Reference: Deployment/mytomcat Metrics: ( current / target )resource cpu on pods (as a percentage of request): 2% (2m) / 60% Min replicas: 2 Max replicas: 8 Deployment pods: 2 current / 2 desired Conditions:Type Status Reason Message---- ------ ------ -------AbleToScale True ScaleDownStabilized recent recommendations were higher than current one, applying the highest recent recommendationScalingActive True ValidMetricFound the HPA was able to successfully calculate a replica count from cpu resource utilization (percentage of request)ScalingLimited False DesiredWithinRange the desired count is within the acceptable range Events:Type Reason Age From Message---- ------ ---- ---- -------Normal SuccessfulRescale 3m40s horizontal-pod-autoscaler New size: 4; reason: All metrics below targetNormal SuccessfulRescale 54s horizontal-pod-autoscaler New size: 2; reason: All metrics below target

從上面可以看出由最初的5個pod副本數降為hpa控制器最小的2個

將上面deployment資源文件鏡像修改為:
image: lorel/docker-stress-ng
args: [“–vm”,“2”,“–vm-bytes”,“256M”]

apiVersion: apps/v1 kind: Deployment metadata:name: mytomcat spec:replicas: 2selector:matchLabels:app: mytomcatminReadySeconds: 1progressDeadlineSeconds: 60revisionHistoryLimit: 5strategy:type: RollingUpdaterollingUpdate:maxSurge: 1maxUnavailable: 1template:metadata:name: mytomcatlabels:app: mytomcatspec:containers:- name: mytomcatimage: lorel/docker-stress-ngargs: ["--vm","2","--vm-bytes","256M"]ports:- containerPort: 8080resources:requests:cpu: 100mmemory: 100Milimits:cpu: 100mmemory: 100Miroot@ubuntu01:/app# kubectl describe hpa demo-tomcat-podautoscaler -n demo Warning: autoscaling/v2beta2 HorizontalPodAutoscaler is deprecated in v1.23+, unavailable in v1.26+; use autoscaling/v2 HorizontalPodAutoscaler Name: demo-tomcat-podautoscaler Namespace: demo Labels: app=demo-tomcat Annotations: <none> CreationTimestamp: Fri, 19 Aug 2022 21:50:01 +0800 Reference: Deployment/mytomcat Metrics: ( current / target )resource cpu on pods (as a percentage of request): 87% (87m) / 60% Min replicas: 2 Max replicas: 8 Deployment pods: 7 current / 8 desired Conditions:Type Status Reason Message---- ------ ------ -------AbleToScale True SucceededRescale the HPA controller was able to update the target scale to 8ScalingActive True ValidMetricFound the HPA was able to successfully calculate a replica count from cpu resource utilization (percentage of request)ScalingLimited True TooManyReplicas the desired replica count is more than the maximum replica count Events:Type Reason Age From Message---- ------ ---- ---- -------Normal SuccessfulRescale 32m horizontal-pod-autoscaler New size: 4; reason: All metrics below targetNormal SuccessfulRescale 30m horizontal-pod-autoscaler New size: 2; reason: All metrics below targetWarning FailedGetScale 4m39s horizontal-pod-autoscaler deployments/scale.apps "mytomcat" not foundWarning FailedGetResourceMetric 4m9s (x2 over 4m24s) horizontal-pod-autoscaler failed to get cpu utilization: unable to get metrics for resource cpu: no metrics returned from resource metrics APIWarning FailedComputeMetricsReplicas 4m9s (x2 over 4m24s) horizontal-pod-autoscaler invalid metrics (1 invalid out of 1), first error is: failed to get cpu utilization: unable to get metrics for resource cpu: no metrics returned from resource metrics APIWarning FailedGetResourceMetric 3m54s horizontal-pod-autoscaler failed to get cpu utilization: did not receive metrics for any ready podsWarning FailedComputeMetricsReplicas 3m54s horizontal-pod-autoscaler invalid metrics (1 invalid out of 1), first error is: failed to get cpu utilization: did not receive metrics for any ready podsNormal SuccessfulRescale 2m54s horizontal-pod-autoscaler New size: 4; reason: cpu resource utilization (percentage of request) above targetNormal SuccessfulRescale 98s horizontal-pod-autoscaler New size: 5; reason: cpu resource utilization (percentage of request) above targetNormal SuccessfulRescale 83s horizontal-pod-autoscaler New size: 7; reason: cpu resource utilization (percentage of request) above targetNormal SuccessfulRescale 20s horizontal-pod-autoscaler New size: 8; reason: cpu resource utilization (percentage of request) above targetroot@ubuntu01:/app# kubectl get pod -n demo NAME READY STATUS RESTARTS AGE myserver-nginx-deployment-56f4ccb9bd-9kjlc 1/1 Running 7 (83m ago) 14d mytomcat-76798bc994-2gxxf 1/1 Running 0 5m51s mytomcat-76798bc994-2z5cm 1/1 Running 0 4m35s mytomcat-76798bc994-5s2tf 1/1 Running 0 4m20s mytomcat-76798bc994-dlbgf 1/1 Running 0 4m20s mytomcat-76798bc994-dncl4 1/1 Running 0 5m51s mytomcat-76798bc994-f5b5c 1/1 Running 0 7m36s mytomcat-76798bc994-r59j7 1/1 Running 0 3m17s mytomcat-76798bc994-rzl5w 1/1 Running 0 7m36s

從圖中可以看出擴容到8個副本數后即使還是超過閥值,但由于hpa控制器最大pod副本數為8,故到8個后pod副本數不在進行擴容。

總結

以上是生活随笔為你收集整理的HPA控制器介绍以及实战案例的全部內容,希望文章能夠幫你解決所遇到的問題。

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