我正在尝试使用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}"
}
有人遇到此错误并且知道如何调试/修复此错误吗?
答案 0 :(得分: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}"
}
答案 1 :(得分:0)
看起来http://ecsInstanceIp:80
没有返回HTTP 200 OK
。我先检查一下。检查实例是否为公共实例很容易。大多数情况下不会是这种情况。否则,我将创建一个EC2实例并发出curl请求以确认这一点。
您还可以检查容器日志以查看其是否记录了运行状况检查响应。
希望这会有所帮助。祝你好运。