AWS-CDK ECS Fargate LoadBalancer 侦听端口 80,并将目标组映射到容器端口

发布于 2025-01-11 01:30:32 字数 3077 浏览 4 评论 0原文

我一直在尝试使用 CDK 构建一个简单的 ECS Fargate 基础设施来学习 ECS 及其组件。 Web 服务器公开端口 8081,服务 SG 入口规则允许来自 ALB SG 的所有 TCP,ALB SG 允许端口 80 上的连接 - 后来更改为所有 TCP 以进行测试。 ALB 在端口 80 上(后来也在端口 8081 上)有一个侦听器,该侦听器通过 HTTP:8081 将流量转发到目标组,fargate 任务会自动注册,运行状况检查也会通过。

一切似乎都以正确的方式设置,但是,当访问 [alb-dns].com 时,我什么也没得到 – 甚至没有 504 – 只是找不到 DNS。但是当我使用 [alb-dns].com:8081 时,它会从网络服务器向我提供“hello world”。无论我的目标组使用的是 HTTP:8081 还是 HTTP:80,这都有效。

我尝试了一个旧的 github/stackoverflow 解决方案,将侦听器作为端口传递映射到容器,但这不再起作用——类型不匹配。

我在这里缺少什么?

代码:

this.cluster = new Cluster(this, 'exanubes-cluster', {
            vpc: props.vpc,
            clusterName: 'exanubes-cluster',
            containerInsights: true,
            enableFargateCapacityProviders: true,
        })

        const albSg = new SecurityGroup(this, 'SecurityGroupLoadBalancer', {
             vpc: props.vpc,
             allowAllOutbound: true
         })
         albSg.addIngressRule(Peer.anyIpv4(), Port.allTcp())

         const alb = new ApplicationLoadBalancer(this, 'alb', {
             vpc: props.vpc,
             loadBalancerName: 'exanubes-ecs-application-LB',
             internetFacing: true,
             securityGroup: albSg,
             http2Enabled: false,
             deletionProtection: false
         })

         const listener = alb.addListener('http listener', {
             port: 80,
             open: true
         })

         const targetGroup = listener.addTargets('tcp-listener-target', {
             targetGroupName: 'tcp-target-ecs-service',
             protocol: ApplicationProtocol.HTTP,
             protocolVersion: ApplicationProtocolVersion.HTTP1,
             port: CONTAINER_PORT
         })

         const taskDefinition = new FargateTaskDefinition(this, 'fargate-task-definition');

          taskDefinition.addContainer('web-server', {
             image: EcrImage.fromEcrRepository(props.repository),
         }).addPortMappings({
              containerPort: CONTAINER_PORT
          })

         const securityGroup = new SecurityGroup(this, 'http-sg', {
             vpc: props.vpc,
         })

         securityGroup.addIngressRule(Peer.securityGroupId(albSg.securityGroupId), Port.allTcp(), 'Allow inbound connections from ALB')
         const fargateService = new FargateService(this, 'fargate-service', {
             cluster: this.cluster,
             assignPublicIp: true,
             taskDefinition,
             capacityProviderStrategies: [
                 {
                     capacityProvider: "FARGATE_SPOT",
                     weight: 0,
                 },
                 {
                     capacityProvider: "FARGATE",
                     weight: 1
                 }
             ],
             securityGroups: [securityGroup],
         })
         targetGroup.addTarget(fargateService)

PS:我知道 ApplicationLoadBalancedFargateService 但我想自己构建它。

I've been trying to build a simple ECS Fargate infrastructure using the CDK to learn ECS and its components. The web server exposes port 8081, service SG ingress rule allows all TCP from ALB SG, ALB SG allows connection on PORT 80 - later changed to all TCP for testing. ALB has a listener on port 80 – later also on port 8081 – which forwards traffic to the Target Group on HTTP:8081, fargate tasks are automatically registered, health checks are passing.

Everything seems to be set up the right way, however, when going to [alb-dns].com I get nothing – not even a 504 – DNS just cannot be found. But when I go with [alb-dns].com:8081 it serves me the "hello world" from the webserver. This works regardless of whether my Target Group is on HTTP:8081 or HTTP:80.

I tried an old github/stackoverflow solution of passing a listener as port mapping to a container, but that doesn't work anymore – type mismatch.

What am I missing here?

Code:

this.cluster = new Cluster(this, 'exanubes-cluster', {
            vpc: props.vpc,
            clusterName: 'exanubes-cluster',
            containerInsights: true,
            enableFargateCapacityProviders: true,
        })

        const albSg = new SecurityGroup(this, 'SecurityGroupLoadBalancer', {
             vpc: props.vpc,
             allowAllOutbound: true
         })
         albSg.addIngressRule(Peer.anyIpv4(), Port.allTcp())

         const alb = new ApplicationLoadBalancer(this, 'alb', {
             vpc: props.vpc,
             loadBalancerName: 'exanubes-ecs-application-LB',
             internetFacing: true,
             securityGroup: albSg,
             http2Enabled: false,
             deletionProtection: false
         })

         const listener = alb.addListener('http listener', {
             port: 80,
             open: true
         })

         const targetGroup = listener.addTargets('tcp-listener-target', {
             targetGroupName: 'tcp-target-ecs-service',
             protocol: ApplicationProtocol.HTTP,
             protocolVersion: ApplicationProtocolVersion.HTTP1,
             port: CONTAINER_PORT
         })

         const taskDefinition = new FargateTaskDefinition(this, 'fargate-task-definition');

          taskDefinition.addContainer('web-server', {
             image: EcrImage.fromEcrRepository(props.repository),
         }).addPortMappings({
              containerPort: CONTAINER_PORT
          })

         const securityGroup = new SecurityGroup(this, 'http-sg', {
             vpc: props.vpc,
         })

         securityGroup.addIngressRule(Peer.securityGroupId(albSg.securityGroupId), Port.allTcp(), 'Allow inbound connections from ALB')
         const fargateService = new FargateService(this, 'fargate-service', {
             cluster: this.cluster,
             assignPublicIp: true,
             taskDefinition,
             capacityProviderStrategies: [
                 {
                     capacityProvider: "FARGATE_SPOT",
                     weight: 0,
                 },
                 {
                     capacityProvider: "FARGATE",
                     weight: 1
                 }
             ],
             securityGroups: [securityGroup],
         })
         targetGroup.addTarget(fargateService)

PS: I know of ApplicationLoadBalancedFargateService but I wanted to build it myself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神魇的王 2025-01-18 01:30:32

我认为源代码中的这个示例 .../aws_cdk/aws_ecs/__init__.py 应该有所帮助

    Example::

    # cluster: ecs.Cluster
    # task_definition: ecs.TaskDefinition
    # vpc: ec2.Vpc
    
    service = ecs.FargateService(self, "Service", cluster=cluster, task_definition=task_definition)
    
    lb = elbv2.ApplicationLoadBalancer(self, "LB", vpc=vpc, internet_facing=True)
    listener = lb.add_listener("Listener", port=80)
    service.register_load_balancer_targets(
        container_name="web",
        container_port=80,
        new_target_group_id="ECS",
        listener=ecs.ListenerConfig.application_listener(listener,
            protocol=elbv2.ApplicationProtocol.HTTPS
        )
    )

编辑:上面的内容对我不起作用,但这确实

        listener.add_targets('tcp-target-group',
                           protocol=ApplicationProtocol.HTTP,
                           target_group_name="my-target",
                           targets=[service.load_balancer_target(
                               container_name=container.container_name,
                               container_port=CONTAINER_PORT
                           )],
                           )

I think this example in the source code .../aws_cdk/aws_ecs/__init__.py should help

    Example::

    # cluster: ecs.Cluster
    # task_definition: ecs.TaskDefinition
    # vpc: ec2.Vpc
    
    service = ecs.FargateService(self, "Service", cluster=cluster, task_definition=task_definition)
    
    lb = elbv2.ApplicationLoadBalancer(self, "LB", vpc=vpc, internet_facing=True)
    listener = lb.add_listener("Listener", port=80)
    service.register_load_balancer_targets(
        container_name="web",
        container_port=80,
        new_target_group_id="ECS",
        listener=ecs.ListenerConfig.application_listener(listener,
            protocol=elbv2.ApplicationProtocol.HTTPS
        )
    )

Edit: the above did not work for me, but this did

        listener.add_targets('tcp-target-group',
                           protocol=ApplicationProtocol.HTTP,
                           target_group_name="my-target",
                           targets=[service.load_balancer_target(
                               container_name=container.container_name,
                               container_port=CONTAINER_PORT
                           )],
                           )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文