Kubernetes no es específico del proveedor; sin embargo, con el fin de proporcionar instrucciones explícitas y completas para implementar todos los elementos de una implementación de Kubernetes Operator de múltiples clústeres, esta sección lo guía a través del proceso de implementación de múltiples clústeres de Kubernetes en GCP específicamente.
Requisitos previos
Antes de comenzar, realice las siguientes acciones:
Crear una cuenta de GCP.
Prepara un proyecto de GCP para que puedas usarlo para crear clústeres de GKE (Google Kubernetes Engine).
Instalar la CLI de GCP.
Autenticarse con el
gcloud auth logindominio.Actualice según sea necesario y configure las variables de entorno definidas en el siguiente archivo
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"
Código fuente
Puede encontrar todo el código fuente incluido en el repositorio del operador Kubernetes de MongoDB.
Procedimiento
Crear clústeres de Kubernetes.
Cree tres clústeres de GKE (Google Kubernetes Engine):
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:-""}"
Almacene las credenciales en su kubeconfig.
Obtener credenciales y guardar contextos en el kubeconfig archivo actual. De forma predeterminada, este archivo se encuentra en el ~/.kube/config directorio y se referencia mediante la $KUBECONFIG variable de entorno.
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}"
Todos los comandos kubectl hacen referencia a estos contextos utilizando las siguientes variables:
$K8S_CLUSTER_0_CONTEXT_NAME$K8S_CLUSTER_1_CONTEXT_NAME$K8S_CLUSTER_2_CONTEXT_NAME
Verifique que kubectl tenga acceso a los clústeres de 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-67d0389d75b70a0007e5894a 2 NAME STATUS ROLES AGE VERSION 3 gke-k8s-mdb-0-67d0389d75-default-pool-bd2d7e42-99mr Ready <none> 30s v1.31.5-gke.1233000 4 gke-k8s-mdb-0-67d0389d75-default-pool-bd2d7e42-d3ql Ready <none> 30s v1.31.5-gke.1233000 5 gke-k8s-mdb-0-67d0389d75-default-pool-bd2d7e42-stxx Ready <none> 29s v1.31.5-gke.1233000 6 7 Nodes in cluster gke_scratch-kubernetes-team_europe-central2-b_k8s-mdb-1-67d0389d75b70a0007e5894a 8 NAME STATUS ROLES AGE VERSION 9 gke-k8s-mdb-1-67d0389d75-default-pool-c4129558-10n1 Ready <none> 76s v1.31.5-gke.1233000 10 gke-k8s-mdb-1-67d0389d75-default-pool-c4129558-gdrg Ready <none> 76s v1.31.5-gke.1233000 11 gke-k8s-mdb-1-67d0389d75-default-pool-c4129558-jbgt Ready <none> 76s v1.31.5-gke.1233000 12 13 Nodes in cluster gke_scratch-kubernetes-team_europe-central2-c_k8s-mdb-2-67d0389d75b70a0007e5894a 14 NAME STATUS ROLES AGE VERSION 15 gke-k8s-mdb-2-67d0389d75-default-pool-0f8822ab-f617 Ready <none> 56s v1.31.5-gke.1233000