以下是一个使用AWS Elastic Container Service (ECS) 部署 Django 和 Nginx,并通过 Application Load Balancer (ALB) 实现负载均衡的示例。
# 基于 Python 3.9 镜像构建
FROM python:3.9
# 设置工作目录
WORKDIR /app
# 安装依赖
COPY requirements.txt .
RUN pip install -r requirements.txt
# 复制 Django 项目到工作目录
COPY . .
# 运行 Django 项目
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
{
"family": "django-nginx-task",
"containerDefinitions": [
{
"name": "django",
"image": "your-docker-image-repo:tag",
"essential": true,
"portMappings": [
{
"containerPort": 8000,
"protocol": "tcp"
}
]
},
{
"name": "nginx",
"image": "nginx:latest",
"essential": true,
"portMappings": [
{
"containerPort": 80,
"protocol": "tcp"
}
],
"links": ["django"],
"mountPoints": [
{
"sourceVolume": "static",
"containerPath": "/app/static",
"readOnly": true
}
]
}
],
"volumes": [
{
"name": "static",
"host": {
"sourcePath": "/path/to/your/static/files"
}
}
]
}
aws ecs create-service --service-name my-ecs-service --cluster my-ecs-cluster --task-definition django-nginx-task --desired-count 2
aws elbv2 create-load-balancer --name my-alb --subnets subnet-12345678 subnet-abcdefgh --security-groups sg-12345678
aws elbv2 create-target-group --name my-target-group --protocol HTTP --port 80 --target-type ip --vpc-id vpc-12345678
aws elbv2 create-listener --load-balancer-arn alb-arn --protocol HTTP --port 80 --default-actions Type=forward,TargetGroupArn=target-group-arn
希望以上示例对您有所帮助!