將 Kubernetes 叢集連結至 Azure Machine Learning 工作區

適用於:Azure CLI ml 延伸模組 v2 (目前)Python SDK azure-ai-ml v2 (目前)

一旦 Azure Machine Learning 延伸模組部署到 AKS 或 Arc Kubernetes 叢集上,您就可以將 Kubernetes 叢集連結至 Azure Machine Learning 工作區,並建立可供 ML 專業人員使用的計算目標。

必要條件

將 Kubernetes 叢集連結至 Azure Machine Learning 工作區可以靈活支援許多不同的案例。 例如,具有多個附件的共用案例、存取 Azure 資源的模型定型指令碼,以及工作區的驗證設定。

多重連結和工作負載隔離

一個叢集到一個工作區,建立多個計算目標

  • 對於相同的 Kubernetes 叢集,您可以多次將其連結至相同的工作區,並針對不同的專案/小組/工作負載建立多個計算目標。

一個叢集到多個工作區

  • 對於相同的 Kubernetes 叢集,您也可以將其連結至多個工作區,而多個工作區可以共用相同的 Kubernetes 叢集。

如果您打算讓不同的專案/小組有不同的計算目標,您可在叢集中為計算目標指定 Kubernetes 命名空間,在不同小組/專案之間隔離工作負載

重要

您打算在將叢集連結至 Azure Machine Learning 工作區時指定的命名空間,應該先前在叢集中建立。

從定型指令碼安全地存取 Azure 資源

如果您需要從定型指令碼安全存取 Azure 資源,請在連結作業期間指定 Kubernetes 計算目標的受控識別

連結至具有使用者指派的受控識別的工作區

Azure Machine Learning 工作區預設會有系統指派的受控識別,可存取 Azure Machine Learning 資源。 如果系統指派的預設設定為開啟,則會完成這些步驟。

否則,如果在建立 Azure Machine Learning 工作區時指定使用者指派的受控識別,則必須先將下列角色指派手動授與受控識別,才能連結計算。

Azure 資源名稱 要指派的角色 描述
Azure 轉送 Azure 轉送擁有者 僅適用於已啟用 Arc 的 Kubernetes 叢集。 無 Arc 連線的 AKS 叢集不會建立 Azure 轉送。
Kubernetes - Azure Arc 或 Azure Kubernetes Service 讀取者
Kube 延伸模組參與者
Azure Kubernetes Service 叢集管理員
適用於已啟用 Arc 的 Kubernetes 叢集和 AKS 叢集。

提示

在與已啟用 Arc 之 Kubernetes 叢集的相同資源群組下,會於延伸模組部署期間建立 Azure 轉送資源。

注意

  • 如果無法使用「Kubernetes 延伸模組參與者」角色權限,叢集連結會失敗,並出現「未安裝延伸模組」錯誤。
  • 如果無法使用「Azure Container Service 叢集管理員」角色權限,叢集連結會失敗,並出現「內部伺服器」錯誤。

如何將 Kubernetes 叢集連結至 Azure Machine Learning 工作區

我們支援兩種方式將 Kubernetes 叢集連結至 Azure Machine Learning 工作區,亦即使用 Azure CLI 或 工作室 UI。

下列 CLI v2 命令會示範如何連結 AKS 和已啟用 Azure Arc 的 Kubernetes 叢集,並使用其作為已啟用受控識別的計算目標。

AKS cluster

az ml compute attach --resource-group <resource-group-name> --workspace-name <workspace-name> --type Kubernetes --name k8s-compute --resource-id "/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.ContainerService/managedclusters/<cluster-name>" --identity-type SystemAssigned --namespace <Kubernetes namespace to run Azure Machine Learning workloads> --no-wait

Arc Kubernetes 叢集

az ml compute attach --resource-group <resource-group-name> --workspace-name <workspace-name> --type Kubernetes --name amlarc-compute --resource-id "/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Kubernetes/connectedClusters/<cluster-name>" --user-assigned-identities "subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<identity-name>" --no-wait

--type 引數設為 Kubernetes。 使用 identity_type 參數來啟用 SystemAssignedUserAssigned 受控識別。

重要

只有 UserAssigned 受控識別才需要 --user-assigned-identities。 您可以提供以逗號分隔的使用者受控識別清單,但是當您附加叢集時,只會使用第一個受控識別。

計算連結不會自動建立 Kubernetes 命名空間,也不會驗證 Kubernetes 命名空間是否存在。 您必須確認指定的命名空間存在於叢集中,否則提交至此計算的任何 Azure Machine Learning 工作負載都會失敗。

將受控識別指派給計算目標

開發人員常見的挑戰就是要管理用來保護解決方案不同元件之間通訊安全的祕密和認證。 受控識別可免除開發人員管理認證的需求。

若要存取 Docker 映像的 Azure Container Registry (ACR),以及用於定型資料的儲存體帳戶,請連結已啟用系統指派或使用者指派受控識別的 Kubernetes 計算。

指派受控識別

  • 您可以將受控識別指派給計算連結步驟中的計算。

  • 如果已連結計算,您可以更新設定以在 Azure Machine Learning 工作室中使用受控識別。

    Screenshot of updating identity of the Kubernetes compute from Azure portal.

    Screenshot of selecting identity of the Kubernetes compute from Azure portal.

將 Azure 角色指派給受控識別

Azure 提供幾種方式將角色指派給受控識別。

如果您使用 Azure 入口網站來指派角色,並具有系統指派的受控識別選取使用者群組主體服務主體,您可以選取 [選取成員] 來搜尋身分識別名稱。 身分識別名稱必須格式化為:<workspace name>/computes/<compute target name>

如果您有使用者指派的受控識別,請選取 [受控識別] 以尋找目標身分識別。

您可以使用受控識別從 Azure Container Registry 提取映像。 將 AcrPull 角色授與計算受控識別。 如需詳細資訊,請參閱 Azure Container Registry 角色和權限

您可以使用受控識別來存取 Azure Blob:

  • 基於唯讀目的,應該將儲存體 Blob 資料讀者角色授與計算受控識別。
  • 基於讀寫目的,應該將儲存體 Blob 資料參與者角色授與計算受控識別。

下一步