prefect-kubernetes contains Prefect tasks, flows, and blocks enabling orchestration, observation and management of Kubernetes resources.
This library is most commonly used to installation with a Kubernetes worker. See the Kubernetes guide to learn how to create and run deployments in Kubernetes.
As noted in that guide, Prefect also provides a Helm chart for deploying a worker, a self-hosted server instance, and other resources to a Kubernetes cluster. See the Prefect Helm chart for more information.
Use with_options to customize options on an existing task or flow¶
fromprefect_kubernetes.flowsimportrun_namespaced_jobcustomized_run_namespaced_job=run_namespaced_job.with_options(name="My flow running a Kubernetes Job",retries=2,retry_delay_seconds=10,)# this is now a new flow object that can be called
Specify and run a Kubernetes Job from a YAML file¶
fromprefect_kubernetes.credentialsimportKubernetesCredentialsfromprefect_kubernetes.flowsimportrun_namespaced_job# this is a flowfromprefect_kubernetes.jobsimportKubernetesJobk8s_creds=KubernetesCredentials.load("k8s-creds")job=KubernetesJob.from_yaml_file(# or create in the UI with a dict manifestcredentials=k8s_creds,manifest_path="path/to/job.yaml",)job.save("my-k8s-job",overwrite=True)if__name__=="__main__":# run the flowrun_namespaced_job(job)
Generate a resource-specific client from KubernetesClusterConfig¶
# with minikube / docker desktop & a valid ~/.kube/config this should ~just work~fromprefect.blocks.kubernetesimportKubernetesClusterConfigfromprefect_kubernetes.credentialsimportKubernetesCredentialsk8s_config=KubernetesClusterConfig.from_file('~/.kube/config')k8s_credentials=KubernetesCredentials(cluster_config=k8s_config)withk8s_credentials.get_client("core")asv1_core_client:fornamespaceinv1_core_client.list_namespace().items:print(namespace.metadata.name)