# 自动伸缩

自动伸缩是一种云计算技术，它自动调整分配给应用程序的计算资源，使容量与工作负载相匹配，无需人工干预。调整可以是横向的，即增加或移除整个实例或副本；也可以是纵向的，即更改现有实例所附带的CPU、内存或磁盘；相关机制进一步分为反应式、主动式（预测式）或混合式。自动伸缩在基础设施层、平台层和软件层均可运作，而几乎所有的实现——无论是基于规则的还是基于机器学习的——都遵循相同的反馈控制结构。<sup>[1](https://pmc.ncbi.nlm.nih.gov/articles/PMC11398277/)</sup><sup> • </sup><sup>[2](https://www.mdpi.com/2297-8747/31/2/49)</sup>

| 关键事实 | 值 |
|---|---|
| 调整维度 | 横向（实例/副本数量）与纵向（每实例内存、CPU、磁盘）<sup>[1](https://pmc.ncbi.nlm.nih.gov/articles/PMC11398277/)</sup> |
| Kubernetes HPA控制循环 | 默认每15秒运行一次；desiredReplicas = ceil(currentReplicas × currentMetricValue / desiredMetricValue)<sup>[3](https://kubernetes.io/docs/concepts/workloads/autoscaling/horizontal-pod-autoscale/)</sup> |
| HPA保护机制（v1.1设计） | 重新伸缩后3分钟内阻止扩容、5分钟内阻止缩容，10%容差带<sup>[4](https://github.com/kubernetes/design-proposals-archive/blob/main/autoscaling/horizontal-pod-autoscaler.md)</sup> |
| AWS动态策略类型 | 目标跟踪、步进伸缩、带冷却时间的简单伸缩（默认300秒；冷却时间仅适用于简单伸缩）<sup>[5](https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-scale-based-on-demand.html)</sup><sup> • </sup><sup>[6](https://raw.githubusercontent.com/rickypinci/rickypinci.github.io/master/files/papers/2024_TSC_Quattrocchi.pdf)</sup> |
| 横向扩展延迟 | Pod：大多5–30秒；虚拟机：在主要云厂商中启动约需一分钟，视应用不同可达数分钟甚至更长<sup>[6](https://raw.githubusercontent.com/rickypinci/rickypinci.github.io/master/files/papers/2024_TSC_Quattrocchi.pdf)</sup><sup> • </sup><sup>[7](https://bogdanghit.github.io/publications/cloud-autoscalers.pdf)</sup> |
| 典型生产CPU目标值 | CPU目标型HPA约为30%，因为应用会过度配置以避免丢失流量<sup>[8](https://upcommons.upc.edu/server/api/core/bitstreams/34caab31-d883-4b17-a72d-43ed4e49346a/content)</sup> |
| 成本效果 | 与最优静态策略相比，MLscale将资源成本降低约41%<sup>[9](https://www3.cs.stonybrook.edu/~anshul/suscom17.pdf)</sup> |

## 工作原理

自动伸缩是一个反馈控制系统。大多数实现遵循MAPE-K循环：监控（Monitor）、分析（Analyze）、规划（Plan）、执行（Execute），外加一个共享的[知识库](https://www.edgepedia.org/zh/knowledge-base)（Knowledge）。<sup>[2](https://www.mdpi.com/2297-8747/31/2/49)</sup> 控制器定期读取CPU利用率、请求速率、队列深度或响应时间等指标，将其与目标值或阈值比较，计算出新容量并加以应用。

[Kubernetes](https://www.edgepedia.org/zh/kubernetes)横向Pod自动伸缩器（Horizontal Pod Autoscaler，HPA）可以说明其中的算术。它是一个间歇运行的控制循环，默认每15秒运行一次，并计算：

\[ \text{desiredReplicas} = \left\lceil \text{currentReplicas} \times \frac{\text{currentMetricValue}}{\text{desiredMetricValue}} \right\rceil \]

因此，观测指标相对于目标值翻倍，副本数也翻倍。<sup>[3](https://kubernetes.io/docs/concepts/workloads/autoscaling/horizontal-pod-autoscale/)</sup> 最初的设计使用相同的比例形式，\( \text{TargetNumOfPods} = \lceil \sum \text{CurrentPodsCPUUtilization} / \text{Target} \rceil \)，并选择相对（百分比）目标而非绝对目标，这样更改Pod资源请求不会使阈值失效。<sup>[4](https://github.com/kubernetes/design-proposals-archive/blob/main/autoscaling/horizontal-pod-autoscaler.md)</sup> 当配置了多个指标时，控制器取建议副本数最大的那个；当指标缺失或无效时，则跳过缩容。<sup>[3](https://kubernetes.io/docs/concepts/workloads/autoscaling/horizontal-pod-autoscale/)</sup>

稳定性是设计出来的，而非自动获得的。冷却时间和稳定窗口抑制振荡：v1.1版HPA设计在先前重新伸缩后3分钟内阻止扩容、5分钟内阻止缩容，并设有10%容差带，<sup>[4](https://github.com/kubernetes/design-proposals-archive/blob/main/autoscaling/horizontal-pod-autoscaler.md)</sup> 而当前的缩容稳定窗口默认为5分钟。<sup>[3](https://kubernetes.io/docs/concepts/workloads/autoscaling/horizontal-pod-autoscale/)</sup> AWS简单伸缩策略在每次操作后进入默认300秒的冷却空闲期，而目标跟踪和步进伸缩可以立即横向扩展，无需等待冷却。<sup>[6](https://raw.githubusercontent.com/rickypinci/rickypinci.github.io/master/files/papers/2024_TSC_Quattrocchi.pdf)</sup>

## 实现方式

在AWS EC2 Auto Scaling中，实例被组织为Auto Scaling组，由最小容量、期望容量和最大容量定义；伸缩策略在这些边界内启动或终止实例，健康检查会替换受损实例。<sup>[10](https://docs.aws.amazon.com/autoscaling/ec2/userguide/what-is-amazon-ec2-auto-scaling.html)</sup> 动态伸缩提供三种命名的策略类型：目标跟踪，它像恒温器保持温度一样将CloudWatch指标保持在目标值；步进伸缩，其调整幅度随告警越过阈值的程度而变化；以及简单伸缩，即单次调整后进入冷却。当多个策略同时触发时，产生最大容量变化的策略胜出。<sup>[5](https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-scale-based-on-demand.html)</sup>

在Kubernetes上，HPA通过最小和最大副本数、目标指标（资源、容器资源、自定义、对象或外部）以及稳定版autoscaling/v2 API中的可配置行为（包括稳定窗口、容差和缩容至零（minReplicas: 0））进行配置。<sup>[3](https://kubernetes.io/docs/concepts/workloads/autoscaling/horizontal-pod-autoscale/)</sup> 纵向伸缩由纵向Pod自动伸缩器（VPA）处理。在GKE上，Recreate模式下的VPA会驱逐Pod以更改资源请求，并设置防止内存不足的安全缓冲，通常为额外20%的内存或100 MB，并且在Auto或Recreate模式下通常仅在Pod至少存在24小时后才更新它们；它专为稳态合理配置而设计，突发峰值则建议使用HPA。<sup>[11](https://docs.cloud.google.com/kubernetes-engine/docs/concepts/verticalpodautoscaler)</sup> Kubernetes自v1.35起已支持原地纵向Pod调整作为稳定功能，不过截至Kubernetes 1.37，VPA尚不能原地调整Pod大小。<sup>[12](https://kubernetes.io/docs/concepts/workloads/autoscaling/)</sup> 通过KEDA（一个CNCF毕业项目）实现的事件驱动自动伸缩，可根据队列深度等外部事件源伸缩工作负载，并包含用于定时调度的Cron伸缩器；KEDA还可以作为外部指标源向HPA提供预测模型。<sup>[12](https://kubernetes.io/docs/concepts/workloads/autoscaling/)</sup><sup> • </sup><sup>[2](https://www.mdpi.com/2297-8747/31/2/49)</sup>

## 起源

多层互联网应用的动态供应由乌尔冈卡及其同事于2008年在《ACM自主与自适应系统汇刊》上正式提出；他们的敏捷供应框架是该领域赖以发展的早期范例。<sup>[13](https://doi.org/10.1145/1342171.1342172)</sup> 在产品方面，AWS于2009年5月18日推出了面向Amazon EC2的Amazon CloudWatch、Auto Scaling和Elastic Load Balancing的公开测试版，而EC2本身早在2006年就已问世。<sup>[14](https://aws.amazon.com/blogs/aws/aws-auto-scaling-unified-scaling-for-your-cloud-applications/)</sup> HorizontalPodAutoscaler允许自动伸缩Pod数量。<sup>[4](https://github.com/kubernetes/design-proposals-archive/blob/main/autoscaling/horizontal-pod-autoscaler.md)</sup> 后来的里程碑包括工作负载自动伸缩系统[Autopilot](https://www.edgepedia.org/zh/autopilot)<sup>[15](https://arxiv.org/pdf/2507.17128)</sup>，以及不确定性感知的预测式自动伸缩器MagicScaler，由潘及其同事于2023年发表在《VLDB基金会会刊》上。<sup>[16](https://doi.org/10.14778/3611540.3611566)</sup>

## 变体

**反应式（基于阈值的）伸缩**在监控指标越过配置阈值时调整容量；它无法预测未来行为，是商业系统中最广泛使用的方法。<sup>[1](https://pmc.ncbi.nlm.nih.gov/articles/PMC11398277/)</sup><sup> • </sup><sup>[17](https://www.mdpi.com/1424-8220/22/3/1221)</sup> **定时伸缩**为已知模式预先供应资源，但无法适应意外的负载变化。<sup>[18](https://www3.cs.stonybrook.edu/~anshul/courses/cse591_s16/autoscaling_survey.pdf)</sup> **预测式（基于预报的）伸缩**从过去的工作负载数据中学习；其性能取决于预测准确性。<sup>[1](https://pmc.ncbi.nlm.nih.gov/articles/PMC11398277/)</sup> EC2的预测式伸缩分析前14天的工作负载模式，并计算接下来2天所需的虚拟机分配。<sup>[6](https://raw.githubusercontent.com/rickypinci/rickypinci.github.io/master/files/papers/2024_TSC_Quattrocchi.pdf)</sup> **强化学习自动伸缩器**从惩罚QoS违规的奖励中学习策略。<sup>[2](https://www.mdpi.com/2297-8747/31/2/49)</sup> 混合设计结合反应式和主动式伸缩；一项对104篇论文的综述发现只有15篇结合了两者，其中一个结论认为反应式伸缩器应驱动扩容，而缩容应仅由主动式组件发起。<sup>[19](https://research.spec.org/icpe_proceedings/2022/proceedings/p105.pdf)</sup> 基于控制理论的设计，如夸特罗基及其同事于2024年在《IEEE服务计算汇刊》上描述的ScaleX，以0.6秒的SLA在SLA违规与资源使用的权衡上优于规则、步进和目标跟踪技术。<sup>[20](https://doi.org/10.1109/tsc.2024.3354062)</sup>

## 应用

大约自2018年以来，随着Kubernetes成为主导的编排平台，微服务自动伸缩从粗粒度转向服务级、依赖感知的策略。<sup>[15](https://arxiv.org/pdf/2507.17128)</sup> 它也适用于批处理和工作流工作负载；数据中心利用率低至6%至12%正是该技术的动机所在。<sup>[21](https://ar5iv.labs.arxiv.org/html/1711.08993)</sup> 一个较新的应用是LLM推理服务，其中预测式伸缩可补偿副本启动时间过长；谷歌的Autopilot展示了针对生产工作负载的、具有数据驱动策略的横向-纵向联合伸缩。<sup>[15](https://arxiv.org/pdf/2507.17128)</sup> Pod的横向扩展大多在5至30秒内完成，而虚拟机在三大云厂商中完全启动或终止约需一分钟；纵向重新配置需要数百毫秒。<sup>[6](https://raw.githubusercontent.com/rickypinci/rickypinci.github.io/master/files/papers/2024_TSC_Quattrocchi.pdf)</sup>

## 局限与替代方案

**抖动与振荡**发生在伸缩操作追逐自身效果之时；容差带、冷却时间和稳定窗口的存在就是为了抑制它们。<sup>[3](https://kubernetes.io/docs/concepts/workloads/autoscaling/horizontal-pod-autoscale/)</sup><sup> • </sup><sup>[4](https://github.com/kubernetes/design-proposals-archive/blob/main/autoscaling/horizontal-pod-autoscaler.md)</sup> **扩展缓慢**使突发流量无法得到服务：虚拟机实例化可能长达15分钟，因此反应式扩容可能来得太晚。<sup>[18](https://www3.cs.stonybrook.edu/~anshul/courses/cse591_s16/autoscaling_survey.pdf)</sup> **冷启动**在LLM服务中占主导地位：新副本需要两到十分钟启动，因为必须加载数GB的模型权重，这使得纯反应式伸缩在结构上必然滞后；与反应式KEDA伸缩相比，延迟感知的前瞻控制器将首令牌时间SLO违规从63.5%降至3.7%。<sup>[22](https://arxiv.org/abs/2609.20874)</sup> **指标失真**会导致容量分配不当：在85%的SLO下，横向自动伸缩中的磁盘故障每月产生约258美元的额外成本，因为当CPU接近100%时自动伸缩器将副本数翻倍；横向伸缩比纵向伸缩对瞬态指标失真更敏感，因为偏差会直接转化为副本数。<sup>[23](https://arxiv.org/abs/2601.04659)</sup> 一份实践者指南列出了七种表明配置错误的自动伸缩反模式，并指出文献缺乏广泛使用的评估方法。<sup>[24](https://research.spec.org/icpe_proceedings/2023/proceedings/p31.pdf)</sup>

替代方案包括手动或预留容量，这在全年通常更便宜但无法跟踪需求<sup>[17](https://www.mdpi.com/1424-8220/22/3/1221)</sup>，以及无服务器自动伸缩，其中每个函数都有一个饱和值（例如每实例平均每秒请求数），更敏感的伸缩会在利用率和更多冷启动之间权衡。<sup>[25](https://www.usenix.org/system/files/atc24-liu-qingyuan.pdf)</sup>

## References

1. [Auto-Scaling Techniques in Cloud Computing: Issues and Research Directions (Sensors 2024)](https://pmc.ncbi.nlm.nih.gov/articles/PMC11398277/)
2. [ML-Based Autoscaling for Elastic Cloud Applications: Taxonomy, Frameworks, and Evaluation (MCA 2026)](https://www.mdpi.com/2297-8747/31/2/49)
3. [Horizontal Pod Autoscaling | Kubernetes](https://kubernetes.io/docs/concepts/workloads/autoscaling/horizontal-pod-autoscale/)
4. [Kubernetes design proposal: Horizontal Pod Autoscaling](https://github.com/kubernetes/design-proposals-archive/blob/main/autoscaling/horizontal-pod-autoscaler.md)
5. [Dynamic scaling for Amazon EC2 Auto Scaling (AWS documentation)](https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-scale-based-on-demand.html)
6. [Autoscaling Solutions for Cloud Applications Under Dynamic Workloads (IEEE TSC, 2024)](https://raw.githubusercontent.com/rickypinci/rickypinci.github.io/master/files/papers/2024_TSC_Quattrocchi.pdf)
7. [Which Cloud Auto-Scaler Should I Use for my Application? / Benchmarking Auto-Scaling Algorithms (Ghit et al.)](https://bogdanghit.github.io/publications/cloud-autoscalers.pdf)
8. [On the Stability of the Kubernetes Horizontal Autoscaler Control Loop (Serracanta et al., 2025)](https://upcommons.upc.edu/server/api/core/bitstreams/34caab31-d883-4b17-a72d-43ed4e49346a/content)
9. [MLscale: A machine learning based application-agnostic autoscaler](https://www3.cs.stonybrook.edu/~anshul/suscom17.pdf)
10. [What is Amazon EC2 Auto Scaling? (AWS documentation)](https://docs.aws.amazon.com/autoscaling/ec2/userguide/what-is-amazon-ec2-auto-scaling.html)
11. [Vertical Pod autoscaling | Google Kubernetes Engine (GKE)](https://docs.cloud.google.com/kubernetes-engine/docs/concepts/verticalpodautoscaler)
12. [Autoscaling Workloads | Kubernetes](https://kubernetes.io/docs/concepts/workloads/autoscaling/)
13. [Bhuvan Urgaonkar and colleagues (2008). Agile dynamic provisioning of multi-tier Internet applications. ACM Transactions on Autonomous and Adaptive Systems.](https://doi.org/10.1145/1342171.1342172)
14. [New AWS Auto Scaling – Unified Scaling For Your Cloud Applications (AWS News Blog, 2017)](https://aws.amazon.com/blogs/aws/aws-auto-scaling-unified-scaling-for-your-cloud-applications/)
15. [Auto-scaling approaches for microservice applications (survey, arXiv 2507.17128)](https://arxiv.org/pdf/2507.17128)
16. [Zhicheng Pan and colleagues (2023). MagicScaler: Uncertainty-Aware, Predictive Autoscaling. Proceedings of the VLDB Endowment.](https://doi.org/10.14778/3611540.3611566)
17. [Performance-Cost Trade-Off in Auto-Scaling Mechanisms for Cloud Computing (Sensors 2022)](https://www.mdpi.com/1424-8220/22/3/1221)
18. [Auto-scaling Techniques for Elastic Applications in Cloud Environments (Lorido-Botrán et al., survey copy)](https://www3.cs.stonybrook.edu/~anshul/courses/cse591_s16/autoscaling_survey.pdf)
19. [Why Is It Not Solved Yet? Challenges for Production-Ready Autoscaling (ICPE 2022)](https://research.spec.org/icpe_proceedings/2022/proceedings/p105.pdf)
20. [Giovanni Quattrocchi and colleagues (2024). Autoscaling Solutions for Cloud Applications Under Dynamic Workloads. IEEE Transactions on Services Computing.](https://doi.org/10.1109/tsc.2024.3354062)
21. [Technical Report: A Trace-Based Performance Study of Autoscaling Workloads of Workflows in Datacenters](https://ar5iv.labs.arxiv.org/html/1711.08993)
22. [Decomposing Predictive Kubernetes Autoscaling for Large Language Model Serving Under Long Startup Delays](https://arxiv.org/abs/2609.20874)
23. [Failure-Induced Misallocation in Cloud Autoscalers (arXiv 2026)](https://arxiv.org/abs/2601.04659)
24. [Autoscaler Evaluation and Configuration: A Practitioner's Guideline (ICPE 2023)](https://research.spec.org/icpe_proceedings/2023/proceedings/p31.pdf)
25. [Harmonizing Efficiency and Practicability: Optimizing Resource Utilization in Serverless (USENIX ATC 2024, JIAGU)](https://www.usenix.org/system/files/atc24-liu-qingyuan.pdf)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/autoscaling

English original: https://www.edgepedia.org/autoscaling
