Skip to content

Instantly share code, notes, and snippets.

@misskecupbung
Created July 3, 2025 03:18
Show Gist options
  • Select an option

  • Save misskecupbung/4367de3bb31245f39f3e8bf827a31869 to your computer and use it in GitHub Desktop.

Select an option

Save misskecupbung/4367de3bb31245f39f3e8bf827a31869 to your computer and use it in GitHub Desktop.

Deploy a Simple MLOps Application to Kubernetes

Requirements

System Requirements

  1. macOS, Linux, or Windows (with WSL2 recommended for Windows)

Software Requirements

  1. Python 3.8+. For model training and serving (FastAPI)
  2. pip. For installing Python packages
  3. Docker. For building and pushing container images
  4. Minikube

Python Package Requirements

fastapi
uvicorn
scikit-learn
joblib
numpy
pydantic

(These are installed in the Dockerfile.)

Cloud/Registry Requirements

  1. Docker Hub username and password/token for image push/pull

Kubernetes Manifests

deployment.yaml
service.yaml

Optional:

  1. curl or Postman for testing the API endpoint

Implementation

Prepare Your App

  1. Create a directory and files
mkdir ml-demo
cd ml-demo
touch train.py app.py Dockerfile
  1. 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")
  1. 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])}
  1. 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 and Push Docker Image

  1. Build the image locally
docker build -t <your-dockerhub-username>/mlops-demo:latest .
  1. Push to Docker Hub
docker login
docker push <your-dockerhub-username>/mlops-demo:latest

Install Minikube and kubectl

  1. Install Minikube. Please refer to here.

  2. Install kubectl. Please refer to here.

  3. Start Minikube

minikube start
    1. 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
  1. 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
  1. Deploy to Minikube
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
  1. Access the App
minikube service mlops-demo --url
  1. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment