工作区和访问控制

在 Alauda AI 中,MLflow 是多租户的:每个 workspace 都是一个 Kubernetes namespace,对 workspace 中的 experiments、runs、datasets 和 registered models 的访问通过 Kubernetes RBAC 授权。本指南将展示如何将一个 namespace 暴露为 workspace,并授予用户对其的访问权限。

关于底层模型,请参见 简介 → 多租户模型。

将 namespace 暴露为 workspace

当一个 namespace 带有 MLflow 配置所选定的标签时,它就会成为一个 MLflow workspace(默认情况下为 mlflow-enabled=true)。只有匹配的 namespaces 才会显示为 workspaces。

apiVersion: v1
kind: Namespace
metadata:
  name: team-a
  labels:
    mlflow-enabled: "true"
  annotations:
    mlflow.kubeflow.org/workspace-description: "Team A MLflow workspace"

应用该标签,或者为现有 namespace 添加标签:

kubectl label namespace team-a mlflow-enabled=true
INFO

跟踪服务器的 default workspace namespace 必须在服务器首次启动之前存在并已打上标签,否则它将无法启动(请参见 安装 → 前提条件)。

授予用户访问权限

MLflow 会使用 mlflow.kubeflow.org API group,根据调用方在目标 namespace 中的 Kubernetes 权限对每个请求进行授权。请在 workspace namespace 中使用 Role 和 RoleBinding 授予访问权限:

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: mlflow-manager
  namespace: team-a
rules:
  - apiGroups: ["mlflow.kubeflow.org"]
    resources: ["experiments", "datasets", "registeredmodels"]
    verbs: ["get", "list", "create", "update", "delete"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: mlflow-manager
  namespace: team-a
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: mlflow-manager
subjects:
  - kind: Group
    name: team-a-mlflow-users
    apiGroup: rbac.authorization.k8s.io

binding 中的 group 或 user name 必须与 OAuth proxy 转发的 token 中的 identity claim 匹配(平台的 OIDC groups/user)。

从客户端选择 workspace

将 MLflow tracking URI 设置为集群内的 Service(或平台 route),并选择 workspace:

import mlflow

mlflow.set_tracking_uri("http://mlflow-tracking-server.kubeflow:5000")  # in-cluster Service, via the OAuth proxy
mlflow.set_workspace("team-a")

with mlflow.start_run():
    mlflow.log_param("tenant", "team-a")

对于原始 HTTP 客户端,请传递 workspace header:

curl \
  -H "X-MLFLOW-WORKSPACE: team-a" \
  "https://<platform>/clusters/<cluster-name>/mlflow/api/2.0/mlflow/experiments/search"

你只能使用你的账户有访问权限的 workspace。关于完整的认证细节——即在无需浏览器的情况下获取 identity token 并连接 SDK——请参见 使用带有 Authentication 和 RBAC 的 MLflow Python SDK。

故障排查

  • workspace 不可见。 验证其 namespace 是否与配置的 workspaceLabelSelector 匹配(默认值为 mlflow-enabled=true)。
  • 403 PERMISSION_DENIED。 该账户无权访问 workspace namespace。请在该 namespace 中为用户或 group 添加一个 RoleBinding。
  • run 显示了错误的 owner 或 workspace。 owner 是经过身份验证的 identity;workspace 是 set_workspace() / MLFLOW_WORKSPACE 所选择的内容(否则为服务器默认值)。请同时检查这两项。