Prefect Kubernetes Integration

0.7.7 · active · verified Tue Apr 14

Prefect-kubernetes provides integrations for running Prefect flows on Kubernetes. It enables Prefect 2.x deployments to provision Kubernetes Jobs for flow runs using `KubernetesDeployment` and manage them with `KubernetesWorker`. The current version is 0.7.7, and it follows the Prefect core library's release cadence, with integrations typically updated alongside major Prefect releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a Prefect flow and then create a `KubernetesDeployment` object to run it on a Kubernetes cluster. To make this runnable: 1. Ensure a Prefect server (e.g., `prefect server start`) and a Kubernetes cluster are running. 2. Configure your `kubectl` context to point to your cluster. 3. Set your `PREFECT_API_URL` environment variable to point to your Prefect server. 4. Create a Kubernetes work pool: `prefect work-pool create kubernetes-work-pool --type kubernetes`. 5. Save the provided Python code as `quickstart_kubernetes.py` and run it: `python quickstart_kubernetes.py`. This will register the deployment with your Prefect server. 6. Start a Kubernetes worker: `prefect worker start --pool kubernetes-work-pool`. 7. Create a flow run from the Prefect UI or CLI: `prefect deployment run 'hello-kubernetes-flow-deployment'`.

import os
from prefect import flow
from prefect_kubernetes.deployments import KubernetesDeployment

# 1. Define a simple Prefect Flow
@flow(log_prints=True)
def hello_kubernetes_flow(name: str = "world"):
    import platform
    print(f"Hello, {name} from Kubernetes! Running on {platform.node()}")

# 2. Create a Kubernetes Deployment definition
# This deployment will tell Prefect how to run `hello_kubernetes_flow` on Kubernetes.
# It requires a work pool named "kubernetes-work-pool" to exist in your Prefect server.
# Create it with: `prefect work-pool create kubernetes-work-pool --type kubernetes`

# IMPORTANT: The 'image' specified here must contain your flow code and Prefect installed.
# For a real scenario, you would build a custom Docker image for your flow.
# Example custom image setup:
#   Dockerfile:
#     FROM prefecthq/prefect:2-python3.10
#     COPY quickstart_kubernetes.py /app/quickstart_kubernetes.py
#     WORKDIR /app
#   Build & Push:
#     docker build -t your-registry/your-flow-image:latest .
#     docker push your-registry/your-flow-image:latest
# Then use `your-registry/your-flow-image:latest` below.

try:
    deployment = KubernetesDeployment(
        name="hello-kubernetes-flow-deployment",
        description="Runs a simple 'Hello, Kubernetes!' flow on Kubernetes.",
        flow_name=hello_kubernetes_flow.name,
        work_pool_name="kubernetes-work-pool", # This work pool MUST exist in your Prefect server
        image="prefecthq/prefect:2-python3.10", # Base Prefect image; replace for custom flows
        entrypoint="quickstart_kubernetes.py:hello_kubernetes_flow", # Assumes this code is saved as quickstart_kubernetes.py
        # You can also pass job_variables or infra_overrides for custom Kubernetes Job spec
        # job_variables={"MY_ENV_VAR": "my_value"},
        # infra_overrides={
        #     "job_template": {
        #         "spec": {"template": {"spec": {"nodeSelector": {"kubernetes.io/hostname": "your-node"}}}}
        #     }
        # }
    )
    deployment_id = deployment.apply()
    print(f"Deployment '{deployment.name}' created/updated with ID: {deployment_id}")

except Exception as e:
    print(f"Failed to create Kubernetes Deployment: {e}")
    print("Ensure PREFECT_API_URL is set and Prefect server is running.")

# To start the Kubernetes Worker that will pick up runs for this deployment:
# prefect worker start --pool kubernetes-work-pool

view raw JSON →