Explore Developer Center's New Chatbot! MongoDB AI Chatbot can be accessed at the top of your navigation to answer all your MongoDB questions.

Join us at AWS re:Invent 2024! Learn how to use MongoDB for AI use cases.
MongoDB Developer
Connectors
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
Connectorschevron-right

Deploying the MongoDB Enterprise Kubernetes Operator on Google Cloud

Arek Borucki6 min read • Published Jan 13, 2023 • Updated Jan 13, 2023
KubernetesConnectors
Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
This article is part of a three-parts series on deploying MongoDB across multiple Kubernetes clusters using the operators.
Deploying and managing MongoDB on Kubernetes can be a daunting task. It requires creating and configuring various Kubernetes resources, such as persistent volumes, services, and deployments, which can be time-consuming and require a deep understanding of both Kubernetes and MongoDB products. Furthermore, tasks such as scaling, backups, and upgrades must be handled manually, which can be complex and error-prone. This can impact the reliability and availability of your MongoDB deployment and may require frequent manual intervention to keep it running smoothly. Additionally, it can be hard to ensure that your MongoDB deployment is running in the desired state and is able to recover automatically from failures.
Fortunately, MongoDB offers operators, which are software extensions to the Kubernetes API that use custom resources to manage applications and their components. The MongoDB Operator translates human knowledge of creating a MongoDB instance into a scalable, repeatable, and standardized method, and leverages Kubernetes features to operate MongoDB for you. This makes it easier to deploy and manage MongoDB on Kubernetes, providing advanced features and functionality for running MongoDB in cloud-native environments.
There are three main Kubernetes operators available for deploying and managing MongoDB smoothly and efficiently in Kubernetes environments:
  • The MongoDB Community Kubernetes Operator is an open-source operator that is available for free and can be used to deploy and manage MongoDB Replica Set on any Kubernetes cluster. It provides basic functionality for deploying and managing MongoDB but does not include some of the more advanced features available in the Enterprise and Atlas operators.
  • The MongoDB Enterprise Kubernetes Operator is a commercial Kubernetes operator included with the MongoDB Enterprise subscription. It allows you to easily deploy and manage any type of MongoDB deployment (standalone, replica set, sharded cluster) on Kubernetes, providing advanced features and functionality for deploying and managing MongoDB in cloud-native environments.
  • The MongoDB Atlas Kubernetes Operator is an operator that is available as part of the Atlas service. It allows you to quickly deploy and manage MongoDB on the Atlas cloud platform, providing features such as automatic provisioning and scaling of MongoDB clusters, integration with Atlas features and services, and automatic backups and restores. You can learn more about this operator in our blog post on application deployment in Kubernetes.
This article will focus on the Enterprise Operator. The MongoDB Enterprise Kubernetes Operator seamlessly integrates with other MongoDB Enterprise features and services, such as MongoDB Ops Manager (which can also run on Kubernetes) and MongoDB Cloud Manager. This allows you to easily monitor, back up, upgrade, and manage your MongoDB deployments from a single, centralized location, and provides access to a range of tools and services for managing, securing, and optimizing your deployment.

MongoDB Enterprise Kubernetes Operator

The MongoDB Enterprise Kubernetes Operator automates the process of creating and managing MongoDB instances in a scalable, repeatable, and standardized manner. It uses the Kubernetes API and tools to handle the lifecycle events of a MongoDB cluster, including provisioning storage and computing resources, configuring network connections, setting up users, and making changes to these settings as needed. This helps to ease the burden of manually configuring and managing stateful applications, such as databases, within the Kubernetes environment.
The Kubernetes Operator manages the typical lifecycle events for a MongoDB cluster: provisioning storage and computing power, configuring network connections, setting up users, and changing these settings as needed

Kubernetes Custom Resource Definitions

Kubernetes CRDs (Custom Resource Definitions) is a feature in Kubernetes that allows users to create and manage custom resources in their Kubernetes clusters. Custom resources are extensions of the Kubernetes API that allow users to define their own object types and associated behaviors. With CRDs, you can create custom resources that behave like built-in Kubernetes resources, such as StatefulSets, Deployments, Pods, and Services, and manage them using the same tools and interfaces. This allows you to extend the functionality of Kubernetes and tailor it to their specific needs and requirements.
The MongoDB Enterprise Operator currently provides the following custom resources for deploying MongoDB on Kubernetes:
The following diagram describes how the Kubernetes Operator behaves if you make changes to a sharded cluster's

Installing and configuring Enterprise Kubernetes Operator

For this tutorial, we will need the following tools: 

GKE Kubernetes cluster creation 

To start, let's create a Kubernetes cluster in a new project. We will be using GKE Kubernetes. I use this script to create the cluster. The cluster will have four worker nodes and act as Ops Manager and MongoDB Enterprise Operators Kubernetes Cluster.
1CLUSTER_NAME=master-operator
2ZONE=us-south1-a
3K8S_VERSION=1.23
4MACHINE=n2-standard-2
5gcloud container clusters create "${CLUSTER_NAME}" \
6 --zone "${ZONE}" \
7 --machine-type "${MACHINE}" --cluster-version="${K8S_VERSION}" \
8 --disk-type=pd-standard --num-nodes 4
Now that the cluster has been created, we need to obtain the credentials.
1gcloud container clusters get-credentials "${CLUSTER_NAME}" \
2 --zone "${ZONE}"
Display the newly created cluster.
1gcloud container clusters list
2
3NAME                LOCATION       MASTER_VERSION    NUM_NODES  STATUS
4master-operator     us-south1-a    1.23.14-gke.1800      4      RUNNING
1kubectx
You should see your cluster listed here. Make sure your context is set to master cluster.
1kubectx $(kubectx | grep "master-operator" | awk '{print $1}')
We are able to start MongoDB Kubernetes Operator installation on our newly created Kubernetes cluster! 

Enterprise Kubernetes Operator 

We can install the MongoDB Enterprise Operator with a single line Helm command. The first step is to  add the MongoDB Helm Charts for Kubernetes repository to Helm.
1helm repo add mongodb https://mongodb.github.io/helm-charts
I want to create the operator in a separate, dedicated Kubernetes namespace (the operator uses default namespace by default). This will allow me to isolate the operator and any resources it creates from other resources in my cluster. The following command will install the CRDs and the Enterprise Operator in the mongodb-operatornamespace. The operator will be watching only the mongodb-operator namespace. You can read more about setting up the operator to watch more namespaces in the official MongoDB documentation.
Start by creating the mongodb-operatornamespace.
1NAMESPACE=mongodb-operator
2kubectl create ns "${NAMESPACE}"
Install the MongoDB Kubernetes Operator and set it to watch only the mongodb-operator namespace.
1HELM_CHART_VERSION=1.16.3
2helm install enterprise-operator mongodb/enterprise-operator \
3 --namespace "${NAMESPACE}" \
4 --version="${HELM_CHART_VERSION}" \
5 --set operator.watchNamespace="${NAMESPACE}"
The namespace has been created and the operator is running! You can see this by listing the pods in the newly created namespace.
1kubectl get ns
2
3NAME               STATUS   AGE
4default            Active   4m9s
5kube-node-lease    Active   4m11s
6kube-public        Active   4m12s
7kube-system        Active   4m12s
8mongodb-operator   Active   75s
1kubectl get po -n "${NAMESPACE}"
2
3NAME                                    READY   STATUS   RESTARTS   AGE
4mongodb-enterprise-operator-649bbdddf5   1/1    Running   0         7m9s
You can see that the helm chart is running with this command.
1helm list --namespace "${NAMESPACE}"
2
3NAME                NAMESPACE     REVISION       VERSION
4enterprise-operator mongodb-operator 1 deployed enterprise-operator-1.17.2

Verify the installation

You can verify that the installation was successful and is currently running with the following command.
1helm get manifest enterprise-operator --namespace "${NAMESPACE}"
Let's display Custom Resource Definitions installed in the step above in the watched namespace.
1kubectl -n "${NAMESPACE}" get crd | grep -E '^(mongo|ops)'
2
3mongodb.mongodb.com                              2022-12-30T16:17:07Z
4mongodbmulti.mongodb.com                         2022-12-30T16:17:08Z
5mongodbusers.mongodb.com                         2022-12-30T16:17:09Z
6opsmanagers.mongodb.com                          2022-12-30T16:17:09Z
All required service accounts has been created in watched namespace.
1kubectl -n "${NAMESPACE}" get sa | grep -E '^(mongo)'
2
3mongodb-enterprise-appdb           1         36s
4mongodb-enterprise-database-pods   1         36s
5mongodb-enterprise-operator        1         36s
6mongodb-enterprise-ops-manager     1         36s
Validate if the Kubernetes Operator was installed correctly by running the following command and verify the output.
1kubectl describe deployments mongodb-enterprise-operator -n \
2 "${NAMESPACE}"
Finally, double-check watched namespaces.
1kubectl describe deploy mongodb-enterprise-operator -n "${NAMESPACE}" | grep WATCH
2
3   WATCH_NAMESPACE:               mongodb-operator
The MongoDB Enterprise Operator is now running in your GKE cluster.

MongoDB Atlas Kubernetes Operator

It's worth mentioning another operator here --- a new service that integrates Atlas resources with your Kubernetes cluster. Atlas can be deployed in multi-cloud environments including Google Cloud. The Atlas Kubernetes Operator allows you to deploy and manage cloud-native applications that require data services in a single control plane with secure enterprise platform integration.
This operator is responsible for managing resources in Atlas using Kubernetes custom resources, ensuring that the configurations of projects, database deployments, and database users in Atlas are consistent with each other. The Atlas Kubernetes Operator uses the AtlasProject, AtlasDeployment, and AtlasDatabaseUser Custom Resources that you create in your Kubernetes cluster to manage resources in Atlas.
These custom resources allow you to define and configure the desired state of your projects, database deployments, and database users in Atlas. To learn more, head over to our blog post on application deployment in Kubernetes with the MongoDB Atlas Operator.

Conclusion

Upon the successful installation of the Kubernetes Operator, we are able to use the capabilities of the MongoDB Enterprise Kubernetes Operator to run MongoDB objects on our Kubernetes cluster. The Operator enables easy deploy of the following applications into Kubernetes clusters:
  • MongoDB --- replica sets, sharded clusters, and standalones --- with authentication, TLS, and many more options.
  • Ops Manager --- enterprise management, monitoring, and backup platform for MongoDB. The Operator can install and manage Ops Manager in Kubernetes for you. Ops Manager can manage MongoDB instances both inside and outside Kubernetes. Installing Ops Manager is covered in the second article of the series.
  • MongoMulti --- Multi-Kubernetes-cluster deployments allow you to add MongoDB instances in global clusters that span multiple geographic regions for increased availability and global distribution of data. This is covered in the final part of this series.
Want to see the MongoDB Enterprise Kubernetes Operator in action and discover all the benefits it can bring to your Kubernetes deployment? Continue reading the next blog of this series and we'll show you how to best utilize the Operator for your needs

Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
This is part of a series
Deploying MongoDB Across Multiple Kubernetes Clusters
Up Next
Continue

More in this series
Related
Article

Learn How to Leverage MongoDB Data Within Kafka with New Tutorials!


Sep 17, 2024 | 1 min read
Tutorial

Go to MongoDB Using Kafka Connectors - Ultimate Agent Guide


Sep 17, 2024 | 7 min read
Tutorial

Mastering MongoDB Ops Manager on Kubernetes


Jan 13, 2023 | 7 min read
Tutorial

Spark Up Your MongoDB and BigQuery Using BigQuery Spark Stored Procedures


Aug 12, 2024 | 5 min read
Table of Contents