Abstract
This post will guide you through setting up a locally MongoDB replica set using Docker for testing and learning purposes. Once you’ve successfully set up the replica set on your local machine, it’s easy to implement the same setup in production.
This setup features auto primary switching using HAProxy, which is useful when the framework you use does not come with built-in support for Connect to a Replica Set. We’ll go through the necessary steps and provide explanations along the way. By the end of this tutorial, you’ll have a local MongoDB replica set with HAProxy that can be used for testing and learning. Additionally, we’ll discuss implementing this setup in production by adding isolation, such as separated Docker virtual machines.
Introduction
A MongoDB replica set is a group of MongoDB instances that maintain the same data set, providing redundancy and increasing data availability. This tutorial will show you how to set up a MongoDB replica set using Docker and HAProxy for auto primary switching.
Attention, if you can use a framework that supports connecting to a replica set, you don’t need HAProxy. Refer to the MongoDB documentation (Archive) for more information.
Prerequisites
- Docker installed on your machine
- Basic knowledge of Docker and MongoDB
Step-by-Step Guide
Step 1: Generate Replica Key
First, we need to generate a replica key for authentication. Run the following commands to create a key file:
|
Replace /path/to/your/replica_key
with the path where you’d like to store the key file.
Step 2: Create Network
Create a Docker network for the MongoDB containers using the following command:
|
Step 3: Create MongoDB Containers
Create three MongoDB containers using the following commands:
|
Replace /path/to/your/mongo/
and /path/to/your/replica_key
with the appropriate paths. These commands create three MongoDB containers, each with a replica key and a data directory mounted.
Step 4: Initiate Replica Set
Run the following command to initiate the replica set:
|
You should see the output { "ok" : 1 }
indicating that the replica set has been successfully initiated.
Step 5: Check Replica Set Status
Check the status of the replica set with the following command:
|
The output should show three members with stateStr
of PRIMARY
or SECONDARY
and health
of 1
. If you see infoMessage: 'Could not find member to sync from'
, wait a few seconds and check again.
Step 6: Create HAProxy Config
Create an HAProxy configuration file named haproxy.cfg
.
|
The configuration above will check the status of the MongoDB containers every 3 seconds and switch the primary if needed.
Step 7: Create HAProxy Container
Create and run the HAProxy container with the following command:
|
Note that if you check its logs now, you’ll see something like this:
|
This is quite normal, as we mentioned earlier that the config will check for primary and point to it. In one replica set, there can only be one primary, so the other two will be shown as down.
If you made any changes to the haproxy.cfg file, restart the HAProxy container:
docker restart haproxy
Step 9: Test the Replica Set
Stop one of the MongoDB containers and restart all containers to test the replica set:
|
Step 10: Connect to the Proxied MongoDB
Finally, connect to the reverse-proxied MongoDB using a client such as MongoDB Compass. Make sure to check the “Direct Connection” checkbox, or you may encounter an error similar to getaddrinfo ENOTFOUND mongo1
.
Conclusion
By following these steps, you have successfully set up a MongoDB replica with HAProxy for auto primary switching. You can now connect to the MongoDB replica set using the HAProxy container’s address and port 27017.
In production, you may not want to set up all MongoDB instances on the same machine. In that case, configure each MongoDB instance on separate physical or virtual machines and adjust the addresses in the HAProxy configuration accordingly.