- macOS, Linux, or Windows (with WSL2 recommended for Windows)
- Python 3.8+. For model training and serving (FastAPI)
- pip. For installing Python packages
- Docker. For building and pushing container images
- Minikube
fastapi
uvicorn
scikit-learn
joblib
numpy
pydantic(These are installed in the Dockerfile.)
- Docker Hub username and password/token for image push/pull
deployment.yaml
service.yamlcurlor Postman for testing the API endpoint
- Create a directory and files
mkdir ml-demo
cd ml-demo
touch train.py app.py Dockerfile- Edit Model Training Script (train.py)
# train.py
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
import joblib
iris = load_iris()
X, y = iris.data, iris.target
clf = RandomForestClassifier()
clf.fit(X, y)
joblib.dump(clf, "model.joblib")- Edit Model Serving App (app.py)
# app.py
from fastapi import FastAPI
from pydantic import BaseModel
import joblib
import numpy as np
app = FastAPI()
model = joblib.load("model.joblib")
class IrisInput(BaseModel):
data: list
@app.post("/predict")
def predict(input: IrisInput):
arr = np.array(input.data).reshape(1, -1)
pred = model.predict(arr)
return {"prediction": int(pred[0])}
- Edit Dockefile
FROM python:3.10-slim
WORKDIR /app
COPY app.py train.py ./
RUN pip install fastapi uvicorn scikit-learn joblib
# Train the model at build time (for demo)
RUN python train.py
EXPOSE 8000
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
- Build the image locally
docker build -t <your-dockerhub-username>/mlops-demo:latest .- Push to Docker Hub
docker login
docker push <your-dockerhub-username>/mlops-demo:latestminikube start-
- Create Kubernetes Manifests (deployment.yaml)
apiVersion: apps/v1
kind: Deployment
metadata:
name: mlops-demo
spec:
replicas: 1
selector:
matchLabels:
app: mlops-demo
template:
metadata:
labels:
app: mlops-demo
spec:
containers:
- name: mlops-demo
image: <your-dockerhub-username>/mlops-demo:latest
ports:
- containerPort: 8000- Edit
services.yaml
apiVersion: v1
kind: Service
metadata:
name: mlops-demo
spec:
type: NodePort
selector:
app: mlops-demo
ports:
- protocol: TCP
port: 80
targetPort: 8000- Deploy to Minikube
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml- Access the App
minikube service mlops-demo --url- Test the Endpoint. You’ll get a URL like http://127.0.0.1:XXXXX.
curl -X POST -H "Content-Type: application/json" \
-d '{"data": [5.1, 3.5, 1.4, 0.2]}' \
http://127.0.0.1:XXXXX/predict