首页 > 解决方案 > 502 网关错误:容器端口 5000 上的节点应用程序

问题描述

我在这里在 aws 中托管了一个 Angular 应用程序作为微服务: http ://test-2079808347.us-east-2.elb.amazonaws.com: 5000 。我收到 502 Bad Gateway 错误。下面给出的是服务的 yaml 模板和任务定义。我正在使用的图像在我运行时在本地环境中工作docker run -p 5000:5000 <image-tag>

    Resources:
      Service:
        Type: AWS::ECS::Service
        DependsOn: ListenerRule
        Properties:
          Cluster: !Ref Cluster
          Role: !Ref ServiceRole
          DesiredCount: !Ref DesiredCount
          TaskDefinition: !Ref TaskDefinition
          LoadBalancers:
            - ContainerName: "website-service"
              ContainerPort: 5000
              TargetGroupArn: !Ref TargetGroup

      TaskDefinition:
        Type: AWS::ECS::TaskDefinition
        Properties:
          Family: website-service
          ContainerDefinitions:
            - Name: website-service
              Essential: true
              Image: registry.hub.docker.com/abameerdeen/activity_service:latest
              Memory: 128
              Environment:
                - Name: PRODUCT_SERVICE_URL
                  Value: !Ref ProductServiceUrl
              PortMappings:
                - ContainerPort: 5000
              LogConfiguration:
                LogDriver: awslogs
                Options:
                  awslogs-group: !Ref CloudWatchLogsGroup
                  awslogs-region: !Ref AWS::Re
  TargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
      VpcId: !Ref VPC
      Port: 5000
      Protocol: HTTP
      Matcher:
        HttpCode: 200-299
      HealthCheckIntervalSeconds: 10
      HealthCheckPath: /profile
      HealthCheckProtocol: HTTP
      HealthCheckTimeoutSeconds: 5
      HealthyThresholdCount: 2

下面给出的是负载均衡器的 yaml 模板。

Resources:
  LoadBalancer:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Properties:
      Name: !Ref EnvironmentName
      Subnets: !Ref Subnets
      SecurityGroups:
        - !Ref SecurityGroup
      Tags:
        - Key: Name
          Value: !Ref EnvironmentName

  LoadBalancerListener:
    Type: AWS::ElasticLoadBalancingV2::Listener
    Properties:
      LoadBalancerArn: !Ref LoadBalancer
      Port: 5000
      Protocol: HTTP
      DefaultActions:
        - Type: forward
          TargetGroupArn: !Ref DefaultTargetGroup

  # We define a default target group here, as this is a mandatory Parameters
  # when creating an Application Load Balancer Listener. This is not used, instead
  # a target group is created per-service in each service template (../services/*)
  DefaultTargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
      Name: !Sub ${EnvironmentName}-default
      VpcId: !Ref VPC
      Port: 80
      Protocol: HTTP

下面给出的是 Cloudformation 堆栈。 在此处输入图像描述

标签: amazon-web-servicesamazon-ecsaws-application-load-balancer

解决方案


错误是我没有从微服务端监听端口 5000 的服务。我的错。所以,如果有人遇到同样的情况。确保您拥有正确的图像。还要确保,ListenerRuleTargetGroup已正确设置。

例如:-

TargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
      VpcId: !Ref VPC
      Port: 80
      Protocol: HTTP
      Matcher:
        HttpCode: 200-299
      HealthCheckIntervalSeconds: 10
      HealthCheckPath: /
      HealthCheckProtocol: HTTP
      HealthCheckTimeoutSeconds: 5
      HealthyThresholdCount: 2

  ListenerRule:
    Type: AWS::ElasticLoadBalancingV2::ListenerRule
    Properties:
      ListenerArn: !Ref Listener
      Priority: 100
      Conditions:
        - Field: path-pattern
          Values: [ "/*" ]
      Actions:
        - TargetGroupAr

推荐阅读