aws(学习笔记第十二课) 使用AWS的RDS-MySQL

news2024/11/17 1:09:30

aws(学习笔记第十二课)

  • 使用AWS的RDS

学习内容:

  • AWS的RDS-MySQL

1. 使用AWS的RDS

  1. 什么是RDS
    RDS就是Relation Database Service的缩写,是AWS提供的托管关系型数据库系统。让用户能够在 AWS Cloud 云中更轻松地设置、操作和扩展关系数据库。

    • 数据库和web server服务器下的架构
      在这里插入图片描述
  2. 使用Cloudformation构建RDS以及AutoScaling
    注意mail地址的时候,要指定postgresql@163.com的形式,要用@加上domain的形式,否则,下面的wordpressinstall会报错,导致该cloudformation创建失败

    • Cloudformation代码
      {
      	"AWSTemplateFormatVersion": "2010-09-09",
      	"Description": "AWS in Action: chapter 9",
      	"Parameters": {
      		"KeyName": {
      			"Description": "Key Pair name",
      			"Type": "AWS::EC2::KeyPair::KeyName",
      			"Default": "my-cli-key"
      		},
      		"BlogTitle": {
      			"Description": "The title of the blog.",
      			"Type": "String",
      			"Default": "Amazon Web Services in Action - Example"
      		},
      		"AdminUsername": {
      			"Description": "A username for admin.",
      			"Type": "String",
      			"Default": "admin"
      		},
      		"AdminPassword": {
      			"Description": "A password for admin",
      			"Type": "String",
      			"NoEcho": "true"
      		},
      		"AdminEMail": {
      			"Description": "The email address of the administrator.",
      			"Type": "String"
      		}
      	},
      	"Mappings": {
      		"EC2RegionMap": {
      			"ap-northeast-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-cbf90ecb"},
      			"ap-southeast-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-68d8e93a"},
      			"ap-southeast-2": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-fd9cecc7"},
      			"eu-central-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-a8221fb5"},
      			"eu-west-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-a10897d6"},
      			"sa-east-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-b52890a8"},
      			"us-east-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-1ecae776"},
      			"us-west-1": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-d114f295"},
      			"us-west-2": {"AmazonLinuxAMIHVMEBSBacked64bit": "ami-e7527ed7"}
      		}
      	},
      	"Resources": {
      		"VPC": {
      			"Type": "AWS::EC2::VPC",
      			"Properties": {
      				"CidrBlock": "172.31.0.0/16",
      				"EnableDnsHostnames": "true"
      			}
      		},
      		"InternetGateway": {
      			"Type": "AWS::EC2::InternetGateway",
      			"Properties": {
      			}
      		},
      		"VPCGatewayAttachment": {
      			"Type": "AWS::EC2::VPCGatewayAttachment",
      			"Properties": {
      				"VpcId": {"Ref": "VPC"},
      				"InternetGatewayId": {"Ref": "InternetGateway"}
      			}
      		},
      		"SubnetA": {
      			"Type": "AWS::EC2::Subnet",
      			"Properties": {
      				"AvailabilityZone": {"Fn::Select": ["0", {"Fn::GetAZs": ""}]},
      				"CidrBlock": "172.31.38.0/24",
      				"VpcId": {"Ref": "VPC"}
      			}
      		},
      		"SubnetB": {
      			"Type": "AWS::EC2::Subnet",
      			"Properties": {
      				"AvailabilityZone": {"Fn::Select": ["1", {"Fn::GetAZs": ""}]},
      				"CidrBlock": "172.31.37.0/24",
      				"VpcId": {"Ref": "VPC"}
      			}
      		},
      		"RouteTable": {
      			"Type": "AWS::EC2::RouteTable",
      			"Properties": {
      				"VpcId": {"Ref": "VPC"}
      			}
      		},
      		"RouteTableAssociationA": {
      			"Type": "AWS::EC2::SubnetRouteTableAssociation",
      			"Properties": {
      				"SubnetId": {"Ref": "SubnetA"},
      				"RouteTableId": {"Ref": "RouteTable"}
      			}
      		},
      		"RouteTableAssociationB": {
      			"Type": "AWS::EC2::SubnetRouteTableAssociation",
      			"Properties": {
      				"SubnetId": {"Ref": "SubnetB"},
      				"RouteTableId": {"Ref": "RouteTable"}
      			}
      		},
      		"RoutePublicNATToInternet": {
      			"Type": "AWS::EC2::Route",
      			"Properties": {
      				"RouteTableId": {"Ref": "RouteTable"},
      				"DestinationCidrBlock": "0.0.0.0/0",
      				"GatewayId": {"Ref": "InternetGateway"}
      			},
      			"DependsOn": "VPCGatewayAttachment"
      		},
      		"NetworkAcl": {
      			"Type": "AWS::EC2::NetworkAcl",
      			"Properties": {
      				"VpcId": {"Ref": "VPC"}
      			}
      		},
      		"SubnetNetworkAclAssociationA": {
      			"Type": "AWS::EC2::SubnetNetworkAclAssociation",
      			"Properties": {
      				"SubnetId": {"Ref": "SubnetA"},
      				"NetworkAclId": {"Ref": "NetworkAcl"}
      			}
      		},
      		"SubnetNetworkAclAssociationB": {
      			"Type": "AWS::EC2::SubnetNetworkAclAssociation",
      			"Properties": {
      				"SubnetId": {"Ref": "SubnetB"},
      				"NetworkAclId": {"Ref": "NetworkAcl"}
      			}
      		},
      		"NetworkAclEntryIngress": {
      			"Type": "AWS::EC2::NetworkAclEntry",
      			"Properties": {
      				"NetworkAclId": {"Ref": "NetworkAcl"},
      				"RuleNumber": "100",
      				"Protocol": "-1",
      				"RuleAction": "allow",
      				"Egress": "false",
      				"CidrBlock": "0.0.0.0/0"
      			}
      		},
      		"NetworkAclEntryEgress": {
      			"Type": "AWS::EC2::NetworkAclEntry",
      			"Properties": {
      				"NetworkAclId": {"Ref": "NetworkAcl"},
      				"RuleNumber": "100",
      				"Protocol": "-1",
      				"RuleAction": "allow",
      				"Egress": "true",
      				"CidrBlock": "0.0.0.0/0"
      			}
      		},
      		"LoadBalancer": {
      			"Type": "AWS::ElasticLoadBalancing::LoadBalancer",
      			"Properties": {
      				"Subnets": [{"Ref": "SubnetA"}, {"Ref": "SubnetB"}],
      				"LoadBalancerName": "awsinaction-elb",
      				"Listeners": [{
      					"InstancePort": "80",
      					"InstanceProtocol": "HTTP",
      					"LoadBalancerPort": "80",
      					"Protocol": "HTTP"
      				}],
      				"HealthCheck": {
      					"HealthyThreshold": "2",
      					"Interval": "5",
      					"Target": "TCP:80",
      					"Timeout": "3",
      					"UnhealthyThreshold": "2"
      				},
      				"SecurityGroups": [{"Ref": "LoadBalancerSecurityGroup"}],
      				"Scheme": "internet-facing"
      			},
      			"DependsOn": "VPCGatewayAttachment"
      		},
      		"LoadBalancerSecurityGroup": {
      			"Type": "AWS::EC2::SecurityGroup",
      			"Properties": {
      				"GroupDescription": "awsinaction-elb-sg",
      				"VpcId": {"Ref": "VPC"},
      				"SecurityGroupIngress": [{
      					"CidrIp": "0.0.0.0/0",
      					"FromPort": 80,
      					"IpProtocol": "tcp",
      					"ToPort": 80
      				}]
      			}
      		},
      		"WebServerSecurityGroup": {
      			"Type": "AWS::EC2::SecurityGroup",
      			"Properties": {
      				"GroupDescription": "awsinaction-sg",
      				"VpcId": {"Ref": "VPC"},
      				"SecurityGroupIngress": [{
      					"CidrIp": "0.0.0.0/0",
      					"FromPort": 22,
      					"IpProtocol": "tcp",
      					"ToPort": 22
      				}, {
      					"FromPort": 80,
      					"IpProtocol": "tcp",
      					"SourceSecurityGroupId": {"Ref": "LoadBalancerSecurityGroup"},
      					"ToPort": 80
      				}]
      			}
      		},
      		"DatabaseSecurityGroup": {
      			"Type": "AWS::EC2::SecurityGroup",
      			"Properties": {
      				"GroupDescription": "awsinaction-db-sg",
      				"VpcId": {"Ref": "VPC"},
      				"SecurityGroupIngress": [{
      					"IpProtocol": "tcp",
      					"FromPort": "3306",
      					"ToPort": "3306",
      					"SourceSecurityGroupId": {"Ref": "WebServerSecurityGroup"}
      				}]
      			}
      		},
      		"Database": {
      			"Type": "AWS::RDS::DBInstance",
      			"DeletionPolicy": "Delete",
      			"Properties": {
      				"AllocatedStorage": "25",
      				"DBInstanceClass": "db.t3.medium",
      				"DBInstanceIdentifier": "awsinaction-db",
      				"DBName": "wordpress",
      				"Engine": "MySQL",
      				"EngineVersion": "5.7",
      				"MasterUsername": "wordpress",
      				"MasterUserPassword": "wordpress",
      				"VPCSecurityGroups": [{"Fn::GetAtt": ["DatabaseSecurityGroup", "GroupId"]}],
      				"DBSubnetGroupName": {"Ref": "DBSubnetGroup"}
      			},
      			"DependsOn": "VPCGatewayAttachment"
      		},
      		"DBSubnetGroup" : {
      			"Type" : "AWS::RDS::DBSubnetGroup",
      			"Properties" : {
      				"DBSubnetGroupDescription" : "DB subnet group",
      				"SubnetIds": [{"Ref": "SubnetA"}, {"Ref": "SubnetB"}]
      			}
      		},
      		"LaunchTemplate": {
      			"Type": "AWS::EC2::LaunchTemplate",
      			"Metadata": {
      				"AWS::CloudFormation::Init": {
      					"config": {
      						"packages": {
      							"yum": {
      								"php": [],
      								"php-mysql": [],
      								"mysql": [],
      								"httpd": []
      							}
      						},
      						"sources": {
      							"/var/www/html": "https://wordpress.org/wordpress-4.2.4.tar.gz"
      						},
      						"files": {
      							"/tmp/config": {
      								"content": {"Fn::Join": ["", [
      									"#!/bin/bash -ex\n",
      									"cp /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php\n",
      									"sed -i \"s/'database_name_here'/'wordpress'/g\" wp-config.php\n",
      									"sed -i \"s/'username_here'/'wordpress'/g\" wp-config.php\n",
      									"sed -i \"s/'password_here'/'wordpress'/g\" wp-config.php\n",
      									"sed -i \"s/'localhost'/'", {"Fn::GetAtt": ["Database", "Endpoint.Address"]}, "'/g\" wp-config.php\n",
      									"chmod -R 777 wp-content/ \n",
      									"curl -O https://raw.githubusercontent.com/AWSinAction/builds/gh-pages/phar/wp-cli.phar \n",
      									"php wp-cli.phar core install --url=\"", {"Fn::GetAtt": ["LoadBalancer", "DNSName"]}, "/wordpress\" --title=\"", {"Ref": "BlogTitle"}, "\" --admin_user=\"", {"Ref": "AdminUsername"}, "\" --admin_password=\"", {"Ref": "AdminPassword"}, "\" --admin_email=\"", {"Ref": "AdminEMail"}, "\" \n"								]]},
      								"mode": "000500",
      								"owner": "root",
      								"group": "root"
      							}
      						},
      						"commands": {
      							"01_config": {
      								"command": "/tmp/config",
      								"cwd": "/var/www/html/wordpress"
      							}
      						},
      						"services": {
      							"sysvinit": {
      								"httpd": {
      									"enabled": "true",
      									"ensureRunning": "true"
      								}
      							}
      						}
      					}
      				}
      			},
      			"Properties": {
      				"LaunchTemplateName": "LaunchTemplate",
      				"LaunchTemplateData":{
      					"EbsOptimized": false,
      					"ImageId": {"Fn::FindInMap": ["EC2RegionMap", {"Ref": "AWS::Region"}, "AmazonLinuxAMIHVMEBSBacked64bit"]},
      					"InstanceType": "t2.micro",
      					"NetworkInterfaces":[
      						{
      							"DeviceIndex":0,
      							"AssociatePublicIpAddress":true,
      							"Groups":[
      								{"Ref": "WebServerSecurityGroup"}
      							],
      							"DeleteOnTermination":true
      						}
      					],
      					"KeyName": {"Ref": "KeyName"},
      					"UserData": {"Fn::Base64": {"Fn::Join": ["", [
      						"#!/bin/bash -ex\n",
      						"yum update -y aws-cfn-bootstrap\n",
      						"/opt/aws/bin/cfn-init -v --stack ", {"Ref": "AWS::StackName"}, " --resource LaunchTemplate --region ", {"Ref": "AWS::Region"}, "\n",
      						"/opt/aws/bin/cfn-signal -e $? --stack ", {"Ref": "AWS::StackName"}, " --resource AutoScalingGroup --region ", {"Ref": "AWS::Region"}, "\n"
      					]]}}}
      			}
      		},
      		"AutoScalingGroup": {
      			"Type": "AWS::AutoScaling::AutoScalingGroup",
      			"Properties": {
      				"LoadBalancerNames": [{"Ref": "LoadBalancer"}],
      				"LaunchTemplate" : {"LaunchTemplateId" : {"Ref" : "LaunchTemplate"},"Version" : {"Fn::GetAtt" : ["LaunchTemplate","LatestVersionNumber"]}},
      				"MinSize": "2",
      				"MaxSize": "2",
      				"DesiredCapacity": "2",
      				"VPCZoneIdentifier": [{"Ref": "SubnetA"}, {"Ref": "SubnetB"}]
      			},
      			"CreationPolicy": {
      				"ResourceSignal": {
      					"Timeout": "PT10M"
      				}
      			},
      			"DependsOn": "VPCGatewayAttachment"
      		}
      	},
      	"Outputs": {
      		"URL": {
      			"Value": {"Fn::Join": ["", ["http://", {"Fn::GetAtt": ["LoadBalancer", "DNSName"]}, "/wordpress"]]},
      			"Description": "Wordpress URL"
      		}
      	}
      }
      
  3. 逐步解析整个Cloudformation

    • 定义VPC以及两个subnet

      • 代码
        "VPC": {
        			"Type": "AWS::EC2::VPC",
        			"Properties": {
        				"CidrBlock": "172.31.0.0/16",
        				"EnableDnsHostnames": "true"
        			}
        		},
        		"InternetGateway": {
        			"Type": "AWS::EC2::InternetGateway",
        			"Properties": {
        			}
        		},
        		"VPCGatewayAttachment": {
        			"Type": "AWS::EC2::VPCGatewayAttachment",
        			"Properties": {
        				"VpcId": {"Ref": "VPC"},
        				"InternetGatewayId": {"Ref": "InternetGateway"}
        			}
        		},
        		"SubnetA": {
        			"Type": "AWS::EC2::Subnet",
        			"Properties": {
        				"AvailabilityZone": {"Fn::Select": ["0", {"Fn::GetAZs": ""}]},
        				"CidrBlock": "172.31.38.0/24",
        				"VpcId": {"Ref": "VPC"}
        			}
        		},
        		"SubnetB": {
        			"Type": "AWS::EC2::Subnet",
        			"Properties": {
        				"AvailabilityZone": {"Fn::Select": ["1", {"Fn::GetAZs": ""}]},
        				"CidrBlock": "172.31.37.0/24",
        				"VpcId": {"Ref": "VPC"}
        			}
        		},
        
      • 系统构成 系统构成图
      • 代码解析
        • 定义了一个VPC
        • 定义了一个InternetGateway,并将其attachVPC上。
        • 定义了两个subnet,其中每个subnet都在一个AvailabilityZoneAZ)里面。
    • 定义RouterRouteTable,并附加在两个subnet

      • 代码
        "RouteTable": {
        			"Type": "AWS::EC2::RouteTable",
        			"Properties": {
        				"VpcId": {"Ref": "VPC"}
        			}
        		},
        		"RouteTableAssociationA": {
        			"Type": "AWS::EC2::SubnetRouteTableAssociation",
        			"Properties": {
        				"SubnetId": {"Ref": "SubnetA"},
        				"RouteTableId": {"Ref": "RouteTable"}
        			}
        		},
        		"RouteTableAssociationB": {
        			"Type": "AWS::EC2::SubnetRouteTableAssociation",
        			"Properties": {
        				"SubnetId": {"Ref": "SubnetB"},
        				"RouteTableId": {"Ref": "RouteTable"}
        			}
        		},
        		"RoutePublicNATToInternet": {
        			"Type": "AWS::EC2::Route",
        			"Properties": {
        				"RouteTableId": {"Ref": "RouteTable"},
        				"DestinationCidrBlock": "0.0.0.0/0",
        				"GatewayId": {"Ref": "InternetGateway"}
        			},
        			"DependsOn": "VPCGatewayAttachment"
        		},
        
        • 对应的系统构成
          在这里插入图片描述
          注意,"DependsOn": "VPCGatewayAttachment",因为需要0.0.0.0路由到InternetGateway的时候,需要VPC attach到InternetGateway作为先行条件,所以加上DependsOn
        • 代码解析
          • 定义一个RouteTable,指定VPC
          • 将这个RouteTable附加到subnet A
          • 将这个RouteTable附加到subnet B
          • 给路由表RouteTable加上路由RouteEC2如果想要访问Internet网络,那么经由InternetGateway
    • 定义RouterRouteTable,并附加在两个subnet

      • 代码

        		"NetworkAcl": {
        			"Type": "AWS::EC2::NetworkAcl",
        			"Properties": {
        				"VpcId": {"Ref": "VPC"}
        			}
        		},
        		"SubnetNetworkAclAssociationA": {
        			"Type": "AWS::EC2::SubnetNetworkAclAssociation",
        			"Properties": {
        				"SubnetId": {"Ref": "SubnetA"},
        				"NetworkAclId": {"Ref": "NetworkAcl"}
        			}
        		},
        		"SubnetNetworkAclAssociationB": {
        			"Type": "AWS::EC2::SubnetNetworkAclAssociation",
        			"Properties": {
        				"SubnetId": {"Ref": "SubnetB"},
        				"NetworkAclId": {"Ref": "NetworkAcl"}
        			}
        		},
        		"NetworkAclEntryIngress": {
        			"Type": "AWS::EC2::NetworkAclEntry",
        			"Properties": {
        				"NetworkAclId": {"Ref": "NetworkAcl"},
        				"RuleNumber": "100",
        				"Protocol": "-1",
        				"RuleAction": "allow",
        				"Egress": "false",
        				"CidrBlock": "0.0.0.0/0"
        			}
        		},
        		"NetworkAclEntryEgress": {
        			"Type": "AWS::EC2::NetworkAclEntry",
        			"Properties": {
        				"NetworkAclId": {"Ref": "NetworkAcl"},
        				"RuleNumber": "100",
        				"Protocol": "-1",
        				"RuleAction": "allow",
        				"Egress": "true",
        				"CidrBlock": "0.0.0.0/0"
        			}
        		},
        
      • 系统构成
        在这里插入图片描述

      • 代码解析

        • 定义一个NetworkAcl
        • 定义两个SubnetNetworkAclAssociation,一个是从0.0.0.0入站(egress = true)的网络都开放,另一个是从subnet出站(egress = true)到0.0.0.0的都开放
    • 定义一个LoadBalancer以及security group

      • 代码
        "LoadBalancer": {
        			"Type": "AWS::ElasticLoadBalancing::LoadBalancer",
        			"Properties": {
        				"Subnets": [{"Ref": "SubnetA"}, {"Ref": "SubnetB"}],
        				"LoadBalancerName": "awsinaction-elb",
        				"Listeners": [{
        					"InstancePort": "80",
        					"InstanceProtocol": "HTTP",
        					"LoadBalancerPort": "80",
        					"Protocol": "HTTP"
        				}],
        				"HealthCheck": {
        					"HealthyThreshold": "2",
        					"Interval": "5",
        					"Target": "TCP:80",
        					"Timeout": "3",
        					"UnhealthyThreshold": "2"
        				},
        				"SecurityGroups": [{"Ref": "LoadBalancerSecurityGroup"}],
        				"Scheme": "internet-facing"
        			},
        			"DependsOn": "VPCGatewayAttachment"
        		},
        		"LoadBalancerSecurityGroup": {
        			"Type": "AWS::EC2::SecurityGroup",
        			"Properties": {
        				"GroupDescription": "awsinaction-elb-sg",
        				"VpcId": {"Ref": "VPC"},
        				"SecurityGroupIngress": [{
        					"CidrIp": "0.0.0.0/0",
        					"FromPort": 80,
        					"IpProtocol": "tcp",
        					"ToPort": 80
        				}]
        			}
        		},
        
      • 系统构成
        在这里插入图片描述
      • 代码解析
        • 定义一个LoadBalancer
        • 定义一个LoadBalancerSecurityGroup,开放internetLoadBalancer80端口访问
    • 定义WebServerSecurityGroup

      • 代码
        "WebServerSecurityGroup": {
        			"Type": "AWS::EC2::SecurityGroup",
        			"Properties": {
        				"GroupDescription": "awsinaction-sg",
        				"VpcId": {"Ref": "VPC"},
        				"SecurityGroupIngress": [{
        					"CidrIp": "0.0.0.0/0",
        					"FromPort": 22,
        					"IpProtocol": "tcp",
        					"ToPort": 22
        				}, {
        					"FromPort": 80,
        					"IpProtocol": "tcp",
        					"SourceSecurityGroupId": {"Ref": "LoadBalancerSecurityGroup"},
        					"ToPort": 80
        				}]
        			}
        		},
        
      • 系统构成
        在这里插入图片描述
        • 代码解析
          • 定义一个WebServerSecurityGroup
          • 允许internet访问22端口
          • 只允许LoadBalancerSecurityGroup所属的主机访问80端口
    • 定义数据库和数据所在的security group

      • 代码
        "DatabaseSecurityGroup": {
        			"Type": "AWS::EC2::SecurityGroup",
        			"Properties": {
        				"GroupDescription": "awsinaction-db-sg",
        				"VpcId": {"Ref": "VPC"},
        				"SecurityGroupIngress": [{
        					"IpProtocol": "tcp",
        					"FromPort": "3306",
        					"ToPort": "3306",
        					"SourceSecurityGroupId": {"Ref": "WebServerSecurityGroup"}
        				}]
        			}
        		},
        		"Database": {
        			"Type": "AWS::RDS::DBInstance",
        			"DeletionPolicy": "Delete",
        			"Properties": {
        				"AllocatedStorage": "25",
        				"DBInstanceClass": "db.t3.medium",
        				"DBInstanceIdentifier": "awsinaction-db",
        				"DBName": "wordpress",
        				"Engine": "MySQL",
        				"EngineVersion": "5.7",
        				"MasterUsername": "wordpress",
        				"MasterUserPassword": "wordpress",
        				"VPCSecurityGroups": [{"Fn::GetAtt": ["DatabaseSecurityGroup", "GroupId"]}],
        				"DBSubnetGroupName": {"Ref": "DBSubnetGroup"}
        			},
        			"DependsOn": "VPCGatewayAttachment"
        		},
        		"DBSubnetGroup" : {
        			"Type" : "AWS::RDS::DBSubnetGroup",
        			"Properties" : {
        				"DBSubnetGroupDescription" : "DB subnet group",
        				"SubnetIds": [{"Ref": "SubnetA"}, {"Ref": "SubnetB"}]
        			}
        		},
        
      • 系统构成
        在这里插入图片描述
      • 代码解析
        • 定义一个RDS(mysql)
        • 定义一个DatabaseSecurityGroup,在这里单单允许从属于WebServerSecurityGroup的虚拟机能访问3306端口
    • 定义LaunchTemplateAutoScaling提供模版

      • 代码
        "LaunchTemplate": {
        			"Type": "AWS::EC2::LaunchTemplate",
        			"Metadata": {
        				"AWS::CloudFormation::Init": {
        					"config": {
        						"packages": {
        							"yum": {
        								"php": [],
        								"php-mysql": [],
        								"mysql": [],
        								"httpd": []
        							}
        						},
        						"sources": {
        							"/var/www/html": "https://wordpress.org/wordpress-4.2.4.tar.gz"
        						},
        						"files": {
        							"/tmp/config": {
        								"content": {"Fn::Join": ["", [
        									"#!/bin/bash -ex\n",
        									"cp /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php\n",
        									"sed -i \"s/'database_name_here'/'wordpress'/g\" wp-config.php\n",
        									"sed -i \"s/'username_here'/'wordpress'/g\" wp-config.php\n",
        									"sed -i \"s/'password_here'/'wordpress'/g\" wp-config.php\n",
        									"sed -i \"s/'localhost'/'", {"Fn::GetAtt": ["Database", "Endpoint.Address"]}, "'/g\" wp-config.php\n",
        									"chmod -R 777 wp-content/ \n",
        									"curl -O https://raw.githubusercontent.com/AWSinAction/builds/gh-pages/phar/wp-cli.phar \n",
        									"php wp-cli.phar core install --url=\"", {"Fn::GetAtt": ["LoadBalancer", "DNSName"]}, "/wordpress\" --title=\"", {"Ref": "BlogTitle"}, "\" --admin_user=\"", {"Ref": "AdminUsername"}, "\" --admin_password=\"", {"Ref": "AdminPassword"}, "\" --admin_email=\"", {"Ref": "AdminEMail"}, "\" \n"								]]},
        								"mode": "000500",
        								"owner": "root",
        								"group": "root"
        							}
        						},
        						"commands": {
        							"01_config": {
        								"command": "/tmp/config",
        								"cwd": "/var/www/html/wordpress"
        							}
        						},
        						"services": {
        							"sysvinit": {
        								"httpd": {
        									"enabled": "true",
        									"ensureRunning": "true"
        								}
        							}
        						}
        					}
        				}
        			},
        			"Properties": {
        				"LaunchTemplateName": "LaunchTemplate",
        				"LaunchTemplateData":{
        					"EbsOptimized": false,
        					"ImageId": {"Fn::FindInMap": ["EC2RegionMap", {"Ref": "AWS::Region"}, "AmazonLinuxAMIHVMEBSBacked64bit"]},
        					"InstanceType": "t2.micro",
        					"NetworkInterfaces":[
        						{
        							"DeviceIndex":0,
        							"AssociatePublicIpAddress":true,
        							"Groups":[
        								{"Ref": "WebServerSecurityGroup"}
        							],
        							"DeleteOnTermination":true
        						}
        					],
        					"KeyName": {"Ref": "KeyName"},
        					"UserData": {"Fn::Base64": {"Fn::Join": ["", [
        						"#!/bin/bash -ex\n",
        						"yum update -y aws-cfn-bootstrap\n",
        						"/opt/aws/bin/cfn-init -v --stack ", {"Ref": "AWS::StackName"}, " --resource LaunchTemplate --region ", {"Ref": "AWS::Region"}, "\n",
        						"/opt/aws/bin/cfn-signal -e $? --stack ", {"Ref": "AWS::StackName"}, " --resource AutoScalingGroup --region ", {"Ref": "AWS::Region"}, "\n"
        					]]}}}
        			}
        		},
        
      • 系统架构
        在这里插入图片描述
      • 代码解析
        • 定义一个LaunchTemplate,这里使用了cfn-init,这里就会回调父节点的"AWS::CloudFormation::Init",能够对使用模版创建的EC2实例进行初期化。类似于使用puppet或者chef来对EC2进行详细的配置。
        • 完成了cfn-init之后调用cfn-signal -e来通知CloudformationAutoScalingGroup已经创建完毕。
    • 定义AutoScalingGroup

      • 代码
        "AutoScalingGroup": {
        			"Type": "AWS::AutoScaling::AutoScalingGroup",
        			"Properties": {
        				"LoadBalancerNames": [{"Ref": "LoadBalancer"}],
        				"LaunchTemplate" : {"LaunchTemplateId" : {"Ref" : "LaunchTemplate"},"Version" : {"Fn::GetAtt" : ["LaunchTemplate","LatestVersionNumber"]}},
        				"MinSize": "2",
        				"MaxSize": "2",
        				"DesiredCapacity": "2",
        				"VPCZoneIdentifier": [{"Ref": "SubnetA"}, {"Ref": "SubnetB"}]
        			},
        			"CreationPolicy": {
        				"ResourceSignal": {
        					"Timeout": "PT10M"
        				}
        			},
        			"DependsOn": "VPCGatewayAttachment"
        		}
        
        • 系统架构
          在这里插入图片描述
        • 代码解析
          • 定义一个AutoScalingGroup,这里使用LaunchTemplate,注意LaunchTemplate定义的时候,使用cfn-init,在初期化的时候,使用DB设定,指向设定的RDS(mysql)
          • propertiesmaxsizeminsize都是2,保证至少有两个EC2来服务wordpress
  4. 确认结果

    • 确认LoadBalancerdomain

      awsinaction-elb-1595505902.ap-northeast-1.elb.amazonaws.com/wordpress
      

      在这里插入图片描述

    • 访问LoadBalancer80端口
      http://awsinaction-elb-1595505902.ap-northeast-1.elb.amazonaws.com
      在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2241851.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

跳房子(弱化版)

题目描述 跳房子,也叫跳飞机,是一种世界性的儿童游戏,也是中国民间传统的体育游戏之一。 跳房子的游戏规则如下: 在地面上确定一个起点,然后在起点右侧画 n 个格子,这些格子都在同一条直线上。每个格子内…

A029-基于Spring Boot的物流管理系统的设计与实现

🙊作者简介:在校研究生,拥有计算机专业的研究生开发团队,分享技术代码帮助学生学习,独立完成自己的网站项目。 代码可以查看文章末尾⬇️联系方式获取,记得注明来意哦~🌹 赠送计算机毕业设计600…

Spring系统框架

Spring Framework系统架构 1.Spring核心概念 代码书写现状 耦合度偏高 解决方案 使用对象时,在程序中不要主动使用new产生对象,转换为外部提供对象 IOC(Inversion of Control)控制反转 对象的创建控制权由程序移到外部,这种思想称为控制…

鸿蒙实战:页面跳转

文章目录 1. 实战概述2. 实现步骤2.1 创建项目2.2 准备图片素材2.3 编写首页代码2.4 创建第二个页面 3. 测试效果4. 实战总结 1. 实战概述 实战概述:本实战通过ArkUI框架,在鸿蒙系统上开发了一个简单的两页面应用。首页显示问候语和“下一页”按钮&…

文献解读-DNAscope: High accuracy small variant calling using machine learning

关键词:基准与方法研究;基因测序;变异检测; 文献简介 标题(英文):DNAscope: High accuracy small variant calling using machine learning标题(中文):DNAsc…

程序设计方法与实践-变治法

变换之美 变治法就是基于变换的思路,进而使原问题的求解变得简单的一种技术。 变治法一般有三种类型: 实例化简:将问题变换为同问题,但换成更为简单、更易求解的实例。改变表现:变化为同实例的不同形式,…

解决Anaconda出现CondaHTTPError: HTTP 000 CONNECTION FAILED for url

解决Anaconda出现CondaHTTPError: HTTP 000 CONNECTION FAILED for url 第一类情况 在anaconda创建新环境时,使用如下代码 conda create -n charts python3.7 错误原因: 默认镜像源访问速度过慢,会导致超时从而导致更新和下载失败。 解决方…

Spring Boot框架:电商系统的技术革新

4 系统设计 网上商城系统的设计方案比如功能框架的设计,比如数据库的设计的好坏也就决定了该系统在开发层面是否高效,以及在系统维护层面是否容易维护和升级,因为在系统实现阶段是需要考虑用户的所有需求,要是在设计阶段没有经过全…

wordpress下载站主题推荐riproV5 wordpress日主题

iPro主题全新V5版本,是一个优秀且功能强大、易于管理、现代化的WordPress虚拟资源商城主题。支持首页模块化布局和WP原生小工具模块化首页可拖拽设置,让您的网站设计体验更加舒适。同时支持了高级筛选、自带会员生态系统、超全支付接口等众多功能&#x…

微服务即时通讯系统的实现(客户端)----(1)

目录 1. 项目整体介绍1.1 项目概况1.2 界面预览和功能介绍1.3 技术重点和服务器架构 2. 项目环境搭建2.1 安装Qt62.3 安装vcpkg2.3 安装protobuf2.4 构建项目2.5 配置CMake属性 3. 项目核心数据结构的实现3.1 创建data.h存放核心的类3.2 工具函数的实现3.3 创建编译开关 4. 界面…

2024年11月15日

1.计算机网络 逻辑右移 做加减法 定点乘法 原码乘法运算 一位乘 计组 2.英语六级

算法定制LiteAIServer摄像机实时接入分析平台玩手机打电话检测算法:智能监控的新篇章

在现代社会,随着智能手机的普及,无论是在工作场所还是公共场所,玩手机或打电话的行为日益普遍。然而,在某些特定环境下,如工厂生产线、仓库、学校课堂等,这些行为可能会影响到工作效率、安全或教学秩序。为…

算法--解决二叉树遍历问题

第一 实现树的结构 class Node(): # 构造函数,初始化节点对象,包含数据和左右子节点 def __init__(self, dataNone): self.data data # 节点存储的数据 self.left None # 左子节点,默认为None self.rig…

深度学习基础—Beam search集束搜索

引言 深度学习基础—Seq2Seq模型https://blog.csdn.net/sniper_fandc/article/details/143781223?fromshareblogdetail&sharetypeblogdetail&sharerId143781223&sharereferPC&sharesourcesniper_fandc&sharefromfrom_link 上篇博客讲到,贪心算…

C++__day1

1、思维导图 2、如果登录失败&#xff0c;提示用户登录失败信息&#xff0c;并且提示错误几次&#xff0c;且重新输入&#xff1b;如果输入错误三次&#xff0c;则退出系统 #include <iostream> using namespace std;int main() {string id , pswd;string user"admi…

【机器学习】数学知识:欧式距离(Euclidean Distance)和曼哈顿距离(Manhattan Distance)

欧式距离和曼哈顿距离是两种常用的距离度量方法&#xff0c;用于衡量两点之间的相似性或差异性。它们在几何分析、数据挖掘、机器学习等领域有广泛应用。 1. 欧式距离 概念 欧式距离&#xff08;Euclidean Distance&#xff09;是最常见的直线距离度量方法&#xff0c;源于欧…

Java之JDBC,Maven,MYBatis

前言 就是用来操作数据库的 1.JDBC快速入门 注意在使用前一定要导入jar包 在模块那里新建目录&#xff0c;新建lib&#xff0c;粘贴复制jar包&#xff0c;我这个jar设置的是模块有效 package test1017;import java.sql.Connection; import java.sql.DriverManager; import…

JavaWeb笔记整理——Spring Task、WebSocket

目录 SpringTask ​cron表达式 WebSocket SpringTask cron表达式 WebSocket

【大数据学习 | HBASE高级】rowkey的设计,hbase的预分区和压缩

1. rowkey的设计 ​ RowKey可以是任意字符串&#xff0c;最大长度64KB&#xff0c;实际应用中一般为10~100bytes&#xff0c;字典顺序排序&#xff0c;rowkey的设计至关重要&#xff0c;会影响region分布&#xff0c;如果rowkey设计不合理还会出现region写热点等一系列问题。 …

如何实现主备租户的无缝切换 | OceanBase应用实践

对于DBA而言&#xff0c;确保数据库的高可用性、容灾等能力是其日常工作中需要持续思考和关注的重要事项。一方面&#xff0c;可以利用数据库自身所具备的功能来实现这些目标&#xff1b;若数据库本身不提供相应功能&#xff0c;DBA则需寻找其他工具来增强数据库的高可用性和容…