|
| 1 | +--- |
| 2 | +title: Attach a Kubernetes cluster to AzureML workspace |
| 3 | +description: Learn about how to attach a Kubernetes cluster |
| 4 | +titleSuffix: Azure Machine Learning |
| 5 | +author: bozhong68 |
| 6 | +ms.author: bozhlin |
| 7 | +ms.reviewer: ssalgado |
| 8 | +ms.service: machine-learning |
| 9 | +ms.subservice: core |
| 10 | +ms.date: 08/31/2022 |
| 11 | +ms.topic: how-to |
| 12 | +ms.custom: build-spring-2022, cliv2, sdkv2, event-tier1-build-2022 |
| 13 | +--- |
| 14 | + |
| 15 | +# Attach a Kubernetes cluster to AzureML workspace |
| 16 | + |
| 17 | +Once AzureML extension is deployed on AKS or Arc Kubernetes cluster, you can attach the Kubernetes cluster to AzureML workspace and create compute targets for ML professionals to use. |
| 18 | + |
| 19 | +Some key considerations when attaching Kubernetes cluster to AzureML workspace: |
| 20 | + * If you need to access Azure resource seamlessly from your training script, you can specify a managed identity for Kubernetes compute target during attach operation. |
| 21 | + * If you plan to have different compute target for different project/team, you can specify Kubernetes namespace for the compute target to isolate workload among different teams/projects. |
| 22 | + * For the same Kubernetes cluster, you can attach it to the same workspace multiple times and create multiple compute targets for different project/team/workload. |
| 23 | + * For the same Kubernetes cluster, you can also attach it to multiple workspaces, and the multiple workspaces can share the same Kubernetes cluster. |
| 24 | + |
| 25 | +### Prerequisite |
| 26 | + |
| 27 | +Azure Machine Learning workspace defaults to having a system-assigned managed identity to access Azure ML resources. The steps are completed if the system assigned default setting is on. |
| 28 | + |
| 29 | + |
| 30 | +Otherwise, if a user-assigned managed identity is specified in Azure Machine Learning workspace creation, the following role assignments need to be granted to the managed identity manually before attaching the compute. |
| 31 | + |
| 32 | +|Azure resource name |Role to be assigned|Description| |
| 33 | +|--|--|--| |
| 34 | +|Azure Relay|Azure Relay Owner|Only applicable for Arc-enabled Kubernetes cluster. Azure Relay isn't created for AKS cluster without Arc connected.| |
| 35 | +|Azure Arc-enabled Kubernetes or AKS|Reader|Applicable for both Arc-enabled Kubernetes cluster and AKS cluster.| |
| 36 | + |
| 37 | +Azure Relay resource is created during the extension deployment under the same Resource Group as the Arc-enabled Kubernetes cluster. |
| 38 | + |
| 39 | + |
| 40 | +### [CLI](#tab/cli) |
| 41 | + |
| 42 | +[!INCLUDE [cli v2](../../includes/machine-learning-cli-v2.md)] |
| 43 | + |
| 44 | +The following commands show how to attach an AKS and Azure Arc-enabled Kubernetes cluster, and use it as a compute target with managed identity enabled. |
| 45 | + |
| 46 | +**AKS cluster** |
| 47 | + |
| 48 | +```azurecli |
| 49 | +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 AzureML workloads> --no-wait |
| 50 | +``` |
| 51 | + |
| 52 | +**Arc Kubernetes cluster** |
| 53 | + |
| 54 | +```azurecli |
| 55 | +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 |
| 56 | +``` |
| 57 | + |
| 58 | +Set the `--type` argument to `Kubernetes`. Use the `identity_type` argument to enable `SystemAssigned` or `UserAssigned` managed identities. |
| 59 | + |
| 60 | +> [!IMPORTANT] |
| 61 | +> `--user-assigned-identities` is only required for `UserAssigned` managed identities. Although you can provide a list of comma-separated user managed identities, only the first one is used when you attach your cluster. |
| 62 | +> |
| 63 | +> Compute attach won't create the Kubernetes namespace automatically or validate whether the kubernetes namespace existed. You need to verify that the specified namespace exists in your cluster, otherwise, any AzureML workloads submitted to this compute will fail. |
| 64 | +### [Python](#tab/python) |
| 65 | + |
| 66 | +[!INCLUDE [sdk v1](../../includes/machine-learning-sdk-v1.md)] |
| 67 | + |
| 68 | +```python |
| 69 | +from azureml.core.compute import KubernetesCompute, ComputeTarget |
| 70 | + |
| 71 | +# Specify a name for your Kubernetes compute |
| 72 | +compute_target_name = "<kubernetes compute target name>" |
| 73 | + |
| 74 | +# resource ID of the Arc-enabled Kubernetes cluster |
| 75 | +cluster_resource_id = "/subscriptions/<sub ID>/resourceGroups/<RG>/providers/Microsoft.Kubernetes/connectedClusters/<cluster name>" |
| 76 | + |
| 77 | +user_assigned_identity_resouce_id = ['subscriptions/<sub ID>/resourceGroups/<RG>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<identity name>'] |
| 78 | + |
| 79 | +# Specify Kubernetes namespace to run AzureML workloads |
| 80 | +ns = "default" |
| 81 | + |
| 82 | +try: |
| 83 | + compute_target = ComputeTarget(workspace=ws, name=compute_target_name) |
| 84 | + print('Found existing cluster, use it.') |
| 85 | +except ComputeTargetException: |
| 86 | + attach_configuration = KubernetesCompute.attach_configuration(resource_id = cluster_resource_id, namespace = ns, identity_type ='UserAssigned',identity_ids = user_assigned_identity_resouce_id) |
| 87 | + compute_target = ComputeTarget.attach(ws, compute_target_name, attach_configuration) |
| 88 | + compute_target.wait_for_completion(show_output=True) |
| 89 | +``` |
| 90 | +### [Studio](#tab/studio) |
| 91 | + |
| 92 | +Attaching a Kubernetes cluster makes it available to your workspace for training or inferencing. |
| 93 | + |
| 94 | +1. Navigate to [Azure Machine Learning studio](https://ml.azure.com). |
| 95 | +1. Under **Manage**, select **Compute**. |
| 96 | +1. Select the **Attached computes** tab. |
| 97 | +1. Select **+New > Kubernetes** |
| 98 | + |
| 99 | + :::image type="content" source="media/how-to-attach-arc-kubernetes/attach-kubernetes-cluster.png" alt-text="Screenshot of settings for Kubernetes cluster to make available in your workspace."::: |
| 100 | + |
| 101 | +1. Enter a compute name and select your Kubernetes cluster from the dropdown. |
| 102 | + |
| 103 | + * **(Optional)** Enter Kubernetes namespace, which defaults to `default`. All machine learning workloads will be sent to the specified Kubernetes namespace in the cluster. Compute attach won't create the Kubernetes namespace automatically or validate whether the kubernetes namespace exists. You need to verify that the specified namespace exists in your cluster, otherwise, any AzureML workloads submitted to this compute will fail. |
| 104 | + |
| 105 | + * **(Optional)** Assign system-assigned or user-assigned managed identity. Managed identities eliminate the need for developers to manage credentials. For more information, see [managed identities overview](../active-directory/managed-identities-azure-resources/overview.md) . |
| 106 | + |
| 107 | + :::image type="content" source="media/how-to-attach-arc-kubernetes/configure-kubernetes-cluster-2.png" alt-text="Screenshot of settings for developer configuration of Kubernetes cluster."::: |
| 108 | + |
| 109 | +1. Select **Attach** |
| 110 | + |
| 111 | + In the Attached compute tab, the initial state of your cluster is *Creating*. When the cluster is successfully attached, the state changes to *Succeeded*. Otherwise, the state changes to *Failed*. |
| 112 | + |
| 113 | + :::image type="content" source="media/how-to-attach-arc-kubernetes/provision-resources.png" alt-text="Screenshot of attached settings for configuration of Kubernetes cluster."::: |
| 114 | + |
| 115 | +--- |
| 116 | + |
| 117 | +## Next steps |
| 118 | + |
| 119 | +- [Create and manage instance types](./how-to-manage-kubernetes-instance-types.md) |
| 120 | +- [AzureML inference router and connectivity requirements](./how-to-kubernetes-inference-routing-azureml-fe.md) |
| 121 | +- [Secure AKS inferencing environment](./how-to-secure-kubernetes-inferencing-environment.md) |
0 commit comments