prefect_kubernetes.worker
Module containing the Kubernetes worker used for executing flow runs as Kubernetes jobs.
To start a Kubernetes worker, run the following command:
my-work-pool with the name of the work pool you want the worker
to poll for flow runs.
Securing your Prefect Cloud API key
If you are using Prefect Cloud and would like to pass your Prefect Cloud API key to created jobs via a Kubernetes secret, set thePREFECT_INTEGRATIONS_KUBERNETES_WORKER_CREATE_SECRET_FOR_API_KEY environment variable before
starting your worker:
Using a custom Kubernetes job manifest template
The default template used for Kubernetes job manifests looks like this:{{ }} is a placeholder that will be replaced with
a value at runtime. The values that can be used a placeholders are defined
by the variables schema defined in the base job template.
The default job manifest and available variables can be customized on a work pool
by work pool basis. These customizations can be made via the Prefect UI when
creating or editing a work pool.
For example, if you wanted to allow custom memory requests for a Kubernetes work
pool you could update the job manifest template to look like this:
memory placeholder allows customization of the memory
allocated to Kubernetes jobs created by workers in this work pool, but the limit
is hard-coded and cannot be changed by deployments.
For more information about work pools and workers,
checkout out the Prefect docs.
Classes
KubernetesImagePullPolicy
Enum representing the image pull policy options for a Kubernetes job.
KubernetesWorkerJobConfiguration
Configuration class used by the Kubernetes worker.
An instance of this class is passed to the Kubernetes worker’s run method
for each flow run. It contains all of the information necessary to execute
the flow run as a Kubernetes job.
Attributes:
name: The name to give to created Kubernetes job.command: The command executed in created Kubernetes jobs to kick off flow run execution.env: The environment variables to set in created Kubernetes jobs.labels: The labels to set on created Kubernetes jobs.namespace: The Kubernetes namespace to create Kubernetes jobs in.job_manifest: The Kubernetes job manifest to use to create Kubernetes jobs.cluster_config: The Kubernetes cluster configuration to use for authentication to a Kubernetes cluster.job_watch_timeout_seconds: The number of seconds to wait for the job to complete before timing out. IfNone, the worker will wait indefinitely.pod_watch_timeout_seconds: The number of seconds to wait for the pod to complete before timing out.stream_output: Whether or not to stream the job’s output.
get_environment_variable_value
prepare_for_flow_run
flow_run: The flow run to prepare the job configuration fordeployment: The deployment associated with the flow run used for preparation.flow: The flow associated with the flow run used for preparation.work_pool: The work pool associated with the flow run used for preparation.worker_name: The name of the worker used for preparation.
KubernetesWorkerVariables
Default variables for the Kubernetes worker.
The schema for this class is used to populate the variables section of the default
base job template.
KubernetesWorkerResult
Contains information about the final state of a completed process
KubernetesWorker
Prefect worker that executes flow runs within Kubernetes Jobs.
Methods:
kill_infrastructure
infrastructure_pid: The infrastructure identifier in format “namespace:job_name”.configuration: The job configuration used to connect to the cluster.grace_seconds: Time to allow for graceful shutdown before force killing.
InfrastructureNotFound: If the job doesn’t exist.InfrastructureNotAvailable: If unable to connect to the cluster.
run
flow_run: The flow run to executeconfiguration: The configuration to use when executing the flow run.task_status: The task status object for the current flow run. If provided, the task will be marked as started.
- A result object containing information about the final state of the flow run