按需扩缩 Memcached 集群
ElastiCache for Memcached 提供一项完全托管式的内存缓存服务,可在 Amazon 云中部署、运行并垂直扩缩 Memcached。
按需垂直扩缩
通过垂直扩缩,ElastiCache for Memcached 可以提供一个高性能的分布式内存缓存系统,该系统被广泛用于通过减轻数据库负载来加快动态应用程序的速度。它将数据和对象存储在 RAM 中,从而减少从外部数据来源读取数据的需求。
您可以对现有的基于节点的集群以及新集群应用垂直扩缩。这能为资源分配提供灵活性,使用户能够高效适应不断变化的工作负载,而无需改变集群架构。这种扩缩能力通过在需求高峰期增加缓存容量来提升性能,在需求低谷期缩减规模来优化成本。这简化了操作,消除了为应对资源需求变化而创建新集群的必要性,并且能快速响应流量波动。总体而言,对 Memcached 基于节点的集群进行垂直扩缩有助于提升成本效益、提高资源利用率,甚至允许用户更改 Memcached 实例类型。所有这些都使用户可以更轻松地根据实际应用程序需求调整其缓存基础设置。
注意
节点类型修改仅适用于使用引擎版本 1.5 或更高版本的基于节点的 Memcached 集群。
必须启用自动发现才能使用垂直扩缩。
为基于节点的 Memcached 集群设置按需垂直扩缩
您可以使用 scale-config 为 Memcached 配置按需垂直扩缩,其中包含两个参数:
ScaleIntervalMinutes:Memcached 升级过程中批量扩缩操作之间的时间间隔(以分钟为单位)
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" ] }
使用Amazon Web Services 管理控制台对 Memcached 进行垂直扩缩
按照以下步骤使用 Amazon Web Services 管理控制台,将基于节点的 Memcached 集群转换为可垂直扩缩的集群。
登录 Amazon Web Services 管理控制台 并打开 ElastiCache 控制台(https://console.aws.amazon.com/elasticache/)。
选择要转换的 Memcached 集群。
选择修改选项卡。
前往缓存设置部分,然后选择所需的节点类型。
选择预览更改,然后查看更改。
选择 Modify。
自动水平扩缩 Memcached
ElastiCache 现已与 Amazon Application Auto Scaling(AAS)服务集成,为 Memcached 集群提供自动水平扩缩。您可以通过 Amazon Application Auto Scaling 服务定义扩缩策略,并根据预定义的指标或计划,按需调整 Memcached 集群中的节点数量。
注意
自动水平扩缩目前在北京和宁夏区域不可用。
以下是可用于自动水平扩缩基于节点的集群的方法:
计划扩缩:按计划扩缩使您可以按照可预测的负载变化来设置您自己的扩缩计划。例如,您的 Web 应用程序的流量会在每周的星期三开始增加,并在星期四保持高流量状态,然后在星期五开始下降。您可以将 Auto Scaling 配置为在星期三增加容量并在星期五减少容量。
目标跟踪:在使用目标跟踪扩缩策略时,您可以选择一个扩缩指标并设置一个目标值。Application Auto Scaling 创建和管理触发扩缩策略的 CloudWatch 警报,并根据指标和目标值计算扩缩调整。扩展策略根据需要增加或减少容量,将指标保持在指定的目标值或接近指定的目标值。
如何通过 CLI 为基于节点的 Memcached 集群设置水平扩缩
水平扩缩基于节点的 Memcached 集群时,您可以使用目标跟踪策略、计划策略,或同时使用两者。
将资源注册为可扩展目标
在 Amazon Application Auto Scaling 中调用
elasticache:cache-cluster:NodesAPI,为可扩展维度RegisterScalableTarget注册目标。API:ApplicationAutoScaling.RegisterScalableTarget
输入:
{ "ScalableDimension": "elasticache:cache-cluster:Nodes", "ResourceId": "cache-cluster/test-cluster-1", "ServiceNamespace": "elasticache", "MinCapacity": 20, "MaxCapacity": 50 }创建目标跟踪扩缩策略
接下来,您可以通过调用 put scaling policy 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" }计划的操作
如果您需要对特定事件进行横向扩展,并在事件结束后进行横向缩减,可通过调用
PutScheduledActionAPI 创建两个计划操作。策略 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", }查看扩缩活动
您可以使用
DescribeScalingActivitiesAPI 查看扩缩活动。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" } ] }编辑/删除扩缩策略
您可以通过再次调用
PutScalingPolicyAPI,或调用DeleteScalingPolicy或DeleteScheduled操作来编辑或删除策略。取消注册可扩缩目标
您可以通过
DeregisterScalableTargetAPI 取消注册可扩缩目标。取消注册可扩缩目标会删除扩缩策略和与其关联的计划操作。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 会根据为横向缩减和横向扩展提供最大容量的策略进行扩展。这样可以更灵活地覆盖多种场景,并确保始终有足够的容量来处理应用程序工作负载。
注意
AmazonApplication Auto Scaling 不会将扩缩策略加入队列。Application Auto Scaling 将等待首次缩放完成,随后进入冷却期,然后重复上述算法。
通过Amazon Web Services 管理控制台自动水平扩缩基于节点的 Memcached 集群
按照以下步骤使用 Amazon Web Services 管理控制台,将现有的基于节点的 Memcached 集群转换为可水平扩缩的集群。
登录 Amazon Web Services 管理控制台 并打开 ElastiCache 控制台(https://console.aws.amazon.com/elasticache/)。
选择要转换的 Memcached 缓存。
转到 Autoscaling 选项卡。
通过选择添加动态扩缩或添加计划扩缩,选择要应用的扩缩策略。
根据需要填写所选策略的详细信息。
单击创建。