水平荚自动缩放器可自动缩放荚的数量 在复制控制器中,根据观察到的CPU利用率(或 自定义指标 支持,以及其他一些应用程序提供的指标)。注意水平 Pod自动缩放不适用于可以’不能缩放,例如DaemonSets。
Horizontal Pod Autoscaler被实现为Kubernetes API资源和控制器。 该资源确定控制器的行为。 控制器会定期调整复制控制器或部署中的副本数 使观察到的平均CPU利用率与用户指定的目标相匹配。
Horizontal Pod Autoscaler被实现为一个控制回路,周期受控
由控制经理’s --horizontal-pod-autoscaler-sync-period
flag (with a default
值15秒)。
在每个期间,控制器管理器都会根据 每个HorizontalPodAutoscaler定义中指定的指标。控制器管理员 从资源指标API(针对每个单元资源指标)获取指标, 或自定义指标API(用于所有其他指标)。
请注意,如果某些吊舱’的容器没有相关的资源请求集, 不会定义pod的CPU使用率,并且自动缩放器将 对该指标不采取任何措施。见 算法 details 以下部分以获取更多有关的信息 自动缩放算法的工作原理。
对于按荚自定义指标,控制器的功能类似于按荚资源指标, 除了它适用于原始值,而不适用于利用率值。
对于对象指标和外部指标,将获取单个指标,该指标描述了
有问题的对象。将该指标与目标进行比较
value, to produce a ratio as above. In the autoscaling/v2beta2
API
版本,此值可以除以除以
comparison is made.
这 HorizontalPodAutoscaler normally fetches metrics from a series of aggregated APIs (metrics.k8s.io
,
custom.metrics.k8s.io
, and external.metrics.k8s.io
). The metrics.k8s.io
API is usually provided by
metrics-server,需要单独启动。看
指标服务器
有关说明。 HorizontalPodAutoscaler也可以直接从Heapster获取指标。
笔记:从Kubernetes 1.11开始,不推荐从Heapster获取指标。
看到 支持指标API for more details.
自动缩放器访问相应的可缩放控制器(例如复制控制器,部署和副本集) 通过使用scale子资源。 Scale是一个界面,可让您动态设置副本数并检查 他们的每个当前状态。可以找到有关规模子资源的更多详细信息 这里 .
从最基本的角度来看,Horizontal Pod Autoscaler控制器 根据所需指标值和当前指标之间的比率进行操作 value:
desiredReplicas = ceil[currentReplicas * ( currentMetricValue / desiredMetricValue )]
For example, if the current metric value is 200m
, and the desired value
is 100m
, the number of replicas will be doubled, since 200.0 / 100.0 ==
2.0
If the current value is instead 50m
, we’将数量减半
replicas, since 50.0 / 100.0 == 0.5
. We’如果比率为
足够接近1.0(在全局可配置的公差范围内,
the --horizontal-pod-autoscaler-tolerance
flag, which defaults to 0.1).
When a targetAverageValue
或者 targetAverageUtilization
is specified,
the currentMetricValue
is computed by taking the average of the given
HorizontalPodAutoscaler中所有Pod的指标’s scale target.
在检查公差并确定最终值之前,我们采用
但是,必须考虑吊舱准备情况和缺少的指标。
设置了删除时间戳记的所有Pod(即处于 关闭),所有失败的Pod都将被丢弃。
如果某个特定的Pod缺少指标,则将其保留以备后用。豆荚 指标缺失的情况将用于调整最终缩放比例。
在CPU上扩展时,是否有任何Pod尚未准备好(即’s still initializing) 或者 吊舱的最新度量标准点位于该吊舱之前 准备好了,那个吊舱也放在一边。
由于技术限制,HorizontalPodAutoscaler控制器
无法确切确定豆荚第一次准备就绪的时间
确定是否预留某些CPU指标。相反,它
considers a Pod “not yet ready” if it’尚未准备好并过渡到
自启动以来在很短的可配置时间内未准备就绪。
This value is configured with the --horizontal-pod-autoscaler-initial-readiness-delay
flag, and its default is 30
秒。吊舱准备就绪后,它将考虑过渡到
如果在更长的可配置时间内发生,则准备成为第一个
since it started. This value is configured with the --horizontal-pod-autoscaler-cpu-initialization-period
flag, and its
默认值为5分钟。
这 currentMetricValue / desiredMetricValue
base scale ratio is then
使用未从上方放置或丢弃的剩余豆荚计算得出。
如果缺少任何指标,我们将重新计算平均值 保守地假设这些豆荚消耗了100%的所需 如果缩小,则值为0;如果缩小,则值为0%。这抑制了 任何潜在规模的大小。
此外,如果存在尚未准备就绪的豆荚,我们将拥有 在不考虑缺少指标或尚未准备就绪的广告连播的情况下进行了扩展,我们 保守地假设尚未准备就绪的广告连播正在消耗0%的广告连播 所需的指标,进一步抑制放大的幅度。
考虑到尚未准备就绪的广告连播和缺少的指标后,我们 重新计算使用率。如果新比例使比例倒转 方向或在公差范围内,我们跳过缩放。否则,我们使用 新的比例比例。
请注意 原版的 报告平均利用率的值 通过HorizontalPodAutoscaler状态返回,而无需考虑 尚未使用的广告连播或缺少指标,即使新的使用率是 used.
如果在HorizontalPodAutoscaler中指定了多个指标,则此
计算每个指标,然后计算最大的指标
选择副本计数。如果这些指标中的任何一个无法转换
放入所需的副本数量(例如,由于获取指标时出错)
从指标API中),然后按指标建议缩小规模
可以获取,跳过缩放。这意味着HPA仍然能够
of scaling up if one or more metrics give a desiredReplicas
greater than
the current value.
最后,在HPA缩放目标之前,就记录了缩放建议。这
控制器会在可配置窗口中考虑所有建议,然后选择
highest recommendation from within that window. This value can be configured using the --horizontal-pod-autoscaler-downscale-stabilization
flag, which defaults to 5 minutes.
这意味着缩减将逐渐发生,以消除迅速的影响。
度量标准值波动。
这 水平荚自动缩放器 is an API resource in the Kubernetes autoscaling
API group.
当前的稳定版本,仅包括对CPU自动缩放的支持,
can be found in the autoscaling/v1
API version.
测试版包含对内存和自定义指标的扩展支持,
can be found in autoscaling/v2beta2
. The new fields introduced in autoscaling/v2beta2
are preserved as annotations when working with autoscaling/v1
.
有关API对象的更多详细信息,请参见 HorizontalPodAutoscaler对象.
水平荚自动缩放器, like every API resource, is supported in a standard way by kubectl
.
We can create a new autoscaler using kubectl create
command.
We can list autoscalers by kubectl get hpa
和 get detailed description by kubectl describe hpa
.
Finally, we can delete an autoscaler using kubectl delete hpa
.
In addition, there is a special kubectl自动缩放
command for easy creation of a 水平荚自动缩放器.
例如,执行 kubectl自动缩放 rs foo --min=2 --max=5 --cpu-percent=80
将为复制集创建一个自动缩放器 富 ,并将目标CPU使用率设置为 80%
副本数在2到5之间。
The detailed documentation of kubectl自动缩放
can be found 这里 .
目前在Kubernetes中,可以执行 滚动更新 通过直接管理复制控制器, 或通过使用部署对象为您管理基础副本集。 Horizontal Pod Autoscaler仅支持后一种方法:Horizontal Pod Autoscaler绑定到部署对象, 它设置部署对象的大小,并且部署负责设置基础副本集的大小。
Horizontal Pod Autoscaler不适用于直接操作复制控制器的滚动更新,
i.e. you cannot bind a 水平荚自动缩放器 to a replication controller and do rolling update (e.g. using kubectl rolling-update
).
这样做的原因’所做的工作是,滚动更新创建新的复制控制器时,
Horizontal Pod Autoscaler将不会绑定到新的复制控制器。
使用Horizontal Pod Autoscaler管理一组副本的规模时, 副本的数量可能会由于 评估指标的动态性质。有时称为 撞 .
从v1.6开始,集群运营商可以通过调整来减轻此问题
the global HPA settings exposed as flags for the kube-controller-manager
component:
从v1.12开始,新的算法更新消除了对 upscale delay.
--horizontal-pod-autoscaler-downscale-stabilization
:此选项的值为a
持续时间,指定自动定标器必须等待多长时间
当前操作完成后,即可执行降级操作。
The default value is 5 minutes (5m0s
).笔记: 调整这些参数值时,集群操作员应了解可能的情况。 结果。如果延迟(冷却时间)值设置得太长,则可能会有投诉 Horizontal Pod Autoscaler无法响应工作负载更改。但是,如果 延迟值设置得太短,副本集的规模可能会持续波动,因为 usual.
Kubernetes 1.6 adds support for scaling based on multiple metrics. You can use the autoscaling/v2beta2
API
版本以指定要扩展的水平Pod自动缩放器的多个指标。然后,水平吊舱
Autoscaler控制器将评估每个指标,并根据该指标提出新的指标。最大的
建议的量表将用作新的量表。
笔记: Kubernetes 1.2添加了alpha支持,可使用特殊注释基于特定于应用程序的指标进行缩放。 为了支持新的自动缩放API,在Kubernetes 1.6中删除了对这些注释的支持。而旧的收集方法 自定义指标仍然可用,这些指标将无法用于Horizontal Pod Autoscaler,而前者 Horizontal Pod Autoscaler控制器不再支持用于指定要缩放的自定义指标的注释。
Kubernetes 1.6增加了对在Horizontal Pod Autoscaler中使用自定义指标的支持。
You can add custom metrics for the 水平荚自动缩放器 to use in the autoscaling/v2beta2
API.
然后Kubernetes查询新的自定义指标API,以获取适当的自定义指标的值。
看到 支持指标API 对于要求。
默认情况下,HorizontalPodAutoscaler控制器从一系列API中检索指标。为了使其能够访问这些 API,集群管理员必须确保:
这 API汇聚层 is enabled.
相应的API已注册:
For resource metrics, this is the metrics.k8s.io
API, generally provided by 指标服务器 .
它可以作为集群附件启动。
For custom metrics, this is the custom.metrics.k8s.io
API. It’s provided by “adapter”指标解决方案供应商提供的API服务器。
检查指标管道,或者 已知解决方案列表.
如果您想自己写,请查看 样板 to get started.
For external metrics, this is the external.metrics.k8s.io
API. It may be provided by the custom metrics adapters provided above.
这 --horizontal-pod-autoscaler-use-rest-clients
is true
或者 unset. Setting this to false switches to Heapster-based autoscaling, which is deprecated.
有关这些不同度量标准路径以及它们如何不同的更多信息,请参见有关的设计建议。 HPA V2 , custom.metrics.k8s.io 和 external.metrics.k8s.io.
有关如何使用它们的示例,请参见 使用自定义指标的演练 和 使用外部指标的演练.
此页面是否有帮助?
感谢您的反馈。如果您对如何使用Kubernetes有一个特定的,可回答的问题,请在 Stack Overflow. 如果您想在GitHub仓库中打开一个问题 report a problem or 建议改善.