下面是一个使用ALB、Nginx和Fargate创建负载均衡器的解决方案,包含了代码示例:
aws ecs create-cluster --cluster-name my-cluster
aws iam create-role --role-name ecsTaskExecutionRole --assume-role-policy-document '{"Version": "2012-10-17","Statement": [{"Effect": "Allow","Principal": {"Service": "ecs-tasks.amazonaws.com"},"Action": "sts:AssumeRole"}]}'
aws iam attach-role-policy --role-name ecsTaskExecutionRole --policy-arn arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy
aws ecs register-task-definition \
--family my-task \
--task-role-arn arn:aws:iam::0123456789012:role/ecsTaskExecutionRole \
--network-mode awsvpc \
--execution-role-arn arn:aws:iam::0123456789012:role/ecsTaskExecutionRole \
--container-definitions '[{"name": "nginx","image": "nginx","portMappings": [{"containerPort": 80,"protocol": "tcp"}],"essential": true,"logConfiguration": {"logDriver": "awslogs","options": {"awslogs-group": "/ecs/nginx","awslogs-region": "us-west-2","awslogs-stream-prefix": "ecs"}}}]'
aws elbv2 create-load-balancer \
--name my-load-balancer \
--subnets subnet-01234567890abcdef subnet-01234567890abcdef \
--security-groups sg-01234567890abcdef
aws elbv2 create-target-group \
--name my-target-group \
--protocol HTTP \
--port 80 \
--vpc-id vpc-01234567890abcdef
aws ecs create-service \
--cluster my-cluster \
--service-name my-service \
--task-definition my-task \
--desired-count 2 \
--network-configuration 'awsvpcConfiguration={subnets=[subnet-01234567890abcdef,subnet-01234567890abcdef],securityGroups=[sg-01234567890abcdef],assignPublicIp=ENABLED}' \
--load-balancers targetGroupArn=arn:aws:elasticloadbalancing:us-west-2:0123456789012:targetgroup/my-target-group/01234567890abcdef
这样,你就创建了一个使用ALB、Nginx和Fargate的负载均衡器。