使用 Kubeflow Pipelines

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

前提条件

安装 KFP SDK

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

python -m pip install kfp

配置 KFP 以使用你的 Object Storage 运行

当你使用外部 Object Storage 安装 Kubeflow 时,需要添加一个 KFP Launcher ConfigMap,以配置当前 namespace 或 user 使用的存储。有关更多详情,请查看 Kubeflow 文档 https://www.kubeflow.org/docs/components/pipelines/operator-guides/configure-object-store/#s3-and-s3-compatible-provider。如果未设置任何配置,pipeline 运行可能仍会尝试访问默认的集群内 Object Storage 端点,这可能与你的部署不匹配。

从 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: 用于存储 pipeline 中间数据和制品的位置
endpoint: Object Storage 服务端点。它不应以 httphttps 开头
disableSSL: 是否禁用到该端点的 HTTPS 访问
region: 你的 Object Storage 提供商所需的地域
credentials: 存储在引用的 Secret 中的访问密钥和 secret key

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

快速开始示例

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

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

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',
    },
)

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

在 UI 中管理 Pipelines

你也可以直接从 Kubeflow 监控面板管理 pipeline、experiment 和 run。

访问 Pipelines 监控面板

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

上传一个 Pipeline

如果你已经将 pipeline 编译为 YAML 文件(例如上面示例中的 pipeline.yaml),就可以将其上传:

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

创建运行

要执行你刚刚上传的 pipeline:

  1. 单击 pipeline 名称以打开其详细信息。
  2. 单击 Create Run
  3. Run Name:输入一个描述性名称。
  4. Experiment:选择一个已有的 experiment 或创建一个新的。Experiment 有助于将相关的 run 分组。
  5. Run Parameters:为任何 pipeline 参数输入值(例如 recipient: World)。
  6. 单击 Start

查看运行详情

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

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

周期性运行

你可以安排 pipeline 在特定时间间隔自动运行:

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