KFP Kubernetes Platform Configuration

2.16.0 · active · verified Fri Apr 17

kfp-kubernetes is a Python library providing Kubernetes platform configuration utilities and generated protobufs for Kubeflow Pipelines (KFP). It enables users to customize Kubernetes resources like volumes, secrets, node selectors, and tolerations for KFP components. This package is part of the KFP SDK ecosystem (version 2.16.0) and is typically released in sync with `kfp`, `kfp-server-api`, and `kfp-pipeline-spec` with frequent updates.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to define a KFP pipeline and use `kfp_kubernetes.common` functions to add Kubernetes-specific configurations like a volume, node selector, and toleration to a component. This pipeline requires a KFP SDK v2 compatible environment for execution and an existing PVC named 'my-data-pvc' on the Kubernetes cluster.

import kfp
from kfp import dsl
from kfp_kubernetes import common

# Define a simple KFP component
@dsl.component
def hello_world_op(name: str) -> str:
    import os
    print(f"Hello, {name} from pod {os.environ.get('KUBERNETES_POD_NAME', 'unknown')}!")
    return f"Hello, {name}!"

# Create a pipeline
@dsl.pipeline(name="kubernetes-config-pipeline")
def kubernetes_config_pipeline():
    task = hello_world_op(name="World")

    # Add a Kubernetes volume to the component's pod
    # This assumes an existing PVC named 'my-data-pvc' on your cluster
    common.add_volume_to_component(
        task,
        volume_name="my-data-volume",
        mount_path="/mnt/data",
        existing_pvc_name="my-data-pvc"
    )

    # Add a node selector to schedule the task on a specific node
    common.add_node_selector_to_component(task, "kubernetes.io/hostname", "my-worker-node-label")

    # Add a toleration to allow scheduling on tainted nodes
    common.add_toleration_to_component(task, "key", "value", "Equal", "NoSchedule")

# Compile the pipeline (requires kfp to be installed)
if __name__ == "__main__":
    try:
        kfp.compiler.Compiler().compile(kubernetes_config_pipeline, "kubernetes_config_pipeline.yaml")
        print("Pipeline compiled successfully to kubernetes_config_pipeline.yaml")
    except Exception as e:
        print(f"Error compiling pipeline: {e}")

view raw JSON →