本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
扩展 Memcached 集群
ElastiCache for Memcached 提供完全托管的内存缓存服务,可在云端部署、操作和垂直扩展 Memcached。 Amazon
按需垂直扩展
通过垂直扩展,f ElastiCache or Memcached 提供了一个高性能的分布式内存缓存系统,广泛用于通过减轻数据库负载来加快动态 Web 应用程序的速度。它将数据和对象存储在 RAM 中,从而减少了从外部数据源读取数据的需求。
您可以将自己设计 ElastiCache 的 Memcached 集群用于垂直扩展。您可以对现有集群和新集群应用垂直扩展。这可以提供资源分配的灵活性,使用户能够在不改变群集架构的情况下有效地适应不断变化的工作负载。这种扩展能力通过在高需求时期增加缓存容量,在低需求时段缩小缓存容量以优化成本,从而提高性能。这简化了操作,无需为不断变化的资源需求创建新的群集,并且可以快速响应流量波动。总体而言,垂直扩展 Memcached 自行设计的缓存可以帮助提高成本效益,提高资源利用率,甚至允许用户更改其 Memcached 实例类型。所有这些都使用户可以更轻松地根据实际应用程序需求调整其缓存基础架构。
注意
自行设计的缓存和节点类型修改仅适用于 Memcached 引擎版本 1.5 或更高版本。
必须启用自动发现才能使用垂直缩放。
为 ElastiCache Memcached 集群设置按需垂直扩展
您可以使用为 Memcached 配置按需垂直扩展scale-config
,其中包含两个参数:
ScaleIntervalMinutes:在 Memcached 升级过程中,两次扩展批次之间的时间(以分钟为单位)
ScalePercentage: 在 Memcached 升级过程中要同时扩展的节点百分比
将现有 Memcached 节点类型转换为可通过 CLI 垂直扩展的缓存
要将现有 Memcached 自行设计的缓存转换为可以垂直扩展的缓存,可以elasticache modify-cache-cluster
通过 CLI 使用。
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
使用 with 及其参数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 Management Console
要将现有 Memcached 自行设计的缓存转换为可以纵向扩展的缓存 https://console.aws.amazon.com/elasticache/
登录 Amazon Web Services Management Console 并打开 ElastiCache 控制台,网址为 https://console.aws.amazon.com/elasticache/
。 选择要转换的 Memcached 缓存。
选择 “修改” 选项卡。
前往缓存设置部分,然后选择所需的节点类型。
选择 “预览更改”,然后查看更改。
选择 Modify。
Memcached 的自动水平缩放
ElastiCache 现在与 App Amazon lication Auto Scaling (AAS) 服务集成,包括了 Memcached 集群的自动水平扩展。您可以通过 App Amazon lication Auto Scaling 服务定义扩展策略,并根据预定义的指标或计划根据需要自动调整 Memcached 集群中的节点数量。
注意
北京和宁夏区域目前不提供自动水平扩展。
这些是自动水平扩展您自己设计的缓存的可用方法。
计划扩展:根据计划进行扩展允许您为可预测的负载变化设置自己的扩展计划。例如,您的 Web 应用程序的流量会在每周的星期三开始增加,并在星期四保持高流量状态,然后在星期五开始下降。您可以将 Auto Scaling 配置为在星期三增加容量,在星期五减少容量。
目标跟踪:使用目标跟踪扩展策略,您可以选择扩展指标并设置目标值。Application Auto Scaling 创建和管理触发扩展策略的 CloudWatch 警报,并根据指标和目标值计算扩展调整。扩展策略根据需要增加或减少容量,将指标保持在指定的目标值或接近指定的目标值。
如何通过 CLI 为 Memcached 自行设计 ElastiCache 的缓存设置水平缩放
要使用 ElastiCache Memcached 进行横向扩展,您可以拥有目标跟踪策略、计划策略或两者兼而有之。
将资源注册为可扩展目标
在 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 }
创建 Target 跟踪扩展策略
接下来,您可以通过调用 put 扩展策略 API 为资源创建目标跟踪扩展策略。
预定义指标
以下是沿缓存节点维度扩展的策略,使用预定义的指标
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" } ] }
自定义指标
您还可以使用基于 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" }
计划的操作
当您需要针对特定事件进行扩展,然后在事件发生后缩小规模时,可以通过调用
PutScheduledAction
API 来创建两个计划操作。策略 1:向外扩展
中的
at
命令将该操作--schedule
安排在未来的指定日期和时间运行一次。计划字段还支持速率(分钟、小时、天等)和 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", }
查看扩展活动
您可以使用
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" } ] }
编辑/删除扩展策略
您可以通过再次调用
PutScalingPolicy
API 或调用或 ActiDeleteScheduled
on 来编辑DeleteScalingPolicy
或删除策略。注销可扩展目标
您可以通过
DeregisterScalableTarget
API 注销可扩展目标的注册。取消注册可扩展目标会删除扩展策略和与其关联的计划操作。API: ApplicationAutoScaling。 DeregisterScalableTarget
输入:
{ "ResourceId": "elasticache/cache-cluster/test-cluster-1", "ServiceNamespace": "elasticache", "ScalableDimension": "elasticache:cache-cluster:Nodes" }
扩展策略清理
多种扩展策略
您可以创建多个扩展策略。以下是 Auto Scaling 目标跟踪中有关行为的关键标注。
一个可扩展目标可以具有多个目标跟踪扩展策略,前提是它们分别使用不同的指标。
Application Auto Scaling 的目的是始终优先考虑可用性,因此其行为会有所不同,具体取决于目标跟踪策略是否已准备好横向扩展或横向缩减。如果任何目标跟踪策略已准备好进行向外扩展,它将向外扩展可扩展目标,但仅在所有目标跟踪策略(启用了缩减部分)准备好缩减时才执行缩减。
如果多个策略指示可扩展目标同时横向扩展或横向缩减,则 Application Auto Scaling 会根据为横向缩减和横向扩展提供最大容量的策略进行扩展。这样可以更灵活地覆盖多种场景,并确保始终有足够的容量来处理应用程序工作负载。
注意
Amazon Application Auto Scaling 不对扩展策略进行排队。Application Auto Scaling 将等待第一次缩放完成,然后等待冷却时间,然后重复上述算法。
通过以下方式自动水平缩放 Memcached 缓存 Amazon Web Services Management Console
要将现有 Memcached 自行设计的缓存转换为可以横向扩展的缓存 https://console.aws.amazon.com/elasticache/
登录 Amazon Web Services Management Console 并打开 ElastiCache 控制台,网址为 https://console.aws.amazon.com/elasticache/
。 选择要转换的 Memcached 缓存。
转到 “自动缩放” 选项卡。
通过选择 “添加动态扩展” 或 “添加计划扩展”,选择要应用的扩展策略。
根据需要填写所选策略的详细信息。
单击创建。