

# SUS02-BP01 动态扩展工作负载基础设施
<a name="sus_sus_user_a2"></a>

利用云的弹性并动态扩展基础设施，以使云资源的供应与需求相匹配，避免在工作负载中过度调配容量。

**常见反模式：**
+ 您没有扩展基础设施以匹配用户负载。
+ 您一直在手动扩展基础设施。
+ 在扩展事件之后保留增加的容量，而不是缩减容量。

 **建立此最佳实践的好处：**配置和测试工作负载弹性有助于有效地将云资源的供应与需求相匹配，并避免过度调配容量。您可以利用云中的弹性，在需求高峰期间和之后自动扩展容量，以确保您只使用满足业务需求所需的适当数量的资源。

 **在未建立这种最佳实践的情况下暴露的风险等级：**中 

## 实施指导
<a name="implementation-guidance"></a>

 云让您能够通过各种机制灵活地动态扩展或缩减资源，以便满足不断变化的需求。供应与需求的最佳匹配提供了最低的工作负载环境影响。

 需求可以是固定的，也可以是变化的，需要指标和自动化来确保管理不会变成沉重负担。应用程序可以通过修改实例大小来纵向扩展或缩减，通过修改实例数量来横向扩展或缩减，或者组合使用这两种方式。

 您可以使用大量不同方法来实现资源的供需匹配。
+  **目标跟踪方法：**监控您的扩缩指标，并根据需要自动增加或减少容量。
+  **预测性扩缩：**根据每日和每周的趋势进行扩缩。
+  **基于计划的方法：**根据可预测的负载变化设置自己的扩缩计划。
+  **服务扩缩：**选择按设计可以原生扩缩或者将自动扩缩作为一项功能提供的服务（如无服务器）。

 确定利用率低或无利用率的时段，缩减资源以消除过剩容量并提高效率。

## 实施步骤
<a name="implementation-steps"></a>
+ 弹性可根据对您拥有的资源的需求来提供这些资源。实例、容器和函数提供了弹性机制，可以与自动扩缩结合使用，也可以作为服务的一项功能。AWS 提供了一系列自动扩缩机制，以确保工作负载可以在低用户负载期间快速轻松地缩减。以下是自动扩缩机制的一些示例：    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/wellarchitected/latest/framework/sus_sus_user_a2.html)
+  扩缩通常与计算服务（如 Amazon EC2 实例或 AWS Lambda 函数）相关。考虑使用非计算服务配置（如 [Amazon DynamoDB](https://aws.amazon.com/dynamodb/) 读写容量单元或 [Amazon Kinesis Data Streams](https://aws.amazon.com/kinesis/data-streams/) 分片）来满足需求。
+  验证衡量扩展或缩减的指标已根据所部署的工作负载类型进行了验证。如果您正在部署一个视频转码应用程序，预计 CPU 利用率为 100%，但不应将此作为主要指标，如果需要，可以对扩缩策略使用[自定义指标](https://aws.amazon.com/blogs/mt/create-amazon-ec2-auto-scaling-policy-memory-utilization-metric-linux/)（如内存利用率）。要选择正确的指标，请考虑以下关于 Amazon EC2 的指导：
  +  指标应该是有效的利用率指标，并描述实例的繁忙程度。
  +  指标值必须随着自动扩缩组中的实例数按比例增加或减少。
+  对自动扩缩组使用[动态扩缩](https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-scale-based-on-demand.html)而不是[手动扩缩](https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-manual-scaling.html)。我们还建议在动态扩缩中使用[目标跟踪扩缩策略](https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-scaling-target-tracking.html)。
+  确认工作负载部署可以处理横向扩展事件和横向缩减事件。为横向缩减事件创建测试场景，以确认工作负载的行为符合预期，并且不会影响用户体验（如丢失粘滞会话）。您可以使用[活动历史记录](https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-verify-scaling-activity.html)验证自动扩缩组的扩缩活动。
+  评估工作负载，得出可预测的模式，从而在预期需求会发生预测性的计划内变化时主动扩缩。预测性扩缩可以避免过度预置容量。有关更多详细信息，请参阅 [Predictive Scaling with Amazon EC2 Auto Scaling](https://aws.amazon.com/blogs/compute/introducing-native-support-for-predictive-scaling-with-amazon-ec2-auto-scaling/)。

## 资源
<a name="resources"></a>

 **相关文档：**
+  [Getting Started with Amazon EC2 Auto Scaling](https://docs.aws.amazon.com/autoscaling/ec2/userguide/GettingStartedTutorial.html) 
+  [Predictive Scaling for EC2, Powered by Machine Learning](https://aws.amazon.com/blogs/aws/new-predictive-scaling-for-ec2-powered-by-machine-learning/) 
+  [使用 Amazon OpenSearch Service、Amazon Data Firehose 和 Kibana 分析用户行为](https://aws.amazon.com/blogs/database/analyze-user-behavior-using-amazon-elasticsearch-service-amazon-kinesis-data-firehose-and-kibana/) 
+  [什么是 Amazon CloudWatch？](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/WhatIsCloudWatch.html) 
+  [在 Amazon RDS 上使用性能详情监控数据库负载](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_PerfInsights.html) 
+  [介绍对 Amazon EC2 Auto Scaling 预测式扩缩的原生支持](https://aws.amazon.com/blogs/compute/introducing-native-support-for-predictive-scaling-with-amazon-ec2-auto-scaling/) 
+  [介绍 Karpenter – 高性能开源 Kubernetes Cluster Autoscaler](https://aws.amazon.com/blogs/aws/introducing-karpenter-an-open-source-high-performance-kubernetes-cluster-autoscaler/) 
+  [Deep Dive on Amazon ECS Cluster Auto Scaling](https://aws.amazon.com/blogs/containers/deep-dive-on-amazon-ecs-cluster-auto-scaling/) 

 **相关视频：**
+ [AWS re:Invent 2023 - Scaling on AWS for the first 10 million users](https://www.youtube.com/watch?v=JzuNJ8OUht0)
+ [AWS re:Invent 2023 - Sustainable architecture: Past, present, and future ](https://www.youtube.com/watch?v=2xpUQ-Q4QcM)
+  [AWS re:Invent 2022 - Build a cost-, energy-, and resource-efficient compute environment](https://www.youtube.com/watch?v=8zsC5e1eLCg) 
+ [AWS re:Invent 2022 - Scaling containers from one user to millions ](https://www.youtube.com/watch?v=hItHqzKoBk0)
+ [AWS re:Invent 2023 - Scaling FM inference to hundreds of models with Amazon SageMaker AI ](https://www.youtube.com/watch?v=6xENDvgnMCs)
+ [AWS re:Invent 2023 - Harness the power of Karpenter to scale, optimize & upgrade Kubernetes ](https://www.youtube.com/watch?v=lkg_9ETHeks)

 **相关示例：**
+ [ Autoscaling ](https://www.eksworkshop.com/docs/autoscaling/)