The Prefect Blog
Published in

The Prefect Blog

How to use Kubernetes with Prefect: Part 3

Store your flow code in your Docker Image

snowy mountain at sunstet
Source: pixabay.com

Custom Docker Image with flow code baked in 🧑‍🍳

from prefect import flow, get_run_logger
from platform import node, platform


@flow
def check():
logger = get_run_logger()
logger.info(f"Network: {node()}. ✅")
logger.info(f"Instance: {platform()}. ✅")


if __name__ == "__main__":
check()
from prefect.infrastructure import KubernetesJob

k8s_job = KubernetesJob(
image="discdiver/prefect:py3.10-baked",
image_pull_policy="Always",
)

k8s_job.save("k8s-flow", overwrite=True)
FROM prefecthq/prefect:2-python3.10

COPY requirements.txt .

RUN pip install -r requirements.txt --trusted-host pypi.python.org --no-cache-dir

COPY flows.py flows/

Build and push Docker image

Jeff’s Dockerhub image showing tag name

Create K8s block

UI screenshot of block

Build deployment

screenshot showing deployment name from the UI

Start agent

Run it!

Wrap

snow on steep mountain
Source:pixabay.com

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jeff Hale

I write about data science. Join my Data Awesome mailing list to stay on top of the latest data tools and tips: https://dataawesome.com