The Easiest Way To Setup A MongoDB Replica Set on Docker-compose/Kubernetes

Let's say you're building an app, and you need to implement multi-document transactions to ensure updates across multiple collections succeed or fail together.

This article will help you set up everything you need to take advantage of this feature of MongoDB in your dev environment or when running your integration tests.

This article will not go into how MongoDB Transactions work or how to use them.
There are already several awesome resources covering that. Links to some of my favorites are included at the bottom of this article.

Beginning at 4.0, MongoDB added support for multi-document ACID transactions.

This is awesome but there's a caveat:

To utilize transactions, MongoDB must be configured as a replica set or a sharded cluster. Transactions are not supported on standalone deployments. If you are using a database hosted on Atlas, you do not need to worry about this as every Atlas cluster is either a replica set or a sharded cluster.

A replica set in MongoDB is a group of mongod processes that are used to maintain the same data set. They provide high redundancy and high availability.

If you aren't using Docker, There is an NPM package called run-rs, which can help you set this up. However, if you are developing using docker-compose/Kubernetes, or need to quickly spin up a zero-config replica set to run in a CI suite, this is for you.

Alright, lets get into it.

With Docker-compose ๐Ÿณ

# docker-compose.yml

version: "3"
services:
  mongodb:
    image : davybello/mongo-replica-set:4.4.8
    container_name: mongodb
    volumes:
      - "./docker/.data/mongo1:/var/lib/mongo1"
      - "./docker/.data/mongo2:/var/lib/mongo2"
      - "./docker/.data/mongo3:/var/lib/mongo3"
    ports:
      - 27017:27017
      - 27018:27018
      - 27019:27019

That's it!!

Really, that's all you need to have a replica set up and running.

A quick breakdown of what that does:

  • Pulls the davybello/mongo-replica-set:4.4.8 image
  • Mounts the volumes for each of the individual mongoDB instances to their respective folders ./docker/.data/mongo1, /mongo2 and /mongo3
  • Exposes the ports for each of the MongoDB instances on ports 21017, 21018, and 21019 respectively

and it's just as easy when;

Working with kubernetes โ˜ธ๏ธ

Here's a sample pod config

apiVersion: v1
kind: Pod
metadata:
  name: mongo-pod
spec:
  containers:
    - name: mongo
    image: davybello/mongo-replica-set:4.4.8

Pretty neat, but how do you connect to this?

The ports can be exposed through a service like so

apiVersion: v1
kind: Service
metadata:
  name: mongo-service
spec:
  selector:
    app: mongo
  ports:
    - name: db1
      protocol: TCP
      port: 27017
      targetPort: 27017
    - name: db2
      protocol: TCP
      port: 27018
      targetPort: 27018
    - name: db3
      protocol: TCP
      port: 27019
      targetPort: 27019

This exposes each MongoDB instance on ports 21017, 21018, and 21019 respectively

You can connect to the replica-set from any app in your cluster using this service.

To persist your data here's a sample config using a PersistentVolumeClaim named mongo-volume

apiVersion: v1
kind: Pod
metadata:
  name: mongo-pod
spec:
  containers:
    - name: mongo
    image: davybello/mongo-replica-set:4.4.8
    volumeMounts:
      - mountPath: '/var/lib/mongo1'
        subPath: "mongo1"
        name: mongo-data
      - mountPath: '/var/lib/mongo2'
        subPath: "mongo2"
        name: mongo-data
      - mountPath: '/var/lib/mongo3'
        subPath: "mongo3"
        name: mongo-data
  volumes:
    - name: mongo-data
      persistentVolumeClaim:
        claimName: mongo-volume

Conclusion ๐Ÿ

In the end, We've seen how to easily set up a replica set, with basically zero config, using docker-compose and Kubernetes.
We've also seen how to expose the ports for each MongoDB instance. As well as persist the data with volume mounts.

If you are curious about how to set up a replica set manually, or you want to do some further reading, links are included below

Let me know what you think about this?
And feel free to share any improvement tips for this. โœŒ๏ธ

Links ๐Ÿ”—

19