Coolify website

How to deploy MongoDB replica set on Coolify

Rufat MAMMADLI
2 min readJun 13, 2024

--

After a careful search for an open-source PaaS, I've found Coolify. Coolify is an awesome product made by Andras Bacsai.

Coolify provides a beautiful user interface to manage the hosted apps on your server. It is an alternative to Heroku, Netlify, Vercel, DigitalOcean Apps, and even some AWS and Azure features.

After starting to migrate to Hetzner & Coolify from MongoDB Atlas and DigitalOcean, I stuck on creating a replica set of instances for MongoDB. With the current version, Coolify does not provide an easy way to set up it.

So, I will explain how I achieved deploying the MongoDB replica set on my server.

1. You will need to create three MongoDB instances on Coolify.

2. Give 5432, 5433, and 5434 public ports and turn on the Make it publicly available check on each instance.

3. Open the terminal via SSH and create a key using:

openssl rand -base64 741

4. Copy the output and create a key file named keyfile-mongo-0 and save it (I created it on /root/mongo-repl-0-mount directory).

5. Give reading permission to the key file:

chmod 400 keyfile-mongo-0
chown 999:999 ./keyfile-mongo-0

6. Go to Coolify and add this to the Custom MongoDB Configuration on each MongoDB instance:


replication:
replSetName: "rs0"
security:
authorization: enabled
keyFile: /tmp/root/keyfile-mongo-0

7. Go to Storages on Coolify and create a volume. /root/mongo-repl-0-mount is your key file’s path on your host server.

8. Finally, deploy all instances, connect the first MongoDB instance, and run this Mongosh command:


rs.initiate({
_id: "rs0",
members: [
{_id: 0, host: "<your_server_ip>:5432"},
{_id: 1, host: "<your_server_ip>:5433"},
{_id: 2, host: "<your_server_ip>:5434"}
]
})

9. You can connect to your replicate set with this format of connection string:

mongodb://<username>:<password>@<your_server_ip>:5432,<your_server_ip>:5433,<your_server_ip>:5434/?replicaSet=rs0

If there is any connectivity issue, make sure your firewall is allowed the port we've used here.

Also, after adding the config to each instance, try to stop and start again (basically restarting the app).

Do you have a startup idea? Let's build the MVP! — https://altailab.com

--

--

Rufat MAMMADLI
Rufat MAMMADLI

Written by Rufat MAMMADLI

Founder at Altailab.com, former owner and founder of Onradar (acquired).

Responses (3)