使用 Kubeflow Pipelines

Kubeflow Pipelines (KFP) 是一个基于 Docker 容器,用于构建和部署可移植、可扩展的机器学习(ML)工作流的平台。KFP SDK 允许你使用 Python 定义和操作流水线及组件。

前提条件

安装 KFP SDK

在你的命名空间中启动一个 Jupyter Notebook(或 Workbench),并安装 KFP SDK:

python -m pip install kfp

配置 KFP 以使用你的对象存储运行

在安装了外部 Object Storage 的 Kubeflow 时,请添加一个 KFP Launcher ConfigMap,用于配置当前命名空间或用户所使用的存储。你可以参考 Kubeflow 文档 https://www.kubeflow.org/docs/components/pipelines/operator-guides/configure-object-store/#s3-and-s3-compatible-provider 获取更多详情。如果未设置任何配置,pipeline 运行仍可能尝试访问默认的集群内对象存储端点,而这可能与你的部署不匹配。

从 Kubeflow 1.11.0 开始,默认捆绑的对象存储将更改为 SeaweedFS。为了让你的环境在不同版本之间保持可移植性,请将 Kubeflow Pipelines 配置为使用你自己的 Object Storage 端点,而不是依赖内置的存储服务名称。

下面是一个供你开始使用的简单示例:

apiVersion: v1
data:
  defaultPipelineRoot: s3://pipeline-artifacts
  providers: |-
    s3:
      default:
        endpoint: object-storage.storage.svc:80
        disableSSL: true
        region: us-east-2
        forcePathStyle: true
        credentials:
          fromEnv: false
          secretRef:
            secretName: mlpipeline-object-storage-artifact
            accessKeyKey: accesskey
            secretKeyKey: secretkey
kind: ConfigMap
metadata:
  name: kfp-launcher
  namespace: wy-testns

例如,在此 ConfigMap 中设置以下值,以指向你自己的 Object Storage:

defaultPipelineRoot:用于存储流水线中间数据和制品的位置
endpoint:Object Storage 服务端点。它不应以 httphttps 开头
disableSSL:是否禁用对该端点的 HTTPS 访问
region:Object Storage 提供商所需的地域
credentials:存储在所引用的 Secret 中的访问密钥和密钥

添加此 ConfigMap 后,新启动的 Kubeflow Pipeline 运行会自动读取该配置,并将流水线数据存储到已配置的 Object Storage 中。

快速开始示例

流水线是 ML 工作流的描述,包括工作流中的所有组件,以及它们如何以图的形式组合在一起。

下面是一个使用 KFP SDK 定义一个打印 "Hello, World!" 的流水线的简单示例。

from kfp import dsl
from kfp import compiler
from kfp.client import Client

@dsl.component
def say_hello(name: str) -> str:
    hello_text = f'Hello, {name}!'
    print(hello_text)
    return hello_text

@dsl.pipeline
def hello_pipeline(recipient: str) -> str:
    hello_task = say_hello(name=recipient)
    return hello_task.output


# Compile the pipeline to a YAML file
compiler.Compiler().compile(hello_pipeline, 'pipeline.yaml')

# Create a KFP client and submit the pipeline run
client = Client(host='<MY-KFP-ENDPOINT>')
run = client.create_run_from_pipeline_package(
    'pipeline.yaml',
    arguments={
        'recipient': 'World',
    },
)

有关如何定义和运行流水线的更多详情,请参阅官方 KFP 文档:https://www.kubeflow.org/docs/components/pipelines/user-guides/

在 UI 中管理流水线

你也可以直接从 Kubeflow 监控面板中管理流水线、实验和运行。

访问 Pipelines 监控面板

  1. 登录 Kubeflow 中央监控面板。
  2. 在侧边栏菜单中点击 Pipelines

上传流水线

如果你已将流水线编译为一个 YAML 文件(例如上面示例中的 pipeline.yaml),则可以上传它:

  1. 点击 Pipelines -> Upload Pipeline
  2. Upload a file:选择你的 pipeline.yaml
  3. Pipeline Name:为其指定一个名称(例如 Hello World Pipeline)。
  4. 点击 Create

创建运行

要执行你刚上传的流水线:

  1. 点击流水线名称以打开其详情。
  2. 点击 Create Run
  3. Run Name:输入一个具有描述性的名称。
  4. Experiment:选择一个现有实验或创建一个新的实验。实验有助于对相关运行进行分组。
  5. Run Parameters:为任何流水线参数输入值(例如,recipient: World)。
  6. 点击 Start

查看运行详情

运行启动后,你将被重定向到 Run Details 页面。

  • Graph:可视化你的流水线步骤(组件)及其状态(Running、Succeeded、Failed)。
  • Logs:在图中点击某个特定步骤,以在侧边面板中查看其容器日志。这对于调试至关重要。
  • Inputs/Outputs:查看在步骤之间传递或作为最终输出生成的制品。
  • Visualizations:如果你的流水线生成指标或图表,它们将显示在 Run OutputVisualizations 选项卡中。

定期运行

你可以安排流水线按特定间隔自动运行:

  1. Pipelines 列表中找到你的流水线。
  2. 点击 Create Run,但将运行类型选择为 Recurring Run(或导航到 Experiments (KFP) -> Create Recurring Run)。
  3. Trigger:设置计划(例如,Periodic、Cron)。
  4. Parameters:配置每次计划执行时将使用的输入。
  5. 点击 Start