The open source community has built many implementations for deploying MongoDB in Kubernetes. However, these implementations lack the crucial enterprise features such as — Backups, Automation, Alerting and, Monitoring. Starting with the MongoDB Ops Manager v4.0, MongoDB officially supports the management and deployment of MongoDB in Kubernetes with Backup, Automation, Alerting and, Monitoring. Thanks to ‘MongoDB Enterprise Operator (beta) for Kubernetes’ for integrating Kubernetes and MongoDB Ops Manager.
This is one of the many articles in multi-part series, Mastering MongoDB — One tip a day, solely created for you to master MongoDB by learning ‘one tip a day’. In a few series of articles, I would like to give various tips to help you master MongoDB in Kubernetes. This article discusses MongoDB Enterprise Operator for Kubernetes, a new feature in MongoDB Ops Manager v4.0 — its applications, use case scenarios and finally some hands-on lab exercises.
Mastering — MongoDB in Kubernetes
What in MongoDB Enterprise Operator
The MongoDB Enterprise Operator for Kubernetes is a MongoDB application-specific controller that extends the Kubernetes API to create, configure, and manage instances of MongoDB deployments on behalf of a Kubernetes user. The operator lets you deploy and manage MongoDB by accessing the Kubernetes API and MongoDB Ops Manager API. It automates common tasks by triggering a series of actions to achieve the desired state. For example, when you create MongoDB Kubernetes resource MongoDbReplicaSet, then the operator
- Automates the provisioning of StatefulSets for the MongoDB replica set
- Creates a deployment in MongoDB Ops Manager project
- Configures Ops Manager for Backup, Automation, Alerting and Monitoring (BAAM!)
- And many more features to come in the future.
MongoDB enterprise operator architecture diagram
The MongoDB Enterprise Operator for Kubernetes architecture diagram
The above diagram shows various components involved in the process and illustrates how they interact with each other. The MongoDB enterprise operator runs in a Kubernetes cluster and intercepts the requests to create MongoDB resources. When a request comes in, it triggers a series of actions as shown below
- Reads the ConfigMap containing the Ops Manager baseUri and projectId
- Reads the Secret containing the Ops Manager publicApiKey
- Provisions the Pods required for MongoDB replica set using a MongoDB container image
- Each MongoDB container image runs an Ops Manager Automation agent
- Ops Manager Automation agent reaches out Ops Manager for it’s configuration state
- Ops Manager gives out desired state for each agent
- Ops Manager Automation agent works towards reaching the goal state.
- The agent downloads / installs mongod, Monitoring and Backup agents
Hands-On lab exercises
This lab exercise helps you understand how to make use of MongoDB Enterprise Operator for Kubernetes to deploy a MongoDB replica set using kubectl.
Setup your local environment
I have created some helper scripts to deploy the MongoDB in Kubernetes. First, let’s get started by downloading the source from my GitHub repo
You would also need a Kubernetes and below set of tools installed in your environment. If you already have them installed, you may skip the next step.
- Kubernetes (GKE or minikube)
If you are getting started with Kubernetes, then the install.sh will install all the required dependencies on a Mac OS. However, If you have any other operating system, please manually install all the dependencies for your OS.
https://medium.com/media/2a4bbc8fba814db1480fc726cf66d83a/hrefInstall the helm chart for MongoDB enterprise operator
The below script will install MongoDB Enterprise Operator for Kubernetes using helm, the Kubernetes package manager.
If helm is not available in your environment, you may install the operator using kubectl.
https://medium.com/media/5a86d1a8b1b195070a073e5bd7c307b7/hrefOps Manager configuration
The MongoDB Enterprise Operator is supported starting from MongoDB Ops Manager v4.0+. So, please ensure that you have Ops Manager v4.0 installed in your environment.
Note: The instructions for installing the Ops Manager v4.0 is out of scope for this article. You may refer to the documentation to install the Ops Manager or use the MongoDB Cloud Manager as an alternate solution.
Once the Ops Manager is up and running, please follow the below instructions and have the information handy for the next step
- Make a note of the Ops Manager base URI
- Create a Project in Ops Manager
- Make a note of the Project IDSettings > Project Settings > General > Project ID
- Create a Public API keyAccount > Public API Access > API Keys
- Find your public IP address
- Whitelist your IP AddressAccount > Public API Access > API Whitelist
- Configure Version Manager with MongoDB versions of your interest Deployments > More > Version Manager
Make note of the Ops Manager Project ID and Create Public API Key & Whitelist your IP addressUpdate the scripts for your environment
Please edit the templates/environment.sh file with appropriate values for your environment. You must update the values for the properties before you start creating a replica set. The required set of properties are as shown below
For example, If you want to use MongoDB Cloud Manager and create a MonogDB v3.6.5 replica set then you must update the properies in the file templates/environment.sh as shown below.
https://medium.com/media/e53a6336f1f28302c606f46e1124af58/hrefCreate a MongoDB replica set in Kubernetes
I have provided a sample template file for your convenience. Using the values defined in the environment file, templates/environment.sh and the template file, templates/generate-yaml-simple-replicaset.sh, you could easily generate a new custom YAML file, samples/<kubernetes_namespace>-replicaset.yaml. You would need to run the above YAML file via kubectl to create the MongoDB replica set.
Based on your download speed, the required resources/images are downloaded/created for you within a few seconds to a few minutes. When you run get all resources you would be see that there are 3 Pods, 1 Statefulset with Desired state of 3 and 2 services were created for you in the namespace you provided.
Finally, if you check the Ops Manager projects, then you would see a MongoDB deployment with the above Pods is created and the Automation and Monitoring is turned on as well.
The Ops Manage deployments showing the newly created MongoDB replica setUnder the hood details of the helper script
Let’s try to understand what happens under the hood when you run the generated YAML. The generate-yaml-simple-replicaset.sh shell script creates a YAML file based on the environment variables you defined in the templates/environment.sh file. In my case, it generated a file called mongodb-world-replicaset.yaml. It simplifies the overall process by performing below tasks
- Create a namespace in Kubernetes
- Create ConfigMap with the Ops Manager Project ID, baseUrl
- Create Secret with the base64 encoded values of Ops Manager Username and Public Api Key
- Create a MongoDbReplicaSet with 3 members, version: 3.6.5
https://medium.com/media/3c94dcbd308df022b5513cba880917d9/hrefConnecting to MongoDB deployment
The connectivity to the MongoDB deployment within the Kubernetes cluster is very simple. Here in this example, I am connecting to the interactive terminal of first replica set member and using the mongo shell to connect to localhost.
Since I am running these pods on the minikube, the Node IP address 10.128.0.7 is resolvable on my laptop. Therefore, I could connect to the MongoDB from outside of the cluster by using the Node Ports. However, you would need to use the Kubernetes ingress to allow connectivity from servers external to Kubernetes.
https://medium.com/media/8c2a2a38bcb1ba0fcf8added547ee81b/hrefRemove the MongoDB Cluster
The below command will delete the MongoDbReplicaSet. The Ops Manager deployment in the given project should be removed as well. If for any reason this project still exists, you have to go to Deployment > … > Remove from Ops Manager to remove it completely.
https://medium.com/media/0b5d0496839fe7840024eaef8c4542e0/hrefCheck Enterprise Operator logs for errors
Sometimes you may notice that MongoDbReplicaSet creation did not succeed. To figure out what went wrong, you would have to check the logs on the mongodb-enterprise-operator pod.
Some of the typical errors, you may incur are related to
- Current IP Address is not added to Whitelist
- MongoDB Version not checked/binaries not available on Ops Manager
If you find such errors in your logs, then have to fix them first and recreate the operator pod before creating the replica set again. Since the operator is a deployment, you could simply delete the pod and a new one would be created for you.
https://medium.com/media/0bcb2355f5943dd6c458cd88b05cd4f4/hrefKey Points & Other challenges
I am very positive that you are very excited if you got it working this far. Before you go ahead and deploy your first MongoDB deployment in your production Kubernetes, I want you to understand below key points.
- Pods are ephemeral. So make sure you use the persistent disks
- Make sure the Pods are provisioned across different nodes to ensure the High Availability
- Explicitly set the wiredTigerCacheSizeGB to 50% of Pod’s memory
- Use Affinity, Anti-Affinity to ensure Pods are distributed and resources are shared evenly
The support for deploying replica sets and clusters is just the beginning. The future release may add lot more features
- Scale up/down the number of members in the replica set
- Convert a replica set to sharded cluster
- Inject advanced mongod configurations, etc.
The MongoDB Enterprise Operator for Kubernetes brings in a lot of excitement for the developers in you. However, I want to stress an important point.
MongoDB offers redundancy and high availability of the database via replica sets. While the operator simplifies the deployment of MongoDB replica sets, you need to take proper measures while provisioning the Pods so that MongoDB is highly available across multiple Kubernetes Nodes and/or Data Centers. Most importantly the Pods should be allocated with the required hardware resources as per the sizing exercise.
Now that you understand all the crucial points for deploying MongoDB in Kubernetes, you might wonder, “How to size my deployments (find out how much hardware resources are needed)?”. Great question! But that’s a topic for another day.
Hopefully, you learned something new today on you scale the path to “Mastering MongoDB — One tip a day”.
- Introducing the MongoDB Enterprise Operator for Kubernetes and OpenShift — by Robert Walters
- GitHub repo: mongodb-enterprise-kubernetes
- Kubernetes Webinars by Janakiram
Previously published articles
- Tip # 004: Faster electionsMeasures to reduce the election time during the rolling maintenance
- Tip # 003: TransactionsA long awaited and most requested feature for many, has finally arrived
- Tip # 002: createRoleAhhh …! Someone just dropped a collection
- Tip # 001: currentOpKnow the operations currently executing on MongoDB server inside out
- Mastering MongoDB — one tip a day series
Getting started with MongoDB Enterprise Operator for Kubernetes was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.