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.
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
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
This lab exercise helps you understand how to make use of MongoDB Enterprise Operator for Kubernetes to deploy a MongoDB replica set using kubectl.
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
A helper scripts to deploy the MongoDB in Kubernetes
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.
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.
Execute the install script to install all the required dependencies on a Mac OS
The below script will install MongoDB Enterprise Operator for Kubernetes using helm, the Kubernetes package manager.
Install MongoDB Enterprise Operator for Kubernetes using helm
If helm is not available in your environment, you may install the operator using kubectl.
If helm is not available, you may install the operator using kubectl.
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 Project IDSettings > Project Settings > General > Project ID
Create a Public API keyAccount > Public API Access > API Keys
Whitelist your IP AddressAccount > Public API Access > API Whitelist
Make note of the Ops Manager Project ID and Create Public API Key & Whitelist your IP address
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
A script file hold all the environment variables for your MongoDB deployments in Kubernetes
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.
An example script with values set for Ops Manager Url, Kubernetes namespace and MongoDB version
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.
Shell commands to generate and apply the YAML file 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.
A kubectl command to list all the resources that the helper script has created
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 set
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
The auto generated YAML file for creating namespace, configmap, secret and MongoDbReplicaSet
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.
A bash script showing the connectivity to the MongoDB replica set member via mongo shell
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.
A bash script showing the connectivity to the MongoDB replica set member using NodePort
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.
A kubectl command to remove the MongoDbReplicaSet from Kubernetes and Ops Manager
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.
A kubectl command to check the logs for MongoDB Enterprise Operator pod
Some of the typical errors, you may incur are related to
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.
A kubectl command to recreate the MongoDB Enterprise Operator pod
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.
The support for deploying replica sets and clusters is just the beginning. The future release may add lot more features
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”.
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