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 your operator Kubernetes cluster, you can then create and manage all of your MongoDB resources across all of your Kubernetes clusters by applying updates to your operator Kubernetes cluster.
Prerequisites
Before you begin the following procedure, perform the following actions:
Install
kubectl.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 mongodbplugin. To install thekubectl mongodbplugin:1Download your desired Kubernetes Operator package version.
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.gzkubectl-mongodb_{{ .Version }}_darwin_arm64.tar.gzkubectl-mongodb_{{ .Version }}_linux_amd64.tar.gzkubectl-mongodb_{{ .Version }}_linux_arm64.tar.gz
23Locate the
kubectl mongodbplugin binary and copy it to its desired destination.Find the
kubectl-mongodbbinary 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 mongodbplugin using the following commands:kubectl mongodb multicluster setup kubectl 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.shfile.1 # Namespace in which Ops Manager and AppDB will be deployed 2 export OM_NAMESPACE="mongodb-om" 3 # Namespace in which the operator will be installed 4 export OPERATOR_NAMESPACE="mongodb-operator" 5 # Namespace in which MongoDB resources will be deployed 6 export MDB_NAMESPACE="mongodb" 7 8 # comma-separated key=value pairs for additional parameters passed to the helm-chart installing the operator 9 export OPERATOR_ADDITIONAL_HELM_VALUES="${OPERATOR_ADDITIONAL_HELM_VALUES:-""}" 10 11 export OFFICIAL_OPERATOR_HELM_CHART="mongodb/mongodb-kubernetes" 12 export OPERATOR_HELM_CHART="${OPERATOR_HELM_CHART:-${OFFICIAL_OPERATOR_HELM_CHART}}"
Source Code
You can find all included source code in the MongoDB Kubernetes Operator repository.
Procedure
Create namespaces for the Kubernetes Operator, MongoDB and Ops Manager in each of your Kubernetes clusters.
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}"
Create image pull secrets.
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.yaml kubectl --context "${K8S_CLUSTER_1_CONTEXT_NAME}" -n "${OM_NAMESPACE}" apply -f secrets/image-registries-secret.yaml kubectl --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.yaml kubectl --context "${K8S_CLUSTER_1_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" apply -f secrets/image-registries-secret.yaml kubectl --context "${K8S_CLUSTER_2_CONTEXT_NAME}" -n "${MDB_NAMESPACE}" apply -f secrets/image-registries-secret.yaml
Configure your Kubernetes clusters.
Run the following command to configure your Kubernetes clusters.
1 kubectl mongodb multicluster setup \ 2 --central-cluster="${K8S_CLUSTER_0_CONTEXT_NAME}" \ 3 --member-clusters="${K8S_CLUSTER_0_CONTEXT_NAME},${K8S_CLUSTER_1_CONTEXT_NAME},${K8S_CLUSTER_2_CONTEXT_NAME}" \ 4 --member-cluster-namespace="${OM_NAMESPACE}" \ 5 --central-cluster-namespace="${OPERATOR_NAMESPACE}" \ 6 --create-service-account-secrets \ 7 --install-database-roles=true \ 8 --image-pull-secrets=image-registries-secret 9 10 kubectl mongodb multicluster setup \ 11 --central-cluster="${K8S_CLUSTER_0_CONTEXT_NAME}" \ 12 --member-clusters="${K8S_CLUSTER_0_CONTEXT_NAME},${K8S_CLUSTER_1_CONTEXT_NAME},${K8S_CLUSTER_2_CONTEXT_NAME}" \ 13 --member-cluster-namespace="${MDB_NAMESPACE}" \ 14 --central-cluster-namespace="${OPERATOR_NAMESPACE}" \ 15 --create-service-account-secrets \ 16 --install-database-roles=true \ 17 --image-pull-secrets=image-registries-secret
Add the MongoDB Helm Charts for Kubernetes.
Add the MongoDB Helm Charts for Kubernetes repository to Helm.
1 helm repo add mongodb https://mongodb.github.io/helm-charts 2 helm repo update mongodb 3 helm search repo "${OFFICIAL_OPERATOR_HELM_CHART}"
Deploy the Kubernetes Operator.
Use the MongoDB Helm Charts for Kubernetes to deploy the Kubernetes Operator.
1 helm upgrade --install \ 2 --debug \ 3 --kube-context "${K8S_CLUSTER_0_CONTEXT_NAME}" \ 4 mongodb-kubernetes-operator-multi-cluster \ 5 "${OPERATOR_HELM_CHART}" \ 6 --namespace="${OPERATOR_NAMESPACE}" \ 7 --set namespace="${OPERATOR_NAMESPACE}" \ 8 --set operator.namespace="${OPERATOR_NAMESPACE}" \ 9 --set operator.watchNamespace="${OM_NAMESPACE}\,${MDB_NAMESPACE}" \ 10 --set operator.name=mongodb-kubernetes-operator-multi-cluster \ 11 --set operator.createOperatorServiceAccount=false \ 12 --set operator.createResourcesServiceAccountsAndRoles=false \ 13 --set "multiCluster.clusters={${K8S_CLUSTER_0_CONTEXT_NAME},${K8S_CLUSTER_1_CONTEXT_NAME},${K8S_CLUSTER_2_CONTEXT_NAME}}" \ 14 --set "${OPERATOR_ADDITIONAL_HELM_VALUES:-"dummy=value"}" \ 15 --set operator.env=dev
Check the status of your MongoDB Controllers for Kubernetes Operator.
1 kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${OPERATOR_NAMESPACE}" rollout status --timeout=2m deployment/mongodb-kubernetes-operator-multi-cluster 2 echo "Operator deployment in ${OPERATOR_NAMESPACE} namespace" 3 kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${OPERATOR_NAMESPACE}" get deployments 4 echo; echo "Operator pod in ${OPERATOR_NAMESPACE} namespace" 5 kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME}" -n "${OPERATOR_NAMESPACE}" get pods