top of page
Walid El Sayed Aly

Deploy MongoDB Replica Sets in Kubernetes with HELM



In this tutorial, I will show you how to deploy two or more Replica Set MongoDB instances running on Kubernetes.


MongoDB is one of the most popular document stores available both as a fully managed cloud service and for the deployment on self-managed infrastructures.

Kubernetes is the industry-leading container orchestration platform.

Helm is in my opinion currently the best way to manage Kubernetes applications.

A Replica Set is a cluster of MongoDB servers that implements replication and automated failover, the MongoDB recommended replication strategy. For more information, look this up: Replication — MongoDB Manual

Requirements

  • Kubernetes // you can also use [minikube]

  • HELM v3

  • fun 😁

I use here the HELM Charts from https://github.com/helm/charts/tree/master/stable/mongodb-replicaset. You can either get them from Github or insert the repo directly into the HELM repo:

*○*→ helm repo add stable https://kubernetes-charts.storage.googleapis.com/
*○*→ helm install —name my-mongo-release stable/mongodb-replicaset

I am going to download it first. That way, you have the possibility to modify the charts on your recipe, add things, change the name of Replica Set, change the number of the Replica Set, modify some Ingress, add some keys, and so on.

  • you can clone first this Project:

*○*→ git clone git@github.com:helm/charts.git
  • After that you can modify the charts/stable/MongoDB Replica Set. If you like, you can change the values.yaml :

-> the number of your replica -> replicas:[1-9]
-> your port number: port
-> replicaSetName: your db name
-> nameOverride: your db name
-> fullnameOverride: your db name /

Also, you can change the other values in values.yaml if you want.

  • So, let us install via HELM and check before your minikube is running:

*○*→ minikube status
m01
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured

Or if you use a real Kubernetes cluster, check your cluster information:

*○*→ kubectl cluster-info
  • In the next step you can install your MongoDB Replica Set charts:

*○*→ helm install esome-mongo charts/stable/mongodb-replicaset

Please note that with HELM V3, the — name is now mandatory, so you do not need to add — name.

You will get some information about your installation, such as:

NAME: esome-mongo
LAST DEPLOYED: Tue May 12 14:06:02 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:1. After the statefulset has been created, you can check which instance is primary by running:

   $ for ((i = 0; i < 3; ++i)); do kubectl exec —namespace default esome-mongo-$i — sh -c ‘mongo —eval=“printjson(rs.isMaster())”’; done

2. You can insert a key into the primary instance of the MongoDB Replica Set by running the following:
   MASTER_POD_NAME must be replaced with the name of the master found in the previous step.

   $ kubectl exec —namespace default MASTER_POD_NAME — mongo —eval=“printjson(db.test.insert({key1: ‘value1’}))”

3. You can get the keys stored in the primary or any of the slave nodes in the following manner.
   POD_NAME must be replaced with the name of the pod being queried.

   $ kubectl exec —namespace default POD_NAME — mongo —eval=“rs.slaveOk(); db.test.find().forEach(printjson)”

Now your MongoDB Replica Set is already on your cluster. It is very easy 😃 don’t you think? I still remember how much time it cost our Operations Teams and Database Administration to install stuff like that about 15 years ago. 🙈

Now check your Pods via kubectl

*○*→ kubectl get pods
NAME            READY   STATUS    RESTARTS   AGE
esome-mongo-0   1/1     Running   0          5m40s
esome-mongo-1   1/1     Running   0          4m42s
esome-mongo-2   1/1     Running   0          4m19s

You will see that you have three running instances of the MongoDB databases.

But how can you apply specific database changes in the HELM charts or how can you apply the database indexes in your charts, your collections or stuff you need at initialization time? You will find that out in the next article. 😉


12 views0 comments

Comments


bottom of page