案例需求:
在多个可用区创建多个ECS实例,弹性伸缩SLB、RDS、ECS资源。
资源编排介绍:
简单介绍下:ROS ROS详细介绍
阿里云资源编排服务(ROS)可帮助用户简化云计算资源管理和自动化运维的服务。
用户遵循ROS定义的模板规范,编写模板文件,在模板中定义所需云计算资源的集合及资源间的依赖关系、资源配置细节等,ROS通过编排引擎自动完成所有资源的创建和配置,以达到自动化部署、运维的目的。
涉及资源类型简介:
ALIYUN::ECS::InstanceGroup: 用于创建一组 ECS 实例;详细介绍
ALIYUN::ECS::VSwitch:用于新建交换机;详细介绍
ALIYUN::ECS::VPC:用于新建专有网络;详细介绍
ALIYUN::ESS::ScalingGroup:用于创建伸缩组;详细介绍
ALIYUN::RDS::DBInstance:用于创建数据库实例;详细介绍
ALIYUN::SLB::LoadBalancer:用于创建负载均衡;详细介绍
ALIYUN::SLB::Listener:用于创建负载均衡监听器;详细介绍
ALIYUN::SLB::BackendServerAttachment:用于添加后端服务器;详细介绍
ALIYUN::ESS::ScalingConfiguration:用于创建伸缩配置;详细介绍
ALIYUN::ESS::ScalingGroupEnable:用于启用伸缩组;详细介绍
方案介绍:
相信大家熟知传统的交换网络,下图采用阿里云VPC产品的示意图,可以看出如果需要在多个可用区域创建资源,可采取下面的方式,创建一个VPC(专有网络),接下来创建多个VSwitch(虚拟交换机),可以完成将多个资源部署在多个可用区域内的:
注意:
本文代码示例完成双可用区创建资源栈;__关于模版代码中的参数具体含义就不一一介绍了, 详情请参考<涉及资源类型简介>。
资源栈感性理解:通过ROS模版的方式创建出一个或多个资源的集合。
需求拆分
- 创建多个虚拟交换机完成多可用区需求,此处举例创建一个VSwitch:
"VSwitch1": {
"Type": "ALIYUN::ECS::VSwitch",
"DependsOn": "Vpc",
"Properties": {
"VSwitchName": {
"Ref": "VSwitch1Name"
},
"VpcId": {
"Ref": "Vpc"
},
"CidrBlock": {
"Ref": "VSW1CidrBlock"
},
"Description": {
"Ref": "VSW1Description"
},
"ZoneId": {
"Ref": "VSW1ZoneId"
}
}
}
- 创建多个ECS实例完成多ECS实例创建的需求,此处举例创建一组ECS实例,并将其部署与一个可用区,同样第二个可用区也创建一组ECS实例:
"EcsInstanceGroup1": {
"Type": "ALIYUN::ECS::InstanceGroup",
"DependsOn": "VSwitch1",
"Properties": {
"IoOptimized": "optimized",
"ImageId": {
"Ref": "EcsGroupImageId"
},
"SecurityGroupId": {
"Ref": "SG"
},
"Password": {
"Ref": "EcsPassword"
},
"MinAmount": {
"Ref": "EcsGroup1MaxAmount"
},
"AllocatePublicIP": "false",
"SystemDiskCategory": {
"Ref": "EcsSystemDiskCategory"
},
"MaxAmount": {
"Ref": "EcsGroup1MaxAmount"
},
"VSwitchId": {
"Fn::GetAtt": [
"VSwitch1",
"VSwitchId"
]
},
"VpcId": {
"Ref": "Vpc"
},
"InstanceType": {
"Ref": "EcsGroupInstanceType"
}
}
}
- 创建弹性伸缩组完成,将RDS和SLB实例都加入到组内,代码示例如下:
"ScalingGroup": {
"Type": "ALIYUN::ESS::ScalingGroup",
"DependsOn": "CreateListener",
"Properties": {
"DBInstanceIds": [
{
"Fn::GetAtt": [
"Database",
"DBInstanceId"
]
}
],
"DefaultCooldown": {
"Ref": "EssDefaultCooldown"
},
"LoadBalancerIds": [
{
"Fn::GetAtt": [
"LoadBalancer",
"LoadBalancerId"
]
}
],
"MaxSize": {
"Ref": "EssMaxSize"
},
"MinSize": {
"Ref": "EssMinSize"
},
"VpcId": {
"Ref": "Vpc"
},
"VSwitchIds": [
{
"Fn::GetAtt": [
"VSwitch1",
"VSwitchId"
]
},
{
"Fn::GetAtt": [
"VSwitch2",
"VSwitchId"
]
}
]
}
}
- 将创建的所有ECS实例,在启用伸缩组时候加入到伸缩组内,代码示例如下:
"ScalingGroupEnable": {
"Type": "ALIYUN::ESS::ScalingGroupEnable",
"DependsOn": "ScalingGroup",
"Properties": {
"ScalingGroupId": {
"Fn::GetAtt": [
"ScalingGroup",
"ScalingGroupId"
]
},
"ScalingConfigurationId": {
"Fn::GetAtt": [
"ScalingConfiguration",
"ScalingConfigurationId"
]
},
"InstanceIds": {
"Fn::ListMerge": [
{
"Fn::GetAtt": [
"EcsInstanceGroup1",
"InstanceIds"
]
},
{
"Fn::GetAtt": [
"EcsInstanceGroup2",
"InstanceIds"
]
}
]
}
}
}
总结:
使用阿里云资源编排服务 ,不需要支付额外的费用。
传统部署打造一个这样的环境,还是需要一定物力、人力、时间的,利用本文中提供的ROS模板,可以很好,很方便的帮你一键部署此类环境,让你把更多的精力放在自己的业务上。
资源编排服务
相关模板
{
"ROSTemplateFormatVersion": "2015-09-01",
"Description": "Create Multi-ECS And Scaling Group And Rds And Slb In Double Zone.",
"Conditions": {
"CreateInstacneGroup2": {
"Fn::Not": {
"Fn::Equals": [
0,
{
"Ref": "EcsGroup2MaxAmount"
}
]
}
}
},
"Resources": {
"Vpc": {
"Type": "ALIYUN::ECS::VPC",
"Properties": {
"VpcName": {
"Ref": "VpcName"
},
"CidrBlock": {
"Ref": "VPCCidrBlock"
},
"Description": {
"Ref": "VPCDescription"
}
}
},
"VSwitch1": {
"Type": "ALIYUN::ECS::VSwitch",
"DependsOn": "Vpc",
"Properties": {
"VSwitchName": {
"Ref": "VSwitch1Name"
},
"VpcId": {
"Ref": "Vpc"
},
"CidrBlock": {
"Ref": "VSW1CidrBlock"
},
"Description": {
"Ref": "VSW1Description"
},
"ZoneId": {
"Ref": "VSW1ZoneId"
}
}
},
"VSwitch2": {
"Type": "ALIYUN::ECS::VSwitch",
"DependsOn": "Vpc",
"Properties": {
"VSwitchName": {
"Ref": "VSwitch2Name"
},
"VpcId": {
"Ref": "Vpc"
},
"CidrBlock": {
"Ref": "VSW2CidrBlock"
},
"Description": {
"Ref": "VSW2Description"
},
"ZoneId": {
"Ref": "VSW2ZoneId"
}
}
},
"SG": {
"Type": "ALIYUN::ECS::SecurityGroup",
"DependsOn": "Vpc",
"Properties": {
"VpcId": {
"Ref": "Vpc"
},
"SecurityGroupName": "mysg",
"SecurityGroupIngress": [
{
"PortRange": "-1/-1",
"Priority": 1,
"SourceCidrIp": "0.0.0.0/0",
"IpProtocol": "all",
"NicType": "intranet"
}
],
"SecurityGroupEgress": [
{
"PortRange": "-1/-1",
"Priority": 1,
"IpProtocol": "all",
"DestCidrIp": "0.0.0.0/0",
"NicType": "intranet"
}
]
}
},
"EcsInstanceGroup1": {
"Type": "ALIYUN::ECS::InstanceGroup",
"DependsOn": "VSwitch1",
"Properties": {
"IoOptimized": "optimized",
"ImageId": {
"Ref": "EcsGroupImageId"
},
"SecurityGroupId": {
"Ref": "SG"
},
"Password": {
"Ref": "EcsPassword"
},
"MinAmount": {
"Ref": "EcsGroup1MaxAmount"
},
"AllocatePublicIP": "false",
"SystemDiskCategory": {
"Ref": "EcsSystemDiskCategory"
},
"MaxAmount": {
"Ref": "EcsGroup1MaxAmount"
},
"VSwitchId": {
"Fn::GetAtt": [
"VSwitch1",
"VSwitchId"
]
},
"VpcId": {
"Ref": "Vpc"
},
"InstanceType": {
"Ref": "EcsGroupInstanceType"
}
}
},
"EcsInstanceGroup2": {
"Type": "ALIYUN::ECS::InstanceGroup",
"DependsOn": "VSwitch2",
"Condition": "CreateInstacneGroup2",
"Properties": {
"IoOptimized": "optimized",
"ImageId": {
"Ref": "EcsGroupImageId"
},
"SecurityGroupId": {
"Ref": "SG"
},
"Password": {
"Ref": "EcsPassword"
},
"MinAmount": {
"Ref": "EcsGroup2MaxAmount"
},
"AllocatePublicIP": "false",
"SystemDiskCategory": {
"Ref": "EcsSystemDiskCategory"
},
"MaxAmount": {
"Ref": "EcsGroup2MaxAmount"
},
"VSwitchId": {
"Fn::GetAtt": [
"VSwitch2",
"VSwitchId"
]
},
"VpcId": {
"Ref": "Vpc"
},
"InstanceType": {
"Ref": "EcsGroupInstanceType"
}
}
},
"ScalingGroup": {
"Type": "ALIYUN::ESS::ScalingGroup",
"DependsOn": "CreateListener",
"Properties": {
"DBInstanceIds": [
{
"Fn::GetAtt": [
"Database",
"DBInstanceId"
]
}
],
"DefaultCooldown": {
"Ref": "EssDefaultCooldown"
},
"LoadBalancerIds": [
{
"Fn::GetAtt": [
"LoadBalancer",
"LoadBalancerId"
]
}
],
"MaxSize": {
"Ref": "EssMaxSize"
},
"MinSize": {
"Ref": "EssMinSize"
},
"VpcId": {
"Ref": "Vpc"
},
"VSwitchIds": [
{
"Fn::GetAtt": [
"VSwitch1",
"VSwitchId"
]
},
{
"Fn::GetAtt": [
"VSwitch2",
"VSwitchId"
]
}
]
}
},
"Database": {
"Type": "ALIYUN::RDS::DBInstance",
"DependOn": "EcsInstanceGroup1",
"Properties": {
"DBInstanceClass": {
"Ref": "RdsDBInstanceClass"
},
"DBInstanceNetType": {
"Ref": "RdsDBInstanceNetType"
},
"DBInstanceStorage": {
"Ref": "RdsDBInstanceStorage"
},
"Engine": {
"Ref": "RdsEngine"
},
"EngineVersion": {
"Ref": "RdsEngineVersion"
},
"SecurityIPList": {
"Ref": "RdsSecurityIPList"
},
"VpcId": {
"Ref": "Vpc"
},
"VSwitchId": {
"Fn::GetAtt": [
"VSwitch1",
"VSwitchId"
]
}
}
},
"LoadBalancer": {
"Type": "ALIYUN::SLB::LoadBalancer",
"Properties": {
"AddressType": "internet"
}
},
"CreateListener": {
"Type": "ALIYUN::SLB::Listener",
"DependOn": "LoadBalancer",
"Properties": {
"BackendServerPort": 8080,
"Bandwidth": 50,
"ListenerPort": "80",
"LoadBalancerId": {
"Ref": "LoadBalancer"
},
"Protocol": "http",
"Scheduler": "wrr"
}
},
"Attachment": {
"Type": "ALIYUN::SLB::BackendServerAttachment",
"DependOn": "Database",
"Properties": {
"BackendServerList": {
"Fn::ListMerge": [
{
"Fn::GetAtt": [
"EcsInstanceGroup1",
"InstanceIds"
]
},
{
"Fn::GetAtt": [
"EcsInstanceGroup2",
"InstanceIds"
]
}
]
},
"LoadBalancerId": {
"Ref": "LoadBalancer"
}
}
},
"ScalingConfiguration": {
"Type": "ALIYUN::ESS::ScalingConfiguration",
"Properties": {
"DiskMappings": [
{
"Category": "cloud_efficiency",
"Device": "/dev/xvdb",
"Size": 100
}
],
"SystemDisk_Category": "cloud_efficiency",
"ScalingGroupId": {
"Fn::GetAtt": [
"ScalingGroup",
"ScalingGroupId"
]
},
"SecurityGroupId": {
"Fn::GetAtt": [
"SG",
"SecurityGroupId"
]
},
"UserData": {
"Fn::Join": [
"",
[
"#!/bin/sh\n",
"echo \"nihao\" > /tmp/test_result.log\n"
]
]
},
"ImageId": {
"Ref": "EcsGroupImageId"
},
"InstanceType": {
"Ref": "EcsGroupInstanceType"
}
}
},
"ScalingGroupEnable": {
"Type": "ALIYUN::ESS::ScalingGroupEnable",
"DependsOn": "ScalingGroup",
"Properties": {
"ScalingGroupId": {
"Fn::GetAtt": [
"ScalingGroup",
"ScalingGroupId"
]
},
"ScalingConfigurationId": {
"Fn::GetAtt": [
"ScalingConfiguration",
"ScalingConfigurationId"
]
},
"InstanceIds": {
"Fn::ListMerge": [
{
"Fn::GetAtt": [
"EcsInstanceGroup1",
"InstanceIds"
]
},
{
"Fn::GetAtt": [
"EcsInstanceGroup2",
"InstanceIds"
]
}
]
}
}
}
},
"Parameters": {
"VpcName": {
"Type": "String",
"Label": "Name For Create VPC",
"Description": "Display name of the vpc instance, [2, 128] English or Chinese characters, must start with a letter or Chinese in size, can contain numbers, '_' or '.', '-'",
"Default": "TwoAvailableZonesVPC"
},
"VPCCidrBlock": {
"Type": "String",
"Label": "The VPC Cidrblock",
"Description": "The IP address range of the VPC in the CIDR block form. You can use the following IP address ranges and their subnets:\n10.0.0.0/8\n172.16.0.0/12 (Default)\n192.168.0.0/16",
"Default": "192.168.0.0/16"
},
"VPCDescription": {
"Type": "String",
"Label": "The Description Of VPC",
"Description": "Description of the vpc, [2, 256] characters. Do not fill or empty, the default is empty.",
"Default": "Description of the two available zones VPC"
},
"VSwitch1Name": {
"Type": "String",
"Label": "Name For Create Zone 1 VSwitch",
"Description": "Display name of the vSwitch instance, [2, 128] English or Chinese characters, must start with a letter or Chinese in size, can contain numbers, '_' or '.', '-'",
"Default": "Zone1VSitchName"
},
"VSW1CidrBlock": {
"Type": "String",
"Label": "The VSwitch 1 Cidrblock",
"Description": "CIDR Block of created VSwitch, It must belong to itself VPC CIDR block.",
"Default": "192.168.1.0/24"
},
"VSW1Description": {
"Type": "String",
"Label": "The Description Of The Zone 1 VSwitch",
"Description": "Description of the VSwitch, [2, 256] characters. Do not fill or empty, the default is empty.",
"Default": "Description of the available zone 1 VSwitch"
},
"VSW1ZoneId": {
"Type": "String",
"Label": " The VSwitch 1 Available Zone ID",
"Description": "VSwitch Available Zone ID, <a href='#/product/cn-beijing/list/zoneList' target='_blank'>View zoneid info</a>"
},
"VSwitch2Name": {
"Type": "String",
"Label": "Name For Create Zone 2 VSwitch",
"Description": "Display name of the vSwitch instance, [2, 128] English or Chinese characters, must start with a letter or Chinese in size, can contain numbers, '_' or '.', '-'",
"Default": "Zone2VSitchName"
},
"VSW2CidrBlock": {
"Type": "String",
"Label": "The VSwitch 2 Cidrblock",
"Description": "CIDR Block of created VSwitch, It must belong to itself VPC CIDR block.",
"Default": "192.168.2.0/24"
},
"VSW2Description": {
"Type": "String",
"Label": "The Description Of The Zone 2 VSwitch",
"Description": "Description of the VSwitch, [2, 256] characters. Do not fill or empty, the default is empty.",
"Default": "Description of the available zone 2 VSwitch"
},
"VSW2ZoneId": {
"Type": "String",
"Label": "The VSwitch 2 Available Zone ID",
"Description": "VSwitch Available Zone ID, <a href='#/product/cn-beijing/list/zoneList' target='_blank'>View zoneid info</a>"
},
"EcsGroupImageId": {
"Type": "String",
"Label": "Ecs Image Id",
"Description": "Image Id, represents the image resource to startup the ECS instance, <a href='#/product/cn-beijing/list/imageList' target='_blank'>View image resources</a>",
"Default": "centos_7_04_64_20G_alibase_201701015.vhd"
},
"EcsGroupInstanceType": {
"Type": "String",
"Label": "Ecs Instance type",
"Description": "The ECS instance type, <a href='#/product/cn-beijing/list/typeList' target='_blank'>View instance types</a>",
"Default": "ecs.c5.large",
"AllowedValues": [
"ecs.c5.large",
"ecs.c5.xlarge",
"ecs.g5.large",
"ecs.g5.xlarge"
]
},
"EcsPassword": {
"Type": "String",
"Label": "Ecs Password",
"ConstraintDescription": "[8, 30] characters, consists of 3 types of uppercase letter, lowercase letter, number or special characters.",
"Description": "The login password of ECS instance",
"MaxLength": 30,
"MinLength": 8,
"NoEcho": true,
"Confirm": true
},
"EcsSystemDiskCategory": {
"Type": "String",
"Label": "Ecs System Disk Category",
"Description": "System disk category: efficient cloud disk(cloud_efficiency) or SSD cloud disk(cloud_ssd)",
"Default": "cloud_ssd",
"AllowedValues": [
"cloud_efficiency",
"cloud_ssd"
]
},
"EssDefaultCooldown": {
"Type": "Number",
"Label": "The Default Cooldown",
"Description": "The Default cooldown in seconds",
"Default": 300
},
"EcsGroup1MaxAmount": {
"Type": "Number",
"Description": "The number of Ecs Group1 instance.",
"Label": "Number of Ecs Group1 instance",
"Default": 2,
"AllowedValues": [
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
]
},
"EcsGroup2MaxAmount": {
"Type": "Number",
"Label": "Number of Ecs Group2 instance",
"Description": "The number of Ecs Group2 iinstance.",
"Default": 2,
"AllowedValues": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
]
},
"EssMaxSize": {
"Type": "Number",
"Label": "The Max Number Of ECS In Scaling Group",
"Description": "The maximum of ECS instances in scaling group",
"MaxValue": 100,
"MinValue": 1,
"Default": 20
},
"EssMinSize": {
"Type": "Number",
"Label": "The Min Number Of ECS In Scaling Group",
"Description": "The minimum of ECS instances in scaling group",
"MaxValue": 100,
"MinValue": 1,
"Default": 1
},
"RdsDBInstanceClass": {
"Type": "String",
"Label": "Rds Instance Class",
"Description": "Database instance type. Refer the RDS database instance type,<a href='https://help.aliyun.com/document_detail/26312.html' target='_blank'>View RDS resources type</a>",
"AllowedValues": [
"rds.mysql.t1.small",
"rds.mysql.s1.small",
"rds.mysql.s2.large",
"rds.mysql.s2.xlarge",
"rds.mysql.s3.large",
"rds.mysql.m1.medium",
"rds.mysql.c1.large",
"rds.mysql.c1.xlarge",
"rds.mysql.c2.xlarge",
"rds.mysql.c2.xlp2"
],
"Default": "rds.mysql.t1.small"
},
"RdsDBInstanceNetType": {
"Type": "String",
"Label": "Database Instance Net Type",
"Description": "Database instance net type, default is Intranet.Internet for public access, Intranet for private access.",
"AllowedValues": [
"Internet",
"Intranet"
],
"Default": "Intranet"
},
"RdsDBInstanceStorage": {
"Type": "Number",
"Label": "Database Instance Storage Size",
"Description": "Incrementing in every 5G, unit: GB",
"ConstraintDescription": "Incrementing in every 5G, unit: GB",
"MaxValue": 2000,
"MinValue": 5,
"Default": 10
},
"RdsEngine": {
"Type": "String",
"Label": "Database Instance Engine Type",
"Description": "Database instance engine type. Support MySQL/SQLServer/PostgreSQL/PPAS/MariaDB now.",
"AllowedValues": [
"MySQL",
"SQLServer",
"PostgreSQL",
"PPAS"
],
"Default": "MySQL"
},
"RdsEngineVersion": {
"Type": "String",
"Label": "Database Engine Version",
"AllowedValues": [
"5.5",
"5.6",
"2008r2",
"9.4",
"9.3"
],
"Description": "MySQL:5.5/5.6; SQLServer:2008r2; PostgreSQL:9.4; PPAS:9.3",
"Default": "5.6"
},
"RdsSecurityIPList": {
"Type": "String",
"Label": "Security Ip To access The Database",
"Description": "The ECS instances IP list access database, separated by commas, do not be repeated",
"Default": "0.0.0.0/0"
}
},
"Outputs": {
"RouteTableId": {
"Description": "The router table id of created VPC.",
"Value": {
"Fn::GetAtt": [
"Vpc",
"RouteTableId"
]
}
},
"VpcId": {
"Description": "Id of created VPC.",
"Value": {
"Fn::GetAtt": [
"Vpc",
"VpcId"
]
}
},
"VRouterId": {
"Description": "Router id of created VPC.",
"Value": {
"Fn::GetAtt": [
"Vpc",
"VRouterId"
]
}
},
"VSwitchId1": {
"Description": "Id of created VSwitch1.",
"Value": {
"Fn::GetAtt": [
"VSwitch1",
"VSwitchId"
]
}
},
"VSwitchId2": {
"Description": "Id of created VSwitch2.",
"Value": {
"Fn::GetAtt": [
"VSwitch2",
"VSwitchId"
]
}
},
"SecurityGroupId": {
"Description": "The Security group id.",
"Value": {
"Fn::GetAtt": [
"SG",
"SecurityGroupId"
]
}
},
"BackendServerList": {
"Description": "Backend server list",
"Value": {
"Fn::ListMerge": [
{
"Fn::GetAtt": [
"EcsInstanceGroup1",
"InstanceIds"
]
},
{
"Fn::GetAtt": [
"EcsInstanceGroup2",
"InstanceIds"
]
}
]
}
}
}
}