1

I am encountering an ENOTFOUND error within a multi-container Kubernetes pod. 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).

/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.

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. This same set of Docker containers work fine with Docker using a docker-compose.yml file. 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. Using "flannel" for CNI, so use the following to initialize Kubernetes.

kubeadm init --pod-network-cidr=10.244.0.0/16
djsys
  • 13
  • 3
  • Would you try `curl http://mongodb:27017` to see the output? Are the application and mongodb in the same pod ? – Kun Li Jul 20 '18 at 01:29

1 Answers1

2

According to the error output, your NodeJS application tries to connect to MongoDB database via mongodb:27017.

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.

So, you need to change NodeJS application's configuration: set connection to MongoDB localhost:27017 instead of mongodb:27017.

nickgryg
  • 25,567
  • 5
  • 77
  • 79
  • Yes. That was the solution switching to "localhost:27017" instead of "mongodb:27017". Now able to see the connection between the MongoDB container and other application containers within the same pod. – djsys Jul 24 '18 at 18:51
  • Nice to hear that! Please approve the answer then. – nickgryg Jul 24 '18 at 18:58
  • Hi, I need assistance with almost similar issue here: https://stackoverflow.com/questions/65870380/connect-to-mongodb-with-mongoose-both-in-kubernetes – Denn Jan 25 '21 at 07:10