Kubernetes (K8s) runs and manages many containers across many machines. When your model API needs to handle lots of traffic, restart automatically when it crashes and update without downtime, Kubernetes does that work.
What You Will Learn
- Pods, Deployments and Services
- Deploy the Docker model API on Kubernetes
- Scale replicas manually and automatically
- Roll out a new model version safely
- Kubernetes tools for ML (KServe, Kubeflow)
Prerequisites
Core Objects
| Object | Role |
|---|---|
| Pod | One or more containers running together – the smallest unit |
| Deployment | Keeps a chosen number of identical pods running; handles updates |
| Service | A stable address and load balancer for the pods |
| ConfigMap / Secret | Configuration and passwords |
| HorizontalPodAutoscaler | Adds or removes pods based on CPU or other metrics |
Practise Locally
# install kubectl and minikube (or kind), then:
minikube start
minikube image load iris-api:1.0
Deployment and Service
apiVersion: apps/v1
kind: Deployment
metadata:
name: iris-api
spec:
replicas: 3
selector:
matchLabels: {app: iris-api}
template:
metadata:
labels: {app: iris-api}
spec:
containers:
- name: api
image: iris-api:1.0
ports: [{containerPort: 8000}]
resources:
requests: {cpu: "250m", memory: "256Mi"}
limits: {cpu: "500m", memory: "512Mi"}
readinessProbe:
httpGet: {path: /health, port: 8000}
---
apiVersion: v1
kind: Service
metadata:
name: iris-api
spec:
type: LoadBalancer
selector: {app: iris-api}
ports: [{port: 80, targetPort: 8000}]
Explanation: The Deployment runs 3 copies of the container; the readiness probe sends traffic only to pods whose /health answers. The Service spreads requests across them.
kubectl apply -f iris.yaml
kubectl get pods
minikube service iris-api --url
NAME READY STATUS RESTARTS iris-api-6d9c7b8f5d-2xk8p 1/1 Running 0 iris-api-6d9c7b8f5d-7qv4m 1/1 Running 0 iris-api-6d9c7b8f5d-tl9zn 1/1 Running 0
Scale and Update
kubectl scale deployment iris-api --replicas=5
kubectl autoscale deployment iris-api --cpu-percent=70 --min=2 --max=10
kubectl set image deployment/iris-api api=iris-api:1.1 # rolling update
kubectl rollout undo deployment/iris-api # roll back
Explanation: A rolling update replaces pods a few at a time, so the API stays online. If the new model misbehaves, rollout undo brings back the previous version.
Kubernetes for ML
| Tool | Purpose |
|---|---|
| KServe | Standard model serving with autoscaling (even to zero) |
| Kubeflow | ML pipelines and notebooks on Kubernetes |
| GPU node pools | Run deep-learning inference on GPUs |
| Managed K8s | EKS (AWS), GKE (Google), AKS (Azure) |
Common Errors
| Problem | Cause and fix |
|---|---|
ImagePullBackOff |
The image name is wrong or not in a registry the cluster can reach. |
CrashLoopBackOff |
The app crashes on start – check kubectl logs <pod>. |
Pods Pending |
Not enough CPU/memory in the cluster for the requests. |
Series
MLOps series: ML Deployment · Flask & FastAPI · Docker · Kubernetes · MLflow · CI/CD for ML · AWS for ML · Google Cloud AI
Interview Questions
Q: Deployment vs Pod?
A Pod is one running unit; a Deployment manages a set of identical pods and their updates.
Q: How does Kubernetes give zero-downtime updates?
Rolling updates with readiness probes replace pods gradually.
Practice Exercises
- Deploy the iris API on minikube with 2 replicas.
- Delete one pod and watch Kubernetes recreate it.
- Roll out version 1.1 and roll back.
Summary
Kubernetes runs containers at scale: Deployments keep replicas alive, Services balance traffic, autoscalers add capacity and rolling updates ship new models safely.
Next: MLflow · CI/CD for ML
Machine Learning Tutorials, Courses and Certifications