From 7c80ffc07fba7fba4d89d175dbb1a80af50143dc Mon Sep 17 00:00:00 2001 From: Hao Yuan Date: Wed, 4 Nov 2020 00:59:31 +0800 Subject: [PATCH] sync docs/tasks/run-application/horizontal-pod-autoscale.md --- .../horizontal-pod-autoscale.md | 315 ++++++++++++++++-- 1 file changed, 278 insertions(+), 37 deletions(-) diff --git a/content/zh/docs/tasks/run-application/horizontal-pod-autoscale.md b/content/zh/docs/tasks/run-application/horizontal-pod-autoscale.md index 91226eaa7c..37315df914 100644 --- a/content/zh/docs/tasks/run-application/horizontal-pod-autoscale.md +++ b/content/zh/docs/tasks/run-application/horizontal-pod-autoscale.md @@ -3,7 +3,7 @@ title: Pod 水平自动扩缩 feature: title: 水平扩缩 description: > - 使用一个简单的命令、一个UI或基于CPU使用情况自动对应用程序进行扩缩。 + 使用一个简单的命令、一个 UI 或基于 CPU 使用情况自动对应用程序进行扩缩。 content_type: concept weight: 90 @@ -12,14 +12,14 @@ weight: 90 Pod 水平自动扩缩(Horizontal Pod Autoscaler) -可以基于 CPU 利用率自动扩缩 ReplicationController、Deployment 和 ReplicaSet 中的 Pod 数量。 +可以基于 CPU 利用率自动扩缩 ReplicationController、Deployment、ReplicaSet 和 StatefulSet 中的 Pod 数量。 除了 CPU 利用率,也可以基于其他应程序提供的[自定义度量指标](https://git.k8s.io/community/contributors/design-proposals/instrumentation/custom-metrics-api.md) 来执行自动扩缩。 Pod 自动扩缩不适用于无法扩缩的对象,比如 DaemonSet。 @@ -61,12 +61,12 @@ or the custom metrics API (for all other metrics). * 对于按 Pod 统计的资源指标(如 CPU),控制器从资源指标 API 中获取每一个 @@ -76,8 +76,8 @@ or the custom metrics API (for all other metrics). 接下来,控制器根据平均的资源使用率或原始值计算出扩缩的比例,进而计算出目标副本数。 -* 如果pod 使用对象指标和外部指标(每个指标描述一个对象信息)。 +* 如果 Pod 使用对象指标和外部指标(每个指标描述一个对象信息)。 这个指标将直接根据目标设定值相比较,并生成一个上面提到的扩缩比例。 - 在 `autoscaling/v2beta2` 版本API中,这个指标也可以根据 Pod 数量平分后再计算。 + 在 `autoscaling/v2beta2` 版本 API 中,这个指标也可以根据 Pod 数量平分后再计算。 自动扩缩控制器使用 scale 子资源访问相应可支持扩缩的控制器(如副本控制器、 -Deployments 和 ReplicaSet)。 +Deployment 和 ReplicaSet)。 `scale` 是一个可以动态设定副本数量和检查当前状态的接口。 关于 scale 子资源的更多信息,请参考[这里](https://git.k8s.io/community/contributors/design-proposals/autoscaling/horizontal-pod-autoscaler.md#scale-subresource). @@ -182,7 +182,7 @@ metric across all Pods in the HorizontalPodAutoscaler's scale target. Before checking the tolerance and deciding on the final values, we take pod readiness and missing metrics into consideration, however. --> -如果 HorizontalPodAutoscaler 指定的是`targetAverageValue` 或 `targetAverageUtilization`, +如果 HorizontalPodAutoscaler 指定的是 `targetAverageValue` 或 `targetAverageUtilization`, 那么将会把指定 Pod 度量值的平均值做为 `currentMetricValue`。 然而,在检查容忍度和决定最终扩缩值前,我们仍然会把那些无法获取指标的 Pod 统计进去。 @@ -193,7 +193,7 @@ shut down) and all failed Pods are discarded. If a particular Pod is missing metrics, it is set aside for later; Pods with missing metrics will be used to adjust the final scaling amount. --> -所有被标记了删除时间戳(Pod 正在关闭过程中)的 Pod 和 失败的 Pod 都会被忽略。 +所有被标记了删除时间戳(Pod 正在关闭过程中)的 Pod 和失败的 Pod 都会被忽略。 如果某个 Pod 缺失度量值,它将会被搁置,只在最终确定扩缩数量时再考虑。 @@ -229,7 +229,7 @@ default is 5 minutes. The `currentMetricValue / desiredMetricValue` base scale ratio is then calculated using the remaining pods not set aside or discarded from above. --> -在排除掉被搁置的 Pod 后,扩缩比例就会根据`currentMetricValue/desiredMetricValue` +在排除掉被搁置的 Pod 后,扩缩比例就会根据 `currentMetricValue/desiredMetricValue` 计算出来。 如果创建 HorizontalPodAutoscaler 时指定了多个指标, -那么会按照每个指标分别计算扩缩副本数,取最大的进行扩缩。 -如果任何一个指标无法顺利的计算出扩缩副本数(比如,通过 API 获取指标时出错), -那么本次扩缩会被跳过。 +那么会按照每个指标分别计算扩缩副本数,取最大值进行扩缩。 +如果任何一个指标无法顺利地计算出扩缩副本数(比如,通过 API 获取指标时出错), +并且可获取的指标建议缩容,那么本次扩缩会被跳过。 +这表示,如果一个或多个指标给出的 `desiredReplicas` 值大于当前值,HPA 仍然能实现扩容。 @@ -321,13 +324,13 @@ API 的 beta 版本(`autoscaling/v2beta2`)引入了基于内存和自定义 创建 HorizontalPodAutoscaler 对象时,需要确保所给的名称是一个合法的 [DNS 子域名](/zh/docs/concepts/overview/working-with-objects/names#dns-subdomain-names)。 -有关 API 对象的更多信息,请查阅[HorizontalPodAutoscaler 对象设计文档](https://git.k8s.io/community/contributors/design-proposals/autoscaling/horizontal-pod-autoscaler.md#horizontalpodautoscaler-object)。 +有关 API 对象的更多信息,请查阅 +[HorizontalPodAutoscaler 对象设计文档](/zh/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#horizontalpodautoscaler-v1-autoscaling)。 -## 滚动升级时扩缩 {#autoscaling-during-roling-update} +## 滚动升级时扩缩 {#autoscaling-during-rolling-update} 目前在 Kubernetes 中,可以针对 ReplicationController 或 Deployment 执行 滚动更新,它们会为你管理底层副本数。 @@ -375,13 +377,12 @@ HPA 设置副本数量时,Deployment 会设置底层副本数。 通过直接操控副本控制器执行滚动升级时,HPA 不能工作, -也就是说你不能将 HPA 绑定到某个 RC 再执行滚动升级 -(例如使用 `kubectl rolling-update` 命令)。 +也就是说你不能将 HPA 绑定到某个 RC 再执行滚动升级。 HPA 不能工作的原因是它无法绑定到滚动更新时所新创建的副本控制器。 * 启用了 [API 聚合层](/zh/docs/tasks/extend-kubernetes/configure-aggregation-layer/) + * 相应的 API 已注册: * 对于资源指标,将使用 `metrics.k8s.io` API,一般由 [metrics-server](https://github.com/kubernetes-incubator/metrics-server) 提供。 它可以做为集群插件启动。 - * 对于自定义指标,将使用 `custom.metrics.k8s.io` API。 + + * 对于自定义指标,将使用 `custom.metrics.k8s.io` API。 它由其他度量指标方案厂商的“适配器(Adapter)” API 服务器提供。 确认你的指标流水线,或者查看[已知方案列表](https://github.com/kubernetes/metrics/blob/master/IMPLEMENTATIONS.md#custom-metrics-api)。 + 如果你想自己编写,请从 [boilerplate](https://github.com/kubernetes-sigs/custommetrics-apiserver)开始。 + * 对于外部指标,将使用 `external.metrics.k8s.io` API。可能由上面的自定义指标适配器提供。 + * `--horizontal-pod-autoscaler-use-rest-clients` 参数设置为 `true` 或者不设置。 如果设置为 false,则会切换到基于 Heapster 的自动扩缩,这个特性已经被弃用了。 @@ -533,6 +544,236 @@ and [the walkthrough for using external metrics](/docs/tasks/run-application/hor [使用自定义指标的教程](/zh/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/#autoscaling-on-multiple-metrics-and-custom-metrics) 和[使用外部指标的教程](/zh/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/#autoscaling-on-metrics-not-related-to-kubernetes-objects)。 + +## 支持可配置的扩缩 {#support-for-configurable-scaling-behaviour} + +从 [v1.18](https://github.com/kubernetes/enhancements/blob/master/keps/sig-autoscaling/20190307-configurable-scale-velocity-for-hpa.md) +开始,`v2beta2` API 允许通过 HPA 的 `behavior` 字段配置扩缩行为。 +在 `behavior` 字段中的 `scaleUp` 和 `scaleDown` 分别指定扩容和缩容行为。 +可以两个方向指定一个稳定窗口,以防止扩缩目标中副本数量的波动。 +类似地,指定扩缩策略可以控制扩缩时副本数的变化率。 + + +### 扩缩策略 {#scaling-policies} +在 spec 字段的 `behavior` 部分可以指定一个或多个扩缩策略。 +当指定多个策略时,默认选择允许更改最多的策略。 +下面的例子展示了缩容时的行为: + +```yaml +behavior: + scaleDown: + policies: + - type: Pods + value: 4 + periodSeconds: 60 + - type: Percent + value: 10 + periodSeconds: 60 +``` + + +当 Pod 数量超过 40 个时,第二个策略将用于缩容。 +例如,如果有 80 个副本,并且目标必须缩小到 10 个副本,那么在第一步中将减少 8 个副本。 +在下一轮迭代中,当副本的数量为 72 时,10% 的 Pod 数为 7.2,但是这个数字向上取整为 8。 +在 autoscaler 控制器的每个循环中,将根据当前副本的数量重新计算要更改的 Pod 数量。 +当副本数量低于 40 时,应用第一个策略 _(Pods)_ ,一次减少 4 个副本。 + + +`periodSeconds` 表示策略的时间长度必须保证有效。 +第一个策略允许在一分钟内最多缩小 4 个副本。 +第二个策略最多允许在一分钟内缩小当前副本的 10%。 + + +可以指定扩缩方向的 `selectPolicy` 字段来更改策略选择。 +通过设置 `Min` 的值,它将选择副本数变化最小的策略。 +将该值设置为 `Disabled` 将完全禁用该方向的缩放。 + + +### 稳定窗口 {#stabilization-window} + +当用于扩缩的指标持续抖动时,使用稳定窗口来限制副本数上下振动。 +自动扩缩算法使用稳定窗口来考虑过去计算的期望状态,以防止扩缩。 +在下面的例子中,稳定化窗口被指定为 `scaleDown`。 + +```yaml +scaleDown: + stabilizationWindowSeconds: 300 +``` + + +当指标显示目标应该缩容时,自动扩缩算法查看之前计算的期望状态,并使用指定时间间隔内的最大值。 +在上面的例子中,过去 5 分钟的所有期望状态都会被考虑。 + + +### 默认行为 {#default-behavior} + +要使用自定义扩缩,不必指定所有字段。 +只有需要自定义的字段才需要指定。 +这些自定义值与默认值合并。 +默认值与 HPA 算法中的现有行为匹配。 + +```yaml +behavior: + scaleDown: + stabilizationWindowSeconds: 300 + policies: + - type: Percent + value: 100 + periodSeconds: 15 + scaleUp: + stabilizationWindowSeconds: 0 + policies: + - type: Percent + value: 100 + periodSeconds: 15 + - type: Pods + value: 4 + periodSeconds: 15 + selectPolicy: Max +``` + + +用于缩小稳定窗口的时间为 _300_ 秒(或是 `--horizontal-pod-autoscaler-downscale-stabilization` 参数设定值)。 +只有一种缩容的策略,允许 100% 删除当前运行的副本,这意味着扩缩目标可以缩小到允许的最小副本数。 +对于扩容,没有稳定窗口。当指标显示目标应该扩容时,目标会立即扩容。 +这里有两种策略,每 15 秒添加 4 个 Pod 或 100% 当前运行的副本数,直到 HPA 达到稳定状态。 + + +### 示例:更改缩容稳定窗口 + +将下面的 behavior 配置添加到 HPA 中,可提供一个 1 分钟的自定义缩容稳定窗口: + +```yaml +behavior: + scaleDown: + stabilizationWindowSeconds: 60 +``` + + +### 示例:限制缩容速率 + +将下面的 behavior 配置添加到 HPA 中,可限制 Pod 被 HPA 删除速率为每分钟 10%: + +```yaml +behavior: + scaleDown: + policies: + - type: Percent + value: 10 + periodSeconds: 60 +``` + + +为了确保每分钟删除的 Pod 数不超过 5 个,可以添加第二个缩容策略,大小固定为 5,并将 `selectPolicy` 设置为最小值。 +将 `selectPolicy` 设置为 `Min` 意味着 autoscaler 会选择影响 Pod 数量最小的策略: + +```yaml +behavior: + scaleDown: + policies: + - type: Percent + value: 10 + periodSeconds: 60 + - type: Pods + value: 5 + periodSeconds: 60 + selectPolicy: Min +``` + + + +### 示例:禁用缩容 + +`selectPolicy` 的值 `Disabled` 会关闭对给定方向的缩容。 +因此使用以下策略,将会阻止缩容: + +```yaml +behavior: + scaleDown: + selectPolicy: Disabled +``` + + + ## {{% heading "whatsnext" %}}