教學:使用服務連接器和工作負載身分識別連接到 AKS 中的 Azure 儲存體

在這個教學中,你會學習如何使用 Service Connector 將 Azure 儲存體 帳號連接到 Azure Kubernetes Service (AKS) 叢集中的 pod,並使用工作負載身份。 您要完成下列工作:

  • 建立一個 AKS 叢集和一個 Azure 儲存體 帳號。
  • 請使用 Service Connector 在 AKS 叢集與 Azure 儲存體 帳號之間建立連線。
  • 複製一個從 AKS 叢集連接 Azure 儲存體 帳號的範例應用程式。
  • 將應用程式部署到 AKS 叢集中的 pod 並測試連線。
  • 清除資源。

必要條件

建立 Azure 資源

  1. 請執行 az login 並依照提示登入Azure。

  2. 建立一個 Azure 資源群組用於此教學,並將 <region> 佔位符替換為有效值。 location 必須是Azure區域,且你的訂閱有足夠的運算配額以應付Azure資源,且對任何服務都沒有限制。

    az group create \
        --name MyResourceGroup \
        --location <region>
    
  3. 透過執行以下指令建立 AKS 叢集,以包含服務連線、Pod 定義及範例應用程式。 更多資訊請參閱 快速入門:使用 Azure CLI 部署 Azure Kubernetes Service (AKS) 叢集。

    az aks create \
        --resource-group MyResourceGroup \
        --name MyAKSCluster \
        --enable-managed-identity \
        --node-count 1
    
  4. 請執行以下指令連接叢集。

    az aks get-credentials \
        --resource-group MyResourceGroup \
        --name MyAKSCluster
    
  5. 建立一個 Azure 儲存體 帳號,讓 AKS 叢集連接到目標服務,並讓範例應用程式互動。 更多資訊請參見 建立Azure儲存帳戶。 執行以下指令,將 <storageaccountname> 替換為一個 3 到 24 個小寫或數字字元,且在 Azure 中唯一無一的名稱。

    az storage account create \
        --resource-group MyResourceGroup \
        --name <storageaccountname> \
        --sku Standard_LRS
    
  6. 建立一個 Azure 容器登錄檔來承載由 AKS Pod 定義所消耗的應用容器映像。 欲了解更多資訊,請參閱 快速入門:使用 Azure portal 建立Azure容器登錄檔。 執行以下命令,將 <registryname> 替換為一個由 5 到 50 個小寫字母或數字組成且在 Azure 中唯一的名稱。

    az acr create \
        --resource-group MyResourceGroup \
        --name <registryname> \
        --sku Standard
    
  7. 啟用匿名提取,讓 AKS 叢集能使用容器映像。 請將 <registryname> 佔位符替換為您的登錄檔名稱。

    az acr update \
        --resource-group MyResourceGroup \
        --name <registryname> \
        --anonymous-pull-enabled
    
  8. 執行下列命令以建立由使用者指派的受控識別,其可用於服務連線建立以啟用 AKS 工作負載的身分識別。 欲了解更多資訊,請參閱 使用Azure入口管理使用者指派的託管身份

    az identity create \
        --resource-group MyResourceGroup \
        --name MyIdentity
    

使用服務連接器建立服務連線

透過使用 Azure CLI 或 Azure portal,建立 AKS 叢集與 Azure 儲存體 帳號之間的服務連線。

執行以下 Azure CLI 指令,建立 Azure 儲存帳號的服務連線。 將 <storageaccountname> 替換為儲存體帳戶名稱,並將 <user-identity-resource-id> 替換為使用者指定的受控識別資源識別碼。

你可以從前述 az identity create 指令的輸出取得使用者指派的管理身份資源 ID,或使用格式 /subscriptions/<subscription-id>/resourceGroups/MyResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/MyIdentity

az aks connection create storage-blob \
  --resource-group MyResourceGroup \
  --name MyAKSCluster \
  --target-resource-group MyResourceGroup \
  --account <storageaccountname> \
  --workload-identity <user-identity-resource-id>

連線建立後,Azure 入口網站服務連接器頁面會顯示新連線的資訊。 你可以在這個教學後面編輯 pod.yaml 檔案時使用這些資訊。

Azure入口網站截圖,檢視由 Service Connector 建立的 Kubernetes 資源。

建立範例應用程式

  1. 先複製範例倉庫,然後切換到包含範例應用程式的目錄。 從這個資料夾執行剩下的指令。

    git clone https://github.com/Azure-Samples/serviceconnector-aks-samples.git
    cd serviceconnector-aks-samples/azure-storage-workload-identity
    
  2. 用指令 az acr build 建立並推送映像檔到容器登錄檔。 請將 <registryname> 佔位符替換為您的登錄檔名稱。

    az acr build --registry <registryname> --image sc-demo-storage-identity:latest ./
    
  3. 請使用 az acr repository list 指令在你的容器登錄檔中查看映像檔。 請將 <registryname> 佔位符替換為您的登錄檔名稱。

    az acr repository list --name <registryname> --output table
    

執行應用程式和測試連線

  1. 請替換本地應用程式資料夾 pod.yaml 檔案中的以下佔位符:

    • <YourContainerImage>:例如,將容器登錄中的映像名稱替換為 <registryname>.azurecr.io/sc-demo-storage-identity:latest
    • <ServiceAccountCreatedByServiceConnector>:替換為連線建立後建立的服務帳戶服務連接器。 你可以在 Azure 入口網站的 AKS 叢集服務連接器頁面查看服務帳號名稱。
    • <SecretCreatedByServiceConnector>:用創建連線後建立的秘密服務連接器替換。 你可以在 Azure 入口網站的 AKS 叢集服務連接器頁面查看服務帳號名稱。
  2. 將 pod 部署到您的叢集中,使用kubectl apply。 命令會在 AKS 叢集的預設命名空間中建立名為 sc-demo-storage-identity 的 Pod。

    kubectl apply -f pod.yaml
    
  3. 請使用 kubectl 查看 pod,檢查部署是否成功。

    kubectl get pod/sc-demo-storage-identity
    
  4. 請透過查看 kubectl日誌來確認連線已建立。

    kubectl logs pod/sc-demo-storage-identity
    

清除資源

如果你不再需要為本教學建立的Azure資源,可以透過刪除 MyResourceGroup 資源群組來刪除它們。

az group delete \
  --resource-group MyResourceGroup