MongoDB Replica Set on Kubernetes + Mongo-Express UI

Marian Stychuk
The Startup
Published in
3 min readAug 3, 2020

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. In this tutorial, I will be using v0.0.7. 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
git checkout v0.0.7
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:

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

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

This will apply the mongo-express deployment and expose it using a LoadBalancer.

mongo-express.yaml

As you can see from the code above, we set up some configuration variables for the mongo-express. The most important one is the ME_CONFIG_MONGODB_SERVER — here we provide a comma-delimited list of the hostnames of the pods, where the replica set instances are located. For more details on the configuration of the mongo-express, visit the official documentation.

For any non-test projects, you should update the username and password.

Note, that this will not run on clusters that do not provide LoadBalancer support (minikube, kubeadm). In order to run this deployment on such clusters, change the type of the mongo-express Service from ‘LoadBalancer’ to ‘NodePort’.

Now let’s wait until the mongo-express service receives the external IP:

Once the mongo-express service got its IP, you can go ahead and browse its 8081 port. You will see the login window where you should provide credentials from the mongo-express.yaml:

After the login you’ll see the mongo-express UI:

--

--