# Deploying the MongoDB Enterprise Kubernetes Operator on Google Cloud

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

-   Mastering MongoDB Ops Manager

-   Deploying MongoDB Across Multiple Kubernetes Clusters With MongoDBMulti

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.

## 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:

-   MongoDBOpsManager Custom Resource

-   MongoDB Custom Resource

    -   Standalone

    -   ReplicaSet

    -   ShardedCluster

-   MongoDBUser Custom Resource

-   MongoDBMulti

Example of Ops Manager and MongoDB Custom Resources on Kubernetes

## Installing and configuring Enterprise Kubernetes Operator

For this tutorial, we will need the following tools: 

-   gcloud 

-   gke-cloud-auth-plugin

-   Helm

-   kubectl

-   kubectx

-   Git

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

```bash
CLUSTER_NAME=master-operator
ZONE=us-south1-a
K8S_VERSION=1.23
MACHINE=n2-standard-2
gcloud container clusters create "${CLUSTER_NAME}" \
  --zone "${ZONE}" \
  --machine-type "${MACHINE}" --cluster-version="${K8S_VERSION}" \
  --disk-type=pd-standard --num-nodes 4
```

Now that the cluster has been created, we need to obtain the credentials.

```bash
gcloud container clusters get-credentials "${CLUSTER_NAME}" \
  --zone "${ZONE}"
```

Display the newly created cluster.

```bash
gcloud container clusters list

NAME                LOCATION       MASTER_VERSION    NUM_NODES  STATUS
master-operator     us-south1-a    1.23.14-gke.1800      4      RUNNING
```

We can also display Kubernetes full cluster name using `kubectx`.

```bash
kubectx
```

You should see your cluster listed here. Make sure your context is set to master cluster.

```bash
kubectx $(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.

```bash
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.

```bash
NAMESPACE=mongodb-operator
kubectl create ns "${NAMESPACE}"
```

Install the MongoDB Kubernetes Operator and set it to watch only the `mongodb-operator` namespace.

```bash
HELM_CHART_VERSION=1.16.3
helm install enterprise-operator mongodb/enterprise-operator \
  --namespace "${NAMESPACE}" \
  --version="${HELM_CHART_VERSION}" \
  --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.

```bash
kubectl get ns

NAME               STATUS   AGE
default            Active   4m9s
kube-node-lease    Active   4m11s
kube-public        Active   4m12s
kube-system        Active   4m12s
mongodb-operator   Active   75s
```

```bash
kubectl get po -n "${NAMESPACE}"

NAME                                    READY   STATUS   RESTARTS   AGE
mongodb-enterprise-operator-649bbdddf5   1/1    Running   0         7m9s
```

You can see that the helm chart is running with this command.

```bash
helm list --namespace "${NAMESPACE}"

NAME                NAMESPACE     REVISION       VERSION
enterprise-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.

```bash
helm get manifest enterprise-operator --namespace "${NAMESPACE}"
```

Let's display Custom Resource Definitions installed in the step above in the watched namespace.

```bash
kubectl -n "${NAMESPACE}" get crd | grep -E '^(mongo|ops)'

mongodb.mongodb.com                              2022-12-30T16:17:07Z
mongodbmulti.mongodb.com                         2022-12-30T16:17:08Z
mongodbusers.mongodb.com                         2022-12-30T16:17:09Z
opsmanagers.mongodb.com                          2022-12-30T16:17:09Z
```

All required service accounts has been created in watched namespace.

```bash
kubectl -n "${NAMESPACE}" get sa | grep -E '^(mongo)'

mongodb-enterprise-appdb           1         36s
mongodb-enterprise-database-pods   1         36s
mongodb-enterprise-operator        1         36s
mongodb-enterprise-ops-manager     1         36s
```

Validate if the Kubernetes Operator was installed correctly by running the following command and verify the output.

```bash
kubectl describe deployments mongodb-enterprise-operator -n \
  "${NAMESPACE}"
```

Finally, double-check watched namespaces.

```bash
kubectl describe deploy mongodb-enterprise-operator -n "${NAMESPACE}" | grep WATCH

      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