使用伸缩组管理多台云服务器ECS

本文以使用伸缩组管理多台云服务器ECS为例,由简入难地向您介绍如何编辑ROS模板。

前提条件

请您提前了解模板语法和结构。更多信息,请参见模板快速入门

场景示例

在阿里云专有网络中创建一个伸缩组并配置伸缩规则,创建ECS实例并加入到伸缩组中,最后根据已设置的伸缩规则对组内的云服务器ECS实现扩容或缩容。2023-03-29_10-36-10

使用须知

您可以访问对应的资源类型查看属性详情。具体操作,请参见查看资源类型

资源类型为每个属性定义了类型、是否必须、是否允许更新等信息。如果为必须,则要求必须在模板Resources的Properties中声明该属性;反之,则为非必须。如果为允许更新,则可以在新模板中修改该属性,然后使用修改后的模板更新资源栈以达到更新云资源属性的目的;反之,则不允许更新。

编辑模板

您可以通过资源类型索引文档查找所需的资源类型。更多信息,请参见资源类型索引

例如:当前场景中需要创建专有网络VPC(ALIYUN::ECS::VPC)、云服务器ECS(ALIYUN::ECS::Instance)和伸缩组(ALIYUN::ESS::ScalingGroup)。还需要创建云服务器ECS所使用的交换机(ALIYUN::ECS::VSwitch)和安全组(ALIYUN::ECS::SecurityGroup)。完成资源创建后,您还需要为伸缩组添加伸缩配置(ALIYUN::ESS::ScalingConfiguration)并启动伸缩组(ALIYUN::ESS::ScalingGroupEnable)。

定义模板资源及其依赖关系

定义基础网络资源

您可以通过模板定义基础网络资源VpcVSwitchEcsSecurityGroup

  • 使用Ref与伪参数ALIYUN::StackName获取资源栈名称作为资源属性的属性值,例如Vpc中的VpcNameVSwitch中的VSwitchName。更多信息,请参见RefALIYUN::StackName

  • 使用Fn::Select与Fn::GetAZs函数结合伪参数ALIYUN::Region获取资源栈所在地域的第一个可用区ID,例如VSwitch中的ZoneId。更多信息,请参见函数(Functions)ALIYUN::Region

Resources:
  Vpc:
    Type: ALIYUN::ECS::VPC
    Properties:
      CidrBlock: 192.168.0.0/16
      VpcName:
        Ref: ALIYUN::StackName
  VSwitch:
    Type: ALIYUN::ECS::VSwitch
    Properties:
      VSwitchName:
        Ref: ALIYUN::StackName
      VpcId:
        Ref: Vpc
      ZoneId:
        Fn::Select:
          - '0'
          - Fn::GetAZs:
              Ref: ALIYUN::Region
      CidrBlock: 192.168.0.0/24
  EcsSecurityGroup:
    Type: ALIYUN::ECS::SecurityGroup
    Properties:
      SecurityGroupName:
        Ref: ALIYUN::StackName
      VpcId:
        Ref: Vpc
      SecurityGroupEgress:
        - PortRange: '-1/-1'
          Priority: 1
          IpProtocol: all
          DestCidrIp: 0.0.0.0/0
          NicType: intranet

定义云服务器ECS资源

您可以通过模板定义云服务器ECS资源EcsInstanceGroup

使用Ref引用模板中资源的逻辑名称,例如Vpc中的VpcIdEcsSecurityGroup中的SecurityGroupIdVSwitch中的VSwitchId。更多信息,请参见Ref

Resources:
	EcsInstanceGroup:
    Type: ALIYUN::ECS::Instance
    Properties:
      VpcId:
        Ref: Vpc
      SecurityGroupId:
        Ref: EcsSecurityGroup
      VSwitchId:
        Ref: VSwitch
      ImageId: centos_7
      AllocatePublicIP: false
      InstanceType: ecs.c5.large
      SystemDiskSize: 40
      SystemDiskCategory: cloud_essd
      Password:
        Ref: EcsInstancePassword

定义伸缩组资源

您可以通过模板定义伸缩组资源EssInstanceScalingGroupEssInstanceScalingGroupEnableEssInstanceScalingConfiguration

  • 使用Fn::GetAtt函数获取资源输出属性值,例如EssInstanceScalingGroupEnable中的InstanceIds。更多信息,请参见Fn::GetAtt

  • 使用Fn::Sub将输入字符串中的变量替换为指定的值,例如EssInstanceScalingConfiguration中的ScalingConfigurationName。更多信息,请参见Fn::Sub

Resources:
	EssInstanceScalingGroup:
    Type: ALIYUN::ESS::ScalingGroup
    Properties:
      ScalingGroupName:
        Ref: ALIYUN::StackName
      RemovalPolicys:
        - NewestInstance
      MinSize: 3
      MaxSize: 50
      VSwitchId:
        Ref: VSwitch
      DefaultCooldown: 300
  EssInstanceScalingConfiguration:
    Type: ALIYUN::ESS::ScalingConfiguration
    Properties:
      SecurityGroupId:
        Ref: EcsSecurityGroup
      ScalingGroupId:
        Ref: EssInstanceScalingGroup
      ScalingConfigurationName:
        Fn::Sub: sc-${ALIYUN::StackName}
      InstanceType: ecs.c5.large
      SystemDiskCategory: cloud_essd
      SystemDiskSize: 200
      ImageId: centos_7_9_x64_20G_alibase_20220727.vhd
      InstanceName:
        Fn::Join:
          - '-'
          - - Ref: ALIYUN::StackName
            - '[1,4]'
  EssInstanceScalingGroupEnable:
    Type: ALIYUN::ESS::ScalingGroupEnable
    Properties:
      ScalingRuleArisExecuteVersion: '1'
      ScalingConfigurationId:
        Ref: EssInstanceScalingConfiguration
      InstanceIds:
        Fn::GetAtt:
          - EcsInstanceGroup
          - InstanceIds
      ScalingGroupId:
        Ref: EssInstanceScalingGroup

完整模板示例

ROSTemplateFormatVersion: '2015-09-01'
Description: { }
Parameters:
  EcsInstancePassword:
    NoEcho: true
    Type: String
    Description:
      en: Server login password, Length 8~30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in).
      zh-cn: 服务器登录密码,长度8~30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符号)。
    AllowedPattern: '[0-9A-Za-z\_\-\&:;''<>,=%`~!@#\(\)\$\^\*\+\|\{\}\[\]\.\?\/]+$'
    Label:
      en: Instance Password
      zh-cn: 实例密码
    ConstraintDescription:
      en: Length 8~30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in).
      zh-cn: 长度8~30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符号)。
    MinLength: 8
    MaxLength: 30
Resources:
  Vpc:
    Type: ALIYUN::ECS::VPC
    Properties:
      CidrBlock: 192.168.0.0/16
      VpcName:
        Ref: ALIYUN::StackName
  VSwitch:
    Type: ALIYUN::ECS::VSwitch
    Properties:
      VSwitchName:
        Ref: ALIYUN::StackName
      VpcId:
        Ref: Vpc
      ZoneId:
        Fn::Select:
          - '0'
          - Fn::GetAZs:
              Ref: ALIYUN::Region
      CidrBlock: 192.168.0.0/24
  EcsSecurityGroup:
    Type: ALIYUN::ECS::SecurityGroup
    Properties:
      SecurityGroupName:
        Ref: ALIYUN::StackName
      VpcId:
        Ref: Vpc
      SecurityGroupEgress:
        - PortRange: '-1/-1'
          Priority: 1
          IpProtocol: all
          DestCidrIp: 0.0.0.0/0
          NicType: intranet
  EcsInstance:
    Type: ALIYUN::ECS::Instance
    Properties:
      VpcId:
        Ref: Vpc
      SecurityGroupId:
        Ref: EcsSecurityGroup
      VSwitchId:
        Ref: VSwitch
      ImageId: centos_7
      AllocatePublicIP: false
      InstanceType: ecs.c5.large
      SystemDiskSize: 40
      SystemDiskCategory: cloud_essd
      Password:
        Ref: EcsInstancePassword
  EssInstanceScalingGroup:
    Type: ALIYUN::ESS::ScalingGroup
    Properties:
      ScalingGroupName:
        Ref: ALIYUN::StackName
      RemovalPolicys:
        - NewestInstance
      MinSize: 3
      MaxSize: 50
      VSwitchId:
        Ref: VSwitchId
      DefaultCooldown: 300
  EssInstanceScalingConfiguration:
    Type: ALIYUN::ESS::ScalingConfiguration
    Properties:
      SecurityGroupId:
        Ref: EcsSecurityGroup
      ScalingGroupId:
        Ref: EssInstanceScalingGroup
      ScalingConfigurationName:
        Fn::Sub: sc-${ALIYUN::StackName}
      InstanceType: ecs.c5.large
      SystemDiskCategory: cloud_essd
      SystemDiskSize: 200
      ImageId: centos_7_9_x64_20G_alibase_20220727.vhd
      InstanceName:
        Fn::Join:
          - '-'
          - - Ref: ALIYUN::StackName
            - '[1,4]'
  EssInstanceScalingGroupEnable:
    Type: ALIYUN::ESS::ScalingGroupEnable
    Properties:
      ScalingRuleArisExecuteVersion: '1'
      ScalingConfigurationId:
        Ref: EssInstanceScalingConfiguration
      InstanceIds:
        Fn::GetAtt:
          - EcsInstanceGroup
          - InstanceIds

添加模板参数分组和动态获取参数配置

在以上模板中完成了对多种资源及其依赖关系的定义,其中EcsInstance属性的InstanceTypeSystemDiskCategory属性值为固定值。当您在不同地域创建资源栈时,需要多次调整模板内容和变更资源属性以达到部署资源栈的目的。

您可以对模板添加参数Parameters,从而提高模板的灵活性和可复用性。

添加模板参数分组

您可以在模板中使用元数据(Metadata)对Parameters中定义的参数进行分组,并定义参数分组标签。

在完成对模板资源和参数的定义后,您可以按照不同资源与资源对应的参数进行参数分组。 以当前模板为例,您可以将资源按照如下结果划分。

资源参数分类

资源名称

参数名称

基础网络配置

VpcVSwitchEcsSecurityGroup

VSwitchZoneIdVpcCidrBlockVSwitchCidrBlock

云服务器ECS配置

EcsInstance

ECSInstanceTypeECSInstanceGroupCountECSImageIdECSDiskCategoryEcsInstancePassword

伸缩组资源配置

EssInstanceScalingGroupEssInstanceScalingGroupEnableEssInstanceScalingConfiguration

ESSGroupMaxSize

动态获取参数配置

EcsInstanceType参数为例,当您需要在控制台上对参数设置筛选条件并动态选择参数配置时,可以按照参数对应的资源类型(ALIYUN::ECS::Instance)在AssociationProperty和AssociationPropertyMetadata文档中查询到该参数支持的AssociationProperty取值(ALIYUN::ECS::Instance::InstanceType),然后查看对筛选到的AssociationProperty设置过滤条件为ZoneIdAssociationPropertyMetadata取值。更多信息,请参见AssociationProperty和AssociationPropertyMetadata

完整模板示例

ROSTemplateFormatVersion: '2015-09-01'
Description:
  en: scaling-ecs.
  zh-cn: ECS部署,支持弹性功能。
Parameters:
  VSwitchZoneId:
    Type: String
    AssociationProperty: ALIYUN::ECS::Instance::ZoneId
    Description:
      en: Availability ID for existing switches.
      zh-cn: 现有交换机的可用区ID。
    Label:
      en: VSwitch Zone ID
      zh-cn: 交换机可用区
  VpcCidrBlock:
    Default: 192.168.0.0/16
    Label:
      zh-cn: 专有网络网段
      en: VPC CIDR Block
    Type: String
    Description:
      zh-cn: 新建专有网络IP地址段范围,推荐使用以下的IP地址段<br><font color='green'>[10.0.XX.XX/8]</font><br><font color='green'>[172.16.XX.XX/12]</font><br><font color='green'>[192.168.XX.XX/16]</font>。
      en: New proprietary network IP address segment range, recommended use of the following IP address segments<br><font color='green'>[10.0.XX.XX/8]</font><br><font color='green'>[172.16.XX.XX/12]</font><br><font color='green'>[192.168.XX.XX/16]</font>.
  VSwitchCidrBlock:
    Default: 192.168.0.0/24
    Type: String
    Description:
      zh-cn: 必须是所属专有网络的子网段,并且没有被其他交换机占用。
      en: Must be a sub-network segment of the proprietary network and is not occupied by other VSwitches.
    Label:
      zh-cn: 交换机网段
      en: VSwitch CIDR Block
  ECSInstanceType:
    Type: String
    Label:
      en: Instance Type
      zh-cn: 实例类型
    AssociationProperty: ALIYUN::ECS::Instance::InstanceType
    AssociationPropertyMetadata:
      ZoneId: ${VSwitchZoneId}
      InstanceChargeType: ${InstanceChargeType}
  ECSDiskCategory:
    Type: String
    Description:
      en: '<font color=''blue''><b>Optional values:</b></font><br>[cloud_efficiency: <font color=''green''>Efficient Cloud Disk</font>]<br>[cloud_ssd: <font color=''green''>SSD Cloud Disk</font>]<br>[cloud_essd: <font color=''green''>ESSD Cloud Disk</font>]<br>[cloud: <font color=''green''>Cloud Disk</font>]<br>[ephemeral_ssd: <font color=''green''>Local SSD Cloud Disk</font>]'.
      zh-cn: '<font color=''blue''><b>可选值:</b></font><br>[cloud_efficiency: <font color=''green''>高效云盘</font>]<br>[cloud_ssd: <font color=''green''>SSD云盘</font>]<br>[cloud_essd: <font color=''green''>ESSD云盘</font>]<br>[cloud: <font color=''green''>普通云盘</font>]<br>[ephemeral_ssd: <font color=''green''>本地SSD盘</font>]'。
    AssociationProperty: ALIYUN::ECS::Disk::SystemDiskCategory
    AssociationPropertyMetadata:
      ZoneId: ${VSwitchZoneId}
      InstanceType: ${ECSInstanceType}
    Label:
      en: System Disk Type
      zh-cn: 系统盘类型
  ECSImageId:
    AssociationProperty: ALIYUN::ECS::Image::ImageId
    Label:
      zh-cn: ECS镜像ID
      en: Image ID
    Description:
      zh-cn: 镜像ID,ECS实例的镜像资源, <font><a href='https://help.aliyun.com/document_detail/112977.html' target='_blank'><b>查看镜像资源</b></font color='blue'></a>。
      en: Image ID, represents the image resource to startup one ECS instance, <font><a href='https://www.alibabacloud.com/help/doc-detail/112977.html' target='_blank'><b>View image resources</b></font color='blue'></a>.
    Type: String
  EcsInstancePassword:
    NoEcho: true
    Type: String
    Description:
      en: Server login password, Length 8~30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in).
      zh-cn: 服务器登录密码,长度8~30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符号)。
    AllowedPattern: '[0-9A-Za-z\_\-\&:;''<>,=%`~!@#\(\)\$\^\*\+\|\{\}\[\]\.\?\/]+$'
    Label:
      en: Instance Password
      zh-cn: 实例密码
    ConstraintDescription:
      en: Length 8~30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in).
      zh-cn: 长度8~30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符号)。
    MinLength: 8
    MaxLength: 30
    AssociationProperty: ALIYUN::ECS::Instance::Password
  ECSInstanceGroupCount:
    Type: Number
    Description:
      en: ECS Instance Count
      zh-cn: ECS实例数量
    Label:
      en: Instance Count
      zh-cn: 实例数量
    Default: 3
  ESSGroupMaxSize:
    Type: Number
    Label:
      en: The maximum number of ECS instances in ESS.
      zh-cn: ESS内ECS实例个数的最大值。
    Default: 50
Resources:
  Vpc:
    Type: ALIYUN::ECS::VPC
    Properties:
      CidrBlock:
        Ref: VpcCidrBlock
      VpcName:
        Ref: ALIYUN::StackName
  VSwitch:
    Type: ALIYUN::ECS::VSwitch
    Properties:
      VSwitchName:
        Ref: ALIYUN::StackName
      VpcId:
        Ref: Vpc
      ZoneId:
        Ref: VSwitchZoneId
      CidrBlock:
        Ref: VSwitchCidrBlock
  EcsSecurityGroup:
    Type: ALIYUN::ECS::SecurityGroup
    Properties:
      SecurityGroupName:
        Ref: ALIYUN::StackName
      VpcId:
        Ref: Vpc
      SecurityGroupIngress:
        - PortRange: 8080/8080
          Priority: 1
          SourceCidrIp: 0.0.0.0/0
          IpProtocol: tcp
          NicType: internet
      SecurityGroupEgress:
        - PortRange: '-1/-1'
          Priority: 1
          IpProtocol: all
          DestCidrIp: 0.0.0.0/0
          NicType: internet
        - PortRange: '-1/-1'
          Priority: 1
          IpProtocol: all
          DestCidrIp: 0.0.0.0/0
          NicType: intranet
  EcsInstanceGroup:
    Type: ALIYUN::ECS::InstanceGroup
    Properties:
      InstanceName:
        Fn::Join:
          - '-'
          - - Ref: ALIYUN::StackName
            - '[1,4]'
      VpcId:
        Ref: Vpc
      VSwitchId:
        Ref: VSwitch
      SecurityGroupId:
        Ref: EcsSecurityGroup
      SystemDiskCategory:
        Ref: ECSDiskCategory
      SystemDiskSize: 200
      MaxAmount:
        Ref: ECSInstanceGroupCount
      ImageId:
        Ref: ECSImageId
      InstanceType:
        Ref: ECSInstanceType
      Password:
        Ref: EcsInstancePassword
      AllocatePublicIP: false
  EssInstanceScalingGroupEnable:
    Type: ALIYUN::ESS::ScalingGroupEnable
    Properties:
      ScalingRuleArisExecuteVersion: '1'
      ScalingConfigurationId:
        Ref: EssInstanceScalingConfiguration
      InstanceIds:
        Fn::GetAtt:
          - EcsInstanceGroup
          - InstanceIds
      ScalingGroupId:
        Ref: EssInstanceScalingGroup
  EssInstanceScalingConfiguration:
    Type: ALIYUN::ESS::ScalingConfiguration
    Properties:
      SecurityGroupId:
        Ref: EcsSecurityGroup
      ScalingGroupId:
        Ref: EssInstanceScalingGroup
      ScalingConfigurationName:
        Fn::Sub: sc-${ALIYUN::StackName}
      InstanceType:
        Ref: ECSInstanceType
      SystemDiskCategory:
        Ref: ECSDiskCategory
      SystemDiskSize: 200
      ImageId:
        Ref: ECSImageId
      InstanceName:
        Fn::Join:
          - '-'
          - - Ref: ALIYUN::StackName
            - '[1,4]'
  EssInstanceScalingGroup:
    Type: ALIYUN::ESS::ScalingGroup
    Properties:
      ScalingGroupName:
        Ref: ALIYUN::StackName
      RemovalPolicys:
        - NewestInstance
      MinSize:
        Ref: ECSInstanceGroupCount
      MaxSize: 50
      VSwitchId:
        Ref: VSwitch
      DefaultCooldown: 300
Metadata:
  ALIYUN::ROS::Interface:
    ParameterGroups:
      - Parameters:
          - VSwitchZoneId
          - VpcCidrBlock
          - VSwitchCidrBlock
        Label:
          default:
            zh-cn: 基础网络配置
            en: Basic Network Configuration
      - Parameters:
          - ECSInstanceType
          - ECSDiskCategory
          - ECSImageId
          - EcsInstancePassword
          - ECSInstanceGroupCount
        Label:
          default:
            en: Instance
            zh-cn: ECS实例配置
      - Parameters:
          - ESSGroupMaxSize
        Label:
          default:
            zh-cn: 伸缩组配置
            en: ESS Configuration