在本文中,我们将创建用于在 Azure Kubernetes 服务 (AKS) 上运行 MongoDB 群集所需的基础结构资源。
先决条件
- 查看在 AKS 上部署 MongoDB 群集的概述。
- Azure 订阅。 如果没有订阅,请创建一个试用帐户。
- Azure CLI 版本 2.61.0。 若要安装或升级,请参阅安装 Azure CLI。
- Helm 3 或更高版本。 若要安装,请参阅安装 Helm。
-
kubectl。 - 已在本地计算机上安装 Docker。 若要安装,请参阅安装 Docker。
设置环境变量
设置本指南中需要使用的环境变量:
random=$(echo $RANDOM | tr '[0-9]' '[a-z]')
export MY_LOCATION=chinanorth3
export MY_RESOURCE_GROUP_NAME=myResourceGroup-rg-$(echo $MY_LOCATION)
export MY_ACR_REGISTRY=mydnsrandomname$(echo $random)
export MY_IDENTITY_NAME=ua-identity-123
export MY_KEYVAULT_NAME=vault-$(echo $random)-kv
export MY_CLUSTER_NAME=cluster-aks
export SERVICE_ACCOUNT_NAME=mongodb
export SERVICE_ACCOUNT_NAMESPACE=mongodb
export AKS_MONGODB_NAMESPACE=mongodb
export AKS_MONGODB_SECRETS_NAME=cluster-aks-mongodb-secrets
export AKS_MONGODB_CLUSTER_NAME=cluster-aks-mongodb
export AKS_MONGODB_SECRETS_ENCRYPTION_KEY=cluster-aks-mongodb-secrets-mongodb-encryption-key
export AKS_AZURE_SECRETS_NAME=cluster-aks-azure-secrets
export AKS_MONGODB_BACKUP_STORAGE_ACCOUNT_NAME=mongodbsa$(echo $random)
export AKS_MONGODB_BACKUP_STORAGE_CONTAINER_NAME=backups
创建资源组
使用
az group create命令创建资源组。az group create --name $MY_RESOURCE_GROUP_NAME --location $MY_LOCATION --output table示例输出:
Location Name ------------- -------------------------------- chinanorth3 myResourceGroup-rg-chinanorth3
创建标识以访问 Azure Key Vault 中的机密
在此步骤中,我们创建一个用户分配的托管标识,外部机密操作员使用该标识访问存储在 Azure Key Vault 中的 MongoDB 密码。
使用
az identity create命令创建用户分配的托管标识。az identity create --name $MY_IDENTITY_NAME --resource-group $MY_RESOURCE_GROUP_NAME --output none export MY_IDENTITY_NAME_ID=$(az identity show --name $MY_IDENTITY_NAME -g $MY_RESOURCE_GROUP_NAME --query id -o tsv) export MY_IDENTITY_NAME_PRINCIPAL_ID=$(az identity show --name $MY_IDENTITY_NAME -g $MY_RESOURCE_GROUP_NAME --query principalId -o tsv) export MY_IDENTITY_NAME_CLIENT_ID=$(az identity show --name $MY_IDENTITY_NAME -g $MY_RESOURCE_GROUP_NAME --query clientId -o tsv)
创建 Azure Key Vault 实例
使用
az keyvault create命令创建 Azure Key Vault 实例。az keyvault create --name $MY_KEYVAULT_NAME --resource-group $MY_RESOURCE_GROUP_NAME --location $MY_LOCATION --enable-rbac-authorization false --output table export KEYVAULTID=$(az keyvault show --name $MY_KEYVAULT_NAME --query "id" --output tsv) export KEYVAULTURL=$(az keyvault show --name $MY_KEYVAULT_NAME --query "properties.vaultUri" --output tsv)示例输出:
Location Name ResourceGroup ------------- -------------- -------------------------------- chinanorth3 vault-cjcfc-kv myResourceGroup-rg-chinanorth3
创建 Azure 容器注册表实例
使用
az acr create命令创建 Azure 容器注册表实例来存储和管理容器映像。az acr create \ --name ${MY_ACR_REGISTRY} \ --resource-group $MY_RESOURCE_GROUP_NAME \ --sku Premium \ --location $MY_LOCATION \ --admin-enabled true \ --output table export MY_ACR_REGISTRY_ID=$(az acr show --name $MY_ACR_REGISTRY --resource-group $MY_RESOURCE_GROUP_NAME --query id -o tsv)示例输出:
NAME RESOURCE GROUP LOCATION SKU LOGIN SERVER CREATION DATE ADMIN ENABLED -------------------- -------------------------------- ------------- ------- ------------------------------- -------------------- --------------- mydnsrandomnamecjcfc myResourceGroup-rg-chinanorth3 chinanorth3 Premium mydnsrandomnamecjcfc.azurecr.cn 2024-07-01T12:18:34Z True
创建 Azure 存储帐户
使用
az acr create命令创建 Azure 存储帐户来存储 MongoDB 备份。az storage account create --name $AKS_MONGODB_BACKUP_STORAGE_ACCOUNT_NAME --resource-group $MY_RESOURCE_GROUP_NAME --location $MY_LOCATION --sku Standard_ZRS --output table az storage container create --name $AKS_MONGODB_BACKUP_STORAGE_CONTAINER_NAME --account-name $AKS_MONGODB_BACKUP_STORAGE_ACCOUNT_NAME --output table export AKS_MONGODB_BACKUP_STORAGE_ACCOUNT_KEY=$(az storage account keys list --account-name $AKS_MONGODB_BACKUP_STORAGE_ACCOUNT_NAME --query "[0].value" -o tsv) az keyvault secret set --vault-name $MY_KEYVAULT_NAME --name AZURE-STORAGE-ACCOUNT-KEY --value $AKS_MONGODB_BACKUP_STORAGE_ACCOUNT_KEY --output none示例输出:
AccessTier AllowBlobPublicAccess AllowCrossTenantReplication CreationTime EnableHttpsTrafficOnly Kind Location MinimumTlsVersion Name PrimaryLocation ProvisioningState ResourceGroup StatusOfPrimary ------------ ----------------------- ----------------------------- -------------------------------- ------------------------ --------- ------------- ------------------- -------------- ----------------- ------------------- -------------------------------- ----------------- Hot False False 2024-08-09T07:06:41.727230+00:00 True StorageV2 chinanorth3 TLS1_0 mongodbsabdibh chinanorth3 Succeeded myResourceGroup-rg-chinanorth3 available Created --------- True
创建 AKS 群集
在以下步骤中,我们创建启用了工作负荷标识和 OpenID Connect (OIDC) 颁发者的 AKS 群集。 工作负载标识为外部机密操作员服务帐户授予访问密钥保管库中存储的 MongoDB 密码的权限。
使用
az aks create命令创建 AKS 群集。az aks create \ --location $MY_LOCATION \ --name $MY_CLUSTER_NAME \ --tier standard \ --resource-group $MY_RESOURCE_GROUP_NAME \ --network-plugin azure \ --node-vm-size Standard_DS4_v2 \ --node-count 1 \ --nodepool-name systempool \ --nodepool-tags "pool=system" \ --auto-upgrade-channel stable \ --node-os-upgrade-channel NodeImage \ --attach-acr ${MY_ACR_REGISTRY} \ --enable-oidc-issuer \ --enable-workload-identity \ --zones 1 2 3 \ --generate-ssh-keys \ --output none使用
az aks nodepool add命令将用户节点池添加到 AKS 群集。 此节点池是 MongoDB Pod 运行的位置。az aks nodepool add \ --resource-group $MY_RESOURCE_GROUP_NAME \ --cluster-name $MY_CLUSTER_NAME \ --name mongodbpool \ --node-vm-size Standard_DS4_v2 \ --node-count 3 \ --zones 1 2 3 \ --mode User \ --output table示例输出:
Name OsType KubernetesVersion VmSize Count MaxPods ProvisioningState Mode ---------- -------- ------------------- --------------- ------- --------- ------------------- ------ userpool Linux 1.28 Standard_DS4_v2 3 30 Succeeded User通过
az aks show命令获取用于工作负荷身份配置的 OIDC 颁发者 URL。export OIDC_URL=$(az aks show --resource-group $MY_RESOURCE_GROUP_NAME --name $MY_CLUSTER_NAME --query oidcIssuerProfile.issuerUrl -o tsv)使用
AcrPull命令将az role assignment create角色分配给 kubelet 标识。export KUBELET_IDENTITY=$(az aks show -g $MY_RESOURCE_GROUP_NAME --name $MY_CLUSTER_NAME -o tsv --query identityProfile.kubeletidentity.objectId) az role assignment create \ --assignee ${KUBELET_IDENTITY} \ --role "AcrPull" \ --scope ${MY_ACR_REGISTRY_ID} \ --output none
将 Percona 映像上传到 Azure 容器注册表
在本部分中,请从 Docker Hub 下载 Percona 映像并将其上传到 Azure 容器注册表。 此步骤可确保映像在专用注册表中可用,并且可以在 AKS 群集中使用。 不建议在生产环境中使用公共映像。
使用以下
az acr import命令从 Docker Hub 导入 Percona 映像,并将其上传到 Azure 容器注册表:az acr import \ --name $MY_ACR_REGISTRY \ --source docker.io/percona/percona-server-mongodb:7.0.8-5 \ --image percona-server-mongodb:7.0.8-5 az acr import \ --name $MY_ACR_REGISTRY \ --source docker.io/percona/pmm-client:2.41.2 \ --image pmm-client:2.41.2 az acr import \ --name $MY_ACR_REGISTRY \ --source docker.io/percona/percona-backup-mongodb:2.4.1 \ --image percona-backup-mongodb:2.4.1 az acr import \ --name $MY_ACR_REGISTRY \ --source docker.io/percona/percona-server-mongodb-operator:1.16.1 \ --image percona-server-mongodb-operator:1.16.1
使用 Terraform 部署基础结构
若要使用 Terraform 部署基础结构,我们将使用适用于 AKS 的 Azure 验证模块 。 存储库 terraform-azurerm-avm-res-containerservice-managedcluster 包含完整示例,其中包含在 Azure Kubernetes 服务(AKS)上运行 MongoDB 群集所需的基础结构。
注释
如果计划在生产环境中运行此功能,建议查看 Azure 验证模块的 AKS 生产模式模块。 这与最佳做法建议结合在一起。
使用 terraform 模块克隆 git 存储库:
git clone https://github.com/Azure/terraform-azurerm-avm-res-containerservice-managedcluster.git cd examples/stateful-workloads使用以下命令创建文件
mongodb.tfvars以定义变量:cat > mongodb.tfvars <<EOL location = "$MY_LOCATION" resource_group_name = "$MY_RESOURCE_GROUP_NAME" acr_registry_name = "$MY_ACR_REGISTRY" cluster_name = "$MY_CLUSTER_NAME" identity_name = "$MY_IDENTITY_NAME" keyvault_name = "$MY_KEYVAULT_NAME" aks_mongodb_backup_storage_account_name = "$AKS_MONGODB_BACKUP_STORAGE_ACCOUNT_NAME" aks_mongodb_backup_storage_container_name = "$AKS_MONGODB_BACKUP_STORAGE_CONTAINER_NAME" mongodb_enabled = true mongodb_namespace = "$AKS_MONGODB_NAMESPACE" service_account_name = "$SERVICE_ACCOUNT_NAME" acr_task_content = <<-EOF version: v1.1.0 steps: - cmd: bash echo Waiting 10 seconds the propagation of the Container Registry Data Importer and Data Reader role - cmd: bash sleep 10 - cmd: az login --identity - cmd: az acr import --name \$RegistryName --source docker.io/percona/percona-server-mongodb:7.0.8-5 --image percona-server-mongodb:7.0.8-5 - cmd: az acr import --name \$RegistryName --source docker.io/percona/pmm-client:2.41.2 --image pmm-client:2.41.2 - cmd: az acr import --name \$RegistryName --source docker.io/percona/percona-backup-mongodb:2.4.1 --image percona-backup-mongodb:2.4.1 - cmd: az acr import --name \$RegistryName --source docker.io/percona/percona-server-mongodb-operator:1.16.1 --image percona-server-mongodb-operator:1.16.1 EOF node_pools = { mongodbserver = { name = "mongodbpool" vm_size = "Standard_D2ds_v4" node_count = 3 zones = [1, 2, 3] os_type = "Linux" } } EOL运行以下 Terraform 命令来部署基础结构:
terraform init terraform fmt terraform apply -var-file="mongodb.tfvars"运行以下命令,将 Terraform 输出值导出为终端中的环境变量,以在后续步骤中使用它们:
export MY_ACR_REGISTRY_ID=$(terraform output -raw acr_registry_id) export MY_ACR_REGISTRY=$(terraform output -raw acr_registry_name) export MY_CLUSTER_NAME=$(terraform output -raw aks_cluster_name) export KUBELET_IDENTITY=$(terraform output -raw aks_kubelet_identity_id) export OIDC_URL=$(terraform output -raw aks_oidc_issuer_url) export identity_name=$(terraform output -raw identity_name) export MY_IDENTITY_NAME_ID=$(terraform output -raw identity_name_id) export MY_IDENTITY_NAME_PRINCIPAL_ID=$(terraform output -raw identity_name_principal_id) export MY_IDENTITY_NAME_CLIENT_ID=$(terraform output -raw identity_name_client_id) export KEYVAULTID=$(terraform output -raw key_vault_id) export KEYVAULTURL=$(terraform output -raw key_vault_uri) export AKS_MONGODB_BACKUP_STORAGE_ACCOUNT_KEY=$(terraform output -raw storage_account_key) export STORAGE_ACCOUNT_NAME=$(terraform output -raw storage_account_name) export TENANT_ID=$(terraform output -raw identity_name_tenant_id)
连接到 AKS 群集
配置
kubectl以使用az aks get-credentials命令连接到 AKS 群集。az aks get-credentials --resource-group $MY_RESOURCE_GROUP_NAME --name $MY_CLUSTER_NAME --overwrite-existing --output table