thiago
11/28/2025, 2:04 PMMarvin
11/28/2025, 2:04 PMMarvin
11/28/2025, 2:08 PMmicrok8s status --wait-ready
- mkdir -p ~/.kube && microk8s config > ~/.kube/config
- Important: MicroK8s kubeconfig usually uses 127.0.0.1 as the API server. The worker uses network_mode: host so that 127.0.0.1 resolves to the host (Linux only). If you prefer not to use host networking, update your kubeconfig to use your host IP instead of 127.0.0.1 and remove network_mode: host below.
docker-compose.yaml:
version: "3.9"
services:
prefect-server:
image: prefecthq/prefect:3-latest
container_name: prefect-server
command: >
bash -lc "prefect server start --host 0.0.0.0 --ui"
ports:
- "4200:4200" # API
- "4433:4433" # UI
environment:
# These help the UI link to the API correctly from your browser
- PREFECT_UI_URL=<http://localhost:4433>
- PREFECT_API_URL=<http://localhost:4200/api>
volumes:
- prefect_data:/root/.prefect
restart: unless-stopped
create-pool:
image: prefecthq/prefect:3-latest
depends_on:
- prefect-server
environment:
# Talk to the server via the Docker network
- PREFECT_API_URL=<http://prefect-server:4200/api>
command: >
bash -lc "prefect work-pool create my-k8s --type kubernetes --overwrite"
restart: "no"
k8s-worker:
image: prefecthq/prefect:3-latest
# The worker needs to reach the MicroK8s API at 127.0.0.1; host networking is the simplest approach on Linux
network_mode: host
depends_on:
- create-pool
environment:
# The worker talks to the Prefect API via the host-published port
- PREFECT_API_URL=<http://localhost:4200/api>
# Point kubernetes client to the mounted kubeconfig
- KUBECONFIG=/root/.kube/config
volumes:
# Mount your host kubeconfig (created from microk8s config)
- ~/.kube/config:/root/.kube/config:ro
command: >
bash -lc "pip install --no-cache-dir 'prefect[kubernetes]' && prefect worker start -p my-k8s"
restart: unless-stopped
volumes:
prefect_data:
How to use:
1) Start everything:
docker compose up -d
2) Open the UI at http://localhost:4433 and confirm:
- Work Pools → “my-k8s” exists (created by the create-pool init container)
- Workers → a worker is online for “my-k8s”
3) Deploy flows to the pool:
- Use flow.from_source(...).deploy(work_pool_name="my-k8s", ...)
- Ensure the image for your deployments is pullable by MicroK8s nodes (default Prefect images are fine for a quick start)
Notes and tips:
- If you don’t want to use host networking for the worker, edit your kubeconfig to replace 127.0.0.1 with your host’s IP (or configure MicroK8s to advertise a non-loopback address), then remove network_mode: host and set PREFECT_API_URL=<http://prefect-server:4200/api> for the worker.
- The compose file installs prefect[kubernetes] at container start. For faster restarts, consider building a tiny custom image that already includes the kubernetes extra and use that image in the worker.