compute-nest-best-practice/opensource/consul/distributed_consul.yaml (496 lines of code) (raw):
ROSTemplateFormatVersion: '2015-09-01'
Description:
zh-cn: 一主多从Consul集群自动部署模版,支持自定义从节点数量与配置,包括VPC、安全组、ECS实例资源创建及初始化脚本执行,可选公网访问与付费方式。
en: A primary-multi-follower Consul cluster automatic deployment template, supporting
customization of the number and configurations of follower nodes, encompassing
VPC setup, security group configuration, creation and initialization of ECS instances
with execution of setup scripts, and optional public network access along with
flexible payment options.
Parameters:
VpcId:
AssociationProperty: ALIYUN::ECS::VPC::VPCId
Type: String
Description:
zh-cn: 控制台-VPC-专有网络下查询
en: Please search the ID starts with (vpc-xxx)from console-Virtual Private Cloud
Label:
zh-cn: 现有VPC的实例ID
en: Existing VPC ID
ZoneId:
AssociationProperty: ALIYUN::ECS::Instance::ZoneId
Type: String
Description:
zh-cn: 现有交换机的可用区ID
en: Availability ID for existing switches
Label:
zh-cn: 交换机可用区
en: VSwitch Zone ID
VSwitchId:
AssociationProperty: ALIYUN::ECS::VSwitch::VSwitchId
AssociationPropertyMetadata:
VpcId: VpcId
ZoneId: ZoneId
Type: String
Description:
zh-cn: 现有业务网络交换机的实例ID,控制台-VPC-专有网络-交换机下查询
en: Please search the business VSwitch ID starts with(vsw-xxx)from console-Virtual Private Cloud-VSwitches
Label:
zh-cn: 网络交换机ID
en: VSwitch ID
PayType:
Type: String
Label:
en: ECS Instance Charge Type
zh-cn: 付费类型
Default: PostPaid
AllowedValues:
- PostPaid
- PrePaid
AssociationProperty: ChargeType
AssociationPropertyMetadata:
LocaleKey: InstanceChargeType
PayPeriodUnit:
Type: String
Description:
en: Pay Period Unit
zh-cn: 购买资源时长周期
Label:
en: Pay Period Unit
zh-cn: 购买资源时长周期
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: 当购买资源时长为Month时,Period取值:1~9 <br><b><font color='red'>当ECS实例类型为PrePaid有效</b></font>
Label:
en: Period
zh-cn: 购买资源时长
Default: 1
AllowedValues:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
AssociationProperty: PayPeriod
AssociationPropertyMetadata:
Visible:
Condition:
Fn::Or:
- Fn::Equals:
- ${PayType}
- PrePaid
- Fn::Equals:
- ${PayType}
- undefined
InstancePassword:
AssociationProperty: ALIYUN::ECS::Instance::Password
Type: String
Description:
zh-cn: 服务器登录密码,长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符号)
en: Server login password, Length 8-30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in)
MinLength: 8
Label:
zh-cn: 实例密码
en: Instance Password
AllowedPattern: '[0-9A-Za-z\_\-\&:;''<>,=%`~!@#\(\)\$\^\*\+\|\{\}\[\]\.\?\/]+$'
NoEcho: true
MaxLength: 30
ConstraintDescription:
zh-cn: 长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符号)。
en: Length 8-30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in).
InstanceType:
AssociationProperty: ALIYUN::ECS::Instance::InstanceType
AssociationPropertyMetadata:
ZoneId: ZoneId
Type: String
Description:
zh-cn: 机型选择
en: Model selection
Label:
zh-cn: 实例规格
en: Instance Type
SlaveAmount:
Description:
zh-cn: 由底层一致性协议决定slave节点最好是偶数个,请根据可选值选择合适的从节点数量
en: The minimum of ECS instances, must be less than or equal to the maximum.
Default: 2
ConstraintDescription:
zh-cn: '[2, 10] 内的偶数,最多支持主从11个节点'
en: An integer within [2, 10]
Label:
zh-cn: Slave数量
en: Slave Amount
Type: Number
AllowedValues:
- 2
- 4
- 6
- 8
- 10
AllocatePublicIp:
Type: Boolean
Description:
zh-cn: 是否开启公网访问
en: Whether to enable public network access
Label:
zh-cn: 是否开启公网访问
en: Whether to enable public network access
SystemDiskCategory:
Type: String
AllowedValues:
- cloud_efficiency
- cloud_essd
Label:
en: System Disk Category
zh-cn: 系统盘类型
Default: cloud_essd
AssociationPropertyMetadata:
LocaleKey: DiskCategory
SystemDiskSize:
Default: 100
Type: Number
Label:
zh-cn: 系统盘空间 (GB)
en: System Disk Space (GB)
DataDiskCategory:
Type: String
AllowedValues:
- cloud_efficiency # 高效云盘
- cloud_essd # ESSD云盘
Default: cloud_efficiency
Label:
en: Disk Type
zh-cn: 数据盘类型
AssociationPropertyMetadata:
LocaleKey: DiskCategory
DataDiskSize:
Default: 40
Type: Number
Description:
zh-cn: 数据盘大小, 取值范围:[40, 500], 单位:GB。
en: 'System disk size of each node, range of values: 40-500, units: GB.'
MinValue: 40
MaxValue: 500
Label:
zh-cn: 数据盘空间
en: Data Disk Space
Resources:
SecurityGroup:
Type: ALIYUN::ECS::SecurityGroup
Properties:
SecurityGroupName:
Ref: ALIYUN::StackName
SecurityGroupIngress:
- Priority: 1
PortRange: 8500/8500
NicType: intranet
SourceCidrIp: 0.0.0.0/0
IpProtocol: tcp
VpcId:
Ref: VpcId
RosWaitConditionHandle:
Type: ALIYUN::ROS::WaitConditionHandle
SlaveGroupWaitCondition:
Type: ALIYUN::ROS::WaitCondition
Properties:
Count:
Ref: SlaveAmount
Handle:
Ref: SlaveGroupConditionHandle
Timeout: 1000
ConsulServer:
DependsOn:
- SecurityGroup
Type: ALIYUN::ECS::Instance
Properties:
UserData:
Fn::Sub:
- |
#!/bin/bash
# 挂盘到/data
cat >> /root/InitDataDisk.sh << "EOF"
#!/bin/bash
echo "p
n
p
w
" | fdisk -u /dev/vdb
EOF
/bin/bash /root/InitDataDisk.sh
rm -f /root/InitDataDisk.sh
mkfs -t ext4 /dev/vdb1
cp /etc/fstab /etc/fstab.bak
mkdir /data
echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\\\"//g'` /data ext4 defaults 0 0 >> /etc/fstab
mount -a
#环境初始化
HOSTNAME=`curl http://100.100.100.200/latest/meta-data/instance/instance-name`
IP_SELF=`hostname -I | awk '{print $1}'`
yum install -y java-1.8.0-openjdk-devel.x86_64
mkdir /opt/consul
cd /opt/consul
wget '{{ computenest::file::DemoFirst }}'
yum -y install unzip
unzip consul_1.7.2_linux_amd64.zip
ln -s /opt/consul/consul /usr/bin/consul
echo 'consul agent -server -bootstrap-expect=${NodeNum} -ui -node='$HOSTNAME '-client=0.0.0.0 -bind='$IP_SELF '-data-dir=/data/consul' >> /opt/consul/startConsul.sh
# 这里配置安装脚本
cat >> /usr/lib/systemd/system/consul.service << "EOF"
[Unit]
Description=consul
After=network.target
[Service]
ExecStart=/bin/bash /opt/consul/startConsul.sh
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
# 配置启动脚本
systemctl enable consul
systemctl start consul
${CurlCli} -d "{\"Data\" : \"Success\", \"status\" : \"SUCCESS\"}"
- CurlCli:
Fn::GetAtt:
- RosWaitConditionHandle
- CurlCli
NodeNum:
Fn::Calculate:
- ({0}+{1})
- 0
- - Ref: SlaveAmount
- 1
SystemDiskCategory:
Ref: SystemDiskCategory
SystemDiskSize:
Ref: SystemDiskSize
DiskMappings:
- Category:
Ref: DataDiskCategory
Size:
Ref: DataDiskSize
VpcId:
Ref: VpcId
SecurityGroupId:
Ref: SecurityGroup
PeriodUnit:
Ref: PayPeriodUnit
Period:
Ref: PayPeriod
InstanceChargeType:
Ref: PayType
ZoneId:
Ref: ZoneId
AllocatePublicIP:
Ref: AllocatePublicIp
InternetMaxBandwidthOut:
Fn::If:
- AllocatePublicIpOn
- 10
- 0
IoOptimized: optimized
VSwitchId:
Ref: VSwitchId
Password:
Ref: InstancePassword
InstanceName: Consul-000000
InstanceType:
Ref: InstanceType
ImageId: centos_7
SlaveGroupConditionHandle:
Type: ALIYUN::ROS::WaitConditionHandle
SlaveConsulServer:
DependsOn:
- RosWaitCondition
- SecurityGroup
Type: ALIYUN::ECS::InstanceGroup
Properties:
UserData:
Fn::Sub:
- |
#!/bin/bash
# 挂盘到/data
cat >> /root/InitDataDisk.sh << "EOF"
#!/bin/bash
echo "p
n
p
w
" | fdisk -u /dev/vdb
EOF
/bin/bash /root/InitDataDisk.sh
rm -f /root/InitDataDisk.sh
mkfs -t ext4 /dev/vdb1
cp /etc/fstab /etc/fstab.bak
mkdir /data
echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\\\"//g'` /data ext4 defaults 0 0 >> /etc/fstab
mount -a
#环境初始化
HOSTNAME=`curl http://100.100.100.200/latest/meta-data/instance/instance-name`
IP_SELF=`hostname -I | awk '{print $1}'`
yum install -y java-1.8.0-openjdk-devel.x86_64
mkdir /opt/consul
cd /opt/consul
wget '{{ computenest::file::DemoFirst }}'
yum -y install unzip
unzip consul_1.7.2_linux_amd64.zip
ln -s /opt/consul/consul /usr/bin/consul
nohup consul agent -server -bootstrap-expect=${NodeNum} -ui -node=$HOSTNAME -client=0.0.0.0 -bind=$IP_SELF -data-dir=/data/consul &
sleep 30
nohup consul join ${MasterIp} &
echo 'consul agent -server -bootstrap-expect=${NodeNum} -ui -node='$HOSTNAME '-client=0.0.0.0 -bind='$IP_SELF '-data-dir=/data/consul' >> /opt/consul/startConsul.sh
# 这里配置安装脚本
cat >> /usr/lib/systemd/system/consul.service << "EOF"
[Unit]
Description=consul
After=network.target
[Service]
ExecStart=/bin/bash /opt/consul/startConsul.sh
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
# 配置启动脚本
systemctl enable consul
${CurlCli} -d "{\"Data\" : \"Success\", \"status\" : \"SUCCESS\"}"
- CurlCli:
Fn::GetAtt:
- SlaveGroupConditionHandle
- CurlCli
MasterIp:
Fn::GetAtt:
- ConsulServer
- PrivateIp
NodeNum:
Fn::Calculate:
- ({0}+{1})
- 0
- - Ref: SlaveAmount
- 1
SystemDiskCategory:
Ref: SystemDiskCategory
SystemDiskSize:
Ref: SystemDiskSize
DiskMappings:
- Category:
Ref: DataDiskCategory
Size:
Ref: DataDiskSize
VpcId:
Ref: VpcId
SecurityGroupId:
Ref: SecurityGroup
PeriodUnit:
Ref: PayPeriodUnit
Period:
Ref: PayPeriod
InstanceChargeType:
Ref: PayType
MaxAmount:
Ref: SlaveAmount
ZoneId:
Ref: ZoneId
AllocatePublicIP: false
IoOptimized: optimized
VSwitchId:
Ref: VSwitchId
Password:
Ref: InstancePassword
InstanceName: Consul-[1,6]
InstanceType:
Ref: InstanceType
InternetMaxBandwidthOut: 0
ImageId: centos_7
RosWaitCondition:
Type: ALIYUN::ROS::WaitCondition
Properties:
Count: 1
Handle:
Ref: RosWaitConditionHandle
Timeout: 1000
Conditions:
AllocatePublicIpOn:
Fn::Equals:
- true
- Ref: AllocatePublicIp
Outputs:
MasterNodePrivateIp:
Description:
zh-cn: consul服务启动时主节点私网ip
en: Private network ip of the master node when the service starts
Value:
Fn::GetAtt:
- ConsulServer
- PrivateIp
SlaveNodesPrivateIp:
Description:
zh-cn: consul服务启动时从节点私网ip
en: Private network ip of the slave node when the service starts
Value:
Fn::GetAtt:
- SlaveConsulServer
- PrivateIps
ServicePort:
Description:
zh-cn: 服务端口
en: ServicePort
Value: 8500
ConsulWebUrl:
Description:
zh-cn: consul服务访问URL,开启公网访问返回公网URL,否则是私网URL
en: Consul service access URL, open public network access and return public network URL, otherwise it is private network URL
Value:
Fn::If:
- AllocatePublicIpOn
- Fn::Sub:
- http://${OutIp}:8500
- OutIp:
Fn::GetAtt:
- ConsulServer
- PublicIp
- Fn::Sub:
- http://${OutIp}:8500
- OutIp:
Fn::GetAtt:
- ConsulServer
- PrivateIp
Metadata:
ALIYUN::ROS::Interface:
ParameterGroups:
- Parameters:
- VpcId
- ZoneId
- VSwitchId
Label:
default:
zh-cn: 基础资源配置(必填)
en: Infrastructure Configuration
- Parameters:
- PayType
- PayPeriodUnit
- PayPeriod
Label:
default:
en: PayType Configuration
zh-cn: 付费类型配置 (必填)
- Parameters:
- InstanceType
- SystemDiskCategory
- SystemDiskSize
- DataDiskCategory
- DataDiskSize
- InstancePassword
- SlaveAmount
- AllocatePublicIp
Label:
default:
zh-cn: Consul 配置(必填)
en: Consul Configuration
TemplateTags:
- Creates ECS(Consul) - Existing Vpc