|
| 1 | +--- |
| 2 | +title: 自动扩缩工作负载 |
| 3 | +description: >- |
| 4 | + 通过自动扩缩,你可以用某种方式自动更新你的工作负载。在面对资源需求变化的时候可以使你的集群更灵活、更高效。 |
| 5 | +content_type: concept |
| 6 | +weight: 40 |
| 7 | +--- |
| 8 | +<!-- |
| 9 | +title: Autoscaling Workloads |
| 10 | +description: >- |
| 11 | + With autoscaling, you can automatically update your workloads in one way or another. This allows your cluster to react to changes in resource demand more elastically and efficiently. |
| 12 | +content_type: concept |
| 13 | +weight: 40 |
| 14 | +--> |
| 15 | + |
| 16 | +<!-- overview --> |
| 17 | + |
| 18 | +<!-- |
| 19 | +In Kubernetes, you can _scale_ a workload depending on the current demand of resources. |
| 20 | +This allows your cluster to react to changes in resource demand more elastically and efficiently. |
| 21 | +
|
| 22 | +When you scale a workload, you can either increase or decrease the number of replicas managed by |
| 23 | +the workload, or adjust the resources available to the replicas in-place. |
| 24 | +
|
| 25 | +The first approach is referred to as _horizontal scaling_, while the second is referred to as |
| 26 | +_vertical scaling_. |
| 27 | +
|
| 28 | +There are manual and automatic ways to scale your workloads, depending on your use case. |
| 29 | +--> |
| 30 | +在 Kubernetes 中,你可以根据当前的资源需求**扩缩**工作负载。 |
| 31 | +这让你的集群可以更灵活、更高效地面对资源需求的变化。 |
| 32 | + |
| 33 | +当你扩缩工作负载时,你可以增加或减少工作负载所管理的副本数量,或者就地调整副本的可用资源。 |
| 34 | + |
| 35 | +第一种手段称为**水平扩缩**,第二种称为**垂直扩缩**。 |
| 36 | + |
| 37 | +扩缩工作负载有手动和自动两种方式,这取决于你的使用情况。 |
| 38 | + |
| 39 | +<!-- body --> |
| 40 | + |
| 41 | +<!-- |
| 42 | +## Scaling workloads manually |
| 43 | +--> |
| 44 | +## 手动扩缩工作负载 {#scaling-workloads-manually} |
| 45 | + |
| 46 | +<!-- |
| 47 | +Kubernetes supports _manual scaling_ of workloads. Horizontal scaling can be done |
| 48 | +using the `kubectl` CLI. |
| 49 | +For vertical scaling, you need to _patch_ the resource definition of your workload. |
| 50 | +
|
| 51 | +See below for examples of both strategies. |
| 52 | +--> |
| 53 | +Kubernetes 支持工作负载的手动扩缩。水平扩缩可以使用 `kubectl` 命令行工具完成。 |
| 54 | +对于垂直扩缩,你需要**更新**工作负载的资源定义。 |
| 55 | + |
| 56 | +这两种策略的示例见下文。 |
| 57 | + |
| 58 | +<!-- |
| 59 | +- **Horizontal scaling**: [Running multiple instances of your app](/docs/tutorials/kubernetes-basics/scale/scale-intro/) |
| 60 | +- **Vertical scaling**: [Resizing CPU and memory resources assigned to containers](/docs/tasks/configure-pod-container/resize-container-resources) |
| 61 | +--> |
| 62 | +- **水平扩缩**:[运行应用程序的多个实例](/docs/tutorials/kubernetes-basics/scale/scale-intro/) |
| 63 | +- **垂直扩缩**:[调整分配给容器的 CPU 和内存资源](/docs/tasks/configure-pod-container/resize-container-resources) |
| 64 | + |
| 65 | +<!-- |
| 66 | +## Scaling workloads automatically |
| 67 | +--> |
| 68 | +## 自动扩缩工作负载 {#scaling-workloads-automatically} |
| 69 | + |
| 70 | +<!-- |
| 71 | +Kubernetes also supports _automatic scaling_ of workloads, which is the focus of this page. |
| 72 | +--> |
| 73 | +Kubernetes 也支持工作负载的**自动扩缩**,这也是本页的重点。 |
| 74 | + |
| 75 | +<!-- |
| 76 | +The concept of _Autoscaling_ in Kubernetes refers to the ability to automatically update an |
| 77 | +object that manages a set of Pods (for example a |
| 78 | +{{< glossary_tooltip text="Deployment" term_id="deployment" >}}. |
| 79 | +--> |
| 80 | +在 Kubernetes 中**自动扩缩**的概念是指自动更新管理一组 Pod 的能力(例如 |
| 81 | +{{< glossary_tooltip text="Deployment" term_id="deployment" >}})。 |
| 82 | + |
| 83 | +<!-- |
| 84 | +### Scaling workloads horizontally |
| 85 | +--> |
| 86 | +### 水平扩缩工作负载 {#scaling-workloads-horizontally} |
| 87 | + |
| 88 | +<!-- |
| 89 | +In Kubernetes, you can automatically scale a workload horizontally using a _HorizontalPodAutoscaler_ (HPA). |
| 90 | +--> |
| 91 | +在 Kubernetes 中,你可以使用 HorizontalPodAutoscaler (HPA) 实现工作负载的自动水平扩缩。 |
| 92 | + |
| 93 | +<!-- |
| 94 | +It is implemented as a Kubernetes API resource and a {{< glossary_tooltip text="controller" term_id="controller" >}} |
| 95 | +and periodically adjusts the number of {{< glossary_tooltip text="replicas" term_id="replica" >}} |
| 96 | +in a workload to match observed resource utilization such as CPU or memory usage. |
| 97 | +--> |
| 98 | +它以 Kubernetes API 资源和{{< glossary_tooltip text="控制器" term_id="controller" >}}的方式实现, |
| 99 | +并定期调整工作负载中{{< glossary_tooltip text="副本" term_id="replica" >}}的数量 |
| 100 | +以满足设置的资源利用率,如 CPU 或内存利用率。 |
| 101 | + |
| 102 | +<!-- |
| 103 | +There is a [walkthrough tutorial](/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough) of configuring a HorizontalPodAutoscaler for a Deployment. |
| 104 | +--> |
| 105 | +这是一个为 Deployment 部署配置 HorizontalPodAutoscaler 的[示例教程](/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough)。 |
| 106 | + |
| 107 | +<!-- |
| 108 | +### Scaling workloads vertically |
| 109 | +--> |
| 110 | +### 垂直扩缩工作负载 {#scaling-workloads-vertically} |
| 111 | + |
| 112 | +{{< feature-state for_k8s_version="v1.25" state="stable" >}} |
| 113 | + |
| 114 | +<!-- |
| 115 | +You can automatically scale a workload vertically using a _VerticalPodAutoscaler_ (VPA). |
| 116 | +Different to the HPA, the VPA doesn't come with Kubernetes by default, but is a separate project |
| 117 | +that can be found [on GitHub](https://github.com/kubernetes/autoscaler/tree/9f87b78df0f1d6e142234bb32e8acbd71295585a/vertical-pod-autoscaler). |
| 118 | +--> |
| 119 | +你可以使用 VerticalPodAutoscaler (VPA) 实现工作负载的垂直扩缩。 |
| 120 | +不同于 HPA,VPA 并非默认来源于 Kubernetes,而是一个独立的项目, |
| 121 | +参见 [on GitHub](https://github.com/kubernetes/autoscaler/tree/9f87b78df0f1d6e142234bb32e8acbd71295585a/vertical-pod-autoscaler)。 |
| 122 | + |
| 123 | +<!-- |
| 124 | +Once installed, it allows you to create {{< glossary_tooltip text="CustomResourceDefinitions" term_id="customresourcedefinition" >}} |
| 125 | +(CRDs) for your workloads which define _how_ and _when_ to scale the resources of the managed replicas. |
| 126 | +--> |
| 127 | +安装后,你可以为工作负载创建 {{< glossary_tooltip text="CustomResourceDefinitions" term_id="customresourcedefinition" >}}(CRDs), |
| 128 | +定义**如何**以及**何时**扩缩被管理副本的资源。 |
| 129 | + |
| 130 | +{{< note >}} |
| 131 | +<!-- |
| 132 | +You will need to have the [Metrics Server](https://github.com/kubernetes-sigs/metrics-server) |
| 133 | +installed to your cluster for the HPA to work. |
| 134 | +--> |
| 135 | +你需要在集群中安装 [Metrics Server](https://github.com/kubernetes-sigs/metrics-server),这样,你的 HPA 才能正常工作。 |
| 136 | +{{< /note >}} |
| 137 | + |
| 138 | +<!-- |
| 139 | +At the moment, the VPA can operate in four different modes: |
| 140 | +--> |
| 141 | +目前,VPA 可以有四种不同的运行模式: |
| 142 | + |
| 143 | +<!-- |
| 144 | +{{< table caption="Different modes of the VPA" >}} |
| 145 | +Mode | Description |
| 146 | +:----|:----------- |
| 147 | +`Auto` | Currently `Recreate`, might change to in-place updates in the future |
| 148 | +`Recreate` | The VPA assigns resource requests on pod creation as well as updates them on existing pods by evicting them when the requested resources differ significantly from the new recommendation |
| 149 | +`Initial` | The VPA only assigns resource requests on pod creation and never changes them later. |
| 150 | +`Off` | The VPA does not automatically change the resource requirements of the pods. The recommendations are calculated and can be inspected in the VPA object. |
| 151 | +{{< /table >}} |
| 152 | +--> |
| 153 | +{{< table caption="VPA 的不同模式" >}} |
| 154 | +模式 | 描述 |
| 155 | +:----|:----------- |
| 156 | +`Auto` | 目前是 `Recreate`,将来可能改为就地更新 |
| 157 | +`Recreate` | VPA 会在创建 Pod 时分配资源请求,并且当请求的资源与新的建议值区别很大时通过驱逐 Pod 的方式来更新现存的 Pod |
| 158 | +`Initial` | VPA 只有在创建时分配资源请求,之后不做更改 |
| 159 | +`Off` | VPA 不会自动更改 Pod 的资源需求,建议值仍会计算并可在 VPA 对象中查看 |
| 160 | +{{< /table >}} |
| 161 | + |
| 162 | +<!-- |
| 163 | +#### Requirements for in-place resizing |
| 164 | +--> |
| 165 | +#### 就地调整的要求 |
| 166 | + |
| 167 | +{{< feature-state for_k8s_version="v1.27" state="alpha" >}} |
| 168 | + |
| 169 | +<!-- |
| 170 | +Resizing a workload in-place **without** restarting the {{< glossary_tooltip text="Pods" term_id="pod" >}} |
| 171 | +or its {{< glossary_tooltip text="Containers" term_id="container" >}} requires Kubernetes version 1.27 or later.<br /> |
| 172 | +Additionally, the `InPlaceVerticalScaling` feature gate needs to be enabled. |
| 173 | +--> |
| 174 | +在**不**重启 {{< glossary_tooltip text="Pod" term_id="pod" >}} 或其中{{< glossary_tooltip text="容器" term_id="container" >}}就地调整工作负载的情况下要求 Kubernetes 版本大于 1.27。 |
| 175 | +此外,特性门控 `InPlaceVerticalScaling` 需要开启。 |
| 176 | + |
| 177 | +{{< feature-gate-description name="InPlacePodVerticalScaling" >}} |
| 178 | + |
| 179 | +<!-- |
| 180 | +### Autoscaling based on cluster size |
| 181 | +--> |
| 182 | +### 根据集群规模自动扩缩 {#autoscaling-based-on-cluster-size} |
| 183 | + |
| 184 | +<!-- |
| 185 | +For workloads that need to be scaled based on the size of the cluster (for example |
| 186 | +`cluster-dns` or other system components), you can use the |
| 187 | +[_Cluster Proportional Autoscaler_](https://github.com/kubernetes-sigs/cluster-proportional-autoscaler).<br /> |
| 188 | +Just like the VPA, it is not part of the Kubernetes core, but hosted as its |
| 189 | +own project on GitHub. |
| 190 | +--> |
| 191 | +对于需要根据集群规模实现扩缩的工作负载(例如:`cluster-dns` 或者其他系统组件), |
| 192 | +你可以使用 [Cluster Proportional Autoscaler](https://github.com/kubernetes-sigs/cluster-proportional-autoscaler)。 |
| 193 | +与 VPA 一样,这个项目不是 Kubernetes 核心项目的一部分,它在 GitHub 上有自己的项目。 |
| 194 | + |
| 195 | +<!-- |
| 196 | +The Cluster Proportional Autoscaler watches the number of schedulable {{< glossary_tooltip text="nodes" term_id="node" >}} |
| 197 | +and cores and scales the number of replicas of the target workload accordingly. |
| 198 | +--> |
| 199 | +集群弹性伸缩器 (Cluster Proportional Autoscaler) 会观测可调度 {{< glossary_tooltip text="节点" term_id="node" >}} 和 内核数量, |
| 200 | +并调整目标工作负载的副本数量。 |
| 201 | + |
| 202 | +<!-- |
| 203 | +If the number of replicas should stay the same, you can scale your workloads vertically according to the cluster size using |
| 204 | +the [_Cluster Proportional Vertical Autoscaler_](https://github.com/kubernetes-sigs/cluster-proportional-vertical-autoscaler). |
| 205 | +The project is **currently in beta** and can be found on GitHub. |
| 206 | +--> |
| 207 | +如果副本的数量需要保持一致,你可以使用 [Cluster Proportional Vertical Autoscaler](https://github.com/kubernetes-sigs/cluster-proportional-vertical-autoscaler) 来根据集群规模进行垂直扩缩。 |
| 208 | +这个项目目前处于 **beta** 阶段,你可以在 GitHub 上找到它。 |
| 209 | + |
| 210 | +<!-- |
| 211 | +While the Cluster Proportional Autoscaler scales the number of replicas of a workload, the Cluster Proportional Vertical Autoscaler |
| 212 | +adjusts the resource requests for a workload (for example a Deployment or DaemonSet) based on the number of nodes and/or cores |
| 213 | +in the cluster. |
| 214 | +--> |
| 215 | +集群弹性伸缩器会扩缩工作负载的副本数量,垂直集群弹性伸缩器 (Cluster Proportional Vertical Autoscaler) 会根据节点和/或核心的数量 |
| 216 | +调整工作负载的资源请求(例如 Deployment 和 DaemonSet)。 |
| 217 | + |
| 218 | +<!-- |
| 219 | +### Event driven Autoscaling |
| 220 | +--> |
| 221 | +### 事件驱动型自动扩缩 {#event-driven-autoscaling} |
| 222 | + |
| 223 | +<!-- |
| 224 | +It is also possible to scale workloads based on events, for example using the |
| 225 | +[_Kubernetes Event Driven Autoscaler_ (**KEDA**)](https://keda.sh/). |
| 226 | +--> |
| 227 | +通过事件驱动实现工作负载的扩缩也是可行的, |
| 228 | +例如使用 [Kubernetes Event Driven Autoscaler (**KEDA**)](https://keda.sh/)。 |
| 229 | + |
| 230 | +<!-- |
| 231 | +KEDA is a CNCF graduated enabling you to scale your workloads based on the number |
| 232 | +of events to be processed, for example the amount of messages in a queue. There exists |
| 233 | +a wide range of adapters for different event sources to choose from. |
| 234 | +--> |
| 235 | +KEDA 是 CNCF 的毕业项目,能让你根据要处理事件的数量对工作负载进行扩缩,例如队列中消息的数量。 |
| 236 | +有多种针对不同事件源的适配可供选择。 |
| 237 | + |
| 238 | +<!-- |
| 239 | +### Autoscaling based on schedules |
| 240 | +--> |
| 241 | +### 根据计划自动扩缩 {#autoscaling-based-on-schedules} |
| 242 | + |
| 243 | +<!-- |
| 244 | +Another strategy for scaling your workloads is to **schedule** the scaling operations, for example in order to |
| 245 | +reduce resource consumption during off-peak hours. |
| 246 | +--> |
| 247 | +扩缩工作负载的另一种策略是**计划**进行扩缩,例如在非高峰时段减少资源消耗。 |
| 248 | + |
| 249 | +<!-- |
| 250 | +Similar to event driven autoscaling, such behavior can be achieved using KEDA in conjunction with |
| 251 | +its [`Cron` scaler](https://keda.sh/docs/2.13/scalers/cron/). The `Cron` scaler allows you to define schedules |
| 252 | +(and time zones) for scaling your workloads in or out. |
| 253 | +--> |
| 254 | +与事件驱动型自动扩缩相似,这种行为可以使用 KEDA 和 [`Cron` scaler](https://keda.sh/docs/2.13/scalers/cron/) 实现。 |
| 255 | +你可以在计划扩缩器 (Cron scaler) 中定义计划来实现工作负载的横向扩缩。 |
| 256 | + |
| 257 | +<!-- |
| 258 | +## Scaling cluster infrastructure |
| 259 | +--> |
| 260 | +## 扩缩集群基础设施 {#scaling-cluster-infrastructure} |
| 261 | + |
| 262 | +<!-- |
| 263 | +If scaling workloads isn't enough to meet your needs, you can also scale your cluster infrastructure itself. |
| 264 | +--> |
| 265 | +如果扩缩工作负载无法满足你的需求,你也可以扩缩集群基础设施本身。 |
| 266 | + |
| 267 | +<!-- |
| 268 | +Scaling the cluster infrastructure normally means adding or removing {{< glossary_tooltip text="nodes" term_id="node" >}}. |
| 269 | +This can be done using one of two available autoscalers: |
| 270 | +--> |
| 271 | +扩缩集群基础设施通常是指增加或移除{{< glossary_tooltip text="节点" term_id="node" >}}。 |
| 272 | +这可以通过以下两种自动扩缩器中的任意一种实现: |
| 273 | + |
| 274 | +- [**Cluster Autoscaler**](https://github.com/kubernetes/autoscaler/tree/master/cluster-autoscaler) |
| 275 | +- [**Karpenter**](https://github.com/kubernetes-sigs/karpenter?tab=readme-ov-file) |
| 276 | + |
| 277 | +<!-- |
| 278 | +Both scalers work by watching for pods marked as _unschedulable_ or _underutilized_ nodes and then adding or |
| 279 | +removing nodes as needed. |
| 280 | +--> |
| 281 | +这两种扩缩器的工作原理都是通过监测节点上被标记为 **unschedulable** 或 **underutilized** 的 Pod 数量, |
| 282 | +然后根据需要增加或移除节点。 |
| 283 | + |
| 284 | +## {{% heading "whatsnext" %}} |
| 285 | + |
| 286 | +<!-- |
| 287 | +- Learn more about scaling horizontally |
| 288 | + - [Scale a StatefulSet](/docs/tasks/run-application/scale-stateful-set/) |
| 289 | + - [HorizontalPodAutoscaler Walkthrough](/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/) |
| 290 | +- [Resize Container Resources In-Place](/docs/tasks/configure-pod-container/resize-container-resources/) |
| 291 | +- [Autoscale the DNS Service in a Cluster](/docs/tasks/administer-cluster/dns-horizontal-autoscaling/) |
| 292 | +--> |
| 293 | +- 了解有关横向扩缩的更多信息 |
| 294 | + - [扩缩 StatefulSet](/zh-cn/docs/tasks/run-application/scale-stateful-set/) |
| 295 | + - [HorizontalPodAutoscaler 演练](/zh-cn/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/) |
| 296 | +- [调整分配给容器的 CPU 和内存资源](/zh-cn/docs/tasks/configure-pod-container/resize-container-resources/) |
| 297 | +- [自动扩缩集群 DNS 服务](/zh-cn/docs/tasks/administer-cluster/dns-horizontal-autoscaling/) |
0 commit comments