This post is the basic guide on installing the MongoDB replica set and providing a UI access to it on a Kubernetes cluster.


I assume that you are already familiar with Kubernetes. I will use Google Kubernetes Engine as the Kubernetes environment, but you should be able to use any other environment. Before starting, make sure you are connected to the Kubernetes cluster.

Install MongoDB Kubernetes Operator

To install MongoDB replica set we will need to install an operator first. We will use the MongoDB Community Kubernetes Operator. For the latest and full version of installation steps, visit the official repository. Short summary:

## Create a namespace
kubectl create namespace mongodb
## Download and install the operator
git clone https://github.com/mongodb/mongodb-kubernetes-operator.git
cd mongodb-kubernetes-operator
kubectl create -f deploy/crds/mongodb.com_mongodb_crd.yaml
kubectl create -f deploy/ --namespace mongodb

To make sure that the operator is installed, check if all resources are in the Ready state:

kubectl get all --namespace mongodb

The output should look like this:

Image for post

Deploy a test replica set

Now you have the operator installed. Let’s deploy a test replica set:

kubectl apply -f deploy/crds/mongodb.com_v1_mongodb_cr.yaml --namespace mongodb

The deployment of the replica set will take some time (up to 5 minutes). Let’s wait until its phase is ‘Running’. To check the current state, run this command:

kubectl get mdb --namespace mongodb

Image for post

Now your replica set is ready to operate. You can connect to it from inside the cluster using the following connection string:

mongodb://example-mongodb-0.example-mongodb-svc.mongodb.svc.cluster.local:27017,example-mongodb-1.example-mongodb-svc.mongodb.svc.cluster.local:27017,example-mongodb-2.example-mongodb-svc.mongodb.svc.cluster.local:27017/?replicaSet=example-mongodb

Deploy mongo-express

Let’s continue and deploy a mongo-express to get the UI access to the replica set:

kubectl apply -f https://gist.githubusercontent.com/StMarian/6b8aa3366ee481d9c2bc79d8ddf0bacc/raw/0a949ad9cad50d3ec8b2df40283efb9633e3e1da/mongo-express.yaml --namespace mongodb

#mongo-express #kubernetes #mongodb-operator #mongodb

MongoDB Replica Set on Kubernetes + Mongo-Express UI
21.15 GEEK