尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

aws-cli 实战:使用 application-autoscaling describe-scalable-targets 查询并解析 Application Auto Scaling 伸缩目标

aws-cli 实战:使用 application-autoscaling describe-scalable-targets 查询并解析 Application Auto Scaling 伸缩目标 aws-cli 实战使用 application-autoscaling describe-scalable-targets 查询并解析 Application Auto Scaling 伸缩目标【免费下载链接】aws-cliUniversal Command Line Interface for Amazon Web Services项目地址: https://gitcode.com/GitHub_Trending/aw/aws-cli本文基于 aws-cli 仓库中的示例文档 describe-scalable-targets.rst讲解如何通过aws application-autoscaling describe-scalable-targets命令查询 Amazon Web Services 上已注册的 Application Auto Scaling 伸缩目标scalable target。读完后你将掌握该命令的完整用法、各请求参数的取值规则包括--service-namespace、--resource-ids、--scalable-dimension的合法格式、返回 JSON 中每个字段的含义以及基于 botocore 服务模型的自动分页机制可直接用于伸缩配置巡检与自动化脚本开发。命令基础用法查询指定服务命名空间下的伸缩目标示例文档给出的最简用法是在ecs服务命名空间下查询所有伸缩目标。该命令只需一个必选参数--service-namespace即可列出该命名空间下全部已注册的伸缩目标。aws application-autoscaling describe-scalable-targets \ --service-namespace ecs命令返回如下 JSON完整输出继承自示例文档{ ScalableTargets: [ { ServiceNamespace: ecs, ScalableDimension: ecs:service:DesiredCount, ResourceId: service/default/web-app, MinCapacity: 1, MaxCapacity: 10, RoleARN: arn:aws:iam::123456789012:role/aws-service-role/ecs.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_ECSService, CreationTime: 1462558906.199, SuspendedState: { DynamicScalingOutSuspended: false, ScheduledScalingSuspended: false, DynamicScalingInSuspended: false }, ScalableTargetARN: arn:aws:application-autoscaling:us-west-2:123456789012:scalable-target/1234abcd56ab78cd901ef1234567890ab123 } ] }请求参数详解aws-cli 的describe-scalable-targets参数集合来自 botocore 内置的服务模型文件 awscli/botocore/data/application-autoscaling/2016-02-06/service-2.json。在该模型的DescribeScalableTargetsRequest定义中ServiceNamespace被标记为唯一必选字段required其余参数均可选。各参数说明如下CLI 参数对应 API 字段必选说明--service-namespaceServiceNamespace是提供资源的 AWS 服务命名空间例如ecs、dynamodb、rds自研应用或服务的自定义资源使用custom-resource--resource-idsResourceIds否资源标识列表由“资源类型 唯一标识”组成用于按具体资源过滤结果--scalable-dimensionScalableDimension否伸缩维度由“服务命名空间:资源类型:伸缩属性”三段组成指定该参数时必须同时指定资源 ID--max-resultsMaxResults否单次返回的伸缩目标数量上限取值范围 1~50默认 50使用时响应中会附带NextToken--next-tokenNextToken否下一页结果令牌将上一次响应中的NextToken传入以获取后续结果ResourceId 的常见格式模型文档中给出了ResourceIds的典型取值格式实际使用时可按此构造过滤条件节选ECS 服务service/my-cluster/my-service资源类型为service唯一标识为集群名 服务名DynamoDB 表table/my-tableDynamoDB 全局二级索引table/my-table/index/my-table-indexSpot Fleetspot-fleet-request/sfr-73fbd2ce-aa30-494c-8788-1cee4EXAMPLEEMR 实例组instancegroup/j-2EEZNYKUA1NTV/ig-1791Y4E1L8YI0AppStream 2.0 车队fleet/sample-fleetAurora DB 集群cluster:my-db-clusterLambda 预留并发function:my-function:prod或function:my-function:1后缀不能是$LATESTAmazon Keyspaces 表keyspace/mykeyspace/table/mytableAmazon MSK 集群使用集群 ARN例如arn:aws:kafka:us-east-1:123456789012:cluster/demo-cluster-1/...ElastiCache 复制组 / 缓存集群replication-group/mycluster、cache-cluster/myclusterWorkSpaces 池workspacespool/wspool-123456。对于自定义资源custom-resource不使用资源类型而须使用 CloudFormation 模板栈的OutputValue作为唯一标识。ScalableDimension 的常见取值ScalableDimension采用命名空间:资源类型:伸缩属性三段式结构常用取值包括ecs:service:DesiredCount—— ECS 服务的期望任务数elasticmapreduce:instancegroup:InstanceCount—— EMR 实例组的实例数ec2:spot-fleet-request:TargetCapacity—— Spot Fleet 的目标容量appstream:fleet:DesiredCapacity—— AppStream 2.0 车队的容量dynamodb:table:ReadCapacityUnits/dynamodb:table:WriteCapacityUnits—— DynamoDB 表的预留读写容量dynamodb:index:ReadCapacityUnits/dynamodb:index:WriteCapacityUnits—— DynamoDB 全局二级索引的预留读写容量rds:cluster:ReadReplicaCount—— Aurora 集群的只读副本数适用于 Aurora MySQL 与 Aurora PostgreSQL 兼容版sagemaker:variant:DesiredInstanceCount—— SageMaker 端点变体的 EC2 实例数lambda:function:ProvisionedConcurrency—— Lambda 函数的预留并发数cassandra:table:ReadCapacityUnits/cassandra:table:WriteCapacityUnits—— Amazon Keyspaces 表的预留读写容量kafka:broker-storage:VolumeSize—— Amazon MSK 集群 Broker 的预留卷大小GiBelasticache:cache-cluster:Nodes、elasticache:replication-group:NodeGroups、elasticache:replication-group:Replicas—— ElastiCache 集群节点数、复制组节点组数与每组副本数neptune:cluster:ReadReplicaCount—— Neptune 集群的只读副本数workspaces:workspacespool:DesiredUserSessions—— WorkSpaces 池的期望用户会话数custom-resource:ResourceType:Property—— 自定义资源的伸缩维度。响应字段逐条解析从示例文档的返回 JSON 可以看到ScalableTargets数组中每个伸缩目标对象包含以下字段字段示例值含义ServiceNamespaceecs提供该资源的 AWS 服务命名空间ScalableDimensionecs:service:DesiredCount该目标被伸缩的具体维度ResourceIdservice/default/web-app资源标识格式为“资源类型/集群名/服务名”MinCapacity1该维度允许缩容的最低容量MaxCapacity10该维度允许扩容的最高容量RoleARNarn:aws:iam::123456789012:role/...ECSServiceApplication Auto Scaling 操作该资源所用的 IAM 角色 ARNECS 场景下是服务关联角色AWSServiceRoleCreationTime1462558906.199伸缩目标创建时间Unix 时间戳秒级带毫秒精度SuspendedState三个布尔字段挂起状态DynamicScalingInSuspended、DynamicScalingOutSuspended、ScheduledScalingSuspended分别表示是否挂起动态缩容、动态扩容和计划伸缩false表示对应伸缩正常进行ScalableTargetARNarn:aws:application-autoscaling:us-west-2:123456789012:scalable-target/...伸缩目标的 ARN可作为其他操作如deregister-scalable-target、delete-scaling-policy的输入巡检实践中SuspendedState是最值得关注的字段只要任意一项为true对应方向的伸缩就不会生效排障时应优先检查。源码级机制分页定义与错误类型从源码结构看aws-cli 并非手写该命令的分页逻辑而是由 botocore 读取服务模型驱动。在 awscli/botocore/data/application-autoscaling/2016-02-06/paginators-1.json 中DescribeScalableTargets的分页配置为DescribeScalableTargets: { input_token: NextToken, output_token: NextToken, limit_key: MaxResults, result_key: ScalableTargets }这解释了参数文档中--max-results/--next-token的行为单次最多返回 50 条MaxResults上限若结果更多则响应附带NextToken无更多结果时为 null。由于该操作注册了分页器当伸缩目标数量超过单页上限时可以直接使用--pager参数让 CLI 自动翻页并聚合所有结果例如aws application-autoscaling describe-scalable-targets --service-namespace ecs --pager。同一服务模型中还为DescribeScalableTargets声明了四类可能抛出的错误ValidationException参数校验失败如命名空间或维度取值不合法、InvalidNextTokenException令牌无效、ConcurrentUpdateException并发更新冲突、InternalServiceException服务端内部错误。在编写自动化脚本时建议至少对前两类做显式处理。在伸缩目标生命周期中的位置describe-scalable-targets处于伸缩目标生命周期中的“查询”环节可与示例目录中的其他命令配合形成完整工作流注册register-scalable-target.rst 展示了如何注册 ECS 服务为伸缩目标--service-namespace ecs --scalable-dimension ecs:service:DesiredCount --resource-id service/default/web-app --min-capacity 1 --max-capacity 10以及如何用--suspended-state挂起/恢复伸缩注销deregister-scalable-target.rst 展示注销操作服务文档同时注明注销会一并删除该目标关联的伸缩策略与计划任务策略与活动查询describe-scaling-policies.rst 和 describe-scaling-activities.rst 分别用于查看伸缩策略与最近六周的伸缩活动记录计划任务describe-scheduled-actions.rst。一个典型的排查流程是先用describe-scalable-targets --service-namespace ecs确认目标的MinCapacity/MaxCapacity和SuspendedState是否符合预期若伸缩未生效再结合describe-scaling-activities查看具体活动的状态与失败原因。适用前提与限制该命令查询的是当前凭证与区域profile/region可见的伸缩目标ScalableTargetARN中包含区域信息示例为us-west-2跨区域查询需切换 region查询结果为只读操作不会修改任何伸缩配置参数取值范围与字段定义以当前仓库版本内的服务模型为准API 版本2016-02-06后续 botocore 升级可能新增命名空间或维度取值使用时可查阅 awscli/examples/application-autoscaling/ 目录下各命令示例获取同类操作参考。【免费下载链接】aws-cliUniversal Command Line Interface for Amazon Web Services项目地址: https://gitcode.com/GitHub_Trending/aw/aws-cli创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表