Deploy a Scalable MongoDB Replica Set on Oracle Cloud with Bitnami's MongoDB Terraform Template

Vikram Vaswani

Vikram Vaswani


Share this article

Bitnami offers a number of different solutions to help you deploy MongoDB in a a scalable, secure manner. In previous blog posts, I've discussed the MongoDB with Replication solution, which can be deployed on multiple virtual machines, and the MongoDB Helm chart, which can be deployed on Kubernetes.

But these aren't the only options. If you're an Oracle Cloud user, you can also deploy MongoDB on Oracle Cloud Infrastructure using Bitnami's MongoDB Terraform template. This template sets up a replication-ready MongoDB cluster that is also preconfigured in line with current best practices for security, scalability and reliability. In this blog post, I'll walk you through the details of working with the MongoDB Terraform template and the resulting cluster.

Deployment

Before starting with the deployment, make sure that you have the following important pieces of information, all of which can be obtained after logging in to the Oracle Cloud Infrastructure Web dashboard. In case you can't find them, our starter guide will show you where to look.

  • OCI tenant ID
  • OCI user ID
  • Region
  • OCI compartment ID
  • API private key
  • API public key fingerprint

Once you've acquired the necessary information, follow the steps below:

  • Prepare an SSH key pair on your local system. This key pair will be needed to log in to your MongoDB instances.

    $ ssh-keygen
    
  • Download and install Terraform (instructions).
  • Download and install the Terraform OCI provider in your Terraform plugins directory (instructions).
  • Initialize Terraform:

    $ terraform init
    
  • Obtain the Bitnami MongoDB Terraform template:

    $ git clone https://github.com/bitnami/oci-multi-tier
    $ cd oci-multi-tier/mongodb
    
  • Edit the env-vars file included with the Bitnami MongoDB Terraform template and fill in the required values using the information obtained from the Oracle Cloud Infrastructure Web dashboard. Add the path to the deployment SSH keys on your local system.

    ### Authentication details
    export TF_VAR_tenancy_ocid="<tenancy OCID>"
    export TF_VAR_user_ocid="<user OCID>"
    export TF_VAR_fingerprint="<PEM key fingerprint>"
    export TF_VAR_private_key_path="<path to the private key that matches the fingerprint above>"
    
    ### Region
    export TF_VAR_region="<region in which to operate, example: us-ashburn-1, us-phoenix-1>"
    
    ### Compartment
    export TF_VAR_compartment_ocid="<compartment OCID>"
    
    ### Public/private keys used on the instances
    export TF_VAR_ssh_public_key_path="<path to public key>"
    export TF_VAR_ssh_private_key_path="<path to private key>"
    
  • Deploy the template:

    $ terraform apply
    

    You will be prompted for some basic information, such as the number and type of nodes, database name and deployment name. Enter the required information and confirm the deployment.

Here's what you should see as the cluster spins up:

Cluster deployment

Basic Usage

Once the deployment is complete, execute the following command to see the IP addresses of the nodes and the MongoDB root password:

$ terraform output

Connect to the primary MongoDB node over SSH using the deployment key pair:

$ ssh bitnami@PRIMARY-NODE-PUBLIC-IP-ADDRESS

Once connected, run the commands below to start the MongoDB CLI and check replication status. Enter the root password when prompted.

$ mongo admin -u root -p
...
mongo:PRIMARY> rs.status()

Check the members section of the output. If you see a list of members similar to that shown below, your cluster is good to go!

Cluster members

Network Configuration and Security

The cluster operates on the standard MongoDB port 27017. For security reasons, this port is not open for external connections by default. To allow external access to the MongoDB cluster, you can use an SSH tunnel or open the port for external access using an IP address whitelist. Refer to our documentation for more information on these options.

The main MongoDB configuration file is at /opt/bitnami/mongodb/conf/mongodb.conf, and the MongoDB logs are stored in the /opt/bitnami/mongodb/logs/mongodb.log file.

Data Replication

The MongoDB Terraform template automatically configures a MongoDB replica set with automatic failover. One node in the MongoDB cluster is designated as the primary node, while other nodes are designated as secondary nodes or arbiter nodes.

The primary node receives all write operations, while the secondary nodes asynchronously replicate the operations performed by the primary node on their own copies of the data set. Arbiter nodes do not store any data; their function is to provide an additional vote in replica set elections.

Cluster topology

If a primary node fails, an election automatically takes place and the first secondary node receiving a majority of votes becomes the new primary node. This approach lets you run your application without worrying about data loss or downtime.

To see this in action, connect to the primary node and use the rs.status() command to check that there are two secondary nodes currently connected, as shown previously:

Cluster members

Add some data to the primary node:

mongo:PRIMARY> db.mycollection.insert( { name: "foo", value: "bar" } )

Now, turn off the primary node. Connect to either of the secondary nodes via SSH and run the rs.status() command again. You will see that the original primary node is now marked "unhealthy" and one of the secondary nodes is now the new primary node:

Cluster status

You will also see that the data added on the original primary node is present on the remaining nodes:

Replicated data

When deploying the Terraform template, you can specify the number of nodes you need in the replica set. If you select an even number of nodes, it's a good idea to add an arbiter node.

Metrics

You can use the mongostat command to keep track of server and cluster metrics, such as the number of queries, connections, memory usage and more. Here's an example:

Cluster metrics

You can also use the mongotop command to see read/write statistics by collection.

If you're looking for a scalable and secure MongoDB deployment on Oracle Cloud, Bitnami's Terraform template is the quickest and easiest way to get it done. Try it today and then tweet @bitnami and tell us what you liked (or didn't like) about it!