Dominik
07/24/2024, 11:40 AMMarvin
07/24/2024, 11:41 AMimage
field in your deployment YAML. The correct URL format for a Docker Hub image would be <http://docker.io/<username>/<image>:<tag|docker.io/<username>/<image>:<tag>>
. For example, if your Docker Hub username is johndoe
and your image is my-image
with the tag latest
, the URL would be:
*docker.io/johndoe/my-image:latest*
References:
- Work Pools Tutorial: Work Pools
- Related GitHub Issue: Deploy with Prefect.yamlDominik
07/24/2024, 11:51 AMMarvin
07/24/2024, 11:51 AMjob_variables
in your deployment YAML which includes the environment variables required by your flows.
Here is an example:
yaml
deployments:
- name: my-deployment
entrypoint: my_flow:main
work_pool:
name: my-work-pool
job_variables:
env:
MY_ENV_VAR_1: "value1"
MY_ENV_VAR_2: "value2"
Environment variables set this way will be passed to the Kubernetes pods running your flow.
References:
- Overriding Job Variables Guide: Overriding Job VariablesDominik
07/24/2024, 11:52 AMMarvin
07/24/2024, 11:52 AMyaml
deployments:
- name: my-deployment
entrypoint: my_flow:main
work_pool:
name: my-work-pool
job_variables:
env:
MY_ENV_VAR_1: "value1"
MY_ENV_VAR_2: "value2"
### References:
- Overriding Job Variables Guide: Overriding Job Variables
Automatic inclusion of environment variables from the worker isn't currently supported, so you'll need to define them manually in your deployments.Dominik
07/24/2024, 11:53 AMMarvin
07/24/2024, 11:53 AMPREFECT_KUBERNETES_WORKER_STORE_PREFECT_API_IN_SECRET
environment variable or customize the Kubernetes job manifest.
### Using Kubernetes Secrets
1. Set the environment variable to store the Prefect API key in a Kubernetes secret:
sh
export PREFECT_KUBERNETES_WORKER_STORE_PREFECT_API_IN_SECRET="true"
prefect worker start --pool 'my-work-pool' --type kubernetes
2. Ensure your worker has permission to create secrets in the relevant namespaces.
3. Customize the Kubernetes job manifest to include the secrets as environment variables.
Here’s an example of how you might configure your worker and reference the secrets:
### Example: Custom Kubernetes Job Manifest
yaml
apiVersion: batch/v1
kind: Job
metadata:
labels: "{{ labels }}"
namespace: "{{ namespace }}"
generateName: "{{ name }}-"
spec:
ttlSecondsAfterFinished: "{{ finished_job_ttl }}"
template:
spec:
parallelism: 1
completions: 1
restartPolicy: Never
serviceAccountName: "{{ service_account_name }}"
containers:
- name: flow-container
image: "{{ image }}"
envFrom:
- secretRef:
name: my-k8s-secret
env:
- name: MY_SECRET_ENV_VAR
valueFrom:
secretKeyRef:
name: my-k8s-secret
key: my-secret-key
In this configuration:
- envFrom
references all keys in my-k8s-secret
as environment variables.
- env
specifies individual environment variables from the secret.
### References:
- Kubernetes Worker Documentation: Prefect Kubernetes Worker
- Overriding Job Variables Guide: Overriding Job Variables
Ensure your Kubernetes manifests and secrets are configured correctly to securely inject necessary environment variables into your flow jobs.Dominik
07/24/2024, 11:57 AMMarvin
07/24/2024, 11:57 AMenvFrom
isn't working as expected, here’s a breakdown of what you should check:
1. Ensure the Secrets Exist: Make sure the Kubernetes secrets exist in the namespace and are correctly referenced by name.
2. Correct Reference in Manifests: Use the envFrom
field in your job manifest to include the secrets.
### Example Configuration
Here's an example of how to reference a Kubernetes secret using envFrom
and env
in your Kubernetes job manifest:
yaml
apiVersion: batch/v1
kind: Job
metadata:
labels: "{{ labels }}"
namespace: "{{ namespace }}"
generateName: "{{ name }}-"
spec:
ttlSecondsAfterFinished: "{{ finished_job_ttl }}"
template:
spec:
parallelism: 1
completions: 1
restartPolicy: Never
serviceAccountName: "{{ service_account_name }}"
containers:
- name: flow-container
image: "{{ image }}"
envFrom:
- secretRef:
name: my-k8s-secret # Ensure this secret exists in the namespace
env:
- name: MY_SECRET_ENV_VAR
valueFrom:
secretKeyRef:
name: my-k8s-secret # Ensure this secret exists in the namespace
key: my-secret-key
### Troubleshooting Steps
1. Verify the Secret:
Ensure that the Kubernetes secret is correctly created and accessible in the namespace the job is running in.
sh
kubectl get secret my-k8s-secret -n <namespace>
2. Inspect the Pod Environment Variables:
After deploying, inspect the running pod to verify if the environment variables are set correctly.
sh
kubectl describe pod <pod-name> -n <namespace>
### Related Documentation:
- Kubernetes Worker Guide: Prefect Kubernetes Worker
- Overriding Job Variables Guide: Overriding Job Variables
If you are still facing issues, ensure your Kubernetes secrets and job definitions are correctly configured and that the Prefect worker has the necessary permissions to access the secrets.Dominik
07/24/2024, 12:00 PMMarvin
07/24/2024, 12:01 PMyaml
# base-job-template.yaml
apiVersion: batch/v1
kind: Job
metadata:
labels: "{{ labels }}"
namespace: "{{ namespace }}"
generateName: "{{ name }}-"
spec:
ttlSecondsAfterFinished: "{{ finished_job_ttl }}"
template:
spec:
parallelism: 1
completions: 1
restartPolicy: Never
serviceAccountName: "{{ service_account_name }}"
containers:
- name: flow-container
image: "{{ image }}"
envFrom:
- secretRef:
name: my-k8s-secret # Ensure this secret exists in the namespace
env:
- name: MY_SECRET_ENV_VAR
valueFrom:
secretKeyRef:
name: my-k8s-secret # Ensure this secret exists in the namespace
key: my-secret-key
2. Configure the Prefect Kubernetes worker to use the base job template:
When starting your Kubernetes worker, reference this base job template:
sh
prefect worker start --pool 'my-work-pool' --type kubernetes --job-template-file path/to/base-job-template.yaml
This ensures that every job created by the worker will include the specified environment variables and secrets.
### Steps to Implement:
1. Create the Secret:
Ensure the Kubernetes secret is created and accessible within the namespace:
sh
kubectl create secret generic my-k8s-secret --from-literal=my-secret-key=my-secret-value -n <namespace>
2. Save the Job Template:
Save the base job template to a file called base-job-template.yaml
.
3. Start the Worker:
Start the Prefect Kubernetes worker with the reference to the job template:
sh
prefect worker start --pool 'my-work-pool' --type kubernetes --job-template-file path/to/base-job-template.yaml
### Related Documentation:
- Kubernetes Worker Guide: Prefect Kubernetes Worker
By configuring the base job template in this manner, you ensure consistency and reduce redundancy, as all jobs will inherit the environment variables and secrets specified.Dominik
07/24/2024, 12:16 PMMarvin
07/24/2024, 12:16 PMjson
{
"apiVersion": "batch/v1",
"kind": "Job",
"metadata": {
"labels": "{{ labels }}",
"namespace": "{{ namespace }}",
"generateName": "{{ name }}-"
},
"spec": {
"ttlSecondsAfterFinished": "{{ finished_job_ttl }}",
"template": {
"spec": {
"parallelism": 1,
"completions": 1,
"restartPolicy": "Never",
"serviceAccountName": "{{ service_account_name }}",
"containers": [
{
"name": "flow-container",
"image": "{{ image }}",
"envFrom": [
{
"secretRef": {
"name": "my-k8s-secret"
}
}
],
"env": [
{
"name": "MY_SECRET_ENV_VAR",
"valueFrom": {
"secretKeyRef": {
"name": "my-k8s-secret",
"key": "my-secret-key"
}
}
}
]
}
]
}
}
}
}
### Steps to Configure in Prefect UI
1. Navigate to the Work Pool Configuration:
Go to the Work Pools section in the Prefect UI and select the work pool you want to configure.
2. Edit the Base Job Template:
Find the Base Job Template section in the configuration and replace its content with the JSON formatted job template.
3. Save the Configuration:
Save the changes to ensure the new job template is applied to all jobs created by the work pool.
### Example Prefect UI Configuration
1. Open the Work Pool Configuration in the Prefect UI.
2. Locate the Base Job Template Field and paste the JSON:
json
{
"apiVersion": "batch/v1",
"kind": "Job",
"metadata": {
"labels": "{{ labels }}",
"namespace": "{{ namespace }}",
"generateName": "{{ name }}-"
},
"spec": {
"ttlSecondsAfterFinished": "{{ finished_job_ttl }}",
"template": {
"spec": {
"parallelism": 1,
"completions": 1,
"restartPolicy": "Never",
"serviceAccountName": "{{ service_account_name }}",
"containers": [
{
"name": "flow-container",
"image": "{{ image }}",
"envFrom": [
{
"secretRef": {
"name": "my-k8s-secret"
}
}
],
"env": [
{
"name": "MY_SECRET_ENV_VAR",
"valueFrom": {
"secretKeyRef": {
"name": "my-k8s-secret",
"key": "my-secret-key"
}
}
}
]
}
]
}
}
}
}
By pasting this JSON configuration in the Base Job Template of your Work Pool in the Prefect UI, you'll ensure that all jobs created by this Work Pool will have the specified environment variables and secrets.