在亚马逊中使用自动化部署方式创建高可用架构(弹性三剑客)

{
    "AWSTemplateFormatVersion":"2010-09-09",
    "Description":"Deploy a VPC",
    "Resources":{
        "VPC":{
            "Type":"AWS::EC2::VPC",
            "Properties":{
                "CidrBlock":"10.0.0.0/16",
                "EnableDnsHostnames":true,
                "Tags":[
                    {
                        "Key":"Name",
                        "Value":"Lab VPC"
                    }
                ]
            }
        },
        "EIP1":{
            "Type":"AWS::EC2::EIP",
            "Properties":{
                "Domain":"VPC"
            }
        },
        "EIP2":{
            "Type":"AWS::EC2::EIP",
            "Properties":{
                "Domain":"VPC"
            }
        },
        "NAT1":{
            "Type":"AWS::EC2::NatGateway",
            "Properties":{
                "AllocationId":{
                    "Fn::GetAtt":[
                        "EIP1",
                        "AllocationId"
                    ]
                },
                "SubnetId":{
                    "Ref":"PublicSubnet1"
                },
                "Tags":[
                    {
                        "Key":"Name",
                        "Value":"NAT1"
                    }
                ]
            }
        },
        "NAT2":{
            "Type":"AWS::EC2::NatGateway",
            "Properties":{
                "AllocationId":{
                    "Fn::GetAtt":[
                        "EIP2",
                        "AllocationId"
                    ]
                },
                "SubnetId":{
                    "Ref":"PublicSubnet2"
                },
                "Tags":[
                    {
                        "Key":"Name",
                        "Value":"NAT2"
                    }
                ]
            }
        },
        "InternetGateway":{
            "Type":"AWS::EC2::InternetGateway",
            "Properties":{
                "Tags":[
                    {
                        "Key":"Name",
                        "Value":"Lab Internet Gateway"
                    }
                ]
            }
        },
        "AttachGateway":{
            "Type":"AWS::EC2::VPCGatewayAttachment",
            "Properties":{
                "VpcId":{
                    "Ref":"VPC"
                },
                "InternetGatewayId":{
                    "Ref":"InternetGateway"
                }
            }
        },
        "WebSG":{
            "Type":"AWS::EC2::SecurityGroup",
            "Properties":{
                "GroupName":"sg_demo",
                "GroupDescription":"sg for web",
                "SecurityGroupIngress":[
                    {
                        "IpProtocol":"tcp",
                        "FromPort":"7777",
                        "ToPort":"7777",
                        "CidrIp":"0.0.0.0/0"
                    },
                    {
                        "IpProtocol":"tcp",
                        "FromPort":"22",
                        "ToPort":"22",
                        "CidrIp":"0.0.0.0/0"
                    }
                ],
                "SecurityGroupEgress":[
                    {
                        "IpProtocol":"tcp",
                        "FromPort":"0",
                        "ToPort":"65535",
                        "CidrIp":"0.0.0.0/0"
                    }
                ],
                "VpcId":{
                    "Ref":"VPC"
                }
            }
        },
        "memcacheSG":{
            "Type":"AWS::EC2::SecurityGroup",
            "Properties":{
                "GroupName":"sg_memcached",
                "GroupDescription":"sg for memcached",
                "SecurityGroupIngress":[
                    {
                        "IpProtocol":"tcp",
                        "FromPort":"11211",
                        "ToPort":"11211",
                        "CidrIp":"0.0.0.0/0"
                    }
                ],
                "SecurityGroupEgress":[
                    {
                        "IpProtocol":"tcp",
                        "FromPort":"0",
                        "ToPort":"65535",
                        "CidrIp":"0.0.0.0/0"
                    }
                ],
                "VpcId":{
                    "Ref":"VPC"
                }
            }
        },
        "elbSG":{
            "Type":"AWS::EC2::SecurityGroup",
            "Properties":{
                "GroupName":"sg_elb",
                "GroupDescription":"sg for elb",
                "SecurityGroupIngress":[
                    {
                        "IpProtocol":"tcp",
                        "FromPort":"80",
                        "ToPort":"80",
                        "CidrIp":"0.0.0.0/0"
                    }
                ],
                "SecurityGroupEgress":[
                    {
                        "IpProtocol":"tcp",
                        "FromPort":"0",
                        "ToPort":"65535",
                        "CidrIp":"0.0.0.0/0"
                    }
                ],
                "VpcId":{
                    "Ref":"VPC"
                }
            }
        },
        "PublicSubnet1":{
            "Type":"AWS::EC2::Subnet",
            "Properties":{
                "VpcId":{
                    "Ref":"VPC"
                },
                "MapPublicIpOnLaunch":true,
                "CidrBlock":"10.0.0.0/24",
                "AvailabilityZone":"cn-northwest-1a",
                "Tags":[
                    {
                        "Key":"Name",
                        "Value":"Public Subnet 1"
                    }
                ]
            }
        },
        "PublicSubnet2":{
            "Type":"AWS::EC2::Subnet",
            "Properties":{
                "VpcId":{
                    "Ref":"VPC"
                },
                "MapPublicIpOnLaunch":true,
                "CidrBlock":"10.0.1.0/24",
                "AvailabilityZone":"cn-northwest-1b",
                "Tags":[
                    {
                        "Key":"Name",
                        "Value":"Public Subnet 2"
                    }
                ]
            }
        },
        "Instance1":{
            "Type":"AWS::EC2::Instance",
            "Properties":{
                "ImageId":"ami-05a85395c8ff37b18",
                "InstanceType":"t3.micro",
                "KeyName":{
                    "Ref":"KeyPair"
                },
                "UserData":{
                    "Fn::Base64":{
                        "Fn::Join":[
                            "",
                            [
                              "#!/bin/bash -xe\n",
                              "yum -y update\n",
                              "yum -y install wget\n",
                              "wget https://yunjisuan.s3-ap-southeast-1.amazonaws.com/caidan.sh -O /root/caidan.sh\n",
                              "wget https://yunjisuan.s3.ap-southeast-1.amazonaws.com/jiance.sh -O /root/jiance.sh\n",
                              "wget http://onlyellow.cstor.cn/q1/server_demo -O /root/server_demo\n",
                              "wget http://onlyellow.cstor.cn/q1/conf.toml -O /root/conf.toml\n",
                              "echo -e \"sudo -i\\ncd /root\\nnohup /root/server_demo &\\nnohup /root/caidan.sh &\\nsleep 60\\nnohup /root/jiance.sh & \" >> /etc/rc.d/rc.local\n",
							  "chmod +x /root/server_demo /root/jiance.sh /root/caidan.sh /etc/rc.d/rc.local\n"
                            ]
                        ]
                    }
                },
                "NetworkInterfaces":[
                    {
                        "DeviceIndex":"0",
                        "GroupSet":[
                            {
                                "Ref":"WebSG"
                            }
                        ],
                        "SubnetId":{
                            "Ref":"PublicSubnet1"
                        }
                    }
                ]
            }
        },
        "PrivateSubnet1":{
            "Type":"AWS::EC2::Subnet",
            "Properties":{
                "VpcId":{
                    "Ref":"VPC"
                },
                "CidrBlock":"10.0.2.0/24",
                "AvailabilityZone":"cn-northwest-1a",
                "Tags":[
                    {
                        "Key":"Name",
                        "Value":"Private Subnet 1"
                    }
                ]
            }
        },
        "PrivateSubnet2":{
            "Type":"AWS::EC2::Subnet",
            "Properties":{
                "VpcId":{
                    "Ref":"VPC"
                },
                "CidrBlock":"10.0.3.0/24",
                "AvailabilityZone":"cn-northwest-1b",
                "Tags":[
                    {
                        "Key":"Name",
                        "Value":"Private Subnet 2"
                    }
                ]
            }
        },
        "PublicRouteTable":{
            "Type":"AWS::EC2::RouteTable",
            "Properties":{
                "VpcId":{
                    "Ref":"VPC"
                },
                "Tags":[
                    {
                        "Key":"Name",
                        "Value":"Public Route Table"
                    }
                ]
            }
        },
        "PublicRoute":{
            "Type":"AWS::EC2::Route",
            "Properties":{
                "RouteTableId":{
                    "Ref":"PublicRouteTable"
                },
                "DestinationCidrBlock":"0.0.0.0/0",
                "GatewayId":{
                    "Ref":"InternetGateway"
                }
            }
        },
        "PublicSubnetRouteTableAssociation1":{
            "Type":"AWS::EC2::SubnetRouteTableAssociation",
            "Properties":{
                "SubnetId":{
                    "Ref":"PublicSubnet1"
                },
                "RouteTableId":{
                    "Ref":"PublicRouteTable"
                }
            }
        },
        "PublicSubnetRouteTableAssociation2":{
            "Type":"AWS::EC2::SubnetRouteTableAssociation",
            "Properties":{
                "SubnetId":{
                    "Ref":"PublicSubnet2"
                },
                "RouteTableId":{
                    "Ref":"PublicRouteTable"
                }
            }
        },
        "PrivateRouteTable1":{
            "Type":"AWS::EC2::RouteTable",
            "Properties":{
                "VpcId":{
                    "Ref":"VPC"
                },
                "Tags":[
                    {
                        "Key":"Name",
                        "Value":"Private Route Table1"
                    }
                ]
            }
        },
        "PrivateRouteTable2":{
            "Type":"AWS::EC2::RouteTable",
            "Properties":{
                "VpcId":{
                    "Ref":"VPC"
                },
                "Tags":[
                    {
                        "Key":"Name",
                        "Value":"Private Route Table2"
                    }
                ]
            }
        },
        "PrivateRoute1":{
            "Type":"AWS::EC2::Route",
            "Properties":{
                "RouteTableId":{
                    "Ref":"PrivateRouteTable1"
                },
                "DestinationCidrBlock":"0.0.0.0/0",
                "NatGatewayId":{
                    "Ref":"NAT1"
                }
            }
        },
        "PrivateRoute2":{
            "Type":"AWS::EC2::Route",
            "Properties":{
                "RouteTableId":{
                    "Ref":"PrivateRouteTable2"
                },
                "DestinationCidrBlock":"0.0.0.0/0",
                "NatGatewayId":{
                    "Ref":"NAT2"
                }
            }
        },
        "PrivateSubnetRouteTableAssociation1":{
            "Type":"AWS::EC2::SubnetRouteTableAssociation",
            "Properties":{
                "SubnetId":{
                    "Ref":"PrivateSubnet1"
                },
                "RouteTableId":{
                    "Ref":"PrivateRouteTable1"
                }
            }
        },
        "PrivateSubnetRouteTableAssociation2":{
            "Type":"AWS::EC2::SubnetRouteTableAssociation",
            "Properties":{
                "SubnetId":{
                    "Ref":"PrivateSubnet2"
                },
                "RouteTableId":{
                    "Ref":"PrivateRouteTable2"
                }
            }
        },
        "CacheSubnetGroup":{
            "Type":"AWS::ElastiCache::SubnetGroup",
            "Properties":{
                "CacheSubnetGroupName":"demo",
                "Description":"memcache for demo",
                "SubnetIds":[
                    {
                        "Ref":"PrivateSubnet1"
                    },
                    {
                        "Ref":"PrivateSubnet2"
                    }
                ]
            }
        },
        "ElastiCache":{
            "Type":"AWS::ElastiCache::CacheCluster",
            "Properties":{
                "AZMode":"cross-az",
                "CacheNodeType":"cache.t3.micro",
                "CacheSubnetGroupName":{
                    "Ref":"CacheSubnetGroup"
                },
                "ClusterName":"elasticache-memcached",
                "Engine":"memcached",
                "NumCacheNodes":"2",
                "Port":"11211",
                "PreferredAvailabilityZones":[
                    "cn-northwest-1a",
                    "cn-northwest-1b"
                ],
                "VpcSecurityGroupIds":[
                    {
                        "Fn::GetAtt":[
                            "memcacheSG",
                            "GroupId"
                        ]
                    }
                ]
            }
        },
        "myELB":{
            "Type":"AWS::ElasticLoadBalancingV2::LoadBalancer",
            "Properties":{
                "Name":"server",
                "IpAddressType":"ipv4",
                "SecurityGroups":[
                    {
                        "Ref":"elbSG"
                    }
                ],
                "Scheme":"internet-facing",
                "Subnets":[
                    {
                        "Ref":"PublicSubnet1"
                    },
                    {
                        "Ref":"PublicSubnet2"
                    }
                ],
                "Type":"application"
            }
        },
        "mytargetgroup":{
            "Type":"AWS::ElasticLoadBalancingV2::TargetGroup",
            "Properties":{
                "Name":"server",
                "HealthCheckEnabled":true,
                "HealthCheckPath":"/healthcheck",
                "Protocol":"HTTP",
                "Port":7777,
                "TargetType":"instance",
                "VpcId":{
                    "Ref":"VPC"
                }
            }
        },
        "Listener":{
            "Type":"AWS::ElasticLoadBalancingV2::Listener",
            "Properties":{
                "DefaultActions":[
                    {
                        "Type":"forward",
                        "TargetGroupArn":{
                            "Ref":"mytargetgroup"
                        }
                    }
                ],
                "LoadBalancerArn":{
                    "Ref":"myELB"
                },
                "Port":"80",
                "Protocol":"HTTP"
            }
        },
        "HTTPCode":{
            "Type":"AWS::CloudWatch::Alarm",
            "Properties":{
                "AlarmName":"HTTP-5XX",
                "EvaluationPeriods":"1",
                "Statistic":"Sum",
                "Threshold":"20",
                "AlarmDescription":"server http 5xx",
                "Period":"60",
                "Namespace":"AWS/ApplicationELB",
                "Dimensions":[
                    {
                        "Name":"LoadBalancer",
                        "Value":{
                            "Fn::GetAtt":[
                                "myELB",
                                "LoadBalancerFullName"
                            ]
                        }
                    }
                ],
                "ComparisonOperator":"GreaterThanOrEqualToThreshold",
                "MetricName":"HTTPCode_Target_5XX_Count"
            }
        }
    },
    "Parameters":{
        "KeyPair":{
            "Type":"AWS::EC2::KeyPair::KeyName",
            "Default":"keypair"
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值