Hi,
I have a Docker Swarm Stack, that consists of a Frontend Service, MongoDB Replicaset with 3 replicas for HA reasons and a service that runs one time on deploy and sets up the replicaset.
For the MongoDB Replicaset I need to specify the DNS Names of the other MongoDBs so that they can communicate with each other. Simplified my docker compose looks like this:
version: '3.8'
services:
rocketchat:
image: Frontend
environment:
- MONGO_URL=mongodb://mongo1:27017,mongo2:27017,mongo3:27017/dbname?replicaSet=rs0&readPreference=primaryPreferred&w=majority
- MONGO_OPLOG_URL=mongodb://mongo1:27017,mongo2:27017,mongo3:27017/local?replicaSet=rs0&readPreference=primaryPreferred
mongo1:
image: mongo
command: mongod --smallfiles --oplogSize 128 --replSet rs0
mongo2:
image: mongo
command: mongod --smallfiles --oplogSize 128 --replSet rs0
mongo3:
image: mongo
command: mongod --smallfiles --oplogSize 128 --replSet rs0
init-replica1:
image: mongo
deploy:
restart_policy:
condition: on-failure
command: 'bash -c "for i in `seq 1 30`; do mongo mongo1/dbname --eval \"rs.initiate({ _id: ''rs0'', members: [ { _id: 0, host: ''mongo1:27017'' }, { _id: 1, host: ''mongo2:27017'' }, { _id: 2, host: ''mongo3:27017'' } ]})\" && s=$$? && break || s=$$?; echo \"Tried $$i times. Waiting 5 secs...\"; sleep 5; done; (exit $$s)"'
This works fine, but when I need to update my MongoDB container (Verision update etc.) then all of them restart at once when I do:
docker stack deploy my-stack
The other option to my knowledge is to have only one Mongo Service with three replicas, so i can specify the update_config but then I dont have a dns name for each replica (here mongo1,mongo2, mongo3) so my replicaset fails:
version: '3.8'
services:
rocketchat:
image: Frontend
environment:
- MONGO_URL=mongodb://mongo1:27017,mongo2:27017,mongo3:27017/dbname?replicaSet=rs0&readPreference=primaryPreferred&w=majority
- MONGO_OPLOG_URL=mongodb://mongo1:27017,mongo2:27017,mongo3:27017/local?replicaSet=rs0&readPreference=primaryPreferred
mongo1:
image: mongo
command: mongod --smallfiles --oplogSize 128 --replSet rs0
deploy:
replicas: 3
update_config:
delay: 1m
failure_action: rollback
init-replica1:
image: mongo
deploy:
restart_policy:
condition: on-failure
command: 'bash -c "for i in `seq 1 30`; do mongo mongo1/dbname --eval \"rs.initiate({ _id: ''rs0'', members: [ { _id: 0, host: ''mongo1:27017'' }, { _id: 1, host: ''mongo2:27017'' }, { _id: 2, host: ''mongo3:27017'' } ]})\" && s=$$? && break || s=$$?; echo \"Tried $$i times. Waiting 5 secs...\"; sleep 5; done; (exit $$s)"'
Does anyone have an idea how to configure this so that only one db container at a time is down, in case I have an update for my mongoDbs?