要解决"AWS EB: 负载均衡和容器化数据库"的问题,并包含代码示例,可以按照以下步骤进行:
创建一个AWS Elastic Beanstalk环境:
eb init -p docker
eb create
创建一个Dockerfile来构建和部署应用程序:
FROM python:3.9
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python", "app.py"]
创建一个负载均衡器:
aws elbv2 create-load-balancer --name my-load-balancer --subnets subnet-12345678 subnet-87654321 --security-groups sg-12345678
创建一个目标组:
aws elbv2 create-target-group --name my-target-group --protocol HTTP --port 80 --vpc-id vpc-12345678
配置负载均衡器监听规则:
aws elbv2 create-listener --load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/1234567890abcdef --protocol HTTP --port 80 --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-target-group/1234567890abcdef
在Elastic Beanstalk环境中配置负载均衡器和目标组:
eb config
在生成的
配置文件中添加以下内容:
aws:elbv2:loadbalancer:
ListenerEnabled: true
LoadBalancerARN: arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/1234567890abcdef
aws:elbv2:targetgroup:
TargetGroupARNs: [arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-target-group/1234567890abcdef]
部署应用程序到AWS Elastic Beanstalk环境:
eb deploy
以上步骤将创建一个具有负载均衡和容器化数据库的AWS Elastic Beanstalk环境,并部署应用程序。请确保替换命令中的
,
,
,
,
,
和
与您自己的值。