【问题标题】:ALB Health checks Targets UnhealthyALB 运行状况检查目标不健康
【发布时间】:2020-01-15 17:12:58
【问题描述】:

我正在尝试使用 Terraform 和 ALB 来配置 ECS 集群。目标为Unhealthy。控制台Health checks failed with these codes: [502]中的错误代码为502 我查看了 AWS 故障排除指南,但没有任何帮助。

编辑:我没有在 EC2 容器上运行任何服务/任务。它是一个普通的 ECS 集群。

这是我的 ALB 相关代码:

# Target Group declaration 

resource "aws_alb_target_group" "lb_target_group_somm" {
  name                 = "${var.alb_name}-default"
  port                 = 80
  protocol             = "HTTP"
  vpc_id               = "${var.vpc_id}"
  deregistration_delay = "${var.deregistration_delay}"
  health_check {
    path     = "/"
    port     = 80
    protocol = "HTTP"
  }

  lifecycle {
    create_before_destroy = true
  }

  tags = {
    Environment = "${var.environment}"
  }

  depends_on = ["aws_alb.alb"]
}

# ALB Listener with default forward rule

resource "aws_alb_listener" "https_listener" {
  load_balancer_arn = "${aws_alb.alb.id}"
  port              = "80"
  protocol          = "HTTP"

  default_action {
    target_group_arn = "${aws_alb_target_group.lb_target_group_somm.arn}"
    type             = "forward"
  }
}

# The ALB has a security group with ingress rules on TCP port 80 and egress rules to anywhere. 
# There is a security group rule for the EC2 instances that allows ingress traffic to the ECS cluster from the ALB: 

resource "aws_security_group_rule" "alb_to_ecs" {
  type                     = "ingress"
  /*from_port                = 32768 */
  from_port                = 80
  to_port                  = 65535
  protocol                 = "TCP"
  source_security_group_id = "${module.alb.alb_security_group_id}"
  security_group_id        = "${module.ecs_cluster.ecs_instance_security_group_id}"
}

有没有人遇到这个错误并且知道如何调试/修复这个?

【问题讨论】:

    标签: amazon-web-services terraform amazon-ecs terraform-provider-aws


    【解决方案1】:

    您似乎正在尝试向 ALB 目标组注册 ECS 集群实例。这不是您通过 ALB 将流量发送到 ECS 服务的方式。

    相反,您应该让您的服务将任务加入目标组。这意味着如果您使用主机网络,那么只有部署了任务的实例才会被注册。如果您使用的是桥接网络,那么它会将您的任务使用的临时端口添加到您的目标组(包括允许在单个实例上有多个目标)。如果您使用awsvpc 网络,那么它将注册服务启动的每个任务的 ENI。

    为此,您应该使用load_balancer block in the aws_ecs_service resource。示例可能如下所示:

    resource "aws_ecs_service" "mongo" {
      name            = "mongodb"
      cluster         = "${aws_ecs_cluster.foo.id}"
      task_definition = "${aws_ecs_task_definition.mongo.arn}"
      desired_count   = 3
      iam_role        = "${aws_iam_role.foo.arn}"
    
      load_balancer {
        target_group_arn = "${aws_lb_target_group.lb_target_group_somm.arn}"
        container_name   = "mongo"
        container_port   = 8080
      }
    }
    

    如果您使用的是桥接网络,这意味着可以在实例的临时端口范围内访问任务,因此您的安全组规则需要如下所示:

    resource "aws_security_group_rule" "alb_to_ecs" {
      type                     = "ingress"
      from_port                = 32768 # ephemeral port range for bridge networking tasks
      to_port                  = 60999 # cat /proc/sys/net/ipv4/ip_local_port_range
      protocol                 = "TCP"
      source_security_group_id = "${module.alb.alb_security_group_id}"
      security_group_id        = "${module.ecs_cluster.ecs_instance_security_group_id}"
    }
    

    【讨论】:

    • 我刚刚确认在容器本身上运行时,实际的 datadog 健康检查运行正常 - /opt/datadog-agent/bin/agent/agent health Agent health: PASS 所以 BAD GATEWAY 错误似乎就像您指出的那样,是因为 ALB 和 EC2 容器实例之间的连接。我做了你提到的修改(现在使用桥接模式)但同样的错误。
    • 我让它工作了。我的端口不匹配,而且超时间隔有点短。
    【解决方案2】:

    看起来http://ecsInstanceIp:80 没有返回HTTP 200 OK。我会先检查一下。很容易检查实例是否是公共的。大多数时候都不会这样。否则我会创建一个 EC2 实例并发出 curl 请求以确认这一点。

    您还可以检查容器日志以查看其是否记录了健康检查响应。

    希望这会有所帮助。祝你好运。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-22
      • 2022-01-01
      • 2020-02-17
      相关资源
      最近更新 更多