我正在尝试使用Terraform和ALB来配置ECS集群。目标出现为Unhealthy。在控制台Health checks failed with these codes: [502]中,错误代码是502,我查看了亚马逊网络服务故障排除指南,但没有任何帮助。
编辑:我没有在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}"
}有没有人遇到这个错误并知道如何调试/修复这个错误?
发布于 2020-01-15 17:11:15
看起来您正在尝试将ECS集群实例注册到ALB目标组。这不是通过ALB将流量发送到ECS服务的方式。
相反,您应该让您的服务将任务加入到目标组中。这意味着如果您使用的是主机网络,那么只会注册部署了任务的实例。如果您正在使用网桥网络,那么它会将您的任务使用的临时端口添加到您的目标组中(包括允许在单个实例上有多个目标)。如果您使用的是awsvpc networking,那么它将注册该服务启动的每个任务的弹性网卡。
要做到这一点,您应该使用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}"
}发布于 2020-01-15 07:16:50
看起来http://ecsInstanceIp:80没有返回HTTP 200 OK。我会先检查一下。检查实例是否为公共实例将很容易。大多数情况下情况并非如此。否则,我将创建一个EC2实例并发出curl请求来确认这一点。
您还可以检查容器日志,以查看其是否记录了健康检查响应。
希望这能有所帮助。祝好运。
https://stackoverflow.com/questions/59742703
复制相似问题