Deploying the MongoDB Enterprise Kubernetes Operator on Google Cloud
Rate this tutorial
This article is part of a three-parts series on deploying MongoDB across multiple Kubernetes clusters using the operators.
- Deploying the MongoDB Enterprise Kubernetes Operator on Google Cloud
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.
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.
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:
- Standalone
- ReplicaSet
- ShardedCluster
For this tutorial, we will need the following tools:
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.
1 CLUSTER_NAME=master-operator 2 ZONE=us-south1-a 3 K8S_VERSION=1.23 4 MACHINE=n2-standard-2 5 gcloud 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.
1 gcloud container clusters get-credentials "${CLUSTER_NAME}" \ 2 --zone "${ZONE}"
Display the newly created cluster.
1 gcloud container clusters list 2 3 NAME LOCATION MASTER_VERSION NUM_NODES STATUS 4 master-operator us-south1-a 1.23.14-gke.1800 4 RUNNING
1 kubectx
You should see your cluster listed here. Make sure your context is set to master cluster.
1 kubectx $(kubectx | grep "master-operator" | awk '{print $1}')
We are able to start MongoDB Kubernetes Operator installation on our newly created Kubernetes cluster!
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.
1 helm 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-operator
namespace. 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-operator
namespace.1 NAMESPACE=mongodb-operator 2 kubectl create ns "${NAMESPACE}"
Install the MongoDB Kubernetes Operator and set it to watch only the
mongodb-operator
namespace.1 HELM_CHART_VERSION=1.16.3 2 helm 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.
1 kubectl get ns 2 3 NAME STATUS AGE 4 default Active 4m9s 5 kube-node-lease Active 4m11s 6 kube-public Active 4m12s 7 kube-system Active 4m12s 8 mongodb-operator Active 75s
1 kubectl get po -n "${NAMESPACE}" 2 3 NAME READY STATUS RESTARTS AGE 4 mongodb-enterprise-operator-649bbdddf5 1/1 Running 0 7m9s
You can see that the helm chart is running with this command.
1 helm list --namespace "${NAMESPACE}" 2 3 NAME NAMESPACE REVISION VERSION 4 enterprise-operator mongodb-operator 1 deployed enterprise-operator-1.17.2
You can verify that the installation was successful and is currently running with the following command.
1 helm get manifest enterprise-operator --namespace "${NAMESPACE}"
Let's display Custom Resource Definitions installed in the step above in the watched namespace.
1 kubectl -n "${NAMESPACE}" get crd | grep -E '^(mongo|ops)' 2 3 mongodb.mongodb.com 2022-12-30T16:17:07Z 4 mongodbmulti.mongodb.com 2022-12-30T16:17:08Z 5 mongodbusers.mongodb.com 2022-12-30T16:17:09Z 6 opsmanagers.mongodb.com 2022-12-30T16:17:09Z
All required service accounts has been created in watched namespace.
1 kubectl -n "${NAMESPACE}" get sa | grep -E '^(mongo)' 2 3 mongodb-enterprise-appdb 1 36s 4 mongodb-enterprise-database-pods 1 36s 5 mongodb-enterprise-operator 1 36s 6 mongodb-enterprise-ops-manager 1 36s
Validate if the Kubernetes Operator was installed correctly by running the following command and verify the output.
1 kubectl describe deployments mongodb-enterprise-operator -n \ 2 "${NAMESPACE}"
Finally, double-check watched namespaces.
1 kubectl 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.
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.
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