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)行伸縮過程
- 收集該HPA控制下所有Pod最近的cpu使用情況(CPU utilization)
- 對比在擴(kuò)容條件里記錄的cpu限額(CPUUtilization)
- 調(diào)整實(shí)例數(shù)(必須要滿足不超過最大/最小實(shí)例數(shù))
- 每隔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)行伸縮算法
分為三種情況:
- 普通情況下啟動(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í)例。
實(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)先級要大于縮容。當(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