/mongo-replica-docker-swarm

Mongo DB Setup by Docker Swarm

Primary LanguageShell

mongo-replica-docker-swarm

Running Mongo DB Replica Set on three different server by Docker Swarm.

The oplog size sets to 3(MB), because of 5% of free disk space.

Server Requirements

  1. Docker (version >= 17.0.0)
  2. Docker-Compose
  3. Compose file(version >= 3.0.0)

Install Docker

sudo apt-get install -y apparmor lxc cgroup-lite curl
wget -qO- https://get.docker.com/ | sh
sudo usermod -aG docker YourUserNameHere
sudo service docker restart

Install Docker-compose

sudo su
curl -L https://github.com/docker/compose/releases/download/1.4.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
exit

Installation Steps

Initialize Manager Node

docker swarm init --listen-addr *manager-address:2377* --advertise-addr *manager-address*

Output

$ docker swarm init --advertise-addr *manager-address*
Swarm initialized: current node (bvz81updecsj6wjz393c09vti) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join \
    --token *docker-swarm-token* \
    *manager-address*

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

Join Worker Node into network

ssh <-- The other servers -->
docker swarm join --token *docker-swarm-token* *manager-address*

Setup Mongo DB on each node

First, you should update label on each node.

ssh <-- The manager node server -->
docker node update --label-add mongo.replica=1 *manager-address*
docker node update --label-add mongo.replica=2 *worker1-address*
docker node update --label-add mongo.replica=3 *worker2-address*

Second, deploy service on manager node.

ssh <-- The manager node server -->
docker stack deploy -c docker-compose.yml overlay

And, you should login Manager node and run the under command.

./initiate-master

# docker service update --publish-rm 27017:27017 overlay_mongo1
# // if publish port was set before, need to remove first and update it.

docker service update --publish-add 27017:27017 overlay_mongo1

Persistent storage

Data is stored at Docker Swarm Volume, if you want to check the Mountpoint on physical machine. Running the following command:

docker volume inspect *service name*

Data will be persistent between service runs. To remove docker stack and all data. Run the following command:

docker stack rm overlay
docker volume rm $(docker volume ls -qf label=com.docker.stack.namespace=overlay)

Leave swarm mode

docker swarm leave

Reference