# 自动扩缩工作负载

> 通过自动扩缩，你可以用某种方式自动更新你的工作负载。在面对资源需求变化的时候可以使你的集群更灵活、更高效。

---

LLMS index: [llms.txt](/llms.txt)

---

<!--
title: Autoscaling Workloads
description: >-
  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.
content_type: concept
weight: 50
-->

<!-- overview -->

<!--
In Kubernetes, you can _scale_ a workload depending on the current demand of resources.
This allows your cluster to react to changes in resource demand more elastically and efficiently.

When you scale a workload, you can either increase or decrease the number of replicas managed by
the workload, or adjust the resources available to the replicas in-place.

The first approach is referred to as _horizontal scaling_, while the second is referred to as
_vertical scaling_.

There are manual and automatic ways to scale your workloads, depending on your use case.
-->
在 Kubernetes 中，你可以根据当前的资源需求**扩缩**工作负载。
这让你的集群可以更灵活、更高效地面对资源需求的变化。

当你扩缩工作负载时，你可以增加或减少工作负载所管理的副本数量，或者就地调整副本的可用资源。

第一种手段称为**水平扩缩**，第二种称为**垂直扩缩**。

扩缩工作负载有手动和自动两种方式，这取决于你的使用情况。

<!-- body -->

<!--
## Scaling workloads manually
-->
## 手动扩缩工作负载   {#scaling-workloads-manually}

<!--
Kubernetes supports _manual scaling_ of workloads. Horizontal scaling can be done
using the `kubectl` CLI.
For vertical scaling, you need to _patch_ the resource definition of your workload.

See below for examples of both strategies.
-->
Kubernetes 支持工作负载的手动扩缩。水平扩缩可以使用 `kubectl` 命令行工具完成。
对于垂直扩缩，你需要**更新**工作负载的资源定义。

这两种策略的示例见下文。

<!--
- **Horizontal scaling**: [Running multiple instances of your app](/docs/tutorials/kubernetes-basics/scale/scale-intro/)
- **Vertical scaling**: [Resizing CPU and memory resources assigned to containers](/docs/tasks/configure-pod-container/resize-container-resources)
-->
- **水平扩缩**：[运行应用程序的多个实例](/zh-cn/docs/tutorials/kubernetes-basics/scale/scale-intro/)
- **垂直扩缩**：[调整分配给容器的 CPU 和内存资源](/zh-cn/docs/tasks/configure-pod-container/resize-container-resources)

<!--
## Scaling workloads automatically
-->
## 自动扩缩工作负载   {#scaling-workloads-automatically}

<!--
Kubernetes also supports _automatic scaling_ of workloads, which is the focus of this page.
-->
Kubernetes 也支持工作负载的**自动扩缩**，这也是本页的重点。

<!--
The concept of _Autoscaling_ in Kubernetes refers to the ability to automatically update an
object that manages a set of Pods (for example a
<a class='glossary-tooltip' title='管理集群上的多副本应用。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/concepts/workloads/controllers/deployment/' target='_blank' aria-label='Deployment'>Deployment</a>).
-->
在 Kubernetes 中**自动扩缩**的概念是指自动更新管理一组 Pod 的能力（例如
<a class='glossary-tooltip' title='管理集群上的多副本应用。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/concepts/workloads/controllers/deployment/' target='_blank' aria-label='Deployment'>Deployment</a>）。

<!--
### Scaling workloads horizontally
-->
### 水平扩缩工作负载   {#scaling-workloads-horizontally}

<!--
In Kubernetes, you can automatically scale a workload horizontally using a [HorizontalPodAutoscaler](/docs/concepts/workloads/autoscaling/horizontal-pod-autoscale/) (HPA).
-->
在 Kubernetes 中，你可以使用
[HorizontalPodAutoscaler](/zh-cn/docs/concepts/workloads/autoscaling/horizontal-pod-autoscale/)（HPA）
实现工作负载的自动水平扩缩。

<!--
It is implemented as a Kubernetes API resource and a <a class='glossary-tooltip' title='控制器通过 API 服务器监控集群的公共状态，并致力于将当前状态转变为期望的状态。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/concepts/architecture/controller/' target='_blank' aria-label='controller'>controller</a>
and periodically adjusts the number of <a class='glossary-tooltip' title='Replica 是 Pod 的副本，通过维护相同的实例确保可用性、可扩缩性和容错性。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/reference/glossary/?all=true#term-replica' target='_blank' aria-label='replicas'>replicas</a>
in a workload to match observed resource utilization such as CPU or memory usage.
-->
它以 Kubernetes API 资源和<a class='glossary-tooltip' title='控制器通过 API 服务器监控集群的公共状态，并致力于将当前状态转变为期望的状态。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/concepts/architecture/controller/' target='_blank' aria-label='控制器'>控制器</a>的方式实现，
并定期调整工作负载中<a class='glossary-tooltip' title='Replica 是 Pod 的副本，通过维护相同的实例确保可用性、可扩缩性和容错性。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/reference/glossary/?all=true#term-replica' target='_blank' aria-label='副本'>副本</a>的数量以满足设置的资源利用率，如
CPU 或内存利用率。

<!--
There is a [walkthrough tutorial](/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough) of configuring a HorizontalPodAutoscaler for a Deployment.
-->
这是一个为 Deployment 部署配置 HorizontalPodAutoscaler
的[示例教程](/zh-cn/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough)。

<!--
### Scaling workloads vertically
-->
### 垂直扩缩工作负载   {#scaling-workloads-vertically}








  <div class="feature-state-notice feature-stable">
      <span class="feature-state-name">特性状态：</span>
      <code>Kubernetes v1.25 [stable]</code>
    </div>
  



<!--
You can automatically scale a workload vertically using a [VerticalPodAutoscaler](/docs/concepts/workloads/autoscaling/vertical-pod-autoscale/) (VPA).
Unlike the HPA, the VPA doesn't come with Kubernetes by default, but is a an add-on that you or a cluster administrator may need to deploy before you can use it.
-->
你可以使用
[VerticalPodAutoscaler](/zh-cn/docs/concepts/workloads/autoscaling/vertical-pod-autoscale/)（VPA）
实现工作负载的垂直扩缩。
不同于 HPA，VPA 并非默认包含于 Kubernetes，而是一个附加组件，
你或集群管理员可能需要在使用它之前部署该组件。

<!--
Once installed, it allows you to create <a class='glossary-tooltip' title='通过定制化的代码给你的 Kubernetes API 服务器增加资源对象，而无需编译完整的定制 API 服务器。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/' target='_blank' aria-label='CustomResourceDefinitions'>CustomResourceDefinitions</a>
(CRDs) for your workloads which define _how_ and _when_ to scale the resources of the managed replicas.
-->
安装后，你可以为工作负载创建 <a class='glossary-tooltip' title='通过定制化的代码给你的 Kubernetes API 服务器增加资源对象，而无需编译完整的定制 API 服务器。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/' target='_blank' aria-label='CustomResourceDefinitions'>CustomResourceDefinitions</a> (CRD)，
定义**如何**以及**何时**扩缩被管理副本的资源。


<div class="alert alert-info" role="note"><h4 class="alert-heading">说明：</h4><!--
You will need to have the [Metrics Server](https://github.com/kubernetes-sigs/metrics-server)
installed to your cluster for the VPA to work.
-->
<p>你需要在集群中安装 <a href="https://github.com/kubernetes-sigs/metrics-server">Metrics Server</a>，这样，你的 VPA 才能正常工作。</p></div>


<!--
#### In-place pod vertical scaling
-->
#### 就地 Pod 垂直扩缩容   {#in-place-pod-vertical-scaling}








  <div class="feature-state-notice feature-stable" title="特性门控： InPlacePodVerticalScaling">
              <span class="feature-state-name">特性状态：</span> 
              <code>Kubernetes v1.35 [stable]</code>（默认启用）</div>


<!--
As of Kubernetes 1.36, VPA does not support resizing pods in-place,
but this integration is being worked on.
For manually resizing pods in-place, see [Resize Container Resources In-Place](/docs/tasks/configure-pod-container/resize-container-resources/).
-->
截至 Kubernetes 1.36，VPA（垂直 Pod 自动伸缩）尚不支持就地调整 Pod 大小，但该集成正在开发中。
如需手动进行就地扩缩容，请参阅[就地调整容器资源](/zh-cn/docs/tasks/configure-pod-container/resize-container-resources/)。

<!--
### Autoscaling based on cluster size
-->
### 根据集群规模自动扩缩   {#autoscaling-based-on-cluster-size}

<!--
For workloads that need to be scaled based on the size of the cluster (for example
`cluster-dns` or other system components), you can use the
[_Cluster Proportional Autoscaler_](https://github.com/kubernetes-sigs/cluster-proportional-autoscaler).
Just like the VPA, it is not part of the Kubernetes core, but hosted as its
own project on GitHub.
-->
对于需要根据集群规模实现扩缩的工作负载（例如：`cluster-dns` 或者其他系统组件），
你可以使用 [Cluster Proportional Autoscaler](https://github.com/kubernetes-sigs/cluster-proportional-autoscaler)。
与 VPA 一样，这个项目不是 Kubernetes 核心项目的一部分，它在 GitHub 上有自己的项目。 

<!--
The Cluster Proportional Autoscaler watches the number of schedulable <a class='glossary-tooltip' title='Kubernetes 中的工作机器称作节点。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/concepts/architecture/nodes/' target='_blank' aria-label='nodes'>nodes</a>
and cores and scales the number of replicas of the target workload accordingly.
-->
集群弹性伸缩器（Cluster Proportional Autoscaler）会观测可调度<a class='glossary-tooltip' title='Kubernetes 中的工作机器称作节点。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/concepts/architecture/nodes/' target='_blank' aria-label='节点'>节点</a>和处理器核数量，
并调整目标工作负载的副本数量。

<!--
If the number of replicas should stay the same, you can scale your workloads vertically according to the cluster size using
the [_Cluster Proportional Vertical Autoscaler_](https://github.com/kubernetes-sigs/cluster-proportional-vertical-autoscaler).
The project is **currently in beta** and can be found on GitHub.
-->
如果副本的数量需要保持一致，
你可以使用 [Cluster Proportional Vertical Autoscaler](https://github.com/kubernetes-sigs/cluster-proportional-vertical-autoscaler)
来根据集群规模进行垂直扩缩。
这个项目目前处于 **Beta** 阶段，你可以在 GitHub 上找到它。

<!--
While the Cluster Proportional Autoscaler scales the number of replicas of a workload,
the Cluster Proportional Vertical Autoscaler adjusts the resource requests for a workload
(for example a Deployment or DaemonSet) based on the number of nodes and/or cores in the cluster.
-->
集群弹性伸缩器（Cluster Proportional Autoscaler）通过调整工作负载副本数量实现扩缩容，
而垂直集群弹性伸缩器 (Cluster Proportional Vertical Autoscaler) 则根据集群中的节点数和/或 CPU 核数，
调整工作负载（例如 Deployment 或 DaemonSet）的资源请求值。

<!--
### Event driven Autoscaling
-->
### 事件驱动型自动扩缩   {#event-driven-autoscaling}

<!--
It is also possible to scale workloads based on events, for example using the
[_Kubernetes Event Driven Autoscaler_ (**KEDA**)](https://keda.sh/).
-->
通过事件驱动实现工作负载的扩缩也是可行的，
例如使用 [Kubernetes Event Driven Autoscaler (**KEDA**)](https://keda.sh/)。

<!--
KEDA is a CNCF-graduated project enabling you to scale your workloads based on the number
of events to be processed, for example the amount of messages in a queue. There exists
a wide range of adapters for different event sources to choose from.
-->
KEDA 是 CNCF 的毕业项目，能让你根据要处理事件的数量对工作负载进行扩缩，例如队列中消息的数量。
有多种针对不同事件源的适配可供选择。

<!--
### Autoscaling based on schedules
-->
### 根据计划自动扩缩   {#autoscaling-based-on-schedules}

<!--
Another strategy for scaling your workloads is to **schedule** the scaling operations, for example in order to
reduce resource consumption during off-peak hours.
-->
扩缩工作负载的另一种策略是**计划**进行扩缩，例如在非高峰时段减少资源消耗。

<!--
Similar to event driven autoscaling, such behavior can be achieved using KEDA in conjunction with
its [`Cron` scaler](https://keda.sh/docs/latest/scalers/cron/).
The `Cron` scaler allows you to define schedules (and time zones) for scaling your workloads in or out.
-->
与事件驱动自动扩缩容类似，
这种行为可以使用 KEDA 和 [`Cron` scaler](https://keda.sh/docs/latest/scalers/cron/) 实现。
`Cron` 扩缩器允许你根据预设的时间表（以及时区）对工作负载进行扩容或缩容。

<!--
## Scaling cluster infrastructure
-->
## 扩缩集群基础设施   {#scaling-cluster-infrastructure}

<!--
If scaling workloads isn't enough to meet your needs, you can also scale your cluster infrastructure itself.
-->
如果扩缩工作负载无法满足你的需求，你也可以扩缩集群基础设施本身。

<!--
Scaling the cluster infrastructure normally means adding or removing <a class='glossary-tooltip' title='Kubernetes 中的工作机器称作节点。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/concepts/architecture/nodes/' target='_blank' aria-label='nodes'>nodes</a>.
-->
扩缩集群基础设施通常是指增加或移除<a class='glossary-tooltip' title='Kubernetes 中的工作机器称作节点。' data-bs-toggle='tooltip' data-bs-placement='top' href='/zh-cn/docs/concepts/architecture/nodes/' target='_blank' aria-label='节点'>节点</a>。

<!--
Read [Node autoscaling](/docs/concepts/cluster-administration/node-autoscaling/)
for more information.
-->
阅读[节点自动扩缩](/zh-cn/docs/concepts/cluster-administration/node-autoscaling/)了解更多信息。

## 接下来

<!--
- Learn more about scaling horizontally
  - [Scale a StatefulSet](/docs/tasks/run-application/scale-stateful-set/)
  - [HorizontalPodAutoscaler Walkthrough](/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/)
- [Resize Container Resources In-Place](/docs/tasks/configure-pod-container/resize-container-resources/)
- [Autoscale the DNS Service in a Cluster](/docs/tasks/administer-cluster/dns-horizontal-autoscaling/)
- Learn about [Node autoscaling](/docs/concepts/cluster-administration/node-autoscaling/)
-->
- 了解有关横向扩缩的更多信息
  - [扩缩 StatefulSet](/zh-cn/docs/tasks/run-application/scale-stateful-set/)
  - [HorizontalPodAutoscaler 演练](/zh-cn/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/)
- [调整分配给容器的 CPU 和内存资源](/zh-cn/docs/tasks/configure-pod-container/resize-container-resources/)
- [自动扩缩集群 DNS 服务](/zh-cn/docs/tasks/administer-cluster/dns-horizontal-autoscaling/)
- 了解[节点自动扩缩]((/zh-cn/docs/concepts/cluster-administration/node-autoscaling/))
