利用负载均衡对服务器做流量转发是企业应用的常用模式。新开一个集群的时候,我们往往会创建一组ECS,然后分别挂载到SLB。本文针对这种场景,介绍如何通过资源编排服务(ROS)快速的搭建一套这样的基础架构。
关于构建负载均衡的其他姿势可参考:
创建的资源包括:
-
- 创建一个负载均衡的基本配置
-
- 创建一组相同配置的ECS
-
- 添加监听到负载均衡并启动监听
ALIYUN::SLB::BackendServerAttachment
- 挂载ECS到负载均衡
步骤详解:
- Step1: 创建一个负载均衡, 配置为公网类型按量付费. 模板片段如下:
"Resources": {
"CreateLoadBalancer": {
"Properties": {
"AddressType": "internet",
"InternetChargeType": "paybytraffic",
"LoadBalancerName": "createByRos"
},
"Type": "ALIYUN::SLB::LoadBalancer"
}
}
- Step2: 创建一组ECS(2台), 配置为2核4G(ecs.s2.large), 选择非IO优化的普通云盘,选择公共镜像,同时创建一个安全组,安全组配置详解。模板片段如下:
"ecsInstanceGroup": {
"Properties": {
"ImageId": "centos7u2_64_40G_cloudinit_20160520.raw",
"InstanceType": "ecs.s2.large",
"MaxAmount": 2,
"MinAmount": 2,
"SecurityGroupId": {
"Fn::GetAtt": [
"securityGroup",
"SecurityGroupId"
]
}
},
"Type": "ALIYUN::ECS::InstanceGroup"
},
"securityGroup": {
"Properties": {
"SecurityGroupEgress": [
{
"DestCidrIp": "0.0.0.0/0",
"IpProtocol": "all",
"NicType": "internet",
"PortRange": "-1/-1",
"Priority": 1
}
],
"SecurityGroupIngress": [
{
"IpProtocol": "all",
"NicType": "internet",
"PortRange": "-1/-1",
"Priority": 1,
"SourceCidrIp": "0.0.0.0/0"
}
],
"SecurityGroupName": {
"Ref": "SecurityGroupName"
}
},
"Type": "ALIYUN::ECS::SecurityGroup"
}
- Step3: 创建SLB监听,添加端口映射80 -> 8080,同时开启健康检查,模板片段如下:
"CreateListener": {
"Type": "ALIYUN::SLB::Listener",
"Properties": {
"LoadBalancerId": {"Ref": "LoadBalancer"},
"ListenerPort": "80",
"BackendServerPort": 8080,
"Bandwidth": 1,
"Protocol": "http",
"HealthCheck": {
"HealthyThreshold": 3,
"UnhealthyThreshold": 3,
"Interval": 2,
"Timeout": 5,
"HttpCode": "http_2xx,http_3xx,http_4xx,http_5xx"
},
"Scheduler": "wrr"
}
}
- Step4: 挂载ECS组到SLB, 设置相同的权重100,模板片段如下:
"Attachment": {
"Properties": {
"BackendServers": [
{
"ServerId": {
"Fn::Select": [
"0",
{
"Fn::GetAtt": [
"ecsInstanceGroup",
"InstanceIds"
]
}
]
},
"Weight": 100
},
{
"ServerId": {
"Fn::Select": [
"1",
{
"Fn::GetAtt": [
"ecsInstanceGroup",
"InstanceIds"
]
}
]
},
"Weight": 100
}
],
"LoadBalancerId": {
"Ref": "LoadBalancer"
}
},
"Type": "ALIYUN::SLB::BackendServerAttachment"
}