paint-brush
Getting started with MongoDB Enterprise Operator for Kubernetesby@shyam.arjarapu
6,914 reads
6,914 reads

Getting started with MongoDB Enterprise Operator for Kubernetes

by Shyam ArjarapuJuly 11th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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. This article 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 every day’ Starting with the MongoDB. Ops Manager, MongoDB officially supports the management and deployment of MongoDB in Kuber.netes.

Company Mentioned

Mention Thumbnail
featured image - Getting started with MongoDB Enterprise Operator for Kubernetes
Shyam Arjarapu HackerNoon profile picture

Introduction

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

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.

  • Kubernetes (GKE or minikube)
  • kubectl
  • kubernetes-helm

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

Install the helm chart for MongoDB enterprise operator

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.

Ops 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 address

Update 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

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

Create 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.

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

Under 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

The auto generated YAML file for creating namespace, configmap, secret and MongoDbReplicaSet

Connecting 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.

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

Remove 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.

A kubectl command to remove the MongoDbReplicaSet from Kubernetes and Ops Manager

Check 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.

A kubectl command to check the logs for 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.

A kubectl command to recreate the MongoDB Enterprise Operator pod

Key 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

Summary

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”.

Resources

Previously published articles