共用方式為


建立用於在 Azure Kubernetes Service (AKS) 上執行 Valkey 叢集的基礎結構

本指南會逐步解說在 AKS 上執行 Valkey 叢集的完整設定程式。 此程式包括設定環境變數、佈建核心 Azure 資源 (例如資源群組、Azure 金鑰保存庫和 Azure 容器登錄 (ACR))、建立具有整合式工作負載身分識別的 AKS 叢集,以及管理秘密。 它還涵蓋為 Valkey 工作負載建立專用節點集區,以及如何將 Valkey 映像匯入您的私人登錄。 如果您偏好基礎結構即程式碼,本指南包含使用 Terraform 搭配 Azure 驗證模組的替代部署路徑,以確保最佳做法和生產整備狀態。

為了使用 Terraform 部署基礎設施,我們使用 Azure Verified 模組用於 AKS

備註

如果您打算在生產環境中執行此部署,建議您查看 Azure 已驗證模組的 AKS 生產模式模組。 本模組附帶最佳實踐建議。

先決條件

設定環境變數

  • 設定所需的環境變數,以在整個指南中使用:

    random=$(echo $RANDOM | tr '[0-9]' '[a-z]')
    export MY_RESOURCE_GROUP_NAME=myResourceGroup-rg-$(echo $random)
    export MY_LOCATION=centralus
    export MY_ACR_REGISTRY=mydns$(echo $random)
    export MY_KEYVAULT_NAME=vault-$(echo $random)-kv
    export MY_CLUSTER_NAME=cluster-aks
    

建立資源群組

  • 使用 az group create (部分機器翻譯) 命令建立資源群組。

    az group create --name $MY_RESOURCE_GROUP_NAME --location $MY_LOCATION --output table
    

    範例輸出︰

    Location    Name
    ----------  ------------------
    eastus      myResourceGroup-rg
    

建立 Azure 金鑰保存庫執行個體

  • 使用 az keyvault create 命令來建立 Azure Key Vault 執行個體。 Azure 金鑰保存庫會安全地儲存和存取秘密,例如 API 金鑰、密碼、憑證或密碼編譯金鑰。

    az keyvault create --name $MY_KEYVAULT_NAME --resource-group $MY_RESOURCE_GROUP_NAME --location $MY_LOCATION --enable-rbac-authorization false --output table
    

    範例輸出︰

    Location    Name            ResourceGroup
    ----------  --------------  ------------------
    eastus      vault-bbbhe-kv  myResourceGroup-rg
    

建立 Azure Container Registry

  • 使用 az acr create 命令來建立 Azure Container Registry 以儲存和管理您的容器映像。

    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 --output tsv)
    

    範例輸出︰

    NAME                  RESOURCE GROUP      LOCATION    SKU      LOGIN SERVER                     CREATION DATE         ADMIN ENABLED
    --------------------  ------------------  ----------  -------  -------------------------------  --------------------  ---------------
    mydnsrandomnamebbbhe  myResourceGroup-rg  eastus      Premium  mydnsrandomnamebbbhe.azurecr.io  2024-06-11T09:36:43Z  True
    

建立 AKS 叢集

在此步驟中,我們會建立 AKS 叢集。 我們啟用 了 Azure Key Vault Secrets 提供者附加元件azure-keyvault-secrets-provider),讓 AKS 叢集能存取儲存在 Azure Key Vault 中的秘密。 我們也啟用 工作負載識別碼,讓 AKS 叢集能安全地存取其他 Azure 資源。

  • 使用 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_D4_v3 \
     --node-count 3 \
     --auto-upgrade-channel stable \
     --node-os-upgrade-channel  NodeImage \
     --attach-acr ${MY_ACR_REGISTRY} \
     --enable-oidc-issuer \
     --enable-workload-identity \
     --enable-addons azure-keyvault-secrets-provider \
     --zones 1 2 3 \
     --generate-ssh-keys \
     --output table
    

    範例輸出︰

    Kind    KubernetesVersion    Location    MaxAgentPools    Name         NodeResourceGroup                         ProvisioningState    ResourceGroup       ResourceUid               SupportPlan
    -----------------------------------------------------------------------  --------------------------  ----------------------  ----------------------------------  ------------------------------------  -------------------------  ------------  ----------------------------------------------------------------  ------  -------------------  ----------  ---------------  -----------  ----------------------------------------  -------------------  ------------------  ------------------------  ------------------
    cluster-ak-myresourcegroup--9b70ac-hhrizake.portal.hcp.eastus.azmk8s.io  1.28.9                      False                   cluster-ak-myResourceGroup--9b70ac  a0a0a0a0-bbbb-cccc-dddd-e1e1e1e1e1e1  False                      True          cluster-ak-myresourcegroup--9b70ac-hhrizake.hcp.eastus.azmk8s.io Base     1.28                 eastus      100              cluster-aks  MC_myResourceGroup-rg_cluster-aks_eastus  Succeeded            myResourceGroup-rg  b1b1b1b1-cccc-dddd-eeee-f2f2f2f2f2f2  KubernetesOfficial
    

取得 Azure Key Vault Secrets 提供者的身份 ID 和物件 ID

  • 用指令 az aks show 取得 Azure Key Vault Secrets 提供者外掛建立的身份 ID 和物件 ID。

    export userAssignedIdentityID=$(az aks show --resource-group $MY_RESOURCE_GROUP_NAME --name $MY_CLUSTER_NAME --query addonProfiles.azureKeyvaultSecretsProvider.identity.clientId --output tsv)
    export userAssignedObjectID=$(az aks show --resource-group $MY_RESOURCE_GROUP_NAME --name $MY_CLUSTER_NAME --query addonProfiles.azureKeyvaultSecretsProvider.identity.objectId --output tsv)
    

AcrPull 角色指派給 kubelet 身份

  • 使用 AcrPull 命令,將 az role assignment create 角色指派給 kubelet 身分識別。

    export KUBELET_IDENTITY=$(az aks show --resource-group $MY_RESOURCE_GROUP_NAME --name $MY_CLUSTER_NAME --output tsv --query identityProfile.kubeletidentity.objectId)
    az role assignment create \
      --assignee ${KUBELET_IDENTITY} \
      --role "AcrPull" \
      --scope ${MY_ACR_REGISTRY_ID} \
      --output table
    

    範例輸出︰

    CreatedBy                             CreatedOn                         Name                                  PrincipalId                           PrincipalName                         PrincipalType     ResourceGroup       RoleDefinitionId                                                                                                                            RoleDefinitionName    Scope                                                                                                                                                        UpdatedBy                             UpdatedOn
    ------------------------------------  --------------------------------  ------------------------------------  ------------------------------------  ------------------------------------  ----------------  ------------------  ------------------------------------------------------------------------------------------------------------------------------------------  --------------------  -----------------------------------------------------------------------------------------------------------------------------------------------------------  ------------------------------------  --------------------------------
    bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f  2024-06-11T09:41:36.631310+00:00  00aa00aa-bb11-cc22-dd33-44ee44ee44ee  aaaaaaaa-bbbb-cccc-1111-222222222222  bbbbbbbb-cccc-dddd-2222-333333333333  ServicePrincipal  myResourceGroup-rg  /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/providers/Microsoft.Authorization/roleDefinitions/aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb  AcrPull               /subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/myResourceGroup-rg/providers/Microsoft.ContainerRegistry/registries/mydnsrandomnamebbbhe  bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f  2024-06-11T09:41:36.631310+00:00
    

為 Valkey 工作負載建立節點集區

在本節中,我們將建立一個專用於執行 Valkey 工作負載的節點集區。 此節點集區已停用自動調整,並以橫跨兩個可用性區域的六個節點建立,因為我們希望在不同的區域中,每個主要節點都擁有一個次要節點。 這些複製品已安排在第三區。

  • 使用 az aks nodepool add 命令建立新的節點集區。

    while [ "$(az aks show --resource-group $MY_RESOURCE_GROUP_NAME --name $MY_CLUSTER_NAME --output tsv --query provisioningState)" != "Succeeded" ]; do echo "waiting for cluster to be ready"; sleep 10; done
    
    az aks nodepool add \
        --resource-group $MY_RESOURCE_GROUP_NAME \
        --cluster-name  $MY_CLUSTER_NAME \
        --name valkey \
        --node-vm-size Standard_D4_v3 \
        --node-count 6 \
        --zones 1 2 3 \
        --output table
    

    範例輸出︰

    Count    CurrentOrchestratorVersion    ETag                                  EnableAutoScaling    EnableCustomCaTrust    EnableEncryptionAtHost    EnableFips    EnableNodePublicIp    EnableUltraSsd    KubeletDiskType    MaxPods    Mode    Name    NodeImageVersion                          OrchestratorVersion    OsDiskSizeGb    OsDiskType    OsSku    OsType    ProvisioningState    ResourceGroup       ScaleDownMode    TypePropertiesType       VmSize           WorkloadRuntime
    -------  ----------------------------  ------------------------------------  -------------------  ---------------------  ------------------------  ------------  --------------------  ----------------  -----------------  ---------  ------  ------  ----------------------------------------  ---------------------  --------------  ------------  -------  --------  -------------------  ------------------  ---------------  -----------------------  ---------------  -----------------
    6        1.28.9                        aaaa0000-bb11-2222-33cc-444444dddddd  False                False                  False                     False         False                 False             OS                 30         User    valkey  AKSUbuntu-2204gen2containerd-202405.27.0  1.28                   128             Managed       Ubuntu   Linux     Succeeded            myResourceGroup-rg  Delete           VirtualMachineScaleSets  Standard_D4s_v3  OCIContainer
    

將 Valkey 映像上傳至您的 Azure Container Registry

在本節中,我們會從 Docker Hub 下載 Valkey 映像,並將它上傳至 Azure Container Registry。 此步驟可確保映像可在您的私人登錄中使用,而且可用於 AKS 叢集中。 不建議在生產環境中取用公用映像。

  • 從 Dockerhub 匯入 Valkey 映像,並使用 az acr import 命令將其上傳至您的 Azure Container Registry。

    az acr import \
        --name $MY_ACR_REGISTRY \
        --source docker.io/valkey/valkey:latest  \
        --image valkey:latest \
        --output table
    

複製 Terraform 模組

  • 用 Terraform 模組克隆 git 倉庫。

    git clone https://github.com/Azure/terraform-azurerm-avm-res-containerservice-managedcluster.git
    cd terraform-azurerm-avm-res-containerservice-managedcluster/tree/stateful-workloads/examples/stateful-workloads-valkey
    

建立 Terraform 變數檔案

  • 透過建立具有 valkey.tfvars 以下內容的檔案來設定 Valkey 變數。 您也可以在此步驟提供特定 變數

        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/valkey/valkey:latest --image valkey:latest
        EOF
    
        valkey_enabled = true
        node_pools = {
          valkey = {
            name       = "valkey"
            vm_size    = "Standard_DS4_v2"
            node_count = 3
            zones      = [1, 2, 3]
            os_type    = "Linux"
          }
        }
    

部署基礎結構

  1. 要部署基礎設施,請執行 Terraform 指令。 在此步驟中,我們設定部署 Valkey 所需的變數。

    terraform init
    export MY_RESOURCE_GROUP_NAME=myResourceGroup-rg
    export MY_LOCATION=centralus
    SECRET=$(openssl rand -base64 32)
    export TF_VAR_valkey_password=${SECRET}
    export TF_VAR_location=${MY_LOCATION}
    export TF_VAR_resource_group_name=${MY_RESOURCE_GROUP_NAME}
    terraform apply -var-file="valkey.tfvars"
    

    備註

    在某些情況下,將 Valkey 映像檔匯入容器登錄的容器登錄工作可能會失敗。 如需詳細資訊,請參閱 container-registry-task。 在大多數情況下,重試可以解決問題。

  2. 執行下列命令,將 Terraform 輸出值匯出為終端機中的環境變數,以便在後續步驟中使用它們:

    export MY_ACR_REGISTRY=$(terraform output -raw acr_registry_name)
    export MY_CLUSTER_NAME=$(terraform output -raw aks_cluster_name)
    

後續步驟

參與者

此文章由 Microsoft 維護。 最初是由下列參與者所撰寫:

  • Nelly Kiboi | 服務工程師
  • Saverio Proto | 首席客戶體驗工程師
  • Don High | 首席客戶工程師
  • LaBrina Loving | 首席服務工程師
  • Ken Kilty | 首席 TPM
  • Russell de Pina | 首席 TPM
  • Colin Mixon | 產品經理
  • Ketan Chawda | 資深客戶工程師
  • Naveed Kharadi | 客戶體驗工程師
  • Erin Schaffer |內容開發人員 2