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

Deploy GKE Clusters

Kubernetes is not vendor specific; however, for the sake of providing explicit and complete instructions for deploying all elements of a multi-cluster Kubernetes Operator implementation, this section walks you through the process of deploying multiple Kubernetes clusters on GCP specifically.

Before you begin, perform the following actions:

  • Create a GCP account.

  • Prepare a GCP project so that you can use it to create GKE (Google Kubernetes Engine) clusters.

  • Install the GCP CLI.

  • Authenticate with the gcloud auth login command.

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

# GCP project name - this is the only parameter that is mandatory to change hereexport MDB_GKE_PROJECT="${MDB_GKE_PROJECT:="### Set your GKE project name here ###"}" # Adjust the values for each Kubernetes cluster in your deployment.# The deployment script references the following variables to get values for each cluster.export K8S_CLUSTER_0="k8s-mdb-0${K8S_CLUSTER_SUFFIX:-""}"export K8S_CLUSTER_0_ZONE="europe-central2-a"export K8S_CLUSTER_0_NUMBER_OF_NODES=3export K8S_CLUSTER_0_MACHINE_TYPE="e2-standard-4"export K8S_CLUSTER_0_CONTEXT_NAME="gke_${MDB_GKE_PROJECT}_${K8S_CLUSTER_0_ZONE}_${K8S_CLUSTER_0}" export K8S_CLUSTER_1="k8s-mdb-1${K8S_CLUSTER_SUFFIX:-""}"export K8S_CLUSTER_1_ZONE="europe-central2-b"export K8S_CLUSTER_1_NUMBER_OF_NODES=3export K8S_CLUSTER_1_MACHINE_TYPE="e2-standard-4"export K8S_CLUSTER_1_CONTEXT_NAME="gke_${MDB_GKE_PROJECT}_${K8S_CLUSTER_1_ZONE}_${K8S_CLUSTER_1}" export K8S_CLUSTER_2="k8s-mdb-2${K8S_CLUSTER_SUFFIX:-""}"export K8S_CLUSTER_2_ZONE="europe-central2-c"export K8S_CLUSTER_2_NUMBER_OF_NODES=1export K8S_CLUSTER_2_MACHINE_TYPE="e2-standard-4"export K8S_CLUSTER_2_CONTEXT_NAME="gke_${MDB_GKE_PROJECT}_${K8S_CLUSTER_2_ZONE}_${K8S_CLUSTER_2}" # Preemptible nodes disabled — caused random mid-test evictions in CI.# export GKE_SPOT_INSTANCES_SWITCH="--preemptible"

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

1
gcloud config set project "${MDB_GKE_PROJECT}"
2

Create three GKE (Google Kubernetes Engine) clusters. The Kubernetes Operator is deployed to one of these clusters, and MongoDB custom resources are deployed across all clusters, and are managed by the Kubernetes Operator.

gcloud container clusters create "${K8S_CLUSTER_0}" \ --zone="${K8S_CLUSTER_0_ZONE}" \ --num-nodes="${K8S_CLUSTER_0_NUMBER_OF_NODES}" \ --machine-type "${K8S_CLUSTER_0_MACHINE_TYPE}" \ --tags=mongodb \ ${GKE_SPOT_INSTANCES_SWITCH:+${GKE_SPOT_INSTANCES_SWITCH}}
gcloud container clusters create "${K8S_CLUSTER_1}" \ --zone="${K8S_CLUSTER_1_ZONE}" \ --num-nodes="${K8S_CLUSTER_1_NUMBER_OF_NODES}" \ --machine-type "${K8S_CLUSTER_1_MACHINE_TYPE}" \ --tags=mongodb \ ${GKE_SPOT_INSTANCES_SWITCH:+${GKE_SPOT_INSTANCES_SWITCH}}
gcloud container clusters create "${K8S_CLUSTER_2}" \ --zone="${K8S_CLUSTER_2_ZONE}" \ --num-nodes="${K8S_CLUSTER_2_NUMBER_OF_NODES}" \ --machine-type "${K8S_CLUSTER_2_MACHINE_TYPE}" \ --tags=mongodb \ ${GKE_SPOT_INSTANCES_SWITCH:+${GKE_SPOT_INSTANCES_SWITCH}}
3

Obtain credentials and save contexts to the current kubeconfig file. By default, this file is located in the ~/.kube/config directory and referenced by the $KUBECONFIG environment variable.

gcloud container clusters get-credentials "${K8S_CLUSTER_0}" --zone="${K8S_CLUSTER_0_ZONE}"gcloud container clusters get-credentials "${K8S_CLUSTER_1}" --zone="${K8S_CLUSTER_1_ZONE}"gcloud container clusters get-credentials "${K8S_CLUSTER_2}" --zone="${K8S_CLUSTER_2_ZONE}"

All kubectl commands reference these contexts using the following variables:

  • $K8S_CLUSTER_0_CONTEXT_NAME

  • $K8S_CLUSTER_1_CONTEXT_NAME

  • $K8S_CLUSTER_2_CONTEXT_NAME

4
echo "Nodes in cluster ${K8S_CLUSTER_0_CONTEXT_NAME}"kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME}" get nodesecho; echo "Nodes in cluster ${K8S_CLUSTER_1_CONTEXT_NAME}"kubectl --context "${K8S_CLUSTER_1_CONTEXT_NAME}" get nodesecho; echo "Nodes in cluster ${K8S_CLUSTER_2_CONTEXT_NAME}"kubectl --context "${K8S_CLUSTER_2_CONTEXT_NAME}" get nodes
Nodes in cluster gke_scratch-kubernetes-team_europe-central2-a_k8s-mdb-0-dce1a82bb9337d1b41NAME STATUS ROLES AGE VERSIONgke-k8s-mdb-0-dce1a82bb9-default-pool-0d7a5b2d-2ccz Ready <none> 3m22s v1.36.4-gke.2046003gke-k8s-mdb-0-dce1a82bb9-default-pool-0d7a5b2d-819p Ready <none> 3m24s v1.36.4-gke.2046003gke-k8s-mdb-0-dce1a82bb9-default-pool-0d7a5b2d-h23c Ready <none> 3m23s v1.36.4-gke.2046003 Nodes in cluster gke_scratch-kubernetes-team_europe-central2-b_k8s-mdb-1-dce1a82bb9337d1b41NAME STATUS ROLES AGE VERSIONgke-k8s-mdb-1-dce1a82bb9-default-pool-49580ebc-hhr8 Ready <none> 38s v1.36.4-gke.2046003gke-k8s-mdb-1-dce1a82bb9-default-pool-49580ebc-t1m0 Ready <none> 32s v1.36.4-gke.2046003gke-k8s-mdb-1-dce1a82bb9-default-pool-49580ebc-wd5c Ready <none> 35s v1.36.4-gke.2046003 Nodes in cluster gke_scratch-kubernetes-team_europe-central2-c_k8s-mdb-2-dce1a82bb9337d1b41NAME STATUS ROLES AGE VERSIONgke-k8s-mdb-2-dce1a82bb9-default-pool-5509574b-bpt0 Ready <none> 3m42s v1.36.4-gke.2046003