How to Create MongoDB cluster as Docker Containers
Solution 1
I used this tutorial myself: https://medium.com/@gargar454/deploy-a-mongodb-cluster-in-steps-9-using-docker-49205e231319#.mle6a8wmg
Step 1: create folders
create folders (local on all nodes):
sudo mkdir -p /dockerlocalstorage/data/mongodb
sudo mkdir -p /dockerlocalstorage/backup/mongodb
sudo mkdir -p /dockersharedstorage/config/mongodb/
Step 2: create keyfile
create keyfile as root user and give correct permissions:
sudo su
cd /dockersharedstorage/config/mongodb/
openssl rand -base64 741 > mongodb-keyfile
chmod 600 mongodb-keyfile
sudo chown 999 mongodb-keyfile
Depending on the mount type you might need to use /dockerlocalstorage/ to keep the certs. Mongodb will complain if the permissions are not set correctly (which could be harder to achieve on lets say a cifs mount)
Step 3: setup first node
create mongodb container without auth/keyfile:
docker run --name mongodb \
-v /dockerlocalstorage/data/mongodb:/data/db \
-v /dockersharedstorage/config/mongodb:/opt/keyfile \
--hostname="dock01" \
-p 27017:27017 \
-d mongo
log in to container:
docker exec -it mongodb mongo
create root users, dont forget to change the passwords
use admin
db.createUser( {
user: "admin",
pwd: "PASSWORD",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
});
db.createUser( {
user: "root",
pwd: "PASSWORD",
roles: [ { role: "root", db: "admin" } ]
});
exit and remove the container
exit
docker stop mongodb
docker rm mongodb
Step 4: start nodes
change NODE_NR:
docker run \
-d \
--name mongodb \
-v /dockerlocalstorage/data/mongodb:/data/db \
-v /dockerlocalstorage/backup/mongodb:/data/backup \
-v /dockersharedstore/config/mongodb:/opt/keyfile \
--restart=always \
--hostname="dock01" \
-p 27017:27017 mongo \
--keyFile /opt/keyfile/mongodb-keyfile \
--replSet "SET_NAME"
Step 5: setup replication
connect to node 1:
docker exec -it mongodb mongo
use admin
db.auth("root", "PASSWORD");
initialize the replication set:
rs.initiate()
Check the replica set with: rs.conf()
or rs.status()
add others:
rs.add("dock02:27017")
rs.add("dock03:27017")
Step 6: setup mongodump
edit crontab:
sudo su
crontab -l > tempcron
echo new cron into cron file
echo "0 4 * * * docker exec -it mongodb mongodump -u root -p PASSWORD -o /data/backup/daily" >> tempcron
echo "30 4 * * 5 docker exec -it mongodb mongodump -u root -p PASSWORD -o /data/backup/weekly" >> tempcron
install new cron file
crontab tempcron
rm tempcron
exit
Solution 2
You could use docker swarm mode if you want docker native mongodb cluster (Docker >= 1.12 needed).
Have a look at this nice tutorial. This will show you how to get a mongodb cluster with docker, replicated with Config Server. Basically, the steps are:
- Create multiple virtual machines (with
docker-machine
or whatever you use to create a new docker host) - Create the swarm (docker cluster of multiple machines)
- Create a swarm overlay network to deal with all your mongodb traffic
- Create the services on each swarm node (that will create your mongodb containers on your hosts)
- Configure your mongodb replica set
This is a bit of work, but worth it, as when you get there, you'll have tools on docker swarm
to orchestrate your mongodb cluster.
Vigneshwaran
Updated on June 16, 2022Comments
-
Vigneshwaran about 2 years
I can able to run single instance on Mongo using the following Docker command
docker run -it --rm -d -p 27017:27017 --user mongodb mongo:3.4
But I can't find out how to configure Config Server and Query Router and also how add shards with Replication
Thanks in Advance