全部產品
Search
文件中心

Resource Orchestration Service:使用伸縮組管理多台Elastic Compute Service

更新時間:Aug 24, 2023

本文以使用伸縮組管理多台Elastic Compute Service為例,由簡入難地向您介紹如何編輯ROS模板。

前提條件

請您提前瞭解模板文法和結構。更多資訊,請參見模板快速入門

情境樣本

在阿里雲專用網路中建立一個伸縮組並配置伸縮規則,建立ECS執行個體並加入到伸縮組中,最後根據已設定的伸縮規則對組內的Elastic Compute Service實現擴容或縮容。2023-03-29_10-36-10

使用須知

您可以訪問對應的資源類型查看屬性詳情。具體操作,請參見查看資源類型

資源類型為每個屬性定義了類型、是否必須、是否允許更新等資訊。如果為必須,則要求必須在模板Resources的Properties中聲明該屬性;反之,則為非必須。如果為允許更新,則可以在新模板中修改該屬性,然後使用修改後的模板更新資源棧以達到更新雲資源屬性的目的;反之,則不允許更新。

編輯模板

您可以通過資源類型索引文檔尋找所需的資源類型。更多資訊,請參見資源類型索引

例如:當前情境中需要建立Virtual Private Cloud(ALIYUN::ECS::VPC)、Elastic Compute Service(ALIYUN::ECS::Instance)和伸縮組(ALIYUN::ESS::ScalingGroup)。還需要建立Elastic Compute Service所使用的交換器(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

定義Elastic Compute Service資源

您可以通過模板定義Elastic Compute Service資源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

Elastic Compute Service配置

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://www.alibabacloud.com/help/doc-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