How to Make use of a MongoDB Database with the StatefulSets on Google Kubernetes Engine

You wish to run the MongoDB on the Google Cloud Kubernetes Engine that demands access to the persistent disk.

Given that MongoDB demands a persistent disk, you should deploy it as a stateful application. This demands that you make use of the StateFulSet controller to roll out the MongoDB for a persistent identity whenever the pods have to be rescheduled or started out afresh.

These steps will certainly help out:

Step 1 : Sign in to the console of Google Cloud

Step 2 : Set up the Google Cloud shell

Step 3 : Run the command below to initiate a hello-world cluster:

Step 4 : The command takes quite some time to run

Step 5 : Run these commands to generate your StorageClass which instructs the Kubernetes the kind of disk you want for your MongoDB database:

Step 6 : Run these commands to apply the StorageClass to the Kubernetes:

Step 7 : In the terms that govern the use of Kubernetes, ‘service’ denotes the set of rules or policies that oversee the access of specific pods. To summarize, a headless service is one that does not vouch for the servicing of the load.

Step 8 : When used alongside the StatefulSet, this grants the individual DNSs that you use to access the pods, and in return, a means by which you connect all your MongoDB nodes one at a time.

Step 9 : Run the command below to generate the mongo-statefulset.yaml file. You may also gain access to this code at:

Step 10 : To set up the Headless Service and the StatefulSet, run this command:

Step 11 : Before interconnecting the MongoDB replica set, you have to validate that it is indeed running by invoking the following command:

Step 12 : You shall receive this input:

Step 13 : To generate the list of the pods in your cluster, run the command below:

Step 14 : Interconnect it to the first set of the replica:

Step 15 : To operationalize the replica set, run this command below:

Step 16 : Just if you may have to upscale the replica set, run the command below to up the replica set from 3 to 5

Step 17 : If on the other hand, you need to downscale the replica set, run this command as it decreases the set from 5 to 3.

Step 18 : You should now go-ahead to connect your MongoDB replica set by adopting the URI formatting below:

Step 19 : Each pod that constitutes the StatefulSet is backed by a Headless Service that possesses a stable DNS name. Its template follows this format : <pod-name >.< service-name>

In this instance, you deployed MongoDB as a form of stateful application. The use of the Headless service empowers you to delineate policies or instructions for accessing specific pods. Nonetheless, it does not recommend load balancing.