HPA(Horizontal Pod Autoscaler)是Openshift中的一个非常重要的对象,它定义了系统如何根据收集对应的Pod的状态(CPU/Memory)对DeploymentConfig、ReplicationController对象进行扩容与缩容。
HPA依赖于收集到的Pod资源的使用状态,所以要使HPA生效,Openshift必须安装好cluster metrics应用。
被监控的pod必须设置好了spec.containers.resources.requests属性,HPA才能正常工作。
仅支持CPU/Memory使用率的判断,如果自定义监控项,只能使用经验值,不能使用使用率。
支持对象:DeploymentConfig、ReplicationController、Deployment、Replica Set。
HPA实现Pod伸缩.JPG
HPA进行伸缩过程
收集该HPA控制下所有Pod最近的cpu使用情况(CPU utilization)
对比在扩容条件里记录的cpu限额(CPUUtilization)
调整实例数(必须要满足不超过最大/最小实例数)
每隔30s做一次自动扩容的判断
说明:
CPU utilization的计算方法是用cpu usage(最近一分钟的平均值,通过heapster可以直接获取到)除以cpu request(这里cpu request就是我们在创建容器时制定的cpu使用核心数)得到一个平均值,这个平均值可以理解为:平均每个Pod CPU核心的使用占比。
最重要的步骤为3,这里即为HPA的算法,计算当前需要启动几个Pod
HPA进行伸缩算法
分为三种情况:
普通情况下启动Pod数量计算方式
TargetNumOfPods = ceil(sum(CurrentPodsCPUUtilization) / Target)
说明:
ceil()表示取大于或等于某数的最近一个整数
例子:
我们有一个集群实例数是3 pods,同时Pod的cpu资源的Request为1.4。cpu限额,即Target是CPU使用率为80%,当cpu的使用量CurrentPodsCPUUtilization为1.1,1.4,1.3时,要扩容成多少个呢?
ceil((1.1+1.4+1.3)/1.4/0.8)= 4
所以扩容成四个实例。
实例刚启动时及刚完成扩容/缩容,会有一段冷却时间
由于启动实例时cpu的使用度会陡增,所以自动扩容会等待一段时间以收集准确的运行时监控数据。每次扩容后冷却3分钟才能再次进行扩容,而缩容则要等5分钟后。这是因为自动扩容使用保守的方法,尽可能满足pods业务的正常使用,所以扩容的优先级要大于缩容。
当前Pod Cpu使用率与目标使用率接近时,不会触发扩容
当满足以下条件才会真正触发扩容/缩容:
avg(CurrentPodsConsumption) / Target >1.1 或 <0.9
这是为了避免出现频繁的扩容缩容。
扩容条件的相对与绝对度量
例子:
我们有一个集群实例数是3 pods,同时Pod的cpu资源的Request为1.5。cpu限额,即Target是CPU使用率为80%,当cpu的使用量CurrentPodsCPUUtilization为1.1,1.4,1.3时,会不会发生扩容,要扩容成多少个呢?
ceil((1.1+1.4+1.3)/1.5/0.8)= 4
按照我们1的说法,它再添加一个pod。但是我们再来算下当前Pod使用率与目标使用率情况。
(1.1 + 1.4 + 1.3)/3/1.5 = 0.84444 #当前Pod CPU平均使用率0.84444 / 0.8 = 1.055555 < 1.1 #当前Pod CPU平均使用率与目标CPU使用率比
综上:1.0555 < 1.1,当前HPA并不会发生扩容,所以最终Pod数仍然是3个。
为 dc/nginx-demo 创建一个 HPA (最小为1个pod,最多为3个pod,cpu使用率目标值为80%)
oc autoscale dc/nginx-demo--min=1 --max=3 --cpu-percent=80
查看当前hpa状态
[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 +0800Reference: DeploymentConfig/nginx-demo Metrics: ( current / target ) resource cpu on pods (as a percentage of request): 0% (0) / 80% Min replicas: 1Max replicas: 3Conditions: 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创建一个HPA(最小为1个pod,最多为3个pod,memory使用率目标值50%)
与CPU使用率作为目标值不同,memory使用率不能使用oc autoscale命令来创建,只能通过yaml文件来创建。
# hpa-memory.ymlapiVersion: 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
查看当前hpa状态
[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 +0800Reference: DeploymentConfig/nginx-demo Metrics: ( current / target ) resource memory on pods (as a percentage of request): 1% (1347584) / 50% Min replicas: 1Max replicas: 3Conditions: 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
作者:潘晓华Michael
链接:https://www.jianshu.com/p/da5b5ee00a52
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/151/viewspace-2820638/ ,如需转载,请注明出处,否则将追究法律责任。