如何在AWS ECS中执行同一任务的容器之间进行通信? [英] How to communicate between containers in the same task in AWS ECS?
问题描述
我有一个 service-A
,其任务定义为 task-A
,其中包括多个容器定义,例如 nginx
和grafana
.这些容器如何相互通信?使用的网络是默认的桥接网络.
I have a service-A
with a task definition task-A
that includes multiple container definitions, for example nginx
and grafana
. How can these containers communicate with each other? The network used is the default bridge network.
我尝试了 curl grafana:3000
,但是容器无法解析名称.如果我在本地计算机上尝试相同的方法,它将可以正常工作.我想念什么?
I have tried curl grafana:3000
, but the container is not able to resolve the name. If I would try the same on my local machine it would work. What am I missing?
这是我的任务定义:
resource "aws_ecs_task_definition" "this" {
family = "x"
execution_role_arn = "x"
task_role_arn = "x"
container_definitions = jsonencode(local.task_definition)
}
容器定义摘录:
locals {
task_definition = [
{
name: "nginx",
image: "nginx:latest",
portMappings: [{
containerPort: 80,
hostPort: 0,
protocol: "tcp"
}],
dependsOn: [{
"containerName": "grafana",
"condition": "START"
}]
},
{
name: "grafana",
image: "grafana/grafana:latest",
portMappings: [{
containerPort : 3000,
hostPort: 0,
protocol: "tcp"
}]
}
]
}
推荐答案
dependsOn
仅在按顺序启动容器时起作用,需要链接以使服务之间进行服务到服务级别的通信.
dependsOn
only work to start the container in order, you need linking to make service to service level communication between container.
dependsOn: [{
"containerName": "grafana",
"condition": "START"
}]
"links": [
"grafana"
]
来自文档
links
Type: string array
Required: no
link参数允许容器相互通信无需端口映射.仅在网络模式下受支持定义的任务集被设置为桥接.名称:internalName构造类似于Docker链接中的name:alias.最多255个字母(大写和小写),数字,连字符和下划线是允许
The link parameter allows containers to communicate with each other without the need for port mappings. Only supported if the network mode of a task definition is set to bridge. The name:internalName construct is analogous to name:alias in Docker links. Up to 255 letters (uppercase and lowercase), numbers, hyphens, and underscores are allowed
这篇关于如何在AWS ECS中执行同一任务的容器之间进行通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!