Andrey Gaev
07/21/2025, 6:01 PMMarvin
07/21/2025, 6:01 PMMarvin
07/21/2025, 6:02 PM└── clusters/
├── cluster-1/
│ ├── prefect-server/
│ │ └── helm-release.yaml
│ └── prefect-worker/
│ └── helm-release.yaml
└── cluster-2/
└── ...
2. Flow Deployment Strategy
In Prefect 3.x, you have two main approaches:
a. Direct Git-based Deployments:
python
from prefect import flow
@flow
def your_flow():
# flow logic here
pass
if __name__ == "__main__":
your_flow.from_source(
source="<https://gitlab.com/your-org/your-repo>",
entrypoint="path/to/flow.py:your_flow"
).deploy(
name="your-flow-deployment",
work_pool_name="your-k8s-pool"
)
b. Kubernetes Manifests for Flows:
Create HelmRelease manifests for your flows that Flux CD can manage:
yaml
apiVersion: <http://helm.toolkit.fluxcd.io/v2beta1|helm.toolkit.fluxcd.io/v2beta1>
kind: HelmRelease
metadata:
name: prefect-worker
namespace: prefect
spec:
interval: 5m
chart:
spec:
chart: prefect-worker
version: "2023.x.x" # Use appropriate version
sourceRef:
kind: HelmRepository
name: prefect
namespace: flux-system
values:
workPool:
name: "kubernetes-pool"
workQueueName: "default"
3. GitOps Workflow:
- Store your flow code, deployment configurations, and Kubernetes manifests in GitLab
- Use Flux CD to watch your GitLab repository for changes
- When changes are pushed to GitLab, Flux CD automatically synchronizes the changes to your Kubernetes clusters
4. Best Practices:
- Use separate work pools for each Kubernetes cluster
- Maintain clear separation between infrastructure code (Prefect Server/Worker deployments) and flow code
- Use GitLab CI/CD in conjunction with Flux CD for testing flows before deployment
- Consider using GitLab environments to manage different clusters (dev, staging, prod)
5. Important Considerations:
- Ensure your Kubernetes clusters have access to your GitLab repository
- Configure appropriate RBAC permissions for Flux CD
- Set up proper network connectivity between Prefect Server and Workers in each cluster
- Consider using GitLab's container registry for storing flow dependencies
Let me know if you need more specific details about any part of this setup, such as:
- Detailed Helm chart configurations
- Specific Flux CD setup instructions
- Worker configuration options
- Flow deployment patterns
Would you like me to elaborate on any of these aspects?Andrey Gaev
07/21/2025, 6:05 PMMarvin
07/21/2025, 7:42 PMMarvin
07/21/2025, 7:44 PMMarvin
07/21/2025, 7:46 PMbash
prefect work-pool create "docker-pool" \
--type docker \
--image-pull-policy "always"
The workflow would be:
1. Development Flow:
- Make changes to your flow code
- Push to GitLab
- GitLab CI builds new Docker image and pushes to Harbor
- GitLab CI updates the image tag in the Kubernetes manifests
- Flux CD detects the change and updates all clusters
- Prefect Worker picks up the new image for flow runs
2. Execution Flow:
- Prefect Worker runs in your Kubernetes cluster
- When a flow run is triggered, the worker pulls the latest image from Harbor
- Flow executes in the container with all dependencies
Key Benefits of this Approach:
- Version control for both code and dependencies
- Consistent environment across all clusters
- Easy rollbacks (just update the image tag)
- Automated deployment through Flux CD
- Harbor provides secure image storage and distribution
Would you like me to elaborate on any part of this setup? For example:
- Detailed worker configuration
- Flow scheduling options
- Specific Flux CD image automation settings
- Harbor integration detailsAndrey Gaev
07/21/2025, 7:48 PMMarvin
07/21/2025, 7:48 PMMarvin
07/21/2025, 7:48 PMAndrey Gaev
07/21/2025, 8:54 PMMarvin
07/21/2025, 8:54 PMMarvin
07/21/2025, 8:55 PMyaml
apiVersion: v1
kind: Secret
metadata:
name: harbor-registry-creds
namespace: prefect
type: <http://kubernetes.io/dockerconfigjson|kubernetes.io/dockerconfigjson>
data:
.dockerconfigjson: <base64-encoded-docker-config> # Create this using 'kubectl create secret docker-registry'
2. Create the Worker Deployment Manifest (e.g., prefect-worker.yaml
):
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: prefect-worker
namespace: prefect
spec:
replicas: 1
selector:
matchLabels:
app: prefect-worker
template:
metadata:
labels:
app: prefect-worker
spec:
imagePullSecrets:
- name: harbor-registry-creds # Reference to the Harbor credentials
containers:
- name: prefect-worker
image: prefecthq/prefect:2-python3.9
command:
- "prefect"
- "worker"
- "start"
- "--pool"
- "docker-pool" # Name of your work pool
env:
- name: PREFECT_API_URL
value: "<http://prefect-server:4200/api>" # Adjust based on your server URL
# Add these env vars to allow the worker to interact with Docker
- name: DOCKER_HOST
value: "unix:///var/run/docker.sock"
volumeMounts:
- name: docker-socket
mountPath: /var/run/docker.sock
# Mount Harbor credentials for Docker
- name: docker-config
mountPath: /root/.docker
volumes:
- name: docker-socket
hostPath:
path: /var/run/docker.sock
- name: docker-config
secret:
secretName: harbor-registry-creds
3. Create a Work Pool with Harbor Configuration:
bash
prefect work-pool create "docker-pool" \
--type docker \
--image-pull-policy "always" \
--base-job-template '{
"job_configuration": {
"image": "<http://harbor.your-domain.com/prefect-flows/your-flow:latest|harbor.your-domain.com/prefect-flows/your-flow:latest>",
"image_pull_policy": "always",
"image_registry": {
"registry_url": "<http://harbor.your-domain.com|harbor.your-domain.com>",
"username": "${HARBOR_USERNAME}",
"password": "${HARBOR_PASSWORD}"
}
}
}'
4. Create RBAC Permissions (if needed):
yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: prefect-worker
namespace: prefect
---
apiVersion: <http://rbac.authorization.k8s.io/v1|rbac.authorization.k8s.io/v1>
kind: Role
metadata:
name: prefect-worker
namespace: prefect
rules:
- apiGroups: [""]
resources: ["pods", "pods/log"]
verbs: ["get", "list", "watch", "create", "delete"]
---
apiVersion: <http://rbac.authorization.k8s.io/v1|rbac.authorization.k8s.io/v1>
kind: RoleBinding
metadata:
name: prefect-worker
namespace: prefect
subjects:
- kind: ServiceAccount
name: prefect-worker
namespace: prefect
roleRef:
kind: Role
name: prefect-worker
apiGroup: <http://rbac.authorization.k8s.io|rbac.authorization.k8s.io>
5. Create a ConfigMap for Worker Settings (optional but recommended):
yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: prefect-worker-config
namespace: prefect
data:
PREFECT_LOGGING_LEVEL: "INFO"
PREFECT_API_URL: "<http://prefect-server:4200/api>"
DOCKER_HOST: "unix:///var/run/docker.sock"
Key Points to Note:
1. The worker needs access to the Docker socket (/var/run/docker.sock
) to create containers
2. Harbor credentials are mounted into the worker pod
3. The imagePullSecrets
ensures Kubernetes can pull the worker image itself
4. The worker uses the mounted Docker socket to pull and run flow images
To deploy this setup with Flux CD:
1. Store these manifests in your GitLab repository under something like:
```
└── clusters/
├── base/
│ └── prefect/
│ ├── worker-deployment.yaml
│ ├── worker-rbac.yaml
│ └── worker-config.yaml
└── overlays/
├── cluster-1/Marvin
07/21/2025, 8:55 PM2. Use Flux CD's Kustomization resource to deploy:
```yaml
apiVersion: <http://kustomize.toolkit.fluxcd.io/v1|kustomize.toolkit.fluxcd.io/v1>
kind: Kustomization
metadata:
name: prefect-worker
namespace: flux-system
spec:
interval: 1h
path: ./clusters/base/prefect
prune: true
sourceRef:
kind: GitRepository
name: your-gitops-repo
targetNamespace: prefect
Would you like me to elaborate on any of these components? For example:
- How to create and manage the Harbor credentials
- How to configure different worker settings for different environments
- How to set up monitoring for the worker
- How to handle worker scaling