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: mytomcathap.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”]
從圖中可以看出擴容到8個副本數后即使還是超過閥值,但由于hpa控制器最大pod副本數為8,故到8個后pod副本數不在進行擴容。
總結
以上是生活随笔為你收集整理的HPA控制器介绍以及实战案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常用传感器讲解五--声音传感器-(KY-
- 下一篇: Python绘图Turtle库详解情人节