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

Custom Docker Image with flow code baked in 🧑‍🍳

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

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

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

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

COPY requirements.txt .

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

COPY 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!


snow on steep mountain



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: