Horizontal Pod Autoscaler 根据观察到的指标(如CPU
或Memory
)自动扩展复制控制器、部署、副本集或有状态集中的 Pod 数量。
有一个官方演练专注于HPA
它的缩放:
缩放副本数量的算法如下:
desiredReplicas = ceil[currentReplicas * ( currentMetricValue / desiredMetricValue )]
一个(已经渲染的)自动缩放的例子可以用YAML
如下的清单来实现:
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: HPA-NAME
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: DEPLOYMENT-NAME
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 75
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 75
旁注!
HPA
将使用计算这两个指标并选择一个更大的desiredReplicas
!
解决我在问题下写的评论:
我想我们互相误解了。“当 CPU >= 90 时按比例放大”是完全可以的,但由于公式背后的逻辑,我认为不可能说“当 CPU <=70 时按比例缩小”。根据公式,它将处于中间状态:当 CPU >= 90 时放大,当 CPU =< 45 时缩小。
此示例可能具有误导性,并且并非在所有情况下都 100% 正确。看看下面的例子:
HPA
设置averageUtilization
为75%
。
具有某种程度的近似值的快速计算(默认容差为HPA
)0.1
:
2
复制品:
scale-up
(by 1
) 应该发生在:currentMetricValue
is >= 80%
:
x = ceil[2 * (80/75)]
, x = ceil[2,1(3)]
,x = 3
scale-down
(by 1
) 应该在currentMetricValue
is <=时发生33%
:
x = ceil[2 * (33/75)]
, x = ceil[0,88]
,x = 1
8
复制品:
scale-up
(by 1
) 应该在currentMetricValue
is >=时发生76%
:
x = ceil[8 * (76/75)]
, x = ceil[8,10(6)]
,x = 9
scale-down
(by 1
) 应该在currentMetricValue
is <=时发生64%
:
x = ceil[8 * (64/75)]
, x = ceil[6,82(6)]
,x = 7
按照此示例,具有8
at currentMetricValue
(55
设置desiredMetricValue
为75
) 的副本应该scale-down
是6
副本。
可以通过运行找到描述决策制定的更多信息HPA
(例如为什么它不能扩展):
$ kubectl describe hpa HPA-NAME
Name: nginx-scaler
Namespace: default
Labels: <none>
Annotations: <none>
CreationTimestamp: Sun, 07 Mar 2021 22:48:58 +0100
Reference: Deployment/nginx-scaling
Metrics: ( current / target )
resource memory on pods (as a percentage of request): 5% (61903667200m) / 75%
resource cpu on pods (as a percentage of request): 79% (199m) / 75%
Min replicas: 1
Max replicas: 10
Deployment pods: 5 current / 5 desired
Conditions:
Type Status Reason Message
---- ------ ------ -------
AbleToScale True ReadyForNewScale recommended size matches current size
ScalingActive 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
---- ------ ---- ---- -------
Warning FailedGetResourceMetric 4m48s (x4 over 5m3s) horizontal-pod-autoscaler did not receive metrics for any ready pods
Normal SuccessfulRescale 103s horizontal-pod-autoscaler New size: 2; reason: cpu resource utilization (percentage of request) above target
Normal SuccessfulRescale 71s horizontal-pod-autoscaler New size: 4; reason: cpu resource utilization (percentage of request) above target
Normal SuccessfulRescale 71s horizontal-pod-autoscaler New size: 5; reason: cpu resource utilization (percentage of request) above target
HPA
扩展程序可以通过 Kubernetes 版本1.18
和更新版本中引入的更改进行修改,其中:
支持可配置的缩放行为
从v1.18开始,v2beta2
API 允许通过 HPAbehavior
字段配置缩放行为。行为是单独指定的,用于在字段中scaleUp
或scaleDown
部分下按比例放大和缩小。behavior
可以为两个方向指定稳定窗口,以防止缩放目标中副本数量的波动。同样,指定扩展策略可以控制扩展时副本的变化率。
Kubernetes.io:文档:任务:运行应用程序:水平 pod 自动缩放:支持可配置的缩放行为
我认为您可以使用新引入的字段,behavior
并stabilizationWindowSeconds
根据您的特定需求调整您的工作量。
我还建议您访问EKS
文档以获取更多参考、对指标和示例的支持。