MongoDB reverse proxy and replica set

Hello guys,
I hope everyone is ok. I am sorry to bother you with some basic questions but I am new to mongodb and i am struggling a bit.

I am a junior devOps, and i have a practice project which consist in:
1 aws instance running a nodejs application (just a simple home page) running on port 3000
2 a mongodb (configured with the application and should connect on port 27017

Inside the app configuration, there is a variable called DB_HOST(where, from the instance, we should import the link

mongodb://10.0.1.100:27017/posts

and we should be able to connect from the app instance IP:27017/posts to the mongodb and display some random blog posts.

Now, the process i followed, was using chef,packer and terraform to spin up aws instances. the app application works just fine, but if i try to access the mongodb, keeps loading the page but nothing happens.

Here are the steps i did.

Withthe application, i implemented a nginx reverse proxy, so the communication get through port 80 instead of port 3000

in mongodb i did this

mongod.conf

# mongod.conf



# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/



# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:



# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log



# network interfaces
net:
  port: <%= @port %>
  bindIp: <%= @bind_ip %>




#processManagement:



#security:



#operationProfiling:



#replication:



#sharding:



## Enterprise-Only Options:



#auditLog:



#snmp:

and in the attributre i set:

default[‘mongodb’][‘port’] = 27017
default[‘mongodb’][‘bind_ip’] = ‘0.0.0.0’

doing this i should be able to connect from any ip to the port 27017.

Now, doing this, if i ssh inside my app instance and node seed my db, i can do so, but as soon as i run the command npm start, i get an error that there is another service running on port 3000.

plus i have another problem, as i need to implement a replicaset, in my DB_HOST i should implemente 3 different mongodb, so i did so:

export DB_HOST=mongodb://10.0.1.100:27017/posts,10.0.2.100:27017/posts,10.0.3.100:27017/posts?replicaSet=rs0

but doing this, i get an error that i cant pass multiple values.

Any idea guys to sort out this? thank you very much for your help.

the format is mongodb://host:port,host:port,host:port/database?replicaSet=setName

The monouri format is documented here.

Hi chris, thank you very much for the time to reply.

i tried your code

export DB_HOST=mongodb://10.0.1.100:27017,10.0.2.100:27017,10.0.3.100:27017?replicaSet=rs0

ERROR:

(node:2579) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(node:2579) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
(node:2579) UnhandledPromiseRejectionWarning: MongoError: no primary found in replicaset or invalid replica set name
    at /home/ubuntu/AppFolder/app/node_modules/mongoose/node_modules/mongodb/lib/core/topologies/replset.js:616:11
    at Server.<anonymous> (/home/ubuntu/AppFolder/app/node_modules/mongoose/node_modules/mongodb/lib/core/topologies/replset.js:338:9)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Server.emit (events.js:211:7)
    at Pool.<anonymous> (/home/ubuntu/AppFolder/app/node_modules/mongoose/node_modules/mongodb/lib/core/topologies/server.js:377:12)
    at emitTwo (events.js:126:13)
    at Pool.emit (events.js:214:7)
    at connect (/home/ubuntu/AppFolder/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/pool.js:624:10)
    at callback (/home/ubuntu/AppFolder/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:109:5)
    at runCommand (/home/ubuntu/AppFolder/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:172:5)
    at Connection.messageHandler (/home/ubuntu/AppFolder/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:334:5)
    at emitTwo (events.js:126:13)
    at Connection.emit (events.js:214:7)
    at processMessage (/home/ubuntu/AppFolder/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connection.js:364:10)
    at Socket.<anonymous> (/home/ubuntu/AppFolder/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connection.js:533:15)
(node:2579) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:2579) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

this is my configuration

bash script for the app to seed the mongodb DB_HOST:

#!/bin/bash


export DB_HOST=mongodb://10.0.1.100:27017,10.0.2.100:27017,10.0.3.100:27017?replicaSet=rs0

cd /home/ubuntu/AppFolder/app
node seeds/seed.js


sudo npm install
sudo npm start &

Primary and other replicasets DB_HOST:

#!/bin/bash

echo '10.0.1.100' >> /etc/hosts

sudo systemctl enable mongod
sudo systemctl start mongod

mongo mongodb://10.0.1.100 --eval "rs.initiate( { _id : 'rs0', members: [{ _id: 0, host: '10.0.1.100:27017' }]})"
mongo mongodb://10.0.1.100 --eval "rs.add( '10.0.2.100:27017' )"
mongo mongodb://10.0.1.100 --eval "rs.add( '10.0.3.100:27017' )"
mongo mongodb://10.0.1.100 --eval "db.isMaster().primary"
mongo mongodb://10.0.1.100 --eval "rs.slaveOk()"


sleep 60; sudo systemctl restart metricbeat
sudo systemctl restart filebeat

sleep 180; sudo filebeat setup -e \
  -E output.logstash.enabled=false \
  -E output.elasticsearch.hosts=['10.0.105.100:9200'] \
  -E setup.kibana.host=10.0.105.101:5601 && sudo metricbeat setup

i realised that when my instance is up, it does execute all the command but it does not export the enviroment variable, so i need to do it manually.