AI Hints
Home›MLOps Tutorial›Deployment and Delivery
MLOps · Lesson 20 of 80

Models on Kubernetes

Scale, heal, and update.

⏱ 2 min read▶ Video at 48:26🎓 Free
Watch this lesson in the MLOps courseWatch on YouTube ↗Full MLOps course

A single container is fine for a demo. In production, you want several copies, that heal themselves, scale with traffic, and update without downtime. That is exactly what Kubernetes does for web services, and a model service is just a web service. Let's deploy our model API the Kubernetes way.

Models on Kubernetes
From the video: Models on Kubernetes
  • Several copies, always on
  • Heal and scale automatically
  • Update without downtime

The model deployment

Here is the deployment of our model service. Three replicas, so that one failure never takes the service down. The container uses our image, tagged with a version. A readiness probe calls the health endpoint, so traffic only reaches pods that have loaded the model. And resource requests and limits tell Kubernetes how much memory and processor the service needs. Models can be hungry, so these numbers matter. If you update the image tag, Kubernetes performs a rolling update, with no downtime.

deployment.yamlyaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: churn-api
spec:
  replicas: 3
  selector:
    matchLabels:
      app: churn-api
  template:
    metadata:
      labels:
        app: churn-api
    spec:
      containers:
      - name: api
        image: registry.example.com/churn-api:1
        ports:
        - containerPort: 8000
        readinessProbe:
          httpGet:
            path: /health
            port: 8000
        resources:
          requests:
            cpu: 250m
            memory: 512Mi
          limits:
            memory: 1Gi

Autoscaling the service

Traffic changes, so the number of pods should too. This autoscaler watches the average processor use of the pods. It keeps at least three pods, and at most twelve. When the average goes above sixty percent, it adds pods. When traffic falls, it removes them. For models, you may also scale on request latency, or on the length of a queue, instead of processor use, if those reflect the real load better.

hpa.yamlyaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: churn-api
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: churn-api
  minReplicas: 3
  maxReplicas: 12
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 60

ML-specific Kubernetes concerns

Models bring a few special concerns. Large models take a long time to load, so use readiness probes, and generous start up time. Models need memory, so set limits carefully. Some need graphics cards, which you request as a resource, and place with node selectors and taints, as you may remember. Keep the model file in a registry or storage, and load it at start, instead of baking huge files into images. And consider tools made for this, like KServe, which adds autoscaling to zero, canary releases, and standard model protocols.

Slow startreadiness probes and patience
Memory and GPUrequests, limits, node selectors
Model storageload from registry at start
KServea platform for model serving

Summary

Excellent. A deployment gives the model service replicas, health checks, and rolling updates. An autoscaler follows the traffic. Remember the special needs of models, memory, graphics cards, and slow starts. Next, automating the whole path, from a code change to a running model, with C I C D for machine learning.

Key takeaways
  • Replicas, probes, rolling updates
  • HPA follows the load
  • Mind memory, GPUs, slow starts
Next up: CI/CD for ML — from commit to production