ECS 的间接级别、控制台魔术和命名重载(“服务对 ECS 和 ALB 有效)通常很难理解。
最终,我的配置思考您要查找的是 ECS 服务定义中的 here。
当您定义您的 ECS 定义时,您可以选择您希望使用的目标组,
像这样:
const svcA = new aws.ecs.Service("example-A", {
cluster: cluster.arn,
desiredCount: 1,
launchType: "FARGATE",
taskDefinition: taskDefinition.arn,
networkConfiguration: {
assignPublicIp: true,
subnets: subnets.ids,
securityGroups: [ securityGroup.id ]
},
loadBalancers: [{
targetGroupArn: targetGroupA.arn,
containerName: "my-app",
containerPort: 80,
}]
})
注意那里定义的loadBalancers 对象。
然后,您可以定义多个服务并将它们指向您喜欢的任何目标组。在这里,我们可以定义一个带有两个监听器和目标组的负载均衡器:
// define a loadbalancer
const lb = new aws.lb.LoadBalancer("example", {
securityGroups: [securityGroup.id],
subnets: subnets.ids,
});
// target group for port 80
const targetGroupA = new aws.lb.TargetGroup("example-A", {
port: 80,
protocol: "HTTP",
targetType: "ip",
vpcId: vpc.id,
});
// listener for port 80
const listenerA = new aws.lb.Listener("example-A", {
loadBalancerArn: lb.arn,
port: 80,
defaultActions: [
{
type: "forward",
targetGroupArn: targetGroupA.arn,
},
],
});
// target group for port 8080
const targetGroupB = new aws.lb.TargetGroup("example-B", {
port: 8080,
protocol: "HTTP",
targetType: "ip",
vpcId: vpc.id,
});
// listener for port 8080
const listenerB = new aws.lb.Listener("example-B", {
loadBalancerArn: lb.arn,
port: 8080,
defaultActions: [
{
type: "forward",
targetGroupArn: targetGroupB.arn,
},
],
});
然后定义两个指向不同目标群体的不同服务:
// service listening on port 80
const svcA = new aws.ecs.Service("example-A", {
cluster: cluster.arn,
desiredCount: 1,
launchType: "FARGATE",
taskDefinition: taskDefinition.arn,
networkConfiguration: {
assignPublicIp: true,
subnets: subnets.ids,
securityGroups: [securityGroup.id],
},
loadBalancers: [
{
targetGroupArn: targetGroupA.arn,
containerName: "my-app",
containerPort: 80,
},
],
});
// service listening on port 8080
const svcB = new aws.ecs.Service("example-B", {
cluster: cluster.arn,
desiredCount: 1,
launchType: "FARGATE",
taskDefinition: taskDefinition.arn,
networkConfiguration: {
assignPublicIp: true,
subnets: subnets.ids,
securityGroups: [securityGroup.id],
},
loadBalancers: [
{
targetGroupArn: targetGroupB.arn,
containerName: "my-app",
containerPort: 80,
},
],
});
您可以找到完整的端到端示例,说明其外观 here