For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.

Deploy the Kubernetes Operator

In order to deploy multiple instances of MongoDB resources across multiple Kubernetes clusters, you first need to deploy the Kubernetes Operator to one of your Kubernetes clusters. Once the Kubernetes Operator is deployed to a central Kubernetes cluster, you can then create and manage all of your MongoDB resources across all of your Kubernetes clusters by applying updates to your central Kubernetes cluster.

Before you begin the following procedure, perform the following actions:

  • Install kubectl.

  • Install Helm.

  • Complete the GKE Clusters procedure or the equivalent.

  • Complete the TLS Certificates procedure or the equivalent.

  • Complete the Istio Service mesh procedure or the equivalent.

  • Install the kubectl mongodb plugin. To install the kubectl mongodb plugin:

    1

    Download your desired Kubernetes Operator package version from the Release Page of the MongoDB Controllers for Kubernetes Operator Repository.

    The package's name uses this pattern: kubectl-mongodb_{{ .Version }}_{{ .Os }}_{{ .Arch }}.tar.gz.

    Use one of the following packages:

    • kubectl-mongodb_{{ .Version }}_darwin_amd64.tar.gz

    • kubectl-mongodb_{{ .Version }}_darwin_arm64.tar.gz

    • kubectl-mongodb_{{ .Version }}_linux_amd64.tar.gz

    • kubectl-mongodb_{{ .Version }}_linux_arm64.tar.gz

    2

    Unpack the package, as in the following example:

    tar -zxvf kubectl-mongodb_<version>_darwin_amd64.tar.gz
    3

    Find the kubectl-mongodb binary in the unpacked directory and move it to its desired destination, inside the PATH for the Kubernetes Operator user, as shown in the following example:

    mv kubectl-mongodb /usr/local/bin/kubectl-mongodb

    Now you can run the kubectl mongodb plugin using the following commands:

    kubectl mongodb multicluster setupkubectl mongodb multicluster recover

    To learn more about the supported flags, see the MongoDB kubectl plugin Reference.

  • Update as needed and set the environment variables defined in the following env_variables.sh file.

    # Namespace in which Ops Manager and AppDB will be deployedexport OM_NAMESPACE="mongodb-om"# Namespace in which the operator will be installedexport OPERATOR_NAMESPACE="mongodb-operator"# Namespace in which MongoDB resources will be deployedexport MDB_NAMESPACE="mongodb" # comma-separated key=value pairs for additional parameters passed to the helm-chart installing the operatorexport OPERATOR_ADDITIONAL_HELM_VALUES="${OPERATOR_ADDITIONAL_HELM_VALUES:-""}" export OFFICIAL_OPERATOR_HELM_CHART="mongodb/mongodb-kubernetes"export OPERATOR_HELM_CHART="${OPERATOR_HELM_CHART:-${OFFICIAL_OPERATOR_HELM_CHART}}"

You can find all included source code in the MongoDB Kubernetes Operator repository.

1
kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME}" create namespace "${OPERATOR_NAMESPACE}"kubectl --context "${K8S_CLUSTER_1_CONTEXT_NAME}" create namespace "${OPERATOR_NAMESPACE}"kubectl --context "${K8S_CLUSTER_2_CONTEXT_NAME}" create namespace "${OPERATOR_NAMESPACE}" kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME}" create namespace "${OM_NAMESPACE}"kubectl --context "${K8S_CLUSTER_1_CONTEXT_NAME}" create namespace "${OM_NAMESPACE}"kubectl --context "${K8S_CLUSTER_2_CONTEXT_NAME}" create namespace "${OM_NAMESPACE}" kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME}" create namespace "${MDB_NAMESPACE}"kubectl --context "${K8S_CLUSTER_1_CONTEXT_NAME}" create namespace "${MDB_NAMESPACE}"kubectl --context "${K8S_CLUSTER_2_CONTEXT_NAME}" create namespace "${MDB_NAMESPACE}"
2
mkdir -p secrets kubectl create secret generic "image-registries-secret" \ --from-file=.dockerconfigjson="${HOME}/.docker/config.json" --type=kubernetes.io/dockerconfigjson \ --dry-run=client -o yaml > secrets/image-registries-secret.yaml kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${OPERATOR_NAMESPACE}" apply -f secrets/image-registries-secret.yaml kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${OM_NAMESPACE}" apply -f secrets/image-registries-secret.yamlkubectl --context "${K8S_CLUSTER_1_CONTEXT_NAME}" -n "${OM_NAMESPACE}" apply -f secrets/image-registries-secret.yamlkubectl --context "${K8S_CLUSTER_2_CONTEXT_NAME}" -n "${OM_NAMESPACE}" apply -f secrets/image-registries-secret.yaml kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" apply -f secrets/image-registries-secret.yamlkubectl --context "${K8S_CLUSTER_1_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" apply -f secrets/image-registries-secret.yamlkubectl --context "${K8S_CLUSTER_2_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" apply -f secrets/image-registries-secret.yaml
3

Run the following command to configure your Kubernetes clusters.

kubectl mongodb multicluster setup \ --central-cluster="${K8S_CLUSTER_0_CONTEXT_NAME}" \ --member-clusters="${K8S_CLUSTER_0_CONTEXT_NAME},${K8S_CLUSTER_1_CONTEXT_NAME},${K8S_CLUSTER_2_CONTEXT_NAME}" \ --member-cluster-namespace="${OM_NAMESPACE}" \ --central-cluster-namespace="${OPERATOR_NAMESPACE}" \ --create-service-account-secrets \ --install-database-roles=true \ --image-pull-secrets=image-registries-secret kubectl mongodb multicluster setup \ --central-cluster="${K8S_CLUSTER_0_CONTEXT_NAME}" \ --member-clusters="${K8S_CLUSTER_0_CONTEXT_NAME},${K8S_CLUSTER_1_CONTEXT_NAME},${K8S_CLUSTER_2_CONTEXT_NAME}" \ --member-cluster-namespace="${MDB_NAMESPACE}" \ --central-cluster-namespace="${OPERATOR_NAMESPACE}" \ --create-service-account-secrets \ --install-database-roles=true \ --image-pull-secrets=image-registries-secret
4

Add the MongoDB Helm Charts for Kubernetes repository to Helm.

helm repo add mongodb https://mongodb.github.io/helm-chartshelm repo update mongodbhelm search repo "${OFFICIAL_OPERATOR_HELM_CHART}"
5

Use the MongoDB Helm Charts for Kubernetes to deploy the Kubernetes Operator.

helm upgrade --install \ --debug \ --kube-context "${K8S_CLUSTER_0_CONTEXT_NAME}" \ mongodb-kubernetes-operator-multi-cluster \ "${OPERATOR_HELM_CHART}" \ --namespace="${OPERATOR_NAMESPACE}" \ --set namespace="${OPERATOR_NAMESPACE}" \ --set operator.namespace="${OPERATOR_NAMESPACE}" \ --set operator.watchNamespace="${OM_NAMESPACE}\,${MDB_NAMESPACE}" \ --set operator.name=mongodb-kubernetes-operator-multi-cluster \ --set operator.createOperatorServiceAccount=false \ --set operator.createResourcesServiceAccountsAndRoles=false \ --set "multiCluster.clusters={${K8S_CLUSTER_0_CONTEXT_NAME},${K8S_CLUSTER_1_CONTEXT_NAME},${K8S_CLUSTER_2_CONTEXT_NAME}}" \ --set "${OPERATOR_ADDITIONAL_HELM_VALUES:-"dummy=value"}" \ --set operator.env=dev
6
kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${OPERATOR_NAMESPACE}" rollout status --timeout=2m deployment/mongodb-kubernetes-operator-multi-clusterecho "Operator deployment in ${OPERATOR_NAMESPACE} namespace"kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${OPERATOR_NAMESPACE}" get deploymentsecho; echo "Operator pod in ${OPERATOR_NAMESPACE} namespace"kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${OPERATOR_NAMESPACE}" get pods