MongoDB Setup on Kubernetes using MongoDB Operator

MongoDB is a popular NoSQL database that supports large as well as small size of datasets. Just like any other database standalone setup, MongoDB is straightforward but we have to make a replicated or shared cluster of MongoDB, and there we have certain complications. Especially if we are doing these kinds of setups in orchestration tools like Kubernetes.

There is a lot of complexity in setting up MongoDB on Kubernetes that people(including me) have faced for a long time which I would like to highlight:-

  • Standalone setup is pretty straightforward but for replicated and sharded clusters additional mongo configurations are required.
  • In the replicated scenario, separate configurations need to be managed for the leader and follower.
  • Monitoring and access management of MongoDB inside Kubernetes is a little tricky part to handle.

Now the tough question arises who will manage this MongoDB setup on Kubernetes considering its monitoring, security, and best practices of it. But you guys don’t have to worry, we do as well have an answer and the answer is MongoDB Operator by Opstree Solutions.

Operator Logo

MongoDB Operator

The MongoDB operator is a custom CRD-based operator inside Kubernetes to create, manage, and auto-heal MongoDB setup. It helps in providing different types of MongoDB setup on Kubernetes like- standalone, replicated, and sharded. There are quite amazing features we have introduced inside the operator and some are in-pipeline on which deployment is going on. Some of the MongoDB operator features are:-

  • Standalone and replicated cluster setup
  • Failover and recovery of MongoDB nodes
  • Inbuilt monitoring support for Prometheus using MongoDB Exporter.
  • Different Kubernetes-related best practices like:- Affinity, Pod Disruption Budget, Resource management, etc, are also part of it.
  • Insightful and detailed monitoring dashboards for Grafana.
  • Custom MongoDB configuration support.

Other than this, there are a lot of features are in the backlog on which active development is happening. For example:-

  • Backup and Restore support
  • TLS encryption and decryption
  • DB management support
  • User management support
  • Roles management support

MongoDB Operator architecture looks like this:-


For more information about MongoDB Operator, please check out the documentation page of it.

Installation and Configuration of MongoDB Operator

MongoDB Operator requires a Kubernetes cluster of version >=1.16.0. If you have just started with the CRD and Operators, it’s highly recommended to use the latest version of Kubernetes.

Setup of MongoDB operator can be easily done by using simple helm and kubectl commands.

First, add the helm repository to your system.

# Add the helm chart
$ helm repo add ot-helm \
"ot-helm" has been added to your repositories

Once the helm repository is configured, we can simply deploy the operator using the helm install command.

# Deploy the MongoDB Operator
$ helm install mongodb-operator \
ot-helm/mongodb-operator --namespace ot-operators
Release "mongodb-operator" does not exist. Installing it now.
NAME: mongodb-operator
LAST DEPLOYED: Sun Jan  9 23:05:13 2022
NAMESPACE: ot-operators
STATUS: deployed

After the operator deployment is successful, we can verify it using the kubectl command line.

# List the pod and status of mongodb-operator
$ kubectl get pods -n ot-operators -l name=mongodb-operator
NAME                               READY   STATUS    RESTARTS   AGE
mongodb-operator-fc88b45b5-8rmtj   1/1     Running   0          21d

MongoDB Cluster setup on Kubernetes

MongoDB cluster can be easily set up on Kubernetes cluster by providing a simple manifest file like this:-

kind: MongoDBCluster
  name: mongodb
  clusterSize: 3
    imagePullPolicy: IfNotPresent
      fsGroup: 1001
    accessModes: ["ReadWriteOnce"]
    storageSize: 1Gi
    storageClass: csi-cephfs-sc
    mongoDBAdminUser: admin
      name: mongodb-secret
      key: password
    enableExporter: true
    image: bitnami/mongodb-exporter:0.11.2-debian-10-r382
    imagePullPolicy: IfNotPresent

Once the manifest is ready we can simply apply it using kubectl.

$ kubectl apply -f clusterd.yaml

$ kubectl get pods --namespace ot-operators -l app=mongodb-cluster
NAME                           READY   STATUS    RESTARTS   AGE
mongodb-ex-cluster-cluster-0   2/2     Running   0          5m57s
mongodb-ex-cluster-cluster-1   2/2     Running   0          5m28s
mongodb-ex-cluster-cluster-2   2/2     Running   0          4m48s

After the setup is done, we can easily verify the MongoDB setup using mongo-cli.

# Verifying the health of the cluster
$ kubectl exec -it mongodb-ex-cluster-cluster-0 -n ot-operators -- bash

$ mongo -u $MONGO_ROOT_USERNAME -p $MONGO_ROOT_PASSWORD --eval "db.adminCommand( { replSetGetStatus: 1 } )"
	"set" : "mongodb-ex-cluster",
	"date" : ISODate("2022-02-03T08:52:09.257Z"),
	"myState" : 1,
	"term" : NumberLong(1),

This example is more of a POC kind of setup, there are multiple examples available inside the examples directory. Also, if you want to set up a cluster using the helm chart, we can simply do it using the helm install commands as well.

# Installation of MongoDB replication cluster
$ helm install mongodb-ex-cluster --namespace ot-operators ot-helm/mongodb-cluster
NAME:          mongodb-ex-cluster
LAST DEPLOYED: Tue Feb  1 23:18:36 2022
NAMESPACE:     ot-operators
STATUS:        deployed
REVISION:      1
  CHART NAME:    mongodb-cluster
  APP VERSION:   0.1.0

The helm chart for MongoDB standalone setup has been deployed.

Get the list of pods by executing:
    kubectl get pods --namespace ot-operators -l app=mongodb-ex-cluster-cluster

For getting the credential for admin user:
    kubectl get secrets -n ot-operators mongodb-ex-secret -o jsonpath="{.data.password}" | base64 -d


This operator is still under development, and we are working towards adding some valuable features inside it. Since it’s an open-source project, so if you guys want to make a contribution please feel free to do it. The contribution to the project can be done by opening feature requests, bugs, and Pull Requests.

For more information, please refer to the repository link.

Thanks for reading, I’d really appreciate any and all feedback, please leave your comment below if you guys have any feedback.

Cheers till next time!!

Blog Pundit: Sandeep Rawat

Opstree is an End to End DevOps solution provider

Connect Us

One thought on “MongoDB Setup on Kubernetes using MongoDB Operator”

  1. Hi Abhishek,
    Thanks for a wonderful presentation. Could you please share the details on sharded mongodb cluster support? The current mongodb operator does not support sharded cluster as far I know. Could you please your thoughts on how this will be handled?



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: