MLOps: deploy Azure ML Models to Azure Kubernetes Service (AKS)

In this article I’ll walk through an end-to-end process of deploying a model from Azure Machine Learning (Azure ML) to Azure Kubernetes Service (AKS), using Docker and Azure Container Registry (ACR).

Prerequisites

  • An Azure account with access to Azure ML, ACR, and AKS.
  • Docker installed on your machine.
  • Azure CLI and kubectl installed on your machine.

Workflow Overview

  1. Export the Model from Azure ML
  2. Create a Docker Image with the Model
  3. Push the Image to Azure Container Registry (ACR)
  4. Deploy the Image to AKS

Step 1: Export the Model from Azure ML

Train and Register Your Model

  • Train your model using Azure ML workspaces. For example, use Azure ML SDK to train a simple scikit-learn model and register it in the Azure ML Model Registry.
from azureml.core import Workspace, Model
from sklearn.ensemble import RandomForestClassifier
import joblib

# Connect to Azure ML Workspace
ws = Workspace.from_config()

# Assume X_train, y_train are training data and labels
model = RandomForestClassifier().fit(X_train, y_train)

# Save the model locally
joblib.dump(model, 'model.pkl')

# Register the model in Azure ML
registered_model = Model.register(model_path='model.pkl',
                                  model_name='my_awesome_model',
                                  workspace=ws)

Download the Registered Model

  • Once the model is registered, you can download it from the Model Registry to your local environment.
from azureml.core.model import Model

# Download the model
model = Model(ws, 'my_awesome_model')
model.download(target_dir='.', exist_ok=True)

Step 2: Create a Docker Image with the Model

Create a Docker container that includes your model and the necessary runtime environment.

Write a Dockerfile

# Dockerfile
FROM python:3.8-slim
COPY . /app
WORKDIR /app
RUN pip install scikit-learn joblib flask
ENTRYPOINT ["python", "app.py"]

Build the Docker Image

docker build -t mymodel:latest .

Step 3: Push the Image to Azure Container Registry (ACR)

Log in to ACR, tag your image with the ACR repository, and push it.

az acr login --name <RegistryName>
docker tag mymodel:latest <RegistryName>.azurecr.io/mymodel:latest
docker push <RegistryName>.azurecr.io/mymodel:latest

Step 4: Deploy the Image to AKS

Deploy your model as a service in AKS using the image stored in ACR.

Kubernetes Deployment YAML

  • Create a Kubernetes deployment file that specifies the use of your image from ACR.
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mymodel-deployment
spec:
  replicas: 2
  template:
    spec:
      containers:
      - name: mymodel
        image: <RegistryName>.azurecr.io/mymodel:latest
        ports:
        - containerPort: 80

Deploy to AKS

  • Apply the Kubernetes manifests to your AKS cluster.
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml