From 53df88afe6ed71a51e6e328e89754a93ea8bcb4c Mon Sep 17 00:00:00 2001 From: Megan Meyer Date: Thu, 13 Mar 2025 14:16:26 -0500 Subject: [PATCH 1/2] Updated guide to include needed model file. Tweaked some commands. Added test calls. --- aks-aml/README.md | 96 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 81 insertions(+), 15 deletions(-) diff --git a/aks-aml/README.md b/aks-aml/README.md index ac030b8..0533b17 100644 --- a/aks-aml/README.md +++ b/aks-aml/README.md @@ -1,14 +1,26 @@ # AKS ALM Integration -## Cluster Setup +## Azure CLI and kubectl Setup + +```bash +# This guides uses commands from two Azure CLI extensions: k8s-extension and ml + +az extension add --upgrade -n ml +az extension add --upgrade -n k8s-extension + +# It also assumes you have kubectl installed. If not, you can run: +az aks install-cli +``` + +## AKS Cluster and Azure ML Workspace Setup ```bash # Set environment variables RG=EphAKSAMLLab CLUSTER_NAME=amltarget LOC=westus3 -ACR_NAME=amllab${RANDOM} AML_WORKSPACE_NAME=amllab +CLUSTER_AML_NAMESPACE=amltest # Create Resource Group az group create -g $RG -l $LOC @@ -20,7 +32,7 @@ AKS_CLUSTER_ID=$(az aks show -g $RG -n $CLUSTER_NAME --query id -o tsv) az aks get-credentials -g $RG -n $CLUSTER_NAME -kubectl create ns amltest +kubectl create ns $CLUSTER_AML_NAMESPACE az k8s-extension create \ --name amlextension \ @@ -33,7 +45,7 @@ az k8s-extension create \ az k8s-extension show \ --name amlextension \ ---cluster-type connectedClusters \ +--cluster-type managedClusters \ --cluster-name $CLUSTER_NAME \ --resource-group $RG @@ -47,36 +59,59 @@ az ml compute attach \ --name k8s-compute \ --resource-id "${AKS_CLUSTER_ID}" \ --identity-type SystemAssigned \ ---namespace amltest +--namespace $CLUSTER_AML_NAMESPACE ``` ## Deploy Inference Endpoint ```bash -az extension add -n ml -az extension update -n ml +# Setup environment export ENDPOINT_NAME="testamlaksendpoint" +export DEPLOYMENT_NAME="blue" +export MODEL_NAME="sklearn-regression" + +# Prepare files to be used by the Azure ML Online Deployment. +# Be sure all files created (endpoint.yaml, deployment.yaml) or +# downloaded (sklearn_regression_model.pkl, score.py, conda.yaml) +# are in the same directory. Run all `az ml online-endpoint create` +# and `az ml online-deployment create` commands from that directory +# as well since they have relative path dependencies. + +# Grant the identity running the CLI commands Storage Blob Data Contributor +# on the Azure ML workspace's storage account. + +az role assignment create \ +--role "Storage Blob Data Contributor" \ +--assignee-object-id "$(az ad signed-in-user show --query id -o tsv)" \ +--assignee-principal-type "User" \ +--scope "$(az ml workspace show -n $AML_WORKSPACE_NAME -g $RG --query storage_account -o tsv)" + +# Create local directory for all needed files mkdir model cd model -cat <<"EOF" > endpoint.yaml -$schema: https://azuremlschemas.azureedge.net/latest/kubernetesOnlineEndpoint.schema.json -name: my-endpoint +# Create Azure ML Online Endpoint specification file + +cat < endpoint.yaml +\$schema: https://azuremlschemas.azureedge.net/latest/kubernetesOnlineEndpoint.schema.json +name: ${ENDPOINT_NAME} compute: azureml:k8s-compute auth_mode: Key tags: - tag1: endpoint-tag1-value + modelName: ${MODEL_NAME} EOF +# Create Azure ML Online Deployment specification file + cat <deployment.yaml \$schema: https://azuremlschemas.azureedge.net/latest/kubernetesOnlineDeployment.schema.json -name: blue +name: ${DEPLOYMENT_NAME} type: kubernetes endpoint_name: ${ENDPOINT_NAME} model: - path: ./ + path: ./sklearn_regression_model.pkl code_configuration: code: ./ scoring_script: score.py @@ -94,26 +129,57 @@ resources: cpu: "0.2" memory: "200Mi" tags: - tag1: deployment-tag1-value + endpointName: ${ENDPOINT_NAME} + modelName: ${MODEL_NAME} instance_count: 1 scale_settings: type: default EOF +# Download required model files + +wget https://raw.githubusercontent.com/Azure/azureml-examples/refs/heads/main/sdk/python/endpoints/online/model-1/model/sklearn_regression_model.pkl + wget https://raw.githubusercontent.com/Azure/azureml-examples/refs/heads/main/sdk/python/endpoints/online/model-1/onlinescoring/score.py wget https://raw.githubusercontent.com/Azure/azureml-examples/refs/heads/main/sdk/python/endpoints/online/model-1/environment/conda.yaml + +# Create the Azure ML Online Endpoint + az ml online-endpoint create \ -g $RG \ -w $AML_WORKSPACE_NAME \ -n $ENDPOINT_NAME \ -f endpoint.yaml +# Create the Azure ML Online Deployment and send all traffic to it + az ml online-deployment create \ -g $RG \ -w $AML_WORKSPACE_NAME \ --n blue \ +-n $DEPLOYMENT_NAME \ --endpoint $ENDPOINT_NAME \ +--all-traffic \ -f deployment.yaml + +``` + +## Test the Inference Endpoint + +```bash +# Get the key required for the Authorization header + +SCORING_ACCESS_KEY=$(kubectl get onlineendpoint $ENDPOINT_NAME -n $CLUSTER_AML_NAMESPACE -o jsonpath={.spec.authKeys.primaryKey}) + +# Get the endpoint URL for the request + +SCORING_ENDPOINT_URL=$(kubectl get onlineendpoint $ENDPOINT_NAME -n $CLUSTER_AML_NAMESPACE -o jsonpath={.status.scoringUri}) + +# Post the test data to test the inference endpoint + +curl -X POST -H "Content-Type: application/json" \ +-H "Authorization: Bearer $SCORING_ACCESS_KEY" \ +--data "$(curl -s https://raw.githubusercontent.com/Azure/azureml-examples/refs/heads/main/sdk/python/endpoints/online/model-1/sample-request.json)" \ +--url $SCORING_ENDPOINT_URL ``` \ No newline at end of file From 461dcc93a0663343e162678cb1e50c6f7873410b Mon Sep 17 00:00:00 2001 From: Megan Meyer Date: Thu, 13 Mar 2025 16:52:23 -0500 Subject: [PATCH 2/2] Updated the node count to better ensure success of the deployment --- aks-aml/README.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/aks-aml/README.md b/aks-aml/README.md index 0533b17..5a2f249 100644 --- a/aks-aml/README.md +++ b/aks-aml/README.md @@ -9,6 +9,7 @@ az extension add --upgrade -n ml az extension add --upgrade -n k8s-extension # It also assumes you have kubectl installed. If not, you can run: + az aks install-cli ``` @@ -16,6 +17,7 @@ az aks install-cli ```bash # Set environment variables + RG=EphAKSAMLLab CLUSTER_NAME=amltarget LOC=westus3 @@ -23,10 +25,12 @@ AML_WORKSPACE_NAME=amllab CLUSTER_AML_NAMESPACE=amltest # Create Resource Group + az group create -g $RG -l $LOC # Create the cluster -az aks create -g $RG -n $CLUSTER_NAME + +az aks create -g $RG -n $CLUSTER_NAME --node-count 5 AKS_CLUSTER_ID=$(az aks show -g $RG -n $CLUSTER_NAME --query id -o tsv) @@ -34,6 +38,8 @@ az aks get-credentials -g $RG -n $CLUSTER_NAME kubectl create ns $CLUSTER_AML_NAMESPACE +# Install the AML extension in the cluster + az k8s-extension create \ --name amlextension \ --extension-type Microsoft.AzureML.Kubernetes \ @@ -50,6 +56,8 @@ az k8s-extension show \ --resource-group $RG +# Create the AML workspace and attach to cluster + az ml workspace create -n $AML_WORKSPACE_NAME -g $RG az ml compute attach \ @@ -162,7 +170,6 @@ az ml online-deployment create \ --endpoint $ENDPOINT_NAME \ --all-traffic \ -f deployment.yaml - ``` ## Test the Inference Endpoint