deploy-ml-model-serving
О программе
Этот навык развертывает ML-модели в продакшн с использованием MLflow, BentoML или Seldon Core, предоставляя REST/gRPC эндпоинты. Он реализует автомасштабирование, мониторинг и A/B-тестирование для высокопроизводительного инференса в масштабе. Используйте его при настройке API для прогнозирования в реальном времени, управлении переменной нагрузкой или переходе от пакетной к онлайн-обработке.
Быстрая установка
Claude Code
Рекомендуетсяnpx skills add pjt222/agent-almanac -a claude-code/plugin add https://github.com/pjt222/agent-almanacgit clone https://github.com/pjt222/agent-almanac.git ~/.claude/skills/deploy-ml-model-servingСкопируйте и вставьте эту команду в Claude Code для установки этого навыка
Документация
Deploy ML Model Serving
See Extended Examples for complete configuration files and templates.
Deploy machine learning models to production with scalable serving infrastructure, monitoring, A/B testing.
When Use
- Deploy trained models to production for real-time inference
- Set up REST or gRPC APIs for model predictions
- Implement autoscaling for variable load patterns
- Run A/B tests between model versions
- Migrate from batch to real-time inference
- Build low-latency prediction services
- Manage multiple model versions in production
Inputs
- Required: Registered model in MLflow Model Registry or trained model artifact
- Required: Kubernetes cluster or container orchestration platform
- Required: Serving framework choice (MLflow, BentoML, Seldon Core, TorchServe)
- Optional: GPU resources for deep learning models
- Optional: Monitoring infrastructure (Prometheus, Grafana)
- Optional: Load balancer and ingress controller
Steps
Step 1: Deploy with MLflow Models Serving
Use MLflow's built-in serving for quick deployment of scikit-learn, PyTorch, TensorFlow models.
# Serve model locally for testing
mlflow models serve \
--model-uri models:/customer-churn-classifier/Production \
--port 5001 \
--host 0.0.0.0
# Test endpoint
curl -X POST http://localhost:5001/invocations \
-H 'Content-Type: application/json' \
-d '{
"dataframe_records": [
{"feature1": 1.0, "feature2": 2.0, "feature3": 3.0}
]
}'
Docker deployment:
# Dockerfile.mlflow-serving
FROM python:3.9-slim
# Install MLflow and dependencies
RUN pip install mlflow boto3 scikit-learn
# Set environment variables
ENV MLFLOW_TRACKING_URI=http://mlflow-server:5000
# ... (see EXAMPLES.md for complete implementation)
Docker Compose for local testing:
# docker-compose.mlflow-serving.yml
version: '3.8'
services:
model-server:
build:
context: .
dockerfile: Dockerfile.mlflow-serving
# ... (see EXAMPLES.md for complete implementation)
Test deployment:
# test_mlflow_serving.py
import requests
import json
def test_prediction():
url = "http://localhost:8080/invocations"
# Prepare input data
# ... (see EXAMPLES.md for complete implementation)
Got: Model server starts. Responds to HTTP POST requests. Returns predictions in JSON format. Docker container runs without errors.
If fail: Check model URI valid (mlflow models list), verify MLflow tracking server accessibility, ensure all model dependencies installed in container, check port availability (netstat -tulpn | grep 8080), verify model flavor compatibility, inspect container logs (docker logs <container-id>).
Step 2: Deploy with BentoML for Production Scale
Use BentoML for advanced serving with better performance and features.
# bentoml_service.py
import bentoml
from bentoml.io import JSON, NumpyNdarray
import numpy as np
import pandas as pd
# Load model from MLflow
import mlflow
# ... (see EXAMPLES.md for complete implementation)
Build and containerize:
# Build Bento
bentoml build
# Containerize
bentoml containerize customer_churn_classifier:latest \
--image-tag customer-churn:v1.0
# Run container
docker run -p 3000:3000 customer-churn:v1.0
BentoML configuration:
# bentofile.yaml
service: "bentoml_service:ChurnPredictionService"
include:
- "bentoml_service.py"
- "preprocessing.py"
python:
packages:
- scikit-learn==1.0.2
- pandas==1.4.0
- numpy==1.22.0
- mlflow==2.0.1
docker:
distro: debian
python_version: "3.9"
cuda_version: null # Set to "11.6" for GPU support
Kubernetes deployment:
# k8s/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: churn-prediction
labels:
app: churn-prediction
spec:
# ... (see EXAMPLES.md for complete implementation)
Deploy to Kubernetes:
# Apply Kubernetes manifests
kubectl apply -f k8s/deployment.yaml
# Check deployment status
kubectl get deployments
kubectl get pods
kubectl get services
# Test endpoint
EXTERNAL_IP=$(kubectl get svc churn-prediction-service -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
curl -X POST http://$EXTERNAL_IP/predict \
-H 'Content-Type: application/json' \
-d '{"instances": [{"tenure": 12, "monthly_charges": 70.35}]}'
Got: BentoML service builds. Container runs and serves predictions. Kubernetes deployment creates 3 replicas. Load balancer exposes external endpoint. Health checks pass.
If fail: Verify BentoML install (bentoml --version), check model exists in BentoML store (bentoml models list), ensure Docker daemon running, verify Kubernetes cluster access (kubectl cluster-info), check resource limits not exceeded, inspect pod logs (kubectl logs <pod-name>), verify service selector matches pod labels.
Step 3: Do Seldon Core for Advanced Features
Use Seldon Core for multi-model serving, A/B testing, explainability.
# seldon_wrapper.py
import logging
from typing import Dict, List, Union
import numpy as np
import mlflow
logger = logging.getLogger(__name__)
# ... (see EXAMPLES.md for complete implementation)
Seldon deployment configuration:
# seldon-deployment.yaml
apiVersion: machinelearning.seldon.io/v1
kind: SeldonDeployment
metadata:
name: churn-classifier
namespace: seldon
spec:
name: churn-classifier
# ... (see EXAMPLES.md for complete implementation)
A/B testing configuration:
# seldon-ab-test.yaml
apiVersion: machinelearning.seldon.io/v1
kind: SeldonDeployment
metadata:
name: churn-classifier-ab
spec:
name: churn-classifier-ab
predictors:
# ... (see EXAMPLES.md for complete implementation)
Deploy to Kubernetes:
# Install Seldon Core operator
kubectl create namespace seldon-system
helm install seldon-core seldon-core-operator \
--repo https://storage.googleapis.com/seldon-charts \
--namespace seldon-system \
--set usageMetrics.enabled=true
# Create namespace for models
# ... (see EXAMPLES.md for complete implementation)
Got: Seldon Core operator installed. Model deployment creates pods. REST endpoint responds to predictions. A/B test splits traffic correctly. Seldon Analytics records metrics.
If fail: Verify Seldon Core operator running (kubectl get pods -n seldon-system), check SeldonDeployment status (kubectl describe seldondeployment), ensure image registry accessible from cluster, verify model URI resolution, check RBAC permissions for Seldon operator, inspect model container logs.
Step 4: Do Monitoring and Observability
Add comprehensive monitoring for model serving infrastructure.
# monitoring.py
from prometheus_client import Counter, Histogram, Gauge, start_http_server
import time
import logging
logger = logging.getLogger(__name__)
# Prometheus metrics
# ... (see EXAMPLES.md for complete implementation)
Prometheus configuration:
# prometheus-config.yaml
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'model-serving'
kubernetes_sd_configs:
# ... (see EXAMPLES.md for complete implementation)
Grafana dashboard JSON:
{
"dashboard": {
"title": "ML Model Serving Metrics",
"panels": [
{
"title": "Predictions Per Second",
"targets": [
{
# ... (see EXAMPLES.md for complete implementation)
Got: Prometheus scrapes metrics. Grafana dashboards display prediction throughput, latency percentiles, error rates, active requests in real-time.
If fail: Verify Prometheus scrape targets UP (http://prometheus:9090/targets), check metrics endpoint accessibility (curl http://model-pod:8000/metrics), ensure Kubernetes service discovery configured, verify Grafana data source connection, check firewall rules for metrics port.
Step 5: Do Autoscaling
Configure horizontal pod autoscaling based on request load.
# hpa.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: churn-prediction-hpa
namespace: seldon
spec:
scaleTargetRef:
# ... (see EXAMPLES.md for complete implementation)
Apply autoscaling:
# Enable metrics server (if not already installed)
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
# Apply HPA
kubectl apply -f hpa.yaml
# Check HPA status
kubectl get hpa -n seldon
kubectl describe hpa churn-prediction-hpa -n seldon
# Load test to trigger scaling
kubectl run -it --rm load-generator --image=busybox --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O- http://churn-prediction-service/predict; done"
# Watch scaling
kubectl get hpa -n seldon --watch
Got: HPA monitors CPU/memory/custom metrics. Scales replicas up under load. Scales down after stabilization period. Min/max replica limits respected.
If fail: Verify metrics-server running (kubectl get deployment metrics-server -n kube-system), check pod resource requests defined (HPA requires requests), ensure custom metrics available if used, verify RBAC permissions for HPA controller, check stabilization windows not too restrictive.
Step 6: Do Canary Deployment Strategy
Gradually roll out new model versions with traffic shifting.
# canary-deployment.yaml
apiVersion: machinelearning.seldon.io/v1
kind: SeldonDeployment
metadata:
name: churn-classifier-canary
spec:
name: churn-classifier-canary
predictors:
# ... (see EXAMPLES.md for complete implementation)
Gradual rollout script:
# canary_rollout.py
import time
import subprocess
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# ... (see EXAMPLES.md for complete implementation)
Got: Canary deployment starts with 0% traffic. Gradual traffic shift occurs. Health checks pass at each stage. Rollback triggered if metrics degrade. Complete rollout after all stages pass.
If fail: Verify Seldon deployment has multiple predictors, check traffic percentages sum to 100, ensure canary image exists and is pullable, verify Prometheus metrics available for health checks, check rollback logic executes correctly, inspect pod logs for both versions.
Checks
- Model server responds to prediction requests
- REST/gRPC endpoints functional and documented
- Docker containers build and run
- Kubernetes deployment creates expected replicas
- Load balancer exposes external endpoint
- Health checks (liveness/readiness) pass
- Prometheus metrics exported and scraped
- Grafana dashboards display real-time metrics
- Autoscaling triggers under load
- A/B test splits traffic correctly
- Canary deployment rolls out gradually
- Rollback works when canary fails
Pitfalls
- Cold start latency: First request slow due to model loading - use readiness probes with adequate delay, implement model caching
- Memory leaks: Long-running servers accumulate memory - monitor memory usage, implement periodic restarts, profile code
- Dependency conflicts: Model dependencies incompatible with serving framework - use exact pinned versions, test in Docker before deployment
- Resource limits too low: Pods OOMKilled or CPU throttled - profile resource usage, set appropriate limits based on load testing
- Missing health checks: Kubernetes routes traffic to unhealthy pods - implement proper liveness/readiness probes
- No rollback strategy: Bad deployment without easy rollback - use canary deployments, keep previous version available
- Ignoring latency: Focusing only on accuracy, not inference speed - benchmark latency, optimize model/code, use batching
- Single replica: No high availability, downtime during deployments - use min 2 replicas, configure anti-affinity
- No monitoring: Issues not detected until customers complain - implement comprehensive metrics from day one
- GPU not utilized: GPU available but not used - set CUDA visible devices, verify GPU allocation in Kubernetes
See Also
register-ml-model- Register models before deploying themrun-ab-test-models- A/B testing between model versionsdeploy-to-kubernetes- General Kubernetes deployment patternsmonitor-ml-model-performance- Monitor model drift and degradationorchestrate-ml-pipeline- Automate model retraining and deployment
GitHub репозиторий
Похожие навыки
evaluating-llms-harness
ТестированиеЭтот навык Claude запускает lm-evaluation-harness для тестирования LLM на более чем 60 стандартизированных академических задачах, таких как MMLU и GSM8K. Он предназначен для разработчиков, чтобы сравнивать качество моделей, отслеживать прогресс обучения или сообщать академические результаты. Инструмент поддерживает различные бэкенды, включая модели HuggingFace и vLLM.
cloudflare-cron-triggers
ТестированиеЭтот навык предоставляет обширные знания по реализации Cloudflare Cron Triggers для планирования запуска Workers с помощью cron-выражений. Он охватывает настройку периодических задач, заданий технического обслуживания и автоматизированных рабочих процессов, а также решение распространенных проблем, таких как неверные cron-выражения и ошибки часовых поясов. Разработчики могут использовать его для настройки планировщиков обработчиков, тестирования cron-триггеров и интеграции с Workflows и Green Compute.
webapp-testing
ТестированиеЭтот навык Claude предоставляет инструментарий на базе Playwright для тестирования локальных веб-приложений с помощью Python-скриптов. Он позволяет проводить проверку фронтенда, отладку интерфейса, создание скриншотов и просмотр логов, одновременно управляя жизненным циклом сервера. Используйте его для задач автоматизации браузера, но запускайте скрипты напрямую, вместо чтения их исходного кода, чтобы избежать загрязнения контекста.
finishing-a-development-branch
ТестированиеЭтот навык помогает разработчикам завершать готовую работу, проверяя прохождение тестов и предлагая структурированные варианты интеграции. Он направляет рабочий процесс по слиянию, созданию пул-реквестов или очистке веток после завершения реализации. Используйте его, когда ваш код готов и протестирован, чтобы систематически завершать процесс разработки.
