

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 按需扩缩 Memcached 集群
<a name="Scaling-self-designed.mem-heading"></a>

ElastiCache for Memcached 提供完全托管的内存缓存服务，可在云端部署、操作和垂直扩展 Memcached。Amazon

**按需垂直扩缩**

通过垂直扩展，f ElastiCache or Memcached 提供了一个高性能的分布式内存缓存系统，广泛用于通过减轻数据库负载来加快动态应用程序的速度。它将数据和对象存储在 RAM 中，从而减少从外部数据来源读取数据的需求。

您可以对现有的基于节点的集群以及新集群应用垂直扩缩。这能为资源分配提供灵活性，使用户能够高效适应不断变化的工作负载，而无需改变集群架构。这种扩缩能力通过在需求高峰期增加缓存容量来提升性能，在需求低谷期缩减规模来优化成本。这简化了操作，消除了为应对资源需求变化而创建新集群的必要性，并且能快速响应流量波动。总体而言，对 Memcached 基于节点的集群进行垂直扩缩有助于提升成本效益、提高资源利用率，甚至允许用户更改 Memcached 实例类型。所有这些都使用户可以更轻松地根据实际应用程序需求调整其缓存基础设置。

**注意**  
节点类型修改仅适用于使用引擎版本 1.5 或更高版本的基于节点的 Memcached 集群。
必须启用自动发现才能使用垂直扩缩。

## 为基于节点的 Memcached 集群设置按需垂直扩缩
<a name="Scaling.Memcached.automatically.setup.cli"></a>

您可以使用 `scale-config` 为 Memcached 配置按需垂直扩缩，其中包含两个参数：

1. **ScaleIntervalMinutes：**在 Memcached 升级过程中，两次扩展批次之间的时间（以分钟为单位）

1. **ScalePercentage:** 在 Memcached 升级过程中要同时扩展的节点百分比

**将现有 Memcached 节点类型转换为可通过 CLI 垂直扩缩的缓存**

要将现有的基于节点的 Memcached 集群转换为可垂直扩缩的缓存，您可以通过 CLI 使用 `elasticache modify-cache-cluster` 命令实现。

```
aws elasticache modify-cache-cluster \
    --cache-cluster-id <your-cluster-id> \
    --cache-node-type <new-node-type> \
    --scale-config <scale-config> \ 
    --apply-immediately
```

**使用 CLI 设置垂直扩缩**

要通过 CLI 为基于节点的 Memcached 集群设置垂直扩缩，请使用 `elasticache modify-cache-cluster` 和 `scale-config` 及其参数 `ScalePercentage` 和 `ScaleIntervalMinutes`。
+ **scale-interval-minutes：**这定义了缩放批次之间的时间（以分钟为单位）。设置范围为 2-30 分钟。如果未指定值，则使用默认值 5 分钟。
+ **缩放百分比：**此参数指定了每批次中需并行扩缩的节点的百分比。设置范围为 10-100 之间。该设置在除法运算时会向上舍入，例如当结果为 49.5 时，实际应用的设置值为 50。如果未指定值，则使用默认值 20。

这些配置选项使您能够根据自己的特定需求微调扩缩过程，在最大限度地减少集群中断和优化扩缩速度之间取得平衡。scale-config 参数仅适用于 Memcached 引擎类型，在其他缓存引擎中将被忽略，从而确保向后兼容其他集群的现有 API 使用方式。

**API 调用**

```
aws elasticache modify-cache-cluster \
    --cache-cluster-id <your-cluster-id> \
    --cache-node-type <new-node-type> \
    --scale-config '{
            "ScalePercentage": 30,
            "ScaleIntervalMinutes": 2
          }'
    --apply-immediately
```

**结果：**

返回集群 ID 和待处理的更改。

```
{
    "CacheCluster": {
        "CacheNodeType": "old_insance_type",
         ...
         ...
         "PendingModifiedValues": {
            "CacheNodeType": "new_instance_type"
         },
    }
}
```

**列出 Memcached 缓存垂直扩缩设置**

您可以检索 Memcached 缓存的扩缩选项，并查看它们当前的垂直扩缩选项。

**API 调用**

```
aws elasticache list-allowed-node-type-modifications --cache-cluster-id <your-cluster-id>
```

**结果：**

```
{ 
  "ScaleUpModifications": [
      "cache.x.xxxx", 
      "cache.x.xxxx"
   	  ],
   "ScaleDownModifications": [ 
      "cache.x.xxxx", 
      "cache.x.xxxx", 
      "cache.x.xxxx" 
      ] 
}
```

**使用 Memcached 进行垂直缩放Amazon Web Services 管理控制台**

按照以下步骤使用将基于节点的 Memcached 集群Amazon Web Services 管理控制台转换为垂直可扩展的集群。

1. 登录Amazon Web Services 管理控制台并打开 ElastiCache 控制台，网址为[ https://console.aws.amazon.com/elasticache/](https://console.amazonaws.cn/elasticache/)。

1. 选择要转换的 Memcached 集群。

1. 选择**修改**选项卡。

1. 前往**缓存设置**部分，然后选择所需的**节点类型**。

1. 选择**预览更改**，然后查看更改。

1. 选择 **Modify**。

## 自动水平扩缩 Memcached
<a name="Scaling-self-designed.mem-heading.horizontal"></a>

ElastiCache 现在与 App Amazon lication Auto Scaling (AAS) 服务集成，包括了 Memcached 集群的自动水平扩展。您可以通过 App Amazon lication Auto Scaling 服务定义扩展策略，并根据预定义的指标或计划根据需要自动调整 Memcached 集群中的节点数量。

**注意**  
自动水平扩缩目前在北京和宁夏区域不可用。

以下是可用于自动水平扩缩基于节点的集群的方法：
+ **计划扩缩：**按计划扩缩使您可以按照可预测的负载变化来设置您自己的扩缩计划。例如，您的 Web 应用程序的流量会在每周的星期三开始增加，并在星期四保持高流量状态，然后在星期五开始下降。您可以将 Auto Scaling 配置为在星期三增加容量并在星期五减少容量。
+ **目标跟踪：**在使用目标跟踪扩缩策略时，您可以选择一个扩缩指标并设置一个目标值。Application Auto Scaling 创建和管理触发扩展策略的 CloudWatch 警报，并根据指标和目标值计算扩展调整。扩展策略根据需要增加或减少容量，将指标保持在指定的目标值或接近指定的目标值。

**如何通过 CLI 为基于节点的 Memcached 集群设置水平扩缩**

水平扩缩基于节点的 Memcached 集群时，您可以使用目标跟踪策略、计划策略，或同时使用两者。

1. **将资源注册为可扩展目标**

   在 Application Auto Scaling 中调Amazon用 `RegisterScalableTarget` API 来注册可扩展维度的目标`elasticache:cache-cluster:Nodes`。

   **API: ApplicationAutoScaling。 RegisterScalableTarget**

   输入:

   ```
   {
   	"ScalableDimension": "elasticache:cache-cluster:Nodes",
   	"ResourceId": "cache-cluster/test-cluster-1",
   	"ServiceNamespace": "elasticache",
   	"MinCapacity": 20,  
   	"MaxCapacity": 50 
   }
   ```

1. **创建目标跟踪扩缩策略**

   接下来，您可以通过调用 put scaling policy API 为资源创建目标跟踪扩缩策略。

1. **预定义的指标**

   以下策略将使用预定义指标 ` ElastiCacheCPUUtilization`（在集群 test-cluster-1 中设置为 50），根据缓存节点维度进行扩缩。在横向缩减过程中删除节点时，将移除最后 n 个节点。

   API: ApplicationAutoScaling。 PutScalingPolicy

   输入:

   ```
   {
   	"PolicyName": "cpu50-target-tracking-scaling-policy",
   	"PolicyType": "TargetTrackingScaling",
   	"TargetTrackingScalingPolicyConfiguration": {
   		"TargetValue": 50,
   		"PredefinedMetricSpecification": {
   			"PredefinedMetricType": "ElastiCacheCPUUtilization"
   			},
   		"ScaleOutCooldown": 600,
   		"ScaleInCooldown": 600
   			},
   	"ServiceNamespace": "elasticache",
   	"ScalableDimension": "elasticache:cache-cluster:Nodes",
   	"ResourceId": "cache-cluster/test-cluster-1"
   }
   ```

   输出：

   ```
   {
   	"PolicyARN": "arn:aws:autoscaling:us-west-2:012345678910:scalingPolicy:6d8972f3-efc8-437c-92d1-6270f29a66e7:resource/elasticache/cache-cluster/test-cluster-1:policyName/cpu50-target-tracking-scaling-policy",
   	"Alarms": [
   		{
   		"AlarmARN": "arn:aws:cloudwatch:us-west-2:012345678910:alarm:TargetTracking-elasticache/cache-cluster/test-cluster-1-AlarmHigh-d4f0770c-b46e-434a-a60f-3b36d653feca",
   		"AlarmName": "TargetTracking-elasticache/cache-cluster/test-cluster-1-AlarmHigh-d4f0770c-b46e-434a-a60f-3b36d653feca"
   		},
   		{
   		"AlarmARN": "arn:aws:cloudwatch:us-west-2:012345678910:alarm:TargetTracking-elasticache/cache-cluster/test-cluster-1-AlarmLow-1b437334-d19b-4a63-a812-6c67aaf2910d",
   		"AlarmName": "TargetTracking-elasticache/cache-cluster/test-cluster-1-AlarmLow-1b437334-d19b-4a63-a812-6c67aaf2910d"
   		}
   	]
   }
   ```

1. **自定义指标**

   您还可以使用基于 Cloudwatch 指标的自定义百分比来设置维度的扩缩策略。

   输入:

   ```
   {
   	"PolicyName": "cpu50-target-tracking-scaling-policy",
   	"PolicyType": "TargetTrackingScaling",
   	"TargetTrackingScalingPolicyConfiguration": {
   		"CustomizedMetricSpecification": { 
   			"Dimensions": [ 
   				{ 
   				"Name": "MyMetricDimension",
   				"Value": "DimensionValue"
   				}
   				],
   			"MetricName": "MyCustomMetric",
   			"Namespace": "MyNamespace",
   			"Statistic": "Average",
   			"Unit": "Percent"
   			},
   		"TargetValue": 40,
   		"ScaleOutCooldown": 600,
   		"ScaleInCooldown": 600
   		},
   	"ServiceNamespace": "elasticache",
   	"ScalableDimension": "elasticache:cache-cluster:Nodes",
   	"ResourceId": "cache-cluster/test-cluster-1"
   }
   ```

1. **计划的操作**

   如果您需要对特定事件进行横向扩展，并在事件结束后进行横向缩减，可通过调用 `PutScheduledAction` API 创建两个计划操作。

   **策略 1：横向扩展**

   `--schedule` 中的 `at` 命令会计划在将来的指定日期和时间要运行一次的操作。计划字段还支持速率（分钟、小时、天等）和 cron（用于 cron 表达式）。

   在指定的日期和时间，Application Auto Scaling 将更新 `MinCapacity` 和 `MaxCapacity` 的值。Application Auto Scaling 扩展 MinCapacity 到，将缓存节点增加到 70。

   **API: ApplicationAutoScaling。 PutScheduledAction**

   输入:

   ```
   {
   	"ResourceId": "elasticache:ache-cluster:test-cluster-1",
   	"ScalableDimension": "elasticache:cache-cluster:Nodes",
   		"ScalableTargetAction": { 
   			"MaxCapacity": 100,
   			"MinCapacity": 70
   			},
   	"Schedule": "at(2020-05-20T17:05:00)",
   	"ScheduledActionName": "ScalingOutScheduledAction",
   	"ServiceNamespace": "elasticache",
   }
   ```

   **策略 2：横向缩减**

   在指定的日期和时间，Application Auto Scaling 将更新表的 `MinCapacity` 和 `MaxCapacity`，并横向缩减到 `MaxCapacity`，将缓存节点恢复到 60 个。

   **API: ApplicationAutoScaling。 PutScheduledAction**

   输入:

   ```
   {
   	"ResourceId": "elasticache:cache-cluster:test-cluster-1",
   	"ScalableDimension": "elasticache:cache-cluster:Nodes",
   	"ScalableTargetAction": { 
   		"MaxCapacity": 60,
   		"MinCapacity": 40
   		},
   	"Schedule": "at(2020-05-21T17:05:00)",
   	"ScheduledActionName": "ScalingInScheduledAction",
   	"ServiceNamespace": "elasticache",
   }
   ```

1. **查看扩缩活动**

   您可以使用 `DescribeScalingActivities` API 查看扩缩活动。

   **API: ApplicationAutoScaling。 DescribeScalingActivities**

   输出：

   ```
   {
   	"ScalingActivities": [
   		{
   		"ScalableDimension": "elasticache:elasticache:DesiredCount",
   		"Description": "Setting desired count to 30.",
   		"ResourceId": "elasticache/cache-cluster/test-cluster-1",
   		"ActivityId": "4d759079-a31f-4d0c-8468-504c56e2eecf",
   		"StartTime": 1462574194.658,
   		"elasticacheNamespace": "elasticache",
   		"EndTime": 1462574276.686,
   		"Cause": "monitor alarm TargetTracking-elasticache/cache-cluster/test-cluster-1-AlarmHigh-d4f0770c-b46e-434a-a60f-3b36d653feca in state ALARM triggered policy cpu50-target-tracking-scaling-policy",
   		"StatusMessage": "Failed to set desired count to 30",
   		"StatusCode": "Failed"
   		},
   		{
   		"ScalableDimension": "elasticache:elasticache:DesiredCount",
   		"Description": "Setting desired count to 25.",
   		"ResourceId": "elasticache/cache-cluster/test-cluster-1",
   		"ActivityId": "90aff0eb-dd6a-443c-889b-b809e78061c1",
   		"StartTime": 1462574254.223,
   		"elasticacheNamespace": "elasticache",
   		"EndTime": 1462574333.492,
   		"Cause": "monitor alarm TargetTracking-elasticache/cache-cluster/test-cluster-1-AlarmHigh-d4f0770c-b46e-434a-a60f-3b36d653feca in state ALARM triggered policy cpu50-target-tracking-scaling-policy",
   		"StatusMessage": "Successfully set desired count to 25. Change successfully fulfilled by elasticache.",
   		"StatusCode": "Successful"
   		}
   	]
   }
   ```

1. **编辑/删除扩缩策略**

   您可以通过再次调用 `PutScalingPolicy` API，或调用 `DeleteScalingPolicy` 或 `DeleteScheduled` 操作来编辑或删除策略。

1. **取消注册可扩缩目标**

   您可以通过 `DeregisterScalableTarget` API 取消注册可扩缩目标。取消注册可扩缩目标会删除扩缩策略和与其关联的计划操作。

   **API: ApplicationAutoScaling。 DeregisterScalableTarget**

   输入:

   ```
   {
   	"ResourceId": "elasticache/cache-cluster/test-cluster-1",
   	"ServiceNamespace": "elasticache",
   	"ScalableDimension": "elasticache:cache-cluster:Nodes"
   }
   ```

1. **扩缩策略清除**

1. **多种扩展策略**

   您可以创建多个扩缩策略。以下是 [Auto Scaling 目标跟踪](https://docs.amazonaws.cn/autoscaling/application/userguide/application-auto-scaling-target-tracking.html)行为的关键标注。
   + 一个可扩展目标可以具有多个目标跟踪扩展策略，前提是它们分别使用不同的指标。
   + Application Auto Scaling 的目的是始终优先考虑可用性，因此其行为会有所不同，具体取决于目标跟踪策略是否已准备好横向扩展或横向缩减。如果任何目标跟踪策略已准备好进行向外扩展，它将向外扩展可扩展目标，但仅在所有目标跟踪策略（启用了缩减部分）准备好缩减时才执行缩减。
   + 如果多个策略指示可扩展目标同时横向扩展或横向缩减，则 Application Auto Scaling 会根据为横向缩减和横向扩展提供最大容量的策略进行扩展。这样可以更灵活地覆盖多种场景，并确保始终有足够的容量来处理应用程序工作负载。
**注意**  
Amazon Application Auto Scaling 不对扩展策略进行排队。Application Auto Scaling 将等待首次缩放完成，随后进入冷却期，然后重复上述算法。

**通过以下方式自动水平扩展基于节点的 Memcached 集群Amazon Web Services 管理控制台**

按照以下步骤使用将现有的基于节点的Amazon Web Services 管理控制台 Memcached 集群转换为可水平扩展的集群。

1. 登录Amazon Web Services 管理控制台并打开 ElastiCache 控制台，网址为[ https://console.aws.amazon.com/elasticache/](https://console.amazonaws.cn/elasticache/)。

1. 选择要转换的 Memcached 缓存。

1. 转到 **Autoscaling** 选项卡。

1. 通过选择**添加动态扩缩**或**添加计划扩缩**，选择要应用的扩缩策略。

1. 根据需要填写所选策略的详细信息。

1. 单击**创建**。