1.on manager,init swarm cluster
mongodb1:~$ sudo docker swarm init
Swarm initialized: current node (aabtkgzoe7cpcgrsrzkp6msp6) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-0ba2xqh7hztgosum88omn4bom1l7k2czi4x9nh9wxib323zvl4-2vd639k85kp7inkm2a2a6ntob 10.0.5.8:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
2. on worker,join swarm cluster
mongodb2:~$ sudo docker swarm join --token SWMTKN-1-0ba2xqh7hztgosum88omn4bom1l7k2czi4x9nh9wxib323zvl4-2vd639k85kp7inkm2a2a6ntob 10.0.5.8:2377
This node joined a swarm as a worker.
3. verify cluster members
mongodb1:~$ sudo docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
aabtkgzoe7cpcgrsrzkp6msp6 * mongodb1 Ready Active Leader 18.03.1-ce
j3yc0hfdrpursh2b9hv54oeg9 mongodb2 Ready Active 18.03.1-ce
4. create a docker network
mongodb1:~$ sudo docker network create -d overlay --attachable mongo
ncg0km83ol2ke0yunelorjlie
5. create docker-compose.yaml
version: '3.3'
services:
mongors1n1:
image: mongo
command: mongod --shardsvr --replSet shard1 --dbpath /data/db --port 27017
networks:
- mongo
volumes:
- /etc/localtime:/etc/localtime
- /data/shard1:/data/db
deploy:
restart_policy:
condition: on-failure
replicas: 1
placement:
constraints:
- node.hostname==mongodb1
mongors2n1:
image: mongo
command: mongod --shardsvr --replSet shard2 --dbpath /data/db --port 27017
networks:
- mongo
volumes:
- /etc/localtime:/etc/localtime
- /data/shard2:/data/db
deploy:
restart_policy:
condition: on-failure
replicas: 1
placement:
constraints:
- node.hostname==mongodb1
mongors1n2:
image: mongo
command: mongod --shardsvr --replSet shard1 --dbpath /data/db --port 27017
networks:
- mongo
volumes:
- /etc/localtime:/etc/localtime
- /data/shard1:/data/db
deploy:
restart_policy:
condition: on-failure
replicas: 1
placement:
constraints:
- node.hostname==mongodb2
mongors2n2:
image: mongo
command: mongod --shardsvr --replSet shard2 --dbpath /data/db --port 27017
networks:
- mongo
volumes:
- /etc/localtime:/etc/localtime
- /data/shard2:/data/db
deploy:
restart_policy:
condition: on-failure
replicas: 1
placement:
constraints:
- node.hostname==mongodb2
cfg1:
image: mongo
command: mongod --configsvr --replSet cfgrs --smallfiles --dbpath /data/db --port 27017
networks:
- mongo
volumes:
- /etc/localtime:/etc/localtime
- /data/config:/data/db
deploy:
restart_policy:
condition: on-failure
replicas: 1
placement:
constraints:
- node.hostname==mongodb1
cfg2:
image: mongo
command: mongod --configsvr --replSet cfgrs --smallfiles --dbpath /data/db --port 27017
networks:
- mongo
volumes:
- /etc/localtime:/etc/localtime
- /data/config:/data/db
deploy:
restart_policy:
condition: on-failure
replicas: 1
placement:
constraints:
- node.hostname==mongodb2
mongos:
image: mongo
command: mongos --configdb cfgrs/cfg1:27017,cfg2:27017,cfg3:27017 --bind_ip 0.0.0.0 --port 27017
networks:
- mongo
ports:
- 27017:27017
volumes:
- /etc/localtime:/etc/localtime
depends_on:
- cfg1
- cfg2
deploy:
restart_policy:
condition: on-failure
mode: global
networks:
mongo:
external: true
6. start services on manager
mongodb1:~$ sudo docker stack deploy -c docker-compose.yaml mongo
Creating service mongo_cfg2
Creating service mongo_mongos
Creating service mongo_mongors1n1
Creating service mongo_mongors2n1
Creating service mongo_mongors1n2
Creating service mongo_mongors2n2
Creating service mongo_cfg1
7. verify service status
mongodb1:~$ sudo docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
v8w3waid5ed9 mongo_cfg1 replicated 1/1 mongo:latest
l0y7e2bdn9v3 mongo_cfg2 replicated 1/1 mongo:latest
pg30hozxo2zo mongo_mongors1n1 replicated 1/1 mongo:latest
i8b3ehsvlwwj mongo_mongors1n2 replicated 1/1 mongo:latest
q8qmidrbqxz9 mongo_mongors2n1 replicated 1/1 mongo:latest
khj5sv15luwu mongo_mongors2n2 replicated 1/1 mongo:latest
cagxcn8uljrn mongo_mongos global 2/2 mongo:latest *:27017->27017/tcp
8. init mongo config replication
mongodb1:~$ sudo docker exec -it $(sudo docker ps | grep "cfg1" | awk '{ print $1 }') bash -c "echo 'rs.initiate({_id: \"cfgrs\",configsvr: true, members: [{ _id : 0, host : \"cfg1\" },{ _id : 1, host : \"cfg2\" }]})' | mongo"
9. init mongo shard
mongodb1:~$ sudo docker exec -it $(sudo docker ps | grep "mongors1n1" | awk '{ print $1 }') bash -c "echo 'rs.initiate({_id : \"shard1\", members: [{ _id : 0, host : \"mongors1n1\" },{ _id : 1, host : \"mongors1n2\" }]})' | mongo"
mongodb1:~$ sudo docker exec -it $(sudo docker ps | grep "mongors1n1" | awk '{ print $1 }') bash -c "echo 'rs.initiate({_id : \"shard2\", members: [{ _id : 0, host : \"mongors2n1\" },{ _id : 1, host : \"mongors2n2\" }]})' | mongo"
10. add shard members
mongodb1:~$ sudo docker exec -it 70fbea598ef7 bash -c "echo 'sh.addShard(\"shard1/mongors1n1:27017,mongors1n2:27017\")' | mongo "
mongodb1:~$ sudo docker exec -it 70fbea598ef7 bash -c "echo 'sh.addShard(\"shard2/mongors2n1:27017,mongors2n2:27017\")' | mongo "