diff --git a/aks-aml/README.md b/aks-aml/README.md index ac030b8..5a2f249 100644 --- a/aks-aml/README.md +++ b/aks-aml/README.md @@ -1,26 +1,44 @@ # 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 # 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) az aks get-credentials -g $RG -n $CLUSTER_NAME -kubectl create ns amltest +kubectl create ns $CLUSTER_AML_NAMESPACE + +# Install the AML extension in the cluster az k8s-extension create \ --name amlextension \ @@ -33,11 +51,13 @@ az k8s-extension create \ az k8s-extension show \ --name amlextension \ ---cluster-type connectedClusters \ +--cluster-type managedClusters \ --cluster-name $CLUSTER_NAME \ --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 \ @@ -47,36 +67,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 +137,56 @@ 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