在 ECS fargate 任务中解析服务主机名
resolve service hostname in ECS fargate task
我正在尝试使用 terraform 使我的 ECS fargate 集群自动化。
我有一个包含微服务容器化的 SpringBoot 项目,我将这些图像放在后端 ECS 服务的单个任务定义中。
ECS 集群最初是 运行,但 Kafka 正在停止并出现错误:
ERROR Unable to resolve address: zookeeper:2181
(org.apache.zookeeper.client.StaticHostProvider)
我已经为 zookeeper(bitnami) 提供了图像并且也使用了 KAFKA_CFG_ZOOKEEPER_CONNECT 环境变量。
编辑:我的任务定义:
resource "aws_ecs_task_definition" "this" {
family = local.application_name
requires_compatibilities = [local.launch_type]
execution_role_arn = data.aws_iam_role.ecs_task_execution_role.arn
network_mode = "awsvpc"
cpu = "4096"
memory = "30720"
container_definitions = jsonencode([
{
name = "zookeeper"
image = "docker.io/bitnami/zookeeper:latest"
essential = true
cpu = 512
memory = 1024
portMappings = [
{
containerPort = 2181
hostPort = 2181
}
]
"environment" : [
{ "name" : "ALLOW_ANONYMOUS_LOGIN", "value" : "yes" },
{ "name" : "ZOO_LISTEN_ALLIPS_ENABLED", "value" : "yes" }
]
},
{
name = "kafka-server"
image = "docker.io/bitnami/kafka:latest"
essential = true
cpu = 512
memory = 1024
portMappings = [
{
containerPort = 9092
hostPort = 9092
}
]
depends_on = [
"zookeeper"
]
"environment" : [
{ "name" : "KAFKA_CFG_ZOOKEEPER_CONNECT", "value" : "zookeeper:2181" },
{ "name" : "ALLOW_ANONYMOUS_LOGIN", "value" : "yes" },
{ "name" : "ALLOW_PLAINTEXT_LISTENER", "value" : "yes" }
],
},
{
name = "email-service"
image = "my-email-image"
essential = true
cpu = 512
memory = 1024
portMappings = [
{
containerPort = 8090
hostPort = 8090
}
]
"environment" : [
{ "name" : "EMAIL_URL", "value" : "email-service" },
{ "name" : "EMAIL_PORT", "value" : "8090" },
{ "name" : "KAFKA_URL", "value" : "kafka-server" },
{ "name" : "KAFKA_PORT", "value" : "9092" },
]
}
])
}
AS写在documentation:
Additionally, containers that belong to the same task can communicate over the localhost interface.
所以我的建议是使用 localhost 而不是服务名称。例如,您想为 Kafka 这样做,但也想为 每个 其他服务(例如电子邮件服务)这样做。
{ "name" : "KAFKA_CFG_ZOOKEEPER_CONNECT", "value" : "localhost:2181" },
我正在尝试使用 terraform 使我的 ECS fargate 集群自动化。
我有一个包含微服务容器化的 SpringBoot 项目,我将这些图像放在后端 ECS 服务的单个任务定义中。
ECS 集群最初是 运行,但 Kafka 正在停止并出现错误:
ERROR Unable to resolve address: zookeeper:2181
(org.apache.zookeeper.client.StaticHostProvider)
我已经为 zookeeper(bitnami) 提供了图像并且也使用了 KAFKA_CFG_ZOOKEEPER_CONNECT 环境变量。
编辑:我的任务定义:
resource "aws_ecs_task_definition" "this" {
family = local.application_name
requires_compatibilities = [local.launch_type]
execution_role_arn = data.aws_iam_role.ecs_task_execution_role.arn
network_mode = "awsvpc"
cpu = "4096"
memory = "30720"
container_definitions = jsonencode([
{
name = "zookeeper"
image = "docker.io/bitnami/zookeeper:latest"
essential = true
cpu = 512
memory = 1024
portMappings = [
{
containerPort = 2181
hostPort = 2181
}
]
"environment" : [
{ "name" : "ALLOW_ANONYMOUS_LOGIN", "value" : "yes" },
{ "name" : "ZOO_LISTEN_ALLIPS_ENABLED", "value" : "yes" }
]
},
{
name = "kafka-server"
image = "docker.io/bitnami/kafka:latest"
essential = true
cpu = 512
memory = 1024
portMappings = [
{
containerPort = 9092
hostPort = 9092
}
]
depends_on = [
"zookeeper"
]
"environment" : [
{ "name" : "KAFKA_CFG_ZOOKEEPER_CONNECT", "value" : "zookeeper:2181" },
{ "name" : "ALLOW_ANONYMOUS_LOGIN", "value" : "yes" },
{ "name" : "ALLOW_PLAINTEXT_LISTENER", "value" : "yes" }
],
},
{
name = "email-service"
image = "my-email-image"
essential = true
cpu = 512
memory = 1024
portMappings = [
{
containerPort = 8090
hostPort = 8090
}
]
"environment" : [
{ "name" : "EMAIL_URL", "value" : "email-service" },
{ "name" : "EMAIL_PORT", "value" : "8090" },
{ "name" : "KAFKA_URL", "value" : "kafka-server" },
{ "name" : "KAFKA_PORT", "value" : "9092" },
]
}
])
}
AS写在documentation:
Additionally, containers that belong to the same task can communicate over the localhost interface.
所以我的建议是使用 localhost 而不是服务名称。例如,您想为 Kafka 这样做,但也想为 每个 其他服务(例如电子邮件服务)这样做。
{ "name" : "KAFKA_CFG_ZOOKEEPER_CONNECT", "value" : "localhost:2181" },