|
| 1 | +--- |
| 2 | +layout: blog |
| 3 | +title: "Kubernetes 1.27:HorizontalPodAutoscaler ContainerResource 类型指标进阶至 Beta" |
| 4 | +date: 2023-05-02T12:00:00+0800 |
| 5 | +slug: hpa-container-resource-metric |
| 6 | +--- |
| 7 | +<!-- |
| 8 | +layout: blog |
| 9 | +title: "Kubernetes 1.27: HorizontalPodAutoscaler ContainerResource type metric moves to beta" |
| 10 | +date: 2023-05-02T12:00:00+0800 |
| 11 | +slug: hpa-container-resource-metric |
| 12 | +--> |
| 13 | + |
| 14 | +<!-- |
| 15 | +**Author:** [Kensei Nakada](https://github.com/sanposhiho) (Mercari) |
| 16 | +--> |
| 17 | +**作者:** [Kensei Nakada](https://github.com/sanposhiho) (Mercari) |
| 18 | + |
| 19 | +**译者:** [Michael Yao](https://github.com/windsonsea) (DaoCloud) |
| 20 | + |
| 21 | +<!-- |
| 22 | +Kubernetes 1.20 introduced the [`ContainerResource` type metric](/docs/tasks/run-application/horizontal-pod-autoscale/#container-resource-metrics) |
| 23 | +in HorizontalPodAutoscaler (HPA). |
| 24 | +
|
| 25 | +In Kubernetes 1.27, this feature moves to beta and the corresponding feature gate (`HPAContainerMetrics`) gets enabled by default. |
| 26 | +--> |
| 27 | +Kubernetes 1.20 在 HorizontalPodAutoscaler (HPA) 中引入了 |
| 28 | +[`ContainerResource` 类型指标](/zh-cn/docs/tasks/run-application/horizontal-pod-autoscale/#container-resource-metrics)。 |
| 29 | + |
| 30 | +在 Kubernetes 1.27 中,此特性进阶至 Beta,相应的特性门控 (`HPAContainerMetrics`) 默认被启用。 |
| 31 | + |
| 32 | +<!-- |
| 33 | +## What is the ContainerResource type metric |
| 34 | +
|
| 35 | +The ContainerResource type metric allows us to configure the autoscaling based on resource usage of individual containers. |
| 36 | +
|
| 37 | +In the following example, the HPA controller scales the target |
| 38 | +so that the average utilization of the cpu in the application container of all the pods is around 60%. |
| 39 | +(See [the algorithm details](/docs/tasks/run-application/horizontal-pod-autoscale/#algorithm-details) |
| 40 | +to know how the desired replica number is calculated exactly) |
| 41 | +--> |
| 42 | +## 什么是 ContainerResource 类型指标 |
| 43 | + |
| 44 | +ContainerResource 类型指标允许我们根据各个容器的资源使用量来配置自动扩缩。 |
| 45 | + |
| 46 | +在下面的示例中,HPA 控制器扩缩目标,以便所有 Pod 的应用程序容器的 CPU 平均利用率约为 60% |
| 47 | +(请参见[算法详情](/zh-cn/docs/tasks/run-application/horizontal-pod-autoscale/#algorithm-details)以了解预期副本数的确切计算方式)。 |
| 48 | + |
| 49 | +```yaml |
| 50 | +type: ContainerResource |
| 51 | +containerResource: |
| 52 | + name: cpu |
| 53 | + container: application |
| 54 | + target: |
| 55 | + type: Utilization |
| 56 | + averageUtilization: 60 |
| 57 | +``` |
| 58 | +
|
| 59 | +<!-- |
| 60 | +## The difference from the Resource type metric |
| 61 | +
|
| 62 | +HPA already had a [Resource type metric](/docs/tasks/run-application/horizontal-pod-autoscale/#support-for-resource-metrics). |
| 63 | +
|
| 64 | +You can define the target resource utilization like the following, |
| 65 | +and then HPA will scale up/down the replicas based on the current utilization. |
| 66 | +--> |
| 67 | +## 与 Resource 类型指标的区别 |
| 68 | +
|
| 69 | +HPA 已具有 [Resource 类型指标](/zh-cn/docs/tasks/run-application/horizontal-pod-autoscale/#support-for-resource-metrics)。 |
| 70 | +
|
| 71 | +你可以定义如下的目标资源利用率,然后 HPA 将基于当前利用率扩缩副本。 |
| 72 | +
|
| 73 | +```yaml |
| 74 | +type: Resource |
| 75 | +resource: |
| 76 | + name: cpu |
| 77 | + target: |
| 78 | + type: Utilization |
| 79 | + averageUtilization: 60 |
| 80 | +``` |
| 81 | +
|
| 82 | +<!-- |
| 83 | +But, this Resource type metric refers to the average utilization of the **Pods**. |
| 84 | +
|
| 85 | +In case a Pod has multiple containers, the utilization calculation would be: |
| 86 | +--> |
| 87 | +但这个 Resource 类型指标指的是 **Pod** 的平均利用率。 |
| 88 | +
|
| 89 | +如果一个 Pod 有多个容器,则利用率计算公式为: |
| 90 | +
|
| 91 | +<!-- |
| 92 | +``` |
| 93 | +sum{the resource usage of each container} / sum{the resource request of each container} |
| 94 | +``` |
| 95 | +--> |
| 96 | +``` |
| 97 | +sum{每个容器的资源使用量} / sum{每个容器的资源请求} |
| 98 | +``` |
| 99 | + |
| 100 | +<!-- |
| 101 | +The resource utilization of each container may not have a direct correlation or may grow at different rates as the load changes. |
| 102 | +--> |
| 103 | +每个容器的资源利用率可能没有直接关系,或可能随着负载变化而以不同的速度增长。 |
| 104 | + |
| 105 | +<!-- |
| 106 | +For example: |
| 107 | +- A sidecar container is only providing an auxiliary service such as log shipping. |
| 108 | + If the application does not log very frequently or does not produce logs in its hotpath |
| 109 | + then the usage of the log shipper will not grow. |
| 110 | +- A sidecar container which provides authentication. Due to heavy caching |
| 111 | + the usage will only increase slightly when the load on the main container increases. |
| 112 | + In the current blended usage calculation approach this usually results in |
| 113 | + the HPA not scaling up the deployment because the blended usage is still low. |
| 114 | +- A sidecar may be injected without resources set which prevents scaling |
| 115 | + based on utilization. In the current logic the HPA controller can only scale |
| 116 | + on absolute resource usage of the pod when the resource requests are not set. |
| 117 | +--> |
| 118 | +例如: |
| 119 | + |
| 120 | +- 边车容器仅提供日志传输这类辅助服务。 |
| 121 | + 如果应用程序不经常记录日志或在其频繁执行的路径中不生成日志,则日志发送器的使用量不会增长。 |
| 122 | +- 提供身份验证的边车容器。由于重度缓存,当主要容器的负载增加时,使用量只会略微增加。 |
| 123 | + 在当前的混合用量计算方法中,这通常导致 HPA 不会对 Deployment 向上扩容,因为混合的使用量仍然很低。 |
| 124 | +- 边车可能在未设置资源的情况下被注入,这会阻止基于利用率进行扩缩。 |
| 125 | + 在当前的逻辑中,当未设置资源请求时,HPA 控制器只能根据 Pod 的绝对资源使用量进行扩缩。 |
| 126 | + |
| 127 | +<!-- |
| 128 | +And, in such case, if only one container's resource utilization goes high, |
| 129 | +the Resource type metric may not suggest scaling up. |
| 130 | +
|
| 131 | +So, for the accurate autoscaling, you may want to use the ContainerResource type metric for such Pods instead. |
| 132 | +--> |
| 133 | +在这种情况下,如果仅有一个容器的资源利用率增加,则 Resource 类型指标可能不会建议扩容。 |
| 134 | + |
| 135 | +因此,为了实现准确的自动扩缩,你可能需要改为使用 ContainerResource 类型指标来替代这些 Pod。 |
| 136 | + |
| 137 | +<!-- |
| 138 | +## What's new for the beta? |
| 139 | +
|
| 140 | +For Kubernetes v1.27, the ContainerResource type metric is available by default as described at the beginning |
| 141 | +of this article. |
| 142 | +(You can still disable it by the `HPAContainerMetrics` feature gate.) |
| 143 | +--> |
| 144 | +## Beta 版本有哪些新内容? |
| 145 | + |
| 146 | +在 Kubernetes v1.27 中,正如本文开头所述,ContainerResource 类型指标默认可用。 |
| 147 | +(你仍然可以通过 `HPAContainerMetrics` 特性门禁用它。) |
| 148 | + |
| 149 | +<!-- |
| 150 | +Also, we've improved the observability of HPA controller by exposing some metrics from the kube-controller-manager: |
| 151 | +- `metric_computation_total`: Number of metric computations. |
| 152 | +- `metric_computation_duration_seconds`: The time that the HPA controller takes to calculate one metric. |
| 153 | +- `reconciliations_total`: Number of reconciliation of HPA controller. |
| 154 | +- `reconciliation_duration_seconds`: The time that the HPA controller takes to reconcile a HPA object once. |
| 155 | +--> |
| 156 | +另外,我们已通过从 kube-controller-manager 中公开一些指标来改进 HPA 控制器的可观测性: |
| 157 | + |
| 158 | +- `metric_computation_total`:指标计算的数量。 |
| 159 | +- `metric_computation_duration_seconds`:HPA 控制器计算一个指标所需的时间。 |
| 160 | +- `reconciliations_total`:HPA 控制器的协调次数。 |
| 161 | +- `reconciliation_duration_seconds`:HPA 控制器协调一次 HPA 对象所需的时间。 |
| 162 | + |
| 163 | +<!-- |
| 164 | +These metrics have labels `action` (`scale_up`, `scale_down`, `none`) and `error` (`spec`, `internal`, `none`). |
| 165 | +And, in addition to them, the first two metrics have the `metric_type` label |
| 166 | +which corresponds to `.spec.metrics[*].type` for a HorizontalPodAutoscaler. |
| 167 | +--> |
| 168 | +这些指标具有 `action`(`scale_up`、`scale_down`、`none`)和 |
| 169 | +`error`(`spec`、`internal`、`none`)标签。 |
| 170 | +除此之外,前两个指标还具有 `metric_type` 标签,该标签对应于 |
| 171 | +HorizontalPodAutoscaler 的 `.spec.metrics[*].type`。 |
| 172 | + |
| 173 | +<!-- |
| 174 | +All metrics are useful for general monitoring of HPA controller, |
| 175 | +you can get deeper insight into which part has a problem, where it takes time, how much scaling tends to happen at which time on your cluster etc. |
| 176 | +--> |
| 177 | +所有指标都可用于 HPA 控制器的常规监控,你可以深入洞察哪部分存在问题,在哪里耗时, |
| 178 | +集群在哪个时间倾向于发生多少次扩缩等问题。 |
| 179 | + |
| 180 | +<!-- |
| 181 | +Another minor stuff, we've changed the `SuccessfulRescale` event's messages |
| 182 | +so that everyone can check whether the events came from the resource metric or |
| 183 | +the container resource metric (See [the related PR](https://github.com/kubernetes/kubernetes/pull/116045)). |
| 184 | +--> |
| 185 | +另一件小事是,我们已更改了 `SuccessfulRescale` 事件的消息, |
| 186 | +这样每个人都可以检查事件是否来自资源指标或容器资源指标 |
| 187 | +(请参见[相关 PR](https://github.com/kubernetes/kubernetes/pull/116045))。 |
| 188 | + |
| 189 | +<!-- |
| 190 | +## Getting involved |
| 191 | +
|
| 192 | +This feature is managed by [SIG Autoscaling](https://github.com/kubernetes/community/tree/master/sig-autoscaling). |
| 193 | +Please join us and share your feedback. We look forward to hearing from you! |
| 194 | +--> |
| 195 | +## 参与其中 |
| 196 | + |
| 197 | +此特性由 [SIG Autoscaling](https://github.com/kubernetes/community/tree/master/sig-autoscaling) |
| 198 | +进行管理。请加入我们分享反馈。我们期待聆听你的声音! |
| 199 | + |
| 200 | +<!-- |
| 201 | +## How can I learn more? |
| 202 | +
|
| 203 | +- [The official document of the ContainerResource type metric](/docs/tasks/run-application/horizontal-pod-autoscale/#container-resource-metrics) |
| 204 | +- [KEP-1610: Container Resource based Autoscaling](https://github.com/kubernetes/enhancements/tree/master/keps/sig-autoscaling/1610-container-resource-autoscaling) |
| 205 | +--> |
| 206 | +## 了解更多 |
| 207 | + |
| 208 | +- [ContainerResource 类型指标的正式文档](/zh-cn/docs/tasks/run-application/horizontal-pod-autoscale/#container-resource-metrics) |
| 209 | +- [KEP-1610:Container Resource based Autoscaling(基于容器资源的自动扩缩)](https://github.com/kubernetes/enhancements/tree/master/keps/sig-autoscaling/1610-container-resource-autoscaling) |
0 commit comments