Link List

Feb 11, 2016

Deploying MongoDB Replica Sets with Docker

I recently set up a 3 Node MongoDB cluster replica set with each Node running MongoDB as a Docker image as the first step to get the same scenario working in Kubernetes. It wasn't very intuitive so I thought I'd write it up so that I could share the recipe.

I started by deploying 3 EC2 instances running Fedora 23 (ami-7b8afa11) and Docker 1.9.1 (the docker version that ships with F23). Make sure your security group has an inbound rule for TCP port 27017 as this is required for MongoDB

On each node, do the following:

1. docker pull mongo

This pulls the official MongoDB image from the Docker Hub

2. mkdir /mnt/mongo

This creates a directory on the Host which we will use to persist the Mongo container instances database state. This allows the container to be stopped and restarted without losing any data.

3. chcon -Rt svirt_sandbox_file_t /mnt/mongo

This sets the SELinux label so that the container has permission to access the /mnt/mongo directory on the host.

7. Congrats. That's it. You can now import data and test your Replica Set. Keep in mind that only the Primary Node can handle writes but all the other members of the replica set (the secondary nodes) can do reads.

If you want to test the read functionality of the replica set on a secondary node, docker exec into the container on that node, run mongo at the prompt and run the command below before you attempt to run any queries (reads).