ALB 健康检查 Targets Unhealthy

ALB Health checks Targets Unhealthy

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

编辑:我在 EC2 容器上没有 services/tasks 运行。它是一个普通的 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}"
}

有没有人遇到过这个错误并且知道如何debug/fix这个?

看起来 http://ecsInstanceIp:80 没有返回 HTTP 200 OK。我会先检查一下。很容易检查实例是否为 public。大多数时候情况并非如此。否则我会创建一个 EC2 实例并发出 curl 请求来确认。

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

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

您似乎正在尝试向 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}"
}