AWS ECS没有链接我的容器

我正在迁移我工作的公司的基础设施的一部分到亚马逊ECS,我被困在试图使我的服务器容器连接到数据库容器。

下面是我如何设置我的任务:

{ "requiresAttributes": [ { "value": null, "name": "com.amazonaws.ecs.capability.docker-remote-api.1.17", "targetId": null, "targetType": null }, { "value": null, "name": "com.amazonaws.ecs.capability.logging-driver.syslog", "targetId": null, "targetType": null }, { "value": null, "name": "com.amazonaws.ecs.capability.docker-remote-api.1.18", "targetId": null, "targetType": null }, { "value": null, "name": "com.amazonaws.ecs.capability.ecr-auth", "targetId": null, "targetType": null } ], "taskDefinitionArn": "arn:aws:ecs:us-east-1:9621234232917455:task-definition/ecv-server:12", "networkMode": "bridge", "status": "ACTIVE", "revision": 12, "taskRoleArn": null, "containerDefinitions": [ { "volumesFrom": [], "memory": 500, "extraHosts": null, "dnsServers": [], "disableNetworking": null, "dnsSearchDomains": null, "portMappings": [], "hostname": "db", "essential": true, "entryPoint": null, "mountPoints": [ { "containerPath": "/var/lib/postgresql/data", "sourceVolume": "dbdata", "readOnly": null } ], "name": "db", "ulimits": null, "dockerSecurityOptions": null, "environment": [ { "name": "POSTGRES_PASSWORD", "value": "jmbrito" }, { "name": "POSTGRES_USER", "value": "jmbrito" } ], "links": [], "workingDirectory": null, "readonlyRootFilesystem": null, "image": "postgres", "command": null, "user": null, "dockerLabels": null, "logConfiguration": { "logDriver": "syslog", "options": null }, "cpu": 0, "privileged": null, "memoryReservation": null }, { "volumesFrom": [], "memory": 400, "extraHosts": null, "dnsServers": [], "disableNetworking": null, "dnsSearchDomains": null, "portMappings": [], "hostname": "redis", "essential": true, "entryPoint": null, "mountPoints": [ { "containerPath": "/data", "sourceVolume": "redisdata", "readOnly": null } ], "name": "redis", "ulimits": null, "dockerSecurityOptions": null, "environment": [], "links": null, "workingDirectory": null, "readonlyRootFilesystem": null, "image": "redis:3.2-alpine", "command": [ "redis-server" ], "user": null, "dockerLabels": null, "logConfiguration": { "logDriver": "syslog", "options": null }, "cpu": 0, "privileged": null, "memoryReservation": null }, { "volumesFrom": [], "memory": 600, "extraHosts": null, "dnsServers": null, "disableNetworking": null, "dnsSearchDomains": null, "portMappings": [ { "hostPort": 80, "containerPort": 3000, "protocol": "tcp" } ], "hostname": null, "essential": true, "entryPoint": [], "mountPoints": [], "name": "server", "ulimits": null, "dockerSecurityOptions": null, "environment": [ { "name": "RAILS_ENV", "value": "production" } ], "links": [ "db:db", "redis:redis" ], "workingDirectory": "/usr/src/app", "readonlyRootFilesystem": null, "image": "MY DOCKER LINK IN ECR", "command": [ "sh", "deploy/init.sh" ], "user": null, "dockerLabels": null, "logConfiguration": { "logDriver": "syslog", "options": null }, "cpu": 0, "privileged": null, "memoryReservation": null } ], "placementConstraints": [], "volumes": [ { "host": { "sourcePath": null }, "name": "dbdata" }, { "host": { "sourcePath": null }, "name": "redisdata" } ], "family": "ecv-server" } 

正如你可以看到我设置我的链接字段正确,当我尝试连接使用名称,如连接到主机数据库或主机的Redis,它不找到它。

我尝试使用VPC地址连接到其他容器,它的工作。 唯一的问题是要做到这一点,我最好自己设置VPC地址(如设置172.13.0.2为数据库),因为当我没有设置,系统获取地址的连接顺序。

我希望你能理解这个问题。

谢谢。

我不认为这个任务定义会按照你想要的方式工作。 当您将三个容器放在一个任务定义中时,它告诉ECS始终将这三个容器一起部署在同一台机器上,每次部署任务。

所以如果你为这个任务部署了一个需要三次的服务,你会得到三个应用程序容器,三个postgres容器和三个redis容器。 这三个应用程序容器将有三个独立的数据持久性堆栈。 应用程序容器A将仅与postgres A和redis A进行通信,而应用程序容器B将只与postgress B和redis B进行通信,因此每个应用程序容器将具有不相互复制的不一致的数据。

在任务定义中运行多个容器实际上仅用于边车容器,如反向代理或临时缓存等。

对于持久层,我的建议是将Amazon RDS用于您的postgres,将Amazon Elasticache用于您的redis。 通过这种方式,所有的任务可以共享相同的postgres和相同的redis,而且使用这些Amazon服务也可以减少很多管理操作。

Interesting Posts