Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create MongoDB cluster as Docker Containers

Tags:

docker

mongodb

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

like image 832
Vigneshwaran Avatar asked Feb 04 '23 13:02

Vigneshwaran


1 Answers

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
like image 199
p.streef Avatar answered Feb 07 '23 07:02

p.streef