简体   繁体   English

Node.js应用程序无法使用Kubernetes连接到MongoDB Docker容器

[英]Node.js application fails to connect to MongoDB Docker container using Kubernetes

I am encountering an ENOTFOUND error within a multi-container Kubernetes pod. 我在多容器Kubernetes容器中遇到ENOTFOUND错误。 MongoDB is in one Docker container and appears to be fully operational, and a Node.js application is in another container (see its error below). MongoDB在一个Docker容器中,并且似乎可以完全运行,而Node.js应用程序在另一个容器中(请参见下面的错误)。

/opt/systran/apps-node/enterprise-server/node_modules/mongoose/node_modules/mongodb/lib/replset.js:365
          process.nextTick(function() { throw err; })
                                        ^
MongoError: failed to connect to server [mongodb:27017] on first connect [MongoError: getaddrinfo ENOTFOUND mongodb mongodb:27017]
    at Pool. (/opt/systran/apps-node/enterprise-server/node_modules/mongodb-core/lib/topologies/server.js:336:35)
    at emitOne (events.js:116:13)
    at Pool.emit (events.js:211:7)
    at Connection. (/opt/systran/apps-node/enterprise-server/node_modules/mongodb-core/lib/connection/pool.js:280:12)
    at Object.onceWrapper (events.js:317:30)
    at emitTwo (events.js:126:13)
    at Connection.emit (events.js:214:7)
    at Socket. (/opt/systran/apps-node/enterprise-server/node_modules/mongodb-core/lib/connection/connection.js:189:49)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at emitErrorNT (internal/streams/destroy.js:64:8)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

In the application container I can do the following, so it seems to know that MongoDB is available on port 27017. 在应用程序容器中,我可以执行以下操作,因此似乎知道MongoDB在端口27017上可用。

curl "http://localhost:27017"
It looks like you are trying to access MongoDB over HTTP on the native driver port.

The application is intended to create databases in MongoDB, and populate collections. 该应用程序旨在在MongoDB中创建数据库,并填充集合。 This same set of Docker containers work fine with Docker using a docker-compose.yml file. 使用docker-compose.yml文件,这组相同的Docker容器可以与Docker正常工作。 The containers are part of a legacy application (there are other containers in the same pod), and I don't have much control over their content. 这些容器是旧版应用程序的一部分(在同一容器中有其他容器),我对其内容没有太多控制。

I have checked logs for the various containers. 我已经检查了各种容器的日志。 Have reviewed all stock pods with "kubectl get pods" and all are working fine. 用“ kubectl get pods”检查了所有库存豆荚,并且一切正常。 Using "flannel" for CNI, so use the following to initialize Kubernetes. 对CNI使用“法兰绒”,因此请使用以下内容初始化Kubernetes。

kubeadm init --pod-network-cidr=10.244.0.0/16

According to the error output, your NodeJS application tries to connect to MongoDB database via mongodb:27017 . 根据错误输出,您的NodeJS应用程序尝试通过mongodb:27017连接到MongoDB数据库。

As both NodeJS application and MongoDB database are containers of the same pod, NodeJS application should be connected to MongoDB database via localhost:27017 instead, because containers in a pod share storage/network resources. 由于NodeJS应用程序和MongoDB数据库都是同一Pod的容器,因此NodeJS应用程序应通过localhost:27017连接到MongoDB数据库,因为Pod中的容器共享存储/网络资源。

So, you need to change NodeJS application's configuration: set connection to MongoDB localhost:27017 instead of mongodb:27017 . 因此,您需要更改NodeJS应用程序的配置:将连接设置为MongoDB localhost:27017而不是mongodb:27017

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Kubernetes node.js 容器无法连接到 MongoDB Atlas - Kubernetes node.js container cannot connect to MongoDB Atlas Node.js 使用 Helm 在 Kubernetes 上使用 MongoDB 的应用程序 - Node.js Application with MongoDB on Kubernetes Using Helm 链接Docker容器:Node.js + MongoDB - Linking Docker container : Node.js + MongoDB 将 mongo docker 容器与 node.js 连接起来 - Connect mongo docker container with node.js 如何使用 mongoose 将本地 mongodb 与 node.js 应用程序连接起来? - how to connect local mongodb with a node.js application using mongoose? 无法通过使用 docker 上的网络连接到在 docker 容器中运行的 mongodb 其他带有节点 js 的容器 - Not able to connect by using network on docker to mongodb running in docker container other container with node js 无法使用node.js连接到MongoDB - Unable to connect to MongoDB using node.js 使用环境变量的 Node.js 和 MongoDB 容器之间的连接问题。 在 Docker 编写 - Problems in connection between Node.js and MongoDB container using environment var. in Docker Compose Docker-Node.js + MongoDB-“错误:无法连接到[localhost:27017]” - Docker - Node.js + MongoDB - “Error: failed to connect to [localhost:27017]” 无法通过 Docker 中的 node.js 连接到 MongoDB - Cannot connect to MongoDB via node.js in Docker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM