Kubernetesはベンダーに固有ではありません。ただし、 マルチクラスターKubernetes Operator実装のすべての要素を配置するための明示的かつ完全な手順を提供するため、このセクションでは具体的には、複数のKubernetesクラスターを GCPに配置するプロセスについて説明します。
前提条件
始める前に、次のアクションを実行してください。
GCPアカウントの作成。
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.必要に応じて を更新し、次の
env_variables.sh
ファイルで定義されている環境変数を設定します。
1 # GCP project name - this is the only parameter that is mandatory to change here 2 export 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. 6 export K8S_CLUSTER_0="k8s-mdb-0${K8S_CLUSTER_SUFFIX:-""}" 7 export K8S_CLUSTER_0_ZONE="europe-central2-a" 8 export K8S_CLUSTER_0_NUMBER_OF_NODES=3 9 export K8S_CLUSTER_0_MACHINE_TYPE="e2-standard-4" 10 export K8S_CLUSTER_0_CONTEXT_NAME="gke_${MDB_GKE_PROJECT}_${K8S_CLUSTER_0_ZONE}_${K8S_CLUSTER_0}" 11 12 export K8S_CLUSTER_1="k8s-mdb-1${K8S_CLUSTER_SUFFIX:-""}" 13 export K8S_CLUSTER_1_ZONE="europe-central2-b" 14 export K8S_CLUSTER_1_NUMBER_OF_NODES=3 15 export K8S_CLUSTER_1_MACHINE_TYPE="e2-standard-4" 16 export K8S_CLUSTER_1_CONTEXT_NAME="gke_${MDB_GKE_PROJECT}_${K8S_CLUSTER_1_ZONE}_${K8S_CLUSTER_1}" 17 18 export K8S_CLUSTER_2="k8s-mdb-2${K8S_CLUSTER_SUFFIX:-""}" 19 export K8S_CLUSTER_2_ZONE="europe-central2-c" 20 export K8S_CLUSTER_2_NUMBER_OF_NODES=1 21 export K8S_CLUSTER_2_MACHINE_TYPE="e2-standard-4" 22 export K8S_CLUSTER_2_CONTEXT_NAME="gke_${MDB_GKE_PROJECT}_${K8S_CLUSTER_2_ZONE}_${K8S_CLUSTER_2}" 23 24 # Comment out the following line so that the script does not create preemptible nodes. 25 # DO NOT USE preemptible nodes in production. 26 export GKE_SPOT_INSTANCES_SWITCH="--preemptible"
ソースコード
手順
Kubernetes クラスターを作成します。
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.
1 gcloud 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:-""}"
1 gcloud 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:-""}"
1 gcloud 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:-""}"
認証情報を kubeconfig
ファイルに保存します。
認証情報を取得し、コンテキストを現在の kubeconfig
ファイルに保存します。デフォルトでは、このファイルは~/.kube/config
ディレクトリにあり、$KUBECONFIG
環境変数によって参照されます。
1 gcloud container clusters get-credentials "${K8S_CLUSTER_0}" --zone="${K8S_CLUSTER_0_ZONE}" 2 gcloud container clusters get-credentials "${K8S_CLUSTER_1}" --zone="${K8S_CLUSTER_1_ZONE}" 3 gcloud container clusters get-credentials "${K8S_CLUSTER_2}" --zone="${K8S_CLUSTER_2_ZONE}"
すべてのkubectl
コマンドは、次の変数を使用してこれらのコンテキストを参照します。
$K8S_CLUSTER_0_CONTEXT_NAME
$K8S_CLUSTER_1_CONTEXT_NAME
$K8S_CLUSTER_2_CONTEXT_NAME
kubectl
がKubernetesクラスターにアクセスできることを確認します。
1 echo "Nodes in cluster ${K8S_CLUSTER_0_CONTEXT_NAME}" 2 kubectl --context "${K8S_CLUSTER_0_CONTEXT_NAME}" get nodes 3 echo; echo "Nodes in cluster ${K8S_CLUSTER_1_CONTEXT_NAME}" 4 kubectl --context "${K8S_CLUSTER_1_CONTEXT_NAME}" get nodes 5 echo; echo "Nodes in cluster ${K8S_CLUSTER_2_CONTEXT_NAME}" 6 kubectl --context "${K8S_CLUSTER_2_CONTEXT_NAME}" get nodes
1 Nodes in cluster gke_scratch-kubernetes-team_europe-central2-a_k8s-mdb-0-686cc1e099f9ef000722c191-862 2 NAME STATUS ROLES AGE VERSION 3 gke-k8s-mdb-0-686cc1e099-default-pool-63c33f99-532x Ready <none> 3m7s v1.32.4-gke.1415000 4 gke-k8s-mdb-0-686cc1e099-default-pool-63c33f99-fs12 Ready <none> 3m7s v1.32.4-gke.1415000 5 gke-k8s-mdb-0-686cc1e099-default-pool-63c33f99-hghj Ready <none> 3m7s v1.32.4-gke.1415000 6 7 Nodes in cluster gke_scratch-kubernetes-team_europe-central2-b_k8s-mdb-1-686cc1e099f9ef000722c191-862 8 NAME STATUS ROLES AGE VERSION 9 gke-k8s-mdb-1-686cc1e099-default-pool-93d5418d-712x Ready <none> 57s v1.32.4-gke.1415000 10 gke-k8s-mdb-1-686cc1e099-default-pool-93d5418d-hpcc Ready <none> 58s v1.32.4-gke.1415000 11 gke-k8s-mdb-1-686cc1e099-default-pool-93d5418d-wg14 Ready <none> 61s v1.32.4-gke.1415000 12 13 Nodes in cluster gke_scratch-kubernetes-team_europe-central2-c_k8s-mdb-2-686cc1e099f9ef000722c191-862 14 NAME STATUS ROLES AGE VERSION 15 gke-k8s-mdb-2-686cc1e099-default-pool-d128293d-tng2 Ready <none> 3m12s v1.32.4-gke.1415000