Web应用部署实战 #

一、架构概述 #

1.1 目标架构 #

text
高可用Web应用架构:
┌─────────────────────────────────────────────────────────────┐
│                          用户                                │
└───────────────────────────┬─────────────────────────────────┘
                            │
                            ▼
┌─────────────────────────────────────────────────────────────┐
│                      Route53 DNS                             │
└───────────────────────────┬─────────────────────────────────┘
                            │
                            ▼
┌─────────────────────────────────────────────────────────────┐
│                    CloudFront CDN                            │
└───────────────────────────┬─────────────────────────────────┘
                            │
                            ▼
┌─────────────────────────────────────────────────────────────┐
│                      应用负载均衡器                          │
└───────────────────────────┬─────────────────────────────────┘
                            │
              ┌─────────────┴─────────────┐
              ▼                           ▼
┌───────────────────────┐   ┌───────────────────────┐
│      可用区 A         │   │      可用区 B         │
│  ┌─────────────────┐  │   │  ┌─────────────────┐  │
│  │   EC2 实例      │  │   │  │   EC2 实例      │  │
│  │   (Web服务器)   │  │   │  │   (Web服务器)   │  │
│  └─────────────────┘  │   │  └─────────────────┘  │
└───────────────────────┘   └───────────────────────┘
              │                           │
              └─────────────┬─────────────┘
                            ▼
┌─────────────────────────────────────────────────────────────┐
│                   Aurora 数据库集群                          │
│  ┌─────────────────┐   ┌─────────────────┐                  │
│  │    主实例       │   │   只读副本      │                  │
│  └─────────────────┘   └─────────────────┘                  │
└─────────────────────────────────────────────────────────────┘

1.2 架构组件 #

组件 服务 用途
DNS Route53 域名解析
CDN CloudFront 内容分发
负载均衡 ALB 流量分发
Web服务器 EC2 应用运行
数据库 Aurora 数据存储
缓存 ElastiCache 会话缓存
文件存储 S3 静态资源

二、VPC网络配置 #

2.1 创建VPC #

bash
aws ec2 create-vpc \
    --cidr-block 10.0.0.0/16 \
    --tag-specifications "ResourceType=vpc,Tags=[{Key=Name,Value=WebApp-VPC}]"

2.2 创建子网 #

bash
VPC_ID=vpc-12345678

aws ec2 create-subnet \
    --vpc-id $VPC_ID \
    --cidr-block 10.0.1.0/24 \
    --availability-zone us-east-1a \
    --tag-specifications "ResourceType=subnet,Tags=[{Key=Name,Value=Public-Subnet-A}]"

aws ec2 create-subnet \
    --vpc-id $VPC_ID \
    --cidr-block 10.0.2.0/24 \
    --availability-zone us-east-1b \
    --tag-specifications "ResourceType=subnet,Tags=[{Key=Name,Value=Public-Subnet-B}]"

aws ec2 create-subnet \
    --vpc-id $VPC_ID \
    --cidr-block 10.0.10.0/24 \
    --availability-zone us-east-1a \
    --tag-specifications "ResourceType=subnet,Tags=[{Key=Name,Value=Private-Subnet-A}]"

aws ec2 create-subnet \
    --vpc-id $VPC_ID \
    --cidr-block 10.0.20.0/24 \
    --availability-zone us-east-1b \
    --tag-specifications "ResourceType=subnet,Tags=[{Key=Name,Value=Private-Subnet-B}]"

2.3 配置Internet网关 #

bash
IGW_ID=$(aws ec2 create-internet-gateway \
    --tag-specifications "ResourceType=internet-gateway,Tags=[{Key=Name,Value=WebApp-IGW}]" \
    --query 'InternetGateway.InternetGatewayId' --output text)

aws ec2 attach-internet-gateway \
    --vpc-id $VPC_ID \
    --internet-gateway-id $IGW_ID

2.4 配置NAT网关 #

bash
EIP_ID=$(aws ec2 allocate-address --domain vpc --query 'AllocationId' --output text)

NAT_ID=$(aws ec2 create-nat-gateway \
    --subnet-id $PUBLIC_SUBNET_A \
    --allocation-id $EIP_ID \
    --tag-specifications "ResourceType=natgateway,Tags=[{Key=Name,Value=WebApp-NAT}]" \
    --query 'NatGateway.NatGatewayId' --output text)

三、安全组配置 #

3.1 ALB安全组 #

bash
ALB_SG=$(aws ec2 create-security-group \
    --group-name webapp-alb-sg \
    --description "Security group for ALB" \
    --vpc-id $VPC_ID \
    --query 'GroupId' --output text)

aws ec2 authorize-security-group-ingress \
    --group-id $ALB_SG \
    --protocol tcp \
    --port 80 \
    --cidr 0.0.0.0/0

aws ec2 authorize-security-group-ingress \
    --group-id $ALB_SG \
    --protocol tcp \
    --port 443 \
    --cidr 0.0.0.0/0

3.2 Web服务器安全组 #

bash
WEB_SG=$(aws ec2 create-security-group \
    --group-name webapp-web-sg \
    --description "Security group for Web servers" \
    --vpc-id $VPC_ID \
    --query 'GroupId' --output text)

aws ec2 authorize-security-group-ingress \
    --group-id $WEB_SG \
    --protocol tcp \
    --port 80 \
    --source-group $ALB_SG

3.3 数据库安全组 #

bash
DB_SG=$(aws ec2 create-security-group \
    --group-name webapp-db-sg \
    --description "Security group for Database" \
    --vpc-id $VPC_ID \
    --query 'GroupId' --output text)

aws ec2 authorize-security-group-ingress \
    --group-id $DB_SG \
    --protocol tcp \
    --port 3306 \
    --source-group $WEB_SG

四、数据库配置 #

4.1 创建数据库子网组 #

bash
aws rds create-db-subnet-group \
    --db-subnet-group-name webapp-db-subnet \
    --db-subnet-group-description "Database subnet group" \
    --subnet-ids $PRIVATE_SUBNET_A $PRIVATE_SUBNET_B

4.2 创建Aurora集群 #

bash
aws rds create-db-cluster \
    --db-cluster-identifier webapp-cluster \
    --engine aurora-mysql \
    --engine-version 8.0 \
    --master-username admin \
    --master-user-password MySecurePassword123! \
    --vpc-security-group-ids $DB_SG \
    --db-subnet-group-name webapp-db-subnet

aws rds create-db-instance \
    --db-instance-identifier webapp-primary \
    --db-cluster-identifier webapp-cluster \
    --db-instance-class db.r5.large \
    --engine aurora-mysql

五、负载均衡器配置 #

5.1 创建应用负载均衡器 #

bash
ALB_ARN=$(aws elbv2 create-load-balancer \
    --name webapp-alb \
    --subnets $PUBLIC_SUBNET_A $PUBLIC_SUBNET_B \
    --security-groups $ALB_SG \
    --query 'LoadBalancers[0].LoadBalancerArn' --output text)

5.2 创建目标组 #

bash
TG_ARN=$(aws elbv2 create-target-group \
    --name webapp-targets \
    --protocol HTTP \
    --port 80 \
    --vpc-id $VPC_ID \
    --health-check-path /health \
    --query 'TargetGroups[0].TargetGroupArn' --output text)

5.3 创建监听器 #

bash
aws elbv2 create-listener \
    --load-balancer-arn $ALB_ARN \
    --protocol HTTP \
    --port 80 \
    --default-actions Type=forward,TargetGroupArn=$TG_ARN

六、自动扩展配置 #

6.1 创建启动模板 #

bash
aws ec2 create-launch-template \
    --launch-template-name webapp-template \
    --launch-template-data '{
        "ImageId": "ami-0c55b159cbfafe1f0",
        "InstanceType": "t3.medium",
        "KeyName": "my-key-pair",
        "SecurityGroupIds": ["'$WEB_SG'"],
        "IamInstanceProfile": {"Name": "EC2Role"},
        "UserData": "'$(base64 -w 0 user-data.sh)'"
    }'

6.2 创建自动扩展组 #

bash
aws autoscaling create-auto-scaling-group \
    --auto-scaling-group-name webapp-asg \
    --launch-template LaunchTemplateId=lt-12345678,Version=1 \
    --min-size 2 \
    --max-size 6 \
    --desired-capacity 2 \
    --vpc-zone-identifier $PRIVATE_SUBNET_A,$PRIVATE_SUBNET_B \
    --target-group-arns $TG_ARN

6.3 配置扩展策略 #

bash
aws autoscaling put-scaling-policy \
    --auto-scaling-group-name webapp-asg \
    --policy-name webapp-scale-up \
    --policy-type TargetTrackingScaling \
    --target-tracking-configuration '{
        "PredefinedMetricSpecification": {
            "PredefinedMetricType": "ASGAverageCPUUtilization"
        },
        "TargetValue": 70.0
    }'

七、CloudFront配置 #

7.1 创建CloudFront分发 #

bash
aws cloudfront create-distribution \
    --origin-domain-name $ALB_DNS_NAME \
    --default-root-object index.html \
    --price-class PriceClass_100

八、Route53配置 #

8.1 创建记录集 #

bash
aws route53 change-resource-record-sets \
    --hosted-zone-id $HOSTED_ZONE_ID \
    --change-batch '{
        "Changes": [{
            "Action": "CREATE",
            "ResourceRecordSet": {
                "Name": "www.example.com",
                "Type": "CNAME",
                "TTL": 300,
                "ResourceRecords": [{"Value": "'$CLOUDFRONT_DOMAIN'"}]
            }
        }]
    }'

九、监控配置 #

9.1 CloudWatch告警 #

bash
aws cloudwatch put-metric-alarm \
    --alarm-name webapp-high-cpu \
    --alarm-description "CPU使用率过高" \
    --metric-name CPUUtilization \
    --namespace AWS/EC2 \
    --statistic Average \
    --period 300 \
    --threshold 80 \
    --comparison-operator GreaterThanThreshold \
    --evaluation-periods 2

十、部署验证 #

10.1 验证清单 #

text
部署验证:
├── [ ] VPC和子网配置正确
├── [ ] 安全组规则正确
├── [ ] 数据库可连接
├── [ ] 负载均衡器健康检查通过
├── [ ] 自动扩展组实例正常
├── [ ] CloudFront缓存正常
├── [ ] DNS解析正确
└── [ ] 应用功能正常

十一、小结 #

本章介绍了Web应用部署实战:

内容 要点
架构设计 高可用、多可用区
VPC配置 公有/私有子网、NAT网关
安全组 最小权限原则
负载均衡 ALB流量分发
自动扩展 弹性伸缩

下一步学习 #

完成Web应用部署后,接下来可以:

  1. 无服务器应用 - 学习无服务器架构
  2. 微服务架构 - 学习微服务部署
  3. 安全最佳实践 - 加强安全配置
最后更新:2026-03-28