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.
Docs Menu

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.

1# GCP project name - this is the only parameter that is mandatory to change here
2export MDB_GKE_PROJECT="${MDB_GKE_PROJECT:="### Set your GKE project name here ###"}"
3
4# Adjust the values for each Kubernetes cluster in your deployment.
5# The deployment script references the following variables to get values for each cluster.
6export K8S_CLUSTER_0="k8s-mdb-0${K8S_CLUSTER_SUFFIX:-""}"
7export K8S_CLUSTER_0_ZONE="europe-central2-a"
8export K8S_CLUSTER_0_NUMBER_OF_NODES=3
9export K8S_CLUSTER_0_MACHINE_TYPE="e2-standard-4"
10export K8S_CLUSTER_0_CONTEXT_NAME="gke_${MDB_GKE_PROJECT}_${K8S_CLUSTER_0_ZONE}_${K8S_CLUSTER_0}"
11
12export K8S_CLUSTER_1="k8s-mdb-1${K8S_CLUSTER_SUFFIX:-""}"
13export K8S_CLUSTER_1_ZONE="europe-central2-b"
14export K8S_CLUSTER_1_NUMBER_OF_NODES=3
15export K8S_CLUSTER_1_MACHINE_TYPE="e2-standard-4"
16export K8S_CLUSTER_1_CONTEXT_NAME="gke_${MDB_GKE_PROJECT}_${K8S_CLUSTER_1_ZONE}_${K8S_CLUSTER_1}"
17
18export K8S_CLUSTER_2="k8s-mdb-2${K8S_CLUSTER_SUFFIX:-""}"
19export K8S_CLUSTER_2_ZONE="europe-central2-c"
20export K8S_CLUSTER_2_NUMBER_OF_NODES=1
21export K8S_CLUSTER_2_MACHINE_TYPE="e2-standard-4"
22export K8S_CLUSTER_2_CONTEXT_NAME="gke_${MDB_GKE_PROJECT}_${K8S_CLUSTER_2_ZONE}_${K8S_CLUSTER_2}"
23
24# Preemptible nodes disabled — caused random mid-test evictions in CI.
25# export GKE_SPOT_INSTANCES_SWITCH="--preemptible"

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

1
1gcloud 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.

1gcloud container clusters create "${K8S_CLUSTER_0}" \
2 --zone="${K8S_CLUSTER_0_ZONE}" \
3 --num-nodes="${K8S_CLUSTER_0_NUMBER_OF_NODES}" \
4 --machine-type "${K8S_CLUSTER_0_MACHINE_TYPE}" \
5 --tags=mongodb \
6 ${GKE_SPOT_INSTANCES_SWITCH:+${GKE_SPOT_INSTANCES_SWITCH}}
1gcloud container clusters create "${K8S_CLUSTER_1}" \
2 --zone="${K8S_CLUSTER_1_ZONE}" \
3 --num-nodes="${K8S_CLUSTER_1_NUMBER_OF_NODES}" \
4 --machine-type "${K8S_CLUSTER_1_MACHINE_TYPE}" \
5 --tags=mongodb \
6 ${GKE_SPOT_INSTANCES_SWITCH:+${GKE_SPOT_INSTANCES_SWITCH}}
1gcloud container clusters create "${K8S_CLUSTER_2}" \
2 --zone="${K8S_CLUSTER_2_ZONE}" \
3 --num-nodes="${K8S_CLUSTER_2_NUMBER_OF_NODES}" \
4 --machine-type "${K8S_CLUSTER_2_MACHINE_TYPE}" \
5 --tags=mongodb \
6 ${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.

1gcloud container clusters get-credentials "${K8S_CLUSTER_0}" --zone="${K8S_CLUSTER_0_ZONE}"
2gcloud container clusters get-credentials "${K8S_CLUSTER_1}" --zone="${K8S_CLUSTER_1_ZONE}"
3gcloud 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
1echo "Nodes in cluster ${K8S_CLUSTER_0_CONTEXT_NAME}"
2kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME}" get nodes
3echo; echo "Nodes in cluster ${K8S_CLUSTER_1_CONTEXT_NAME}"
4kubectl --context "${K8S_CLUSTER_1_CONTEXT_NAME}" get nodes
5echo; echo "Nodes in cluster ${K8S_CLUSTER_2_CONTEXT_NAME}"
6kubectl --context "${K8S_CLUSTER_2_CONTEXT_NAME}" get nodes
1Nodes in cluster gke_scratch-kubernetes-team_europe-central2-a_k8s-mdb-0-6aa01fd8df4x772f980-1668
2NAME STATUS ROLES AGE VERSION
3gke-k8s-mdb-0-6aa01fd8df-default-pool-54114de2-0jv5 Ready <none> 3m v1.35.7-gke.1150000
4gke-k8s-mdb-0-6aa01fd8df-default-pool-54114de2-gjgw Ready <none> 2m55s v1.35.7-gke.1150000
5gke-k8s-mdb-0-6aa01fd8df-default-pool-54114de2-hwjw Ready <none> 3m v1.35.7-gke.1150000
6
7Nodes in cluster gke_scratch-kubernetes-team_europe-central2-b_k8s-mdb-1-6aa01fd8df4x772f980-1668
8NAME STATUS ROLES AGE VERSION
9gke-k8s-mdb-1-6aa01fd8df-default-pool-003bd080-5dtq Ready <none> 80s v1.35.7-gke.1150000
10gke-k8s-mdb-1-6aa01fd8df-default-pool-003bd080-fz0d Ready <none> 80s v1.35.7-gke.1150000
11gke-k8s-mdb-1-6aa01fd8df-default-pool-003bd080-qg3n Ready <none> 77s v1.35.7-gke.1150000
12
13Nodes in cluster gke_scratch-kubernetes-team_europe-central2-c_k8s-mdb-2-6aa01fd8df4x772f980-1668
14NAME STATUS ROLES AGE VERSION
15gke-k8s-mdb-2-6aa01fd8df-default-pool-b6805d5a-rqjw Ready <none> 31s v1.35.7-gke.1150000