具有负载均衡器、目标组、非标准端口上的目标的 ApplicationLoadBalancedFargateService

0

【以下的问题经过翻译处理】 我有一个公开端口 8080 的 ECS 服务。我想让负载均衡器、目标组和目标使用该端口而不是端口 80。这是我的代码片段:

    const servicePort = 8888;
    const metricsPort = 8888;

    const taskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef');
    const repository = ecr.Repository.fromRepositoryName(this, 'cloud-config-server', 'cloud-config-server');

    taskDefinition.addContainer('Config', {
      image:ecs.ContainerImage.fromEcrRepository(repository),
      portMappings: [{containerPort : servicePort, hostPort: servicePort}],
    });
    
    const albFargateService = new ecsPatterns.ApplicationLoadBalancedFargateService(this, 'AlbConfigService', {
      cluster,
      publicLoadBalancer : false,
      taskDefinition: taskDefinition,
      desiredCount: 1,
    });

    const applicationTargetGroup = new elbv2.ApplicationTargetGroup(this, 'AlbConfigServiceTargetGroup', {
      targetType: elbv2.TargetType.IP,
      protocol: elbv2.ApplicationProtocol.HTTP,
      port: servicePort,
      vpc,
      healthCheck: {path: "/CloudConfigServer/actuator/env/profile", port: String(servicePort)}
    });

    const addApplicationTargetGroupsProps: elbv2.AddApplicationTargetGroupsProps = {
     targetGroups:[applicationTargetGroup],
    };

    albFargateService.loadBalancer.addListener('alb-listener', {
      protocol: elbv2.ApplicationProtocol.HTTP, port: servicePort, defaultTargetGroups: [applicationTargetGroup]}
    );
  }
}

但这行不通。健康检查在端口 80 以及 默认 URL 为“/” 上进行,并且失败了,任务在不断循环创建和失败。有一个target group, 配置了8080 端口和 正确的的健康检查URL,但target group 中没有 target。

请问: 在 80 以外的端口上实现负载平衡的推荐方法是什么?

谢谢

1 Antwort
0

【以下的回答经过翻译处理】 由于 ApplicationLoadBalancedFargateService 是一个高级构造,使用它时我们不需要配置目标组端口和侦听器端口(默认为 80 或 443)。只要我们在任务定义中提供容器端口(服务端口),ApplicationLoadBalancedFargateService 将负责正确的配置从 ALB 到 ECS 任务的端口和连接。

因此,在您的情况下,您不需要指定 applicationTargetGroupaddApplicationTargetGroupsPropsalbFargateService.loadBalancer.addListenerApplicationLoadBalancedFargateService 已经代表您完成了这项工作。

您可以参考 文档 以获得更好的理解。

profile picture
EXPERTE
beantwortet vor 5 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen