Connect Fiware IoT Agent to sharded MongoDB - mongodb

I am using to following docker-compose file to setup my sharded MongoDB which has 3 shards, 3 config servers and 2 router instances:
version: "3.5"
services:
mongorsn1:
container_name: mongors1n1
hostname: mongors1n1
image: mongo
command: mongod --shardsvr --replSet mongors1 --dbpath /data/db --port 27017
ports:
- 27017:27017
expose:
- "27017"
environment:
TERM: xterm
volumes:
- /etc/localtime:/etc/localtime:ro
- /mongo_cluster/data1:/data/db
mongors1n2:
container_name: mongors1n2
hostname: mongors1n2
image: mongo
command: mongod --shardsvr --replSet mongors1 --dbpath /data/db --port 27017
ports:
- 27027:27017
expose:
- "27017"
environment:
TERM: xterm
volumes:
- /etc/localtime:/etc/localtime:ro
- /mongo_cluster/data2:/data/db
mongors1n3:
container_name: mongors1n3
hostname: mongors1n3
image: mongo
command: mongod --shardsvr --replSet mongors1 --dbpath /data/db --port 27017
ports:
- 27037:27017
expose:
- "27017"
environment:
TERM: xterm
volumes:
- /etc/localtime:/etc/localtime:ro
- /mongo_cluster/data3:/data/db
mongocfg1:
container_name: mongocfg1
image: mongo
command: mongod --configsvr --replSet mongors1conf --dbpath /data/db --port 27017
environment:
TERM: xterm
expose:
- "27017"
volumes:
- /etc/localtime:/etc/localtime:ro
- /mongo_cluster/config1:/data/db
mongocfg2:
container_name: mongocfg2
image: mongo
command: mongod --configsvr --replSet mongors1conf --dbpath /data/db --port 27017
environment:
TERM: xterm
expose:
- "27017"
volumes:
- /etc/localtime:/etc/localtime:ro
- /mongo_cluster/config2:/data/db
mongocfg3:
container_name: mongocfg3
image: mongo
command: mongod --configsvr --replSet mongors1conf --dbpath /data/db --port 27017
environment:
TERM: xterm
expose:
- "27017"
volumes:
- /etc/localtime:/etc/localtime:ro
- /mongo_cluster/config3:/data/db
mongos1:
container_name: mongos1
image: mongo
hostname: mongos1
depends_on:
- mongocfg1
- mongocfg2
command: mongos --configdb mongors1conf/mongocfg1:27017,mongocfg2:27017,mongocfg3:27017 --port 27017
ports:
- 27019:27017
expose:
- "27017"
volumes:
- /etc/localtime:/etc/localtime:ro
mongos2:
container_name: mongos2
image: mongo
hostname: mongos2
depends_on:
- mongocfg1
- mongocfg2
command: mongos --configdb mongors1conf/mongocfg1:27017,mongocfg2:27017,mongocfg3:27017 --port 27017
ports:
- 27020:27017
expose:
- "27017"
volumes:
- /etc/localtime:/etc/localtime:ro
As you can see it is up and running correctly.
docker exec -it mongos1 bash -c "echo 'sh.status()' | mongo "
MongoDB shell version v4.4.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("f00e3ac7-e114-40b3-b7f8-0e5cf9c56e10") }
MongoDB server version: 4.4.6
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("609e21145937100b3f006e20")
}
shards:
{ "_id" : "mongors1", "host" : "mongors1/mongors1n1:27017,mongors1n2:27017,mongors1n3:27017", "state" : 1 }
active mongoses:
"4.4.6" : 2
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
bye
Now here is the docker-compose file I use to connect Orion and the IoT Agent to my MongoDB shards.
version: "3.5"
services:
orion:
image: atos/smbt/orion:2.5.2
hostname: orion
container_name: orion
restart: unless-stopped
ports:
- "1026:1026"
entrypoint:
- /usr/bin/contextBroker
- -fg
- -multiservice
- -ngsiv1Autocast
- -corsOrigin
- __ALL
command: -dbhost mongors1n1:27017,mongors1n2:27027,mongors1n3:27037 -rplSet mongors1 -logLevel ERROR -noCache
healthcheck:
test: curl --fail -s http://orion:1026/version || exit 1
networks:
- fiware_mongo_sharding_default
iot-agent:
image: atos/smbt/iotagent-ul:1.15.0
hostname: iot-agent
container_name: iot-agent
restart: unless-stopped
expose:
- "4041"
- "7896"
ports:
- "4041:4041"
- "7896:7896"
environment:
- IOTA_CB_HOST=orion
- IOTA_CB_PORT=1026
- IOTA_NORTH_PORT=4041
- IOTA_REGISTRY_TYPE=mongodb
- IOTA_LOG_LEVEL=ERROR
- IOTA_TIMESTAMP=true
- IOTA_CB_NGSI_VERSION=v2
- IOTA_AUTOCAST=true
- IOTA_MONGO_HOST=mongors1n1
- IOTA_MONGO_PORT=27017
- IOTA_MONGO_DB=iotagentul
- IOTA_HTTP_PORT=7896
- IOTA_PROVIDER_URL=http://iot-agent:4041
networks:
- fiware_mongo_sharding_default
networks:
fiware_mongo_sharding_default:
external: true
Orion is connected to MongoDB but with the IoT Agent I get the following error message.
time=2021-05-14T07:51:51.086Z | lvl=ERROR | corr=28202651-30e1-48dc-88a9-53de603e7e6d | trans=28202651-30e1-48dc-88a9-53de603e7e6d | op=IoTAgentNGSI.DbConn | from=n/a | srv=n/a | subsrv=n/a | msg=MONGODB-001: Error trying to connect to MongoDB: MongoNetworkError: failed to connect to server [mongos1:27019] on first connect [MongoNetworkError: connect ECONNREFUSED 172.20.0.9:27019] | comp=IoTAgent
I have tried different things such as:
- IOTA_MONGO_HOST=mongos1
- IOTA_MONGO_PORT=27019
but to no avail unfortunately. Is there something I am doing wrong?
Thanks in advance for your help.

I found the solution myself. What I was doing wrong is that the IoT Agent should be NOT be connected with a Sharded MongoDB as it only expects one Mongo instance. That instance is used to persist and retrieve IoT device created when you provision a sensor via the IoT Agent. Device measurement are sent to the Orion Context Broket which will persist into the Sharded MongoDB. Hope it can help somebody else.

Related

How to connect to MongoDB replica set?

I want to connect to MongoDB cluster using
mongodb://localhost:27017
It shows me an error
getaddrinfo ENOTFOUND messenger-mongodb-1
This is my docker-compose.yml file
version: '3'
services:
messenger-mongodb-1:
container_name: messenger-mongodb-1
image: mongo:6.0.3
command: mongod --replSet messenger-mongodb-replica-set --bind_ip_all
ports:
- 27017:27017
networks:
- messenger-mongodb-cluster
volumes:
- messenger-mongodb-1-data:/data/db
depends_on:
- messenger-mongodb-2
- messenger-mongodb-3
healthcheck:
test: test $$(echo "rs.initiate({_id:\"messenger-mongodb-replica-set\",members:[{_id:0,host:\"messenger-mongodb-1\"},{_id:1,host:\"messenger-mongodb-2\"},{_id:2,host:\"messenger-mongodb-3\"}]}).ok || rs.status().ok" | mongo --quiet) -eq 1
interval: 10s
start_period: 30s
messenger-mongodb-2:
container_name: messenger-mongodb-2
image: mongo:6.0.3
command: mongod --replSet messenger-mongodb-replica-set --bind_ip_all
ports:
- 27018:27017
networks:
- messenger-mongodb-cluster
environment:
- MONGO_INITDB_DATABASE=messenger-db
volumes:
- messenger-mongodb-2-data:/data/db
messenger-mongodb-3:
container_name: messenger-mongodb-3
image: mongo:6.0.3
command: mongod --replSet messenger-mongodb-replica-set --bind_ip_all
ports:
- 27019:27017
networks:
- messenger-mongodb-cluster
environment:
- MONGO_INITDB_DATABASE=messenger-db
volumes:
- messenger-mongodb-3-data:/data/db
networks:
messenger-mongodb-cluster:
volumes:
messenger-mongodb-1-data:
messenger-mongodb-2-data:
messenger-mongodb-3-data:
I run it like
docker-compose up -d
How can I connect to my replica set?
I want to use it for the local development of my node.js application
My operating system is Windows 11 Pro

how to run mongodb replica set in docker compose

I have tried to run mongodb replicaSet in local with mongoldb-community in my Mac I follow mongodb doc I can run it by this command
mongod --port 27017 --dbpath /usr/local/var/mongodb --replSet rs0 --bind_ip localhost,127.0.0.1
but it doesn't run on background, so every time I want to start replica set mongodb I should run that command, before I run it I should stop mongo first, then on the next tab console I should run mongo --eval "rs.initiate()" to create to replicaSet again
here is my docker compose:
version: "3.7"
services:
mongodb_container:
image: mongo:latest
ports:
- 27017:27017
volumes:
- mongodb_data_container:/data/db
volumes:
mongodb_data_container:
how to convert that into docker-compose ? is it possible ?
can I do docker exec CONTAINER_ID [commands] ? to run command mongo like above , but must stop the mongodb run in that docker ?
You can have a mongodb replica-set with this docker-compose services:
mongodb-primary:
image: "bitnami/mongodb:4.2"
user: root
volumes:
- ./mongodb-persistence/bitnami:/bitnami
networks:
- parse_network
environment:
- MONGODB_REPLICA_SET_MODE=primary
- MONGODB_REPLICA_SET_KEY=123456789
- MONGODB_ROOT_USERNAME=admin-123
- MONGODB_ROOT_PASSWORD=password-123
- MONGODB_USERNAME=admin-123
- MONGODB_PASSWORD=password-123
- MONGODB_DATABASE=my_database
ports:
- 27017:27017
mongodb-secondary:
image: "bitnami/mongodb:4.2"
depends_on:
- mongodb-primary
environment:
- MONGODB_REPLICA_SET_MODE=secondary
- MONGODB_REPLICA_SET_KEY=123456789
- MONGODB_PRIMARY_HOST=mongodb-primary
- MONGODB_PRIMARY_PORT_NUMBER=27017
- MONGODB_PRIMARY_ROOT_USERNAME=admin-123
- MONGODB_PRIMARY_ROOT_PASSWORD=password-123
networks:
- parse_network
ports:
- 27027:27017
mongodb-arbiter:
image: "bitnami/mongodb:4.2"
depends_on:
- mongodb-primary
environment:
- MONGODB_ADVERTISED_HOSTNAME=mongodb-arbiter
- MONGODB_REPLICA_SET_MODE=arbiter
- MONGODB_PRIMARY_HOST=mongodb-primary
- MONGODB_PRIMARY_PORT_NUMBER=27017
- MONGODB_PRIMARY_ROOT_PASSWORD=password-123
- MONGODB_REPLICA_SET_KEY=123456789
networks:
- parse_network
ports:
- 27037:27017
networks:
parse_network:
driver: bridge
ipam:
driver: default
volumes:
mongodb_master_data:
driver: local

sharding mogodb using docker container :docker-compose MongoDB Error: network error while attempting to run command 'isMaster' on host

I am trying sharding mongodbs. I have replicated the config server and also shards.But while connecting using router I get "Error: network error while attempting to run command 'isMaster' on host '127.0.0.1:60001'". I tried this but still getting same error. I am new to mongodb
I am using docker containers for each mongo instance
Here is .yaml file
""""
version: '3'
services:
cfgsvr1:
container_name: cfgsvr1
image: mongo
command: mongod --configsvr --replSet cfgrs --port 27017 --dbpath /data/db
ports:
- 40001:27017
volumes:
- cfgsvr1:/data/db
cfgsvr2:
container_name: cfgsvr2
image: mongo
command: mongod --configsvr --replSet cfgrs --port 27017 --dbpath /data/db
ports:
- 40002:27017
volumes:
- cfgsvr2:/data/db
cfgsvr3:
container_name: cfgsvr3
image: mongo
command: mongod --configsvr --replSet cfgrs --port 27017 --dbpath /data/db
ports:
- 40003:27017
volumes:
- cfgsvr3:/data/db
volumes:
cfgsvr1: {}
cfgsvr2: {}
cfgsvr3: {}
version: '3'
services:
mongos:
container_name: mongos
image: mongo
command: mongos --bind_ip_all --port 27017 --configdb cfgrs/cfgsvr3:27017,cfgsvr2:27017,cfgsvr1:27017
ports:
- 60001:27017
version: '3'
services:
shard1svr1:
container_name: shard1svr1
image: mongo
command: mongod --shardsvr --replSet shard1rs --port 27017 --dbpath /data/db
ports:
- 50001:27017
volumes:
- shard1svr1:/data/db
shard1svr2:
container_name: shard1svr2
image: mongo
command: mongod --shardsvr --replSet shard1rs --port 27017 --dbpath /data/db
ports:
- 50002:27017
volumes:
- shard1svr2:/data/db
shard1svr3:
container_name: shard1svr3
image: mongo
command: mongod --shardsvr --replSet shard1rs --port 27017 --dbpath /data/db
ports:
- 50003:27017
volumes:
- shard1svr3:/data/db
volumes:
shard1svr1: {}
shard1svr2: {}
shard1svr3: {}
version: '3'
services:
shard2svr1:
container_name: shard2svr1
image: mongo
command: mongod --shardsvr --replSet shard2rs --port 27017 --dbpath /data/db
ports:
- 50004:27017
volumes:
- shard2svr1:/data/db
shard2svr2:
container_name: shard2svr2
image: mongo
command: mongod --shardsvr --replSet shard2rs --port 27017 --dbpath /data/db
ports:
- 50005:27017
volumes:
- shard2svr2:/data/db
shard2svr3:
container_name: shard2svr3
image: mongo
command: mongod --shardsvr --replSet shard2rs --port 27017 --dbpath /data/db
ports:
- 50006:27017
volumes:
- shard2svr3:/data/db
volumes:
shard2svr1: {}
shard2svr2: {}
shard2svr3: {}
""""
my ubuntu version is 18.04
my dbversion is 3.6.3
my shell version is 3.6.3
logs
mongos | 2019-12-19T09:14:24.527+0000 I CONNPOOL [ReplicaSetMonitor-TaskExecutor] Connecting to cfgsvr3:27017
mongos | 2019-12-19T09:14:24.527+0000 I CONNPOOL [ReplicaSetMonitor-TaskExecutor] Connecting to cfgsvr1:27017
mongos | 2019-12-19T09:14:24.536+0000 W NETWORK [ReplicaSetMonitor-TaskExecutor] Unable to reach primary for set cfgrs
mongos | 2019-12-19T09:14:25.027+0000 I CONNPOOL [ReplicaSetMonitor-TaskExecutor] Connecting to cfgsvr2:27017
mongos | 2019-12-19T09:14:25.033+0000 W NETWORK [ReplicaSetMonitor-TaskExecutor] Unable to reach primary for set cfgrs
mongos | 2019-12-19T09:14:25.539+0000 W NETWORK [ReplicaSetMonitor-TaskExecutor] Unable to reach primary for set cfgrs
Found error as I was replicating config server without " configsvr: true,"
and
sh.addShard("shard_replica/shard1,shard2,shard3")
sh.addShard("shard_replica2/shard4,shard5,shard6")
version: '2'
services:
shard1:
container_name: shard1
image: mongo
command: mongod --shardsvr --replSet shard_replica --dbpath /data/db --port 27017 --bind_ip 0.0.0.0
ports:
- 40001:27017
environment:
TERM: xterm
expose:
- "27017"
volumes:
- /etc/localtime:/etc/localtime:ro
- /home/Chinmay/Documents/Chinmay/learn-mongodb/new/data1:/data/db
shard2:
container_name: shard2
image: mongo
command: mongod --shardsvr --replSet shard_replica --dbpath /data/db --port 27017 --bind_ip 0.0.0.0
environment:
TERM: xterm
ports:
- 40002:27017
expose:
- "27017"
volumes:
- /etc/localtime:/etc/localtime:ro
- /home/Chinmay/Documents/Chinmay/learn-mongodb/new/data2:/data/db
shard3:
container_name: shard3
image: mongo
command: mongod --shardsvr --replSet shard_replica --dbpath /data/db --port 27017 --bind_ip 0.0.0.0
environment:
TERM: xterm
ports:
- 40003:27017
expose:
- "27017"
volumes:
- /etc/localtime:/etc/localtime:ro
- /home/Chinmay/Documents/Chinmay/learn-mongodb/new/data3:/data/db
cfg1:
container_name: cfg1
image: mongo
command: mongod --configsvr --replSet conf_replica --dbpath /data/db --port 27017 --bind_ip 0.0.0.0
environment:
TERM: xterm
ports:
- 50001:27017
expose:
- "27017"
volumes:
- /etc/localtime:/etc/localtime:ro
- /home/Chinmay/Documents/Chinmay/learn-mongodb/new/config1:/data/db
cfg2:
container_name: cfg2
image: mongo
command: mongod --configsvr --replSet conf_replica --dbpath /data/db --port 27017 --bind_ip 0.0.0.0
ports:
- 50002:27017
expose:
- "27017"
volumes:
- /etc/localtime:/etc/localtime:ro
- /home/Chinmay/Documents/Chinmay/learn-mongodb/new/config2:/data/db
cfg3:
container_name: cfg3
image: mongo
command: mongod --configsvr --replSet conf_replica --dbpath /data/db --port 27017 --bind_ip 0.0.0.0
environment:
TERM: xterm
ports:
- 50003:27017
expose:
- "27017"
volumes:
- /etc/localtime:/etc/localtime:ro
- /home/Chinmay/Documents/Chinmay/learn-mongodb/new/config3:/data/db
shard4:
container_name: shard4
image: mongo
command: mongod --shardsvr --replSet shard_replica2 --dbpath /data/db --port 27017 --bind_ip 0.0.0.0
ports:
- 30001:27017
environment:
TERM: xterm
expose:
- "27017"
volumes:
- /etc/localtime:/etc/localtime:ro
- /home/Chinmay/Documents/Chinmay/learn-mongodb/new/data4:/data/db
shard5:
container_name: shard5
image: mongo
command: mongod --shardsvr --replSet shard_replica2 --dbpath /data/db --port 27017 --bind_ip 0.0.0.0
environment:
TERM: xterm
ports:
- 30002:27017
expose:
- "27017"
volumes:
- /etc/localtime:/etc/localtime:ro
- /home/Chinmay/Documents/Chinmay/learn-mongodb/new/data5:/data/db
shard6:
container_name: shard6
image: mongo
command: mongod --shardsvr --replSet shard_replica2 --dbpath /data/db --port 27017 --bind_ip 0.0.0.0
environment:
TERM: xterm
ports:
- 30003:27017
expose:
- "27017"
volumes:
- /etc/localtime:/etc/localtime:ro
- /home/Chinmay/Documents/Chinmay/learn-mongodb/new/data6:/data/db
mongos:
container_name: mongos
image: mongo
depends_on:
- cfg1
- cfg2
- cfg3
- shard1
- shard2
- shard3
- shard4
- shard5
- shard6
command: mongos --configdb conf_replica/cfg1:27017,cfg2:27017,cfg3:27017 --port 27017 --bind_ip 0.0.0.0
environment:
TERM: xterm
ports:
- 27019:27017
expose:
- "27017"
volumes:
- /etc/localtime:/etc/localtime:ro

Mongodb: Deploy a minimal mongodb cluster into docker swarm

I'm trying to deploy a minimal mongodb cluster into my development swarm environment like this:
Up to now:
version: "3.3"
networks:
net:
driver: overlay
services:
data1:
image: mongo:3.6
container_name: data1
command: mongod --shardsvr --replSet datars --smallfiles --port 27017
expose:
- 27017
networks:
- net
cfg1:
image: mongo:3.6
container_name: cfg1
command: mongod --configsvr --replSet cfgrs --smallfiles --port 27017
expose:
- 27017
networks:
- net
mongos1:
image: mongo:3.4
container_name: mongos1
command: mongos --configdb cfgrs/cfg1:27017
expose:
- 27017
networks:
- net
Into my config server, I'm getting these message:
2019-09-06T09:22:15.693+0000 I SHARDING [shard registry reload] Periodic reload of shard registry failed :: caused by :: 134 could not get updated shard list from config server due to Read concern majority reads are currently not possible.; will retry after 30s,
Any ideas?

Mongo + Docker not allowing overlay driver for replica set

I'm running this docker-compose.yml on my mac over a fresh Docker for Mac environment.
Gist here!
version: '2'
services:
replica1:
image: mongo:3.0
container_name: mongo1
ports:
- "27017:27017"
volumes:
- ./mongodata/replica1:/data/db
command: mongod --smallfiles --replSet "mrmtx"
networks:
- mongo_cluster
replica2:
image: mongo:3.0
container_name: mongo2
ports:
- "27017:27017"
volumes:
- ./mongodata/replica2:/data/db
command: mongod --smallfiles --replSet "mrmtx"
networks:
- mongo_cluster
replica3:
image: mongo:3.0
container_name: mongo3
ports:
- "27017:27017"
volumes:
- ./mongodata/replica3:/data/db
command: mongod --smallfiles --replSet "mrmtx"
networks:
- mongo_cluster
networks:
mongo_cluster:
driver: overlay
I'm getting this error:
Cannot start service replica2: b'Could not attach to network mongo-rplset_mongo_cluster: rpc error: code = PermissionDenied desc = network mongo-rplset_mongo_cluster not manually attachable'