← Back to DevBytes

Designing a Recommendation Engine on Kubernetes

Designing a Recommendation Engine on Kubernetes

What Is a Recommendation Engine on Kubernetes?

A recommendation engine is a system that predicts user preferences and suggests relevant items (products, movies, articles, etc.). Deploying such an engine on Kubernetes means packaging the model serving logic, data pipelines, and APIs as containerized microservices that run in a Kubernetes cluster. Kubernetes provides orchestration, scaling, self-healing, and rolling updates, making it an ideal platform for production-grade recommendation systems.

Why It Matters

Running a recommendation engine on Kubernetes offers several advantages:

How to Use It: Building and Deploying a Recommendation Engine

We’ll walk through a practical example: a simple collaborative filtering recommendation engine that predicts movie ratings using a pre-trained matrix factorization model. The stack includes a Python Flask API serving the model, a Docker container, and Kubernetes manifests.

Step 1: Prepare the Model and API Code

Create a file app.py that loads a trained model (stored as model.pkl) and exposes a REST endpoint:

import pickle
from flask import Flask, request, jsonify
import numpy as np

app = Flask(__name__)

# Load model (trained offline)
with open('model.pkl', 'rb') as f:
    model = pickle.load(f)

# Example: user-item matrix factors
user_factors = model['user_factors']
item_factors = model['item_factors']

@app.route('/predict', methods=['POST'])
def predict():
    data = request.get_json()
    user_id = data['user_id']
    item_id = data['item_id']
    # Compute dot product
    rating = np.dot(user_factors[user_id], item_factors[item_id])
    return jsonify({'rating': float(rating)})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

Step 2: Containerize the API

Write a Dockerfile:

FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app.py model.pkl ./
EXPOSE 5000
CMD ["python", "app.py"]

requirements.txt:

flask==2.3.2
numpy==1.24.3
scikit-learn==1.2.2

Build and push the image:

docker build -t myrepo/recommender:v1 .
docker push myrepo/recommender:v1

Step 3: Kubernetes Deployment

Create deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: recommender
spec:
  replicas: 3
  selector:
    matchLabels:
      app: recommender
  template:
    metadata:
      labels:
        app: recommender
    spec:
      containers:
      - name: recommender
        image: myrepo/recommender:v1
        ports:
        - containerPort: 5000
        resources:
          requests:
            memory: "256Mi"
            cpu: "250m"
          limits:
            memory: "512Mi"
            cpu: "500m"
        livenessProbe:
          httpGet:
            path: /health
            port: 5000
          initialDelaySeconds: 5
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /ready
            port: 5000
          initialDelaySeconds: 3
          periodSeconds: 5

Add a simple health endpoint in app.py:

@app.route('/health')
def health():
    return "OK", 200

@app.route('/ready')
def ready():
    return "Ready", 200

Step 4: Service and Ingress

Create service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: recommender-service
spec:
  selector:
    app: recommender
  ports:
  - protocol: TCP
    port: 80
    targetPort: 5000
  type: ClusterIP

Expose externally via Ingress (if needed):

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: recommender-ingress
spec:
  rules:
  - host: recommender.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: recommender-service
            port:
              number: 80

Step 5: Deploy and Test

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl apply -f ingress.yaml

# Test with curl
curl -X POST http://recommender.example.com/predict \
  -H "Content-Type: application/json" \
  -d '{"user_id": 42, "item_id": 7}'

Best Practices

Conclusion

Designing a recommendation engine on Kubernetes transforms a static model into a resilient, scalable, and production-ready service. By containerizing the inference logic and leveraging Kubernetes primitives—Deployments, Services, HPA, and Ingress—you can deliver personalized recommendations with high availability and low latency. The approach described here can be extended to more complex architectures, including real-time feature pipelines, A/B testing frameworks, and multi-model serving. Embrace Kubernetes as the backbone of your recommendation infrastructure to iterate faster and serve users reliably.

🚀 Need a reliable AI agent for your project?

Deploy Hermes Agent in 10 minutes. Managed hosting, zero DevOps.

Get Started — $23.99/mo
← Back to all articles