多用户隔离
Kubeflow 流水线中的多用户隔离工作原理
Kubeflow 流水线的多用户隔离是 Kubeflow 整体配置文件和命名空间隔离策略的一部分。
资源如何隔离?
Kubeflow 流水线使用由 Kubeflow Profiles 管理的 Kubernetes 命名空间来隔离资源。其他用户未经许可无法查看您的 Profile/Namespace 中的资源,因为 Kubeflow 流水线 API 服务器会拒绝当前用户无权访问的命名空间的请求。
“实验”直接属于命名空间,运行和周期性运行属于其父实验的命名空间。
“流水线运行”在用户命名空间中执行,以便用户可以利用 Kubernetes 命名空间隔离。例如,他们可以在不同的命名空间中为不同的服务配置不同的 secrets。
警告
Kubeflow 对配置文件隔离不作任何硬性安全保证。用户配置文件除了 Kubernetes 命名空间提供的隔离之外,没有额外的隔离。
使用 UI 时
当您从 Kubeflow Dashboard 访问 Kubeflow 流水线 UI 时,它仅显示您所选命名空间中的“实验”、“运行”和“周期性运行”。类似地,当您从 UI 创建资源时,它们也属于您选择的命名空间。有关更多详细信息,请参阅配置文件和命名空间。
警告
当前,流水线定义未隔离,并且在所有命名空间中共享,更多详细信息请参阅当前限制。使用 SDK 时
如何将流水线 SDK 连接到 Kubeflow 流水线将取决于您拥有的 Kubeflow 部署的**类型**以及您**运行代码的位置**。
以下 Python 代码将从完整 Kubeflow 集群内的 Pod 中创建一个实验(以及相关的运行)。
import kfp
# the namespace in which you deployed Kubeflow Pipelines
kubeflow_namespace = "kubeflow"
# the namespace of your pipelines user (where the pipeline will be executed)
user_namespace = "jane-doe"
# the KF_PIPELINES_SA_TOKEN_PATH environment variable is used when no `path` is set
# the default KF_PIPELINES_SA_TOKEN_PATH is /var/run/secrets/kubeflow/pipelines/token
credentials = kfp.auth.ServiceAccountTokenVolumeCredentials(path=None)
# create a client
client = kfp.Client(host=f"http://ml-pipeline-ui.{kubeflow_namespace}", credentials=credentials)
# create an experiment
client.create_experiment(name="<YOUR_EXPERIMENT_ID>", namespace=user_namespace)
print(client.list_experiments(namespace=user_namespace))
# create a pipeline run
client.run_pipeline(
experiment_id="<YOUR_EXPERIMENT_ID>", # the experiment determines the namespace
job_name="<YOUR_RUN_NAME>",
pipeline_id="<YOUR_PIPELINE_ID>" # the pipeline definition to run
)
print(client.list_runs(experiment_id="<YOUR_EXPERIMENT_ID>"))
print(client.list_runs(namespace=user_namespace))
提示
- 要为流水线 SDK 命令设置默认命名空间,请使用
kfp.Client().set_user_namespace()
方法,该方法将您的用户命名空间存储在$HOME/.config/kfp/context.json
处的配置文件中。 - kfp.Client() 的详细文档可在 Kubeflow 流水线 SDK 参考中找到。
使用 REST API 时
调用 Kubeflow 流水线 REST API 时,实验 API 需要命名空间参数。
命名空间由一个“资源引用”指定,其 type
为 NAMESPACE
,key.id
等于命名空间名称。
以下代码使用 生成的 Python API 客户端创建一个实验和流水线运行。
import kfp
from kfp_server_api import *
# the namespace in which you deployed Kubeflow Pipelines
kubeflow_namespace = "kubeflow"
# the namespace of your pipelines user (where the pipeline will be executed)
user_namespace = "jane-doe"
# the KF_PIPELINES_SA_TOKEN_PATH environment variable is used when no `path` is set
# the default KF_PIPELINES_SA_TOKEN_PATH is /var/run/secrets/kubeflow/pipelines/token
credentials = kfp.auth.ServiceAccountTokenVolumeCredentials(path=None)
# create a client
client = kfp.Client(host=f"http://ml-pipeline-ui.{kubeflow_namespace}", credentials=credentials)
# create an experiment
experiment: ApiExperiment = client._experiment_api.create_experiment(
body=ApiExperiment(
name="<YOUR_EXPERIMENT_ID>",
resource_references=[
ApiResourceReference(
key=ApiResourceKey(
id=user_namespace,
type=ApiResourceType.NAMESPACE,
),
relationship=ApiRelationship.OWNER,
)
],
)
)
print("-------- BEGIN: EXPERIMENT --------")
print(experiment)
print("-------- END: EXPERIMENT ----------")
# get the experiment by name (only necessary if you comment out the `create_experiment()` call)
# experiment: ApiExperiment = client.get_experiment(
# experiment_name="<YOUR_EXPERIMENT_ID>",
# namespace=user_namespace
# )
# create a pipeline run
run: ApiRunDetail = client._run_api.create_run(
body=ApiRun(
name="<YOUR_RUN_NAME>",
pipeline_spec=ApiPipelineSpec(
# replace <YOUR_PIPELINE_ID> with the UID of a pipeline definition you have previously uploaded
pipeline_id="<YOUR_PIPELINE_ID>",
),
resource_references=[ApiResourceReference(
key=ApiResourceKey(
id=experiment.id,
type=ApiResourceType.EXPERIMENT,
),
relationship=ApiRelationship.OWNER,
)
],
)
)
print("-------- BEGIN: RUN --------")
print(run)
print("-------- END: RUN ----------")
# view the pipeline run
runs: ApiListRunsResponse = client._run_api.list_runs(
resource_reference_key_type=ApiResourceType.EXPERIMENT,
resource_reference_key_id=experiment.id,
)
print("-------- BEGIN: RUNS --------")
print(runs)
print("-------- END: RUNS ----------")
当前限制
未隔离的资源
以下资源目前不支持隔离,并且在没有访问控制的情况下共享
- 流水线 (流水线定义)。
- Machine Learning Metadata (MLMD) 中的 Artifacts、Executions 和其他元数据实体。
- Minio Artifact 存储,其中包含流水线运行的输入/输出 Artifacts。
上次修改日期:2025 年 2 月 4 日:移除“Concepts”/“Multi-Tenancy”部分 (#3977) (8d8f8f1)