跳到主要内容

Kubernetes Helm 部署

MLflow Helm chart 提供了一种在任何 Kubernetes 集群上部署 MLflow 的生产就绪方式。

该 Chart 附带以下功能:

  • MLflow 跟踪服务器,具有可配置的 CLI 选项
  • TLS 支持,通过现有的 Kubernetes Secret 实现
  • 持久化存储,使用 PersistentVolumeClaim 用于 SQLite 或基于文件的工件存储
  • Ingress,用于外部访问
  • Prometheus 指标,以及用于 Prometheus Operator 的可选 ServiceMonitor
  • NetworkPolicy,限制入站和出站流量至所需的端口
  • RBAC,具有命名空间范围和集群范围的规则
  • 垃圾回收,通过运行 mlflow gc 的可选 CronJob 实现

前提条件

  • Kubernetes 1.23+
  • Helm 3.8+
  • 配置为指向您集群的 kubectl
  • 下载 MLflow Helm chart 到您当前的工作目录。

快速入门

最简单的路径——不需要外部数据库或对象存储。MLflow 将元数据存储在 SQLite 中,将工件存储在 PersistentVolumeClaim 中。

1. 安装 Chart

bash
helm install mlflow ./charts \
--namespace mlflow \
--create-namespace \
--set storage.enabled=true \
--set mlflow.backendStoreUri="sqlite:////mlflow/mlflow.db" \
--set mlflow.artifactsDestination="/mlflow/artifacts"

2. 等待 Pod 就绪

bash
kubectl get pods -n mlflow -w

您应该看到

text
NAME READY STATUS RESTARTS AGE
mlflow-mlflow-xxxxxxxxxx-xxxxx 1/1 Running 0 30s

3. 访问 UI

bash
kubectl port-forward -n mlflow svc/mlflow-mlflow 5000:5000

在浏览器中打开 https://:5000

SQLite 不适用于生产环境

SQLite 和本地文件存储不适用于多用户或高并发部署。对于生产环境,请使用 PostgreSQL 后端和云对象存储。请参阅下方的生产部署

生产部署

后端存储 (PostgreSQL)

将数据库 URI 存储在 Kubernetes Secret 中,以避免在 values 文件中暴露凭据

bash
kubectl create secret generic mlflow-db-secret \
--namespace mlflow \
--from-literal=uri="postgresql://user:password@postgres:5432/mlflow"

在您的 values 文件中引用该 Secret

my-values.yaml
yaml
mlflow:
backendStoreUriFrom:
secretKeyRef:
name: mlflow-db-secret
key: uri

工件存储 (S3)

bash
kubectl create secret generic s3-credentials \
--namespace mlflow \
--from-literal=access-key-id=<key> \
--from-literal=secret-access-key=<secret>
my-values.yaml
yaml
mlflow:
artifactsDestination: "s3://my-bucket/mlflow"

env:
- name: AWS_ACCESS_KEY_ID
valueFrom:
secretKeyRef:
name: s3-credentials
key: access-key-id
- name: AWS_SECRET_ACCESS_KEY
valueFrom:
secretKeyRef:
name: s3-credentials
key: secret-access-key

对于 GCS 或 Azure Blob Storage,请提供相应的凭据和 URI(gs://...wasbs://...)。

TLS

从您的证书和密钥创建 TLS Secret

bash
kubectl create secret tls mlflow-tls \
--namespace mlflow \
--cert=tls.crt \
--key=tls.key

在您的 values 文件中启用 TLS

my-values.yaml
yaml
tls:
enabled: true
secretName: mlflow-tls

Ingress

MLflow 的主机验证中间件默认只允许 localhost 和私有 IP 主机。通过带有公共主机名的 Ingress 公开 MLflow 时,请设置 allowed_hosts 以匹配该主机名——否则请求将被 HTTP 403 拒绝。

my-values.yaml
yaml
server:
value_options:
allowed_hosts: "mlflow.example.com"

ingress:
enabled: true
className: nginx
hosts:
- host: mlflow.example.com
paths:
- path: /
pathType: Prefix
tls:
- secretName: mlflow-tls
hosts:
- mlflow.example.com

使用您的 values 进行部署

bash
helm install mlflow ./charts \
--namespace mlflow \
--create-namespace \
-f my-values.yaml

工作空间 (Workspaces)

工作空间在共享服务器上跨团队对实验、注册模型、提示词 (prompts) 和工件进行分区。启用工作空间需要 SQL 数据库后端(PostgreSQL、MySQL 或 MSSQL,不能使用基于文件的存储)和一个 defaultArtifactRoot

my-values.yaml
yaml
server:
flag_options:
- enable_workspaces

当启用 garbageCollection 时,请设置 allWorkspaces: true,以便 GC 作业清理所有工作空间中已软删除的资源,而不仅仅是默认工作空间。

my-values.yaml
yaml
garbageCollection:
enabled: true
schedule: "0 2 * * 0"
allWorkspaces: true

有关工作空间创建和客户端配置,请参阅工作空间入门

基础 HTTP 认证

MLflow 内置的 basic-auth 插件需要一个 CSRF 密钥。将其存储在 Kubernetes Secret 中

bash
kubectl create secret generic mlflow-auth-secret \
--namespace mlflow \
--from-literal=secret-key="$(openssl rand -hex 32)"

在您的 values 文件中引用该 Secret 并启用插件

my-values.yaml
yaml
server:
value_options:
app_name: "basic-auth"

env:
- name: MLFLOW_FLASK_SERVER_SECRET_KEY
valueFrom:
secretKeyRef:
name: mlflow-auth-secret
key: secret-key

有关用户和权限管理,请参阅基础 HTTP 认证

配置参考

持久化本地存储

为 SQLite 或本地文件工件存储启用 PersistentVolumeClaim

yaml
storage:
enabled: true
size: 10Gi
storageClassName: "gp2" # match your cluster (e.g. "standard" for kind/minikube)

mlflow:
backendStoreUri: "sqlite:////mlflow/mlflow.db"
artifactsDestination: "/mlflow/artifacts"

Prometheus 指标

yaml
metrics:
enabled: true
path: /metrics

serviceMonitor:
enabled: true # requires Prometheus Operator

垃圾回收

定期删除软删除的运行记录、实验及其工件

yaml
garbageCollection:
enabled: true
schedule: "0 2 * * 0" # weekly at 2 AM on Sunday
olderThan: "30d" # only remove resources soft-deleted for 30+ days

资源限制

yaml
resources:
requests:
cpu: 500m
memory: 512Mi
limits:
cpu: 2000m
memory: 2Gi

服务器选项

通过 server.value_options (键/值) 或 server.flag_options (裸标志) 传递任何 mlflow server CLI 标志

yaml
server:
value_options:
host: "0.0.0.0"
port: 5000
workers: 4

flag_options: []
# - no_serve_artifacts

完整示例:PostgreSQL + S3

该仓库提供了一个面向生产的示例,位于 charts/example-mlflow-charts.yaml

bash
helm install mlflow ./charts \
--namespace mlflow \
--create-namespace \
-f charts/example-mlflow-charts.yaml

升级

bash
helm upgrade mlflow ./charts \
--namespace mlflow \
-f my-values.yaml

卸载

bash
helm uninstall mlflow --namespace mlflow
PersistentVolumeClaims 不会被删除

helm uninstall 不会删除 PersistentVolumeClaims。如果 storage.enabled=true,请在卸载后手动删除 PVC。

bash
kubectl delete pvc -n mlflow --all

连接 MLflow 客户端

一旦服务器运行(通过端口转发或 Ingress),请将 MLflow 客户端指向它

python
import mlflow

mlflow.set_tracking_uri("https://:5000") # or your Ingress hostname

with mlflow.start_run():
mlflow.log_param("learning_rate", 0.01)
mlflow.log_metric("accuracy", 0.95)