应用程序负载平衡器-运行状况检查失败,并显示以下代码:[301]

时间:2020-08-06 17:37:19

标签: amazon-web-services amazon-cloudformation amazon-ecs aws-application-load-balancer

嗨,我正在使用cloudformation创建ecs集群,由于实例运行不正常,我在配置任务和应用程序负载平衡器端口时遇到问题。我可能输入了错误的端口,请指出错误。我只是将模板附在我认为错误所在的位置。

任务模板:

---
AWSTemplateFormatVersion: 2010-09-09 
Parameters:
    ExRole:
      Type: String
    
    RDS:
      Type: String
 
    Dbname:
      Type: String
    
    dbpassword:
      Type: String

    containerName:
      Type: String
    
    taskFamily:
      Type: String


Resources:
    Task:
        Type: AWS::ECS::TaskDefinition
        Properties:
            Family: !Ref taskFamily
            Cpu: 1 vCPU
            ExecutionRoleArn: !Ref ExRole
            Memory: 1 GB
            NetworkMode: bridge
            RequiresCompatibilities:
                - EC2
            TaskRoleArn: !Ref ExRole
            ContainerDefinitions: 
              - Essential: true
                Image: wordpress:latest
                Name: !Ref containerName
                PortMappings:  
                  - ContainerPort: 80
                    HostPort: 0
                    Protocol: tcp 
                Environment:
                  - Name: WORDPRESS_DB_HOST
                    Value: !Ref RDS 
                  - Name: WORDPRESS_DB_USER
                    Value: !Ref Dbname 
                  - Name: WORDPRESS_DB_PASSWORD
                    Value: !Ref dbpassword
                  - Name: WORDPRESS_DB_NAME
                    Value: !Ref Dbname
    
Outputs:
  Task:
    Description: Contains all the task specifications
    Value: !Ref Task
    Export:
      Name: !Sub "${AWS::StackName}-Task"

应用程序负载平衡器:

---
AWSTemplateFormatVersion: 2010-09-09
Parameters:
    
    SubnetA:
      Type: String
    
    SubnetB:
      Type: String
    
    VpcID:
      Type: String
      
    Env:
      Type: String

Resources:
    Albsg:
        Type: AWS::EC2::SecurityGroup
        Properties:
            GroupName: !Sub "albsg-${Env}"
            VpcId: !Ref VpcID
            SecurityGroupIngress:
                - IpProtocol: tcp
                  FromPort: 80
                  ToPort: 80
                  CidrIp: 0.0.0.0/0
                  Description: For traffic from Internet
            GroupDescription: Security Group for demo server
    Alb:
        Type: AWS::ElasticLoadBalancingV2::LoadBalancer
        Properties: 
            IpAddressType: ipv4
            Name: !Sub "Alb-${Env}"
            Scheme: internet-facing
            SecurityGroups: 
                - !Ref Albsg
            Subnets:
                - Ref: "SubnetA"
                - Ref: "SubnetB"
            Type: application
    DefaultTargetGroup:
        Type: AWS::ElasticLoadBalancingV2::TargetGroup
        DependsOn: Alb
        Properties:
            Name: !Sub "Albtg-${Env}"
            VpcId: !Ref VpcID
            Port: 80
            Protocol: HTTP
            Matcher:
              HttpCode: 302
    LoadBalancerListener:
        Type: AWS::ElasticLoadBalancingV2::Listener
        Properties:
            LoadBalancerArn: !Ref Alb
            Port: 80
            Protocol: HTTP
            DefaultActions:
                - Type: forward
                  TargetGroupArn: !Ref DefaultTargetGroup
Outputs:
  Albsg:
    Description: security group for application load balancer
    Value: !Ref Albsg
    Export:
        Name: !Sub "${AWS::StackName}-Albsg"
  Alb:
    Description: application load balancer
    Value: !Ref Alb
    Export:
      Name: !Sub "${AWS::StackName}-Alb"
  DefaultTargetGroup:
    Description: Default Target Group
    Value: !Ref DefaultTargetGroup
    Export:
      Name: !Sub "${AWS::StackName}-DefaultTargetGroup"       

我的实例处于耗尽状态并且运行不正常,请参见以下屏幕截图: enter image description here

1 个答案:

答案 0 :(得分:0)

301表示在其目标运行状况检查路径上访问ECS主机正在执行重定向(因此301)。要解决此问题,请要么将运行状况检查中的路径更新为不会重定向的路径,要么更新运行状况检查状态代码以包含301。这可以作为单个状态码(301)或范围(200-399)来完成。