【问题标题】:How to attach a fargate service to an existing target-group (pulumi)?如何将 Fargate 服务附加到现有的目标组(pulumi)?
【发布时间】:2022-11-11 17:24:24
【问题描述】:

我正在尝试复制here 中描述的流程。

我有一个负载均衡器,一个 80 端口上的侦听器,它应该将调用重定向到不同的目标组和一个特定服务的目标组。

我如何告诉 pulumi 我想将特定服务与现有的 lb、侦听器和目标组相关联?来自here 的示例创建了一个侦听器,但我不希望它从我的目标组派生,它应该能够处理多个目标组,如我之前所述。

当我使用 AWS 控制台时,我可以轻松地在服务定义中指定目标组,如图所示:

但我无法在pulumi doc 中找到类似的字段。

【问题讨论】:

    标签: amazon-web-services amazon-ecs aws-fargate pulumi


    【解决方案1】:

    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

    【讨论】:

    • 感谢您的回复。在您的示例中,您在端口 80 和 8080 上为同一个 lb 创建 2 个侦听器。我想为多个服务使用一个侦听器,就像我可以为 lambdas 做的那样 - 我可以为每个 lambda 创建一个新的目标组并注册它们都在端​​口 80 上的同一个侦听器中,并根据路径规则路由请求。
    • 您不能在同一个端口上为基于 Fargate/IP 的网络创建多个侦听器,AWS API 将引发错误。如果您想将不同的规则路由到不同的目标组,您可以使用 listenerrule:pulumi.com/registry/packages/aws/api-docs/alb/listenerrule
    • 明白了,但是如果我想在端口 80 上提供多个服务,我需要创建多个 LB 吗?
    • 更新了我的评论,没有。您将使用 listenerrule 来捕获主机标头并路由到不同的目标组。
    • @nicks 这是否回答了您的问题,或者 listenerrules 的示例会有所帮助吗?
    猜你喜欢
    • 2019-09-04
    • 2020-02-09
    • 2022-10-06
    • 2020-06-17
    • 2021-08-04
    • 2018-04-13
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    相关资源
    最近更新 更多