All You Need to Know About MongoDB on Google Cloud

Raju Dawadi
Apr 30, 2018 · 4 min read

Google Cloud offers a marketplace with many stacks & service solutions with Click to Deploy. The solutions includes database solutions(mongodb, cassandra, mysql) to Blog & CMS(wordpress, joomla) along with Developer tools(gitlab, jenkins). They are not a fully managed solution and google doesn’t offer support for any but the stacks decreases time to set up solutions and one can enjoy the services by paying only for the GCP resources but not for the solutions*.

Among the deployments offered on Google Cloud Launcher, mongodb is one which can be deployed at a click with creation of primary, secondary instances (compute engines) in a replica set along with arbiter node.

How to Create MongoDB Replica Set

Head over towards MongoDB Launcher on Google Cloud Console for deployment of the NoSQL database in cluster of instances. Click on “LAUNCH ON COMPUTE ENGINE”.

Image for post
Image for post

Configure deployment, replica set name and disk type on the console. Choose the number of nodes among which one will be primary replica and rest secondaries. Also, choose a arbiter node which can be a small instance as it is utilized just for choosing the primary replica by voting. Its recommended to choose SSD for the data storage which has high IOPS & throughput. Grab details about the performance difference from official optimization guide.

Now, within few minutes, the clusters are up not only with the number of replicas specified and arbiter but also with a separate disks for each node which has naming standard of mongodb-servers-vm-0-data that are attached to respective instance. The data on those disk are synced with each other. Also, by selecting External IP option None, we can hide the nodes from outside the specific subnet.

Connecting with MongoDB ReplicaSet

There are different ways of connecting to the mongodb replica set:

Connecting to each node: On the Compute Instances page on console, hit the SSH button and new window opens with ssh connection. Also, SSHing into google cloud instances is easy with gcloud sdk: gcloud compute ssh [Instance Name] --zone [zone] --project [project]

On arbiter node:

On primary node:

On secondary:

Connect to ReplicaSet

As we have primary and secondary replicas running, to establish mongodb connection with the replica set rather than each one, enter this command on any of the node:

This way, we get connected to the primary replica/node at first and in case of fail over, the arbiter node chooses another node as primary and the connection is established to new primary.

Fail Over Demo

First, we connect to the replica set from arbiter node

Here, the instance mongodb-servers-vm-0 is primary and mongodb-servers-vm-1 is secondary. Now, lets restart mongod service on the primary node

On the arbiter connection:

Now, we are connected to the instance mongodb-servers-vm-1 which has become the primary replica now. Check the replica set status:

We get the status:

MongoDB ReplicaSet Connection from Compute Engine Instances

From any of the Google Cloud compute engine instances, the mongodb connection can established by passing the replicaset host name: mongodb://mongodb-servers-vm-0,mongodb-servers-vm-1/myDB?replicaSet=rs0

But we cannot get connected to the set by providing the ip address of any of the nodes. The name of the replicas can be found on /etc/hosts of respective instance.

MongoDB ReplicaSet Connection from Localhost

Sometime, we may need to connect to the replicaSet launched on Google Cloud from local computer, for that we need to create SSH tunnel:

By that way, we can connect to replicaSet even from MongoDB Compass for exploring and manipulating data.

Monitoring MongoDB Cluster

As they say Make life easier with Ops Manager, its one of the best solution provided by MongoDB which offers performance visibility to query optimization to backup and alerts out of the box. Ops Manager can be downloaded for various platforms but I faced issues with ubuntu and later succeeded for CentOS without any hurdle.

Google Cloud - Community

Google Cloud community articles and blogs

Raju Dawadi

Written by

DevOps | SRE | #GDE

Google Cloud - Community

A collection of technical articles and blogs published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Raju Dawadi

Written by

DevOps | SRE | #GDE

Google Cloud - Community

A collection of technical articles and blogs published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store