问题描述
我正在使用AWS ECS,并且为我的前端(Node应用程序)和后端(mongo数据库)提供了一个容器.
I am using AWS ECS and have a container for my frontend (Node app) and for my backend (mongo database).
mongo容器暴露了端口27017,但是我不知道如何从前端容器连接到该端口.如果我尝试使用'mongodb://localhost:27017/db_name'连接到数据库,则会收到ECONNREFUSED错误.
The mongo container is exposing port 27017, but I cannot figure out how to connect to it from my frontend container. If I try to connect to the db using 'mongodb://localhost:27017/db_name' I get an ECONNREFUSED error.
我为这两个任务定义都运行了一个服务,并为前端提供了ALB.我没有在相同的任务定义中使用它们,因为将它们一起缩放似乎并不是最佳选择.
I have a service running for both of these task definitions with an ALB for the frontend. I don't have them in the same task definition because it doesn't seem optimal to have to scale them together.
我尝试了该网址的多种变体
I have tried multiple variations of the url
- mongodb://0.0.0.0:27017/db_name
- mongodb://localhost:27017/db_name
如果我从EC2实例中卷曲" mongo容器,则会收到来自服务器的空回复.
If I "curl" the mongo container from within the EC2 instance, I get an empty reply from server.
数据库任务定义:
{
"executionRoleArn": null,
"containerDefinitions": [
{
"dnsSearchDomains": null,
"logConfiguration": null,
"entryPoint": null,
"portMappings": [
{
"hostPort": 27017,
"protocol": "tcp",
"containerPort": 27017
}
],
"command": null,
"linuxParameters": null,
"cpu": 0,
"environment": [
{
"name": "MONGODB_ADMIN_PASS",
"value": <PASSWORD>
},
{
"name": "MONGODB_APPLICATION_DATABASE",
"value": <DB NAME>
},
{
"name": "MONGODB_APPLICATION_PASS",
"value": <PASSWORD>
},
{
"name": "MONGODB_APPLICATION_USER",
"value": <USERNAME>
}
],
"ulimits": null,
"dnsServers": null,
"mountPoints": [],
"workingDirectory": null,
"dockerSecurityOptions": null,
"memory": null,
"memoryReservation": 128,
"volumesFrom": [],
"image": "registry.hub.docker.com/library/mongo:latest",
"disableNetworking": null,
"healthCheck": null,
"essential": true,
"links": null,
"hostname": null,
"extraHosts": null,
"user": null,
"readonlyRootFilesystem": null,
"dockerLabels": null,
"privileged": null,
"name": "mongo"
}
],
"placementConstraints": [],
"memory": null,
"taskRoleArn": null,
"compatibilities": [
"EC2"
],
"taskDefinitionArn": "arn:aws:ecs:us-east-2:821819063141:task-definition/dappy_coin_database:2",
"family": "dappy_coin_database",
"requiresAttributes": [
{
"targetId": null,
"targetType": null,
"value": null,
"name": "com.amazonaws.ecs.capability.docker-remote-api.1.21"
}
],
"requiresCompatibilities": null,
"networkMode": null,
"cpu": null,
"revision": 2,
"status": "ACTIVE",
"volumes": []
}
推荐答案
OLD:
新:
刚刚看到您想要它们在单独的任务定义中.这非常复杂,我想劝阻您不要走这条路,因为您面临的选择包括:ELB,通过DNS进行服务发现,大使容器模式(每个-如果您只想要这个,则这个问题是重复的).如果必须这样做,请查看该答案,然后哭泣.
Just saw that you want them in separate task definitions. That's a lot of complexity and I want to dissuade you from this path, because you are facing options like: ELB, service discovery via DNS, ambassador container pattern (per this answer - which, if that is all you wanted, this question is a dupe). If you have to do it, see that answer, and weep.
也许您会考虑将Node应用程序部署为单个容器的Elastic Beanstalk应用程序,并将其连接到 MongoDB地图集?这样,您就可以实现负载平衡,自动扩展,监视等所有内置功能,而无需自己进行操作.
Maybe you would consider deploying your Node app as a single-container Elastic Beanstalk app, and connecting it to MongoDB Atlas? That way you get load balancing, auto-scaling, monitoring, all built in, instead of needing to do it yourself.
或者,至少您可以使用AWS Fargate .这是ECS的启动模式,可为您处理更多基础架构和网络.要引用文档,
Or, at least you could use AWS Fargate. It is a launch mode of ECS that handles more of the infrastructure and networking for you. To quote the docs,
localhost/127.0.0.1:<some_port_number>
在这种情况下为some_port_number = 27017
.
这篇关于在单独的Docker容器(AWS ECS)中连接到MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!