資源編排
在服務(wù)商創(chuàng)建計(jì)算巢服務(wù)的過(guò)程中,需要錄入模板。服務(wù)商需要先通過(guò)完成模板編寫,再將模板復(fù)制到計(jì)算巢控制臺(tái)的模板框中或?qū)⒛0逦募蟼魍瓿射浫搿1疚囊詣?chuàng)建VPC為例,介紹如何使用JSON/YAML模板語(yǔ)法來(lái)編寫模板并在ROS控制臺(tái)驗(yàn)證模板。
背景信息
計(jì)算巢中的模板本質(zhì)上是ROS模板。您可以遵循ROS定義的模板規(guī)范編寫資源棧模板,在模板中定義所需的云計(jì)算資源(例如:ECS實(shí)例、RDS數(shù)據(jù)庫(kù)實(shí)例)、資源間的依賴關(guān)系等。ROS的編排引擎將根據(jù)模板自動(dòng)完成所有資源的創(chuàng)建和配置,實(shí)現(xiàn)自動(dòng)化部署及運(yùn)維。資源編排服務(wù)是阿里云提供的一項(xiàng)簡(jiǎn)化云計(jì)算資源管理的服務(wù)。更多信息,請(qǐng)參見(jiàn)什么是資源編排服務(wù)。
模板編寫
ROS支持JSON/YAML和Terraform兩種模板語(yǔ)法。編寫模板時(shí)可以基于模板示例進(jìn)行修改,也可以直接編寫模板。
針對(duì)常見(jiàn)的場(chǎng)景,ROS提供了模板示例,您可以在ROS控制臺(tái)的模板示例中選擇適用的參考模板進(jìn)行修改。其中應(yīng)用場(chǎng)景為ISV軟件部署類別的模板為計(jì)算巢場(chǎng)景常用模板,在模板描述中給出了基于該示例的修改步驟。
如果您采用了模板示例且改動(dòng)簡(jiǎn)單,我們建議您直接進(jìn)入到在ROS控制臺(tái)驗(yàn)證模板的步驟,在創(chuàng)建資源棧的過(guò)程中選擇模板示例并修改。
針對(duì)復(fù)雜的場(chǎng)景,需要您自己編寫模板時(shí),您可以使用任何文本編輯工具來(lái)編寫模板。
針對(duì)JSON/YAML模板,我們推薦您在VSCode或者IntelliJ IDEA上安裝插件Alibaba Cloud Toolkit來(lái)編寫模板,以便獲得語(yǔ)法自動(dòng)提示。下載及使用方法請(qǐng)參考使用Alibaba Cloud Toolkit管理模板及資源棧(Visual Studio Code)。
本文以創(chuàng)建ECS的YAML模板為例,介紹創(chuàng)建模板的操作:
ROSTemplateFormatVersion: '2015-09-01'
# 參數(shù)配置
Parameters:
PayType:
Type: String
Label:
en: ECS Instance Charge Type
zh-cn: 付費(fèi)類型
Default: PostPaid
AllowedValues:
- PostPaid
- PrePaid
AssociationProperty: ChargeType
AssociationPropertyMetadata:
LocaleKey: InstanceChargeType
PayPeriodUnit:
Type: String
Label:
en: Pay Period Unit
zh-cn: 購(gòu)買資源時(shí)長(zhǎng)周期
Default: Month
AllowedValues:
- Month
- Year
AssociationProperty: PayPeriodUnit
AssociationPropertyMetadata:
Visible:
Condition:
Fn::Not:
Fn::Equals:
- ${PayType}
- PostPaid
PayPeriod:
Type: Number
Description:
en: When the resource purchase duration is Month, the value of Period ranges from 1 to 9, 12, 24, 36, 48, or 60. <br><b><font color='red'> When ECS instance types are PrePaid valid </b></font>
zh-cn: 當(dāng)購(gòu)買資源時(shí)長(zhǎng)為Month時(shí),Period取值:1~9 <br><b><font color='red'>當(dāng)ECS實(shí)例類型為PrePaid有效</b></font>
Label:
en: Period
zh-cn: 購(gòu)買資源時(shí)長(zhǎng)
Default: 1
AllowedValues:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
AssociationProperty: PayPeriod
AssociationPropertyMetadata:
Visible:
Condition:
Fn::Not:
Fn::Equals:
- ${PayType}
- PostPaid
EcsInstanceType:
Type: String
Label:
en: Instance Type
zh-cn: 實(shí)例類型
AssociationProperty: ALIYUN::ECS::Instance::InstanceType
AssociationPropertyMetadata:
ZoneId: ${ZoneId}
InstanceChargeType: ${InstanceChargeType}
InstancePassword:
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: 服務(wù)器登錄密碼,長(zhǎng)度8-30,必須包含三項(xiàng)(大寫字母、小寫字母、數(shù)字、 ()`~!@#$%^&*_-+=|{}[]:;<>,.?/ 中的特殊符號(hào))
Label:
en: Instance Password
zh-cn: 實(shí)例密碼
ConstraintDescription:
en: Length 8-30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;<>,.?/ Special symbol in)
zh-cn: 長(zhǎng)度8-30,必須包含三項(xiàng)(大寫字母、小寫字母、數(shù)字、 ()`~!@#$%^&*_-+=|{}[]:;<>,.?/ 中的特殊符號(hào))
AssociationProperty: ALIYUN::ECS::Instance::Password
AllowedPattern: '^[a-zA-Z0-9-\(\)\`\~\!\@\#\$\%\^\&\*\_\-\+\=\|\{\}\[\]\:\;\<\>\,\.\?\/]*$'
MinLength: 8
MaxLength: 30
ZoneId:
Type: String
AssociationProperty: ALIYUN::ECS::Instance::ZoneId
Label:
en: VSwitch Availability Zone
zh-cn: 交換機(jī)可用區(qū)
VpcId:
AssociationProperty: ALIYUN::ECS::VPC::VPCId
Type: String
Description:
en: Please search the ID starting with (vpc-xxx) from console-Virtual Private Cloud
zh-cn: 現(xiàn)有虛擬專有網(wǎng)絡(luò)的實(shí)例ID
Label:
en: VPC ID
zh-cn: 專有網(wǎng)絡(luò)VPC實(shí)例ID
VSwitchId:
AssociationProperty: ALIYUN::ECS::VSwitch::VSwitchId
AssociationPropertyMetadata:
VpcId: ${VpcId}
ZoneId: ${ZoneId}
Type: String
Description:
en: Instance ID of existing business network switches, console-Virtual Private Cloud-VSwitches under query
zh-cn: 現(xiàn)有業(yè)務(wù)網(wǎng)絡(luò)交換機(jī)的實(shí)例ID
Label:
en: VSwitch ID
zh-cn: 交換機(jī)實(shí)例ID
# 資源配置
Resources:
EcsSecurityGroup:
Type: ALIYUN::ECS::SecurityGroup
Properties:
SecurityGroupName:
Ref: ALIYUN::StackName
VpcId:
Ref: VpcId
# 安全組入端口
SecurityGroupIngress:
- PortRange: 80/80
Priority: 1
SourceCidrIp: 0.0.0.0/0
IpProtocol: tcp
NicType: internet
EcsInstanceGroup:
Type: ALIYUN::ECS::InstanceGroup
Properties:
# 實(shí)例名
InstanceName:
Fn::Join:
- '-'
- - Ref: ALIYUN::StackName
- '[1,4]'
IoOptimized: optimized
# 付費(fèi)類型
InstanceChargeType:
Ref: PayType
PeriodUnit:
Ref: PayPeriodUnit
Period:
Ref: PayPeriod
# 網(wǎng)絡(luò)配置
VpcId:
Ref: VpcId
ZoneId:
Ref: ZoneId
VSwitchId:
Ref: VSwitchId
SecurityGroupId:
Ref: EcsSecurityGroup
# 磁盤類型和大小
SystemDiskCategory: cloud_essd
SystemDiskSize: 200
MaxAmount: 1
# 鏡像
ImageId: centos_7
# 實(shí)例類型
InstanceType:
Ref: EcsInstanceType
Password:
Ref: InstancePassword
# 公網(wǎng)開(kāi)啟
AllocatePublicIP: true
# 公網(wǎng)帶寬
InternetMaxBandwidthOut: 1
ECSRunCommand:
Type: ALIYUN::ECS::RunCommand
Properties:
InstanceIds:
Fn::GetAtt:
- EcsInstanceGroup
- InstanceIds
Type: RunShellScript
Sync: true
Timeout: 3600
CommandContent: |-
#!/bin/bash
# 定義輸出
Outputs:
# 將公網(wǎng)ip做為http返回的地址顯示在控制臺(tái)
Endpoint:
Description:
zh-cn: 對(duì)外暴露的公網(wǎng)IP地址
en: Public IP Addresses
Value:
Fn::Sub:
- http://${ServerAddress}
- ServerAddress:
Fn::Select:
- 0
- Fn::GetAtt:
- EcsInstanceGroup
- PublicIps
Metadata:
ALIYUN::ROS::Interface:
# 分組信息
ParameterGroups:
- Parameters:
- PayType
- PayPeriodUnit
- PayPeriod
Label:
default:
en: PayType Configuration
zh-cn: 付費(fèi)類型配置
- Parameters:
- EcsInstanceType
- InstancePassword
Label:
default:
en: ECS Instance Configuration
zh-cn: ECS實(shí)例配置
- Parameters:
- ZoneId
- VpcId
- VSwitchId
Label:
default:
zh-cn: 選擇已有基礎(chǔ)資源
en: Choose existing Infrastructure
模板中主要定義了參數(shù)(Parameters)、資源(Resources)、輸出(Outputs)以及元數(shù)據(jù)(Metadata):
Parameters
參數(shù)(Parameters)定義了用戶在創(chuàng)建服務(wù)實(shí)例時(shí),需要設(shè)置的參數(shù)。本文示例中,Parameters中包含以下參數(shù):
參數(shù)名稱 | 參數(shù)描述 |
PayType | 付費(fèi)類型 |
PayPeriodUnit | 購(gòu)買資源時(shí)長(zhǎng)周期 |
PayPeriod | 購(gòu)買資源時(shí)長(zhǎng) |
EcsInstanceType | 實(shí)例類型 |
InstancePassword | 實(shí)例密碼 |
ZoneId | 交換機(jī)可用區(qū) |
VpcId | 專有網(wǎng)絡(luò)VPC實(shí)例ID |
VSwitchId | 交換機(jī)實(shí)例ID |
Resources
資源(Resources)定義了需要?jiǎng)?chuàng)建的資源,以及每一個(gè)資源包含的Type
和Properties
。本文示例中,Resources中包含的資源如下表所示。
ROS中定義的資源類型 | 資源說(shuō)明 |
ALIYUN::ECS::SecurityGroup | 創(chuàng)建ECS安全組 |
ALIYUN::ECS::InstanceGroup | 創(chuàng)建ECS實(shí)例 |
ALIYUN::ECS::RunCommand | 在ECS實(shí)例中執(zhí)行Shell腳本 |
針對(duì)每一種阿里云的資源類型,您都可以在資源類型索引處查找其語(yǔ)法、屬性和返回值(即輸出參數(shù)),用于編寫模板時(shí)的參考,以申明對(duì)于資源的具體要求。
Outputs
輸出(Outputs)定義了用戶創(chuàng)建服務(wù)完畢后,需要輸出的信息。本文示例中,Outputs以Endpoint(http://PublicIp)
作為輸出。用戶在資源創(chuàng)建完畢后,即可獲取訪問(wèn)服務(wù)的地址。
Metadata
元數(shù)據(jù)(Metadata)不僅支持對(duì)Parameters中定義的參數(shù)進(jìn)行分組,還支持對(duì)自定義參數(shù)進(jìn)行隱藏,您可以在資源編排控制臺(tái)的配置參數(shù)頁(yè)面查看效果。本文示例中,參數(shù)分組如下:
分組名稱 | 參數(shù)名稱 |
PayType Configuration | PayType |
PayPeriodUnit | |
PayPeriod | |
ECS Instance Configuration | EcsInstanceType |
InstancePassword | |
Choose existing Infrastructure | ZoneId |
VpcId | |
VSwitchId |
您還可以使用進(jìn)階功能Mappings、Conditions,進(jìn)階功能的詳細(xì)信息,請(qǐng)參見(jiàn)模板結(jié)構(gòu)說(shuō)明。
您還可以使用Terraform創(chuàng)建模板,詳細(xì)操作請(qǐng)參見(jiàn)創(chuàng)建Terraform類型模板。
在ROS控制臺(tái)驗(yàn)證模板
完成模板編寫后,您可以通過(guò)ROS控制臺(tái)創(chuàng)建資源棧進(jìn)行驗(yàn)證。
登錄ROS控制臺(tái)。
在左側(cè)導(dǎo)航欄中,單擊資源棧。
在頁(yè)面左上角,選擇創(chuàng)建資源棧>使用ROS。
在創(chuàng)建資源棧頁(yè)面,完成頁(yè)面信息配置。
在指定模板框中,可以選擇選擇已有模板,將之前編寫完成的模板復(fù)制到模板內(nèi)容中;也可以選擇使用示例模板,選擇示例模板呈現(xiàn)在模板內(nèi)容中后進(jìn)行修改。
單擊下一步,進(jìn)入配置參數(shù)頁(yè)面,檢查參數(shù)的正確性。并通過(guò)預(yù)覽模板資源功能驗(yàn)證資源設(shè)置是否正確。
可選:單擊下一步,進(jìn)入合規(guī)預(yù)檢(可選)頁(yè)面。完成頁(yè)面信息配置。
可選:單擊下一步,進(jìn)入檢查并確認(rèn)(可選)頁(yè)面,確認(rèn)配置信息。
單擊創(chuàng)建。
說(shuō)明配置資源棧(可選)和檢查并確認(rèn)(可選)為可選步驟,可根據(jù)實(shí)際規(guī)劃選擇是否需要執(zhí)行這兩步。
創(chuàng)建資源棧成功即校驗(yàn)?zāi)0宄晒Γ绻麆?chuàng)建失敗,請(qǐng)查看失敗原因,并調(diào)整模板。