Prefect integrations for interacting with Kubernetes.
Project description
prefect-kubernetes
Welcome!
prefect-kubernetes
is a collection of Prefect tasks, flows, and blocks enabling orchestration, observation and management of Kubernetes resources.
Jump to examples.
Resources
For more tips on how to use tasks and flows in a Collection, check out Using Collections!
Installation
Install prefect-kubernetes
with pip
:
pip install prefect-kubernetes
Requires an installation of Python 3.8+.
We recommend using a Python virtual environment manager such as pipenv, conda or virtualenv.
These tasks are designed to work with Prefect 2. For more information about how to use Prefect, please refer to the Prefect documentation.
Then, to register blocks on Prefect Cloud:
prefect block register -m prefect_kubernetes
Note, to use the load
method on Blocks, you must already have a block document saved through code or saved through the UI.
Example Usage
Use with_options
to customize options on any existing task or flow
from prefect_kubernetes.flows import run_namespaced_job
customized_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
For more tips on how to use tasks and flows in a Collection, check out Using Collections!
Specify and run a Kubernetes Job from a yaml file
from prefect_kubernetes.credentials import KubernetesCredentials
from prefect_kubernetes.flows import run_namespaced_job # this is a flow
from prefect_kubernetes.jobs import KubernetesJob
k8s_creds = KubernetesCredentials.load("k8s-creds")
job = KubernetesJob.from_yaml_file( # or create in the UI with a dict manifest
credentials=k8s_creds,
manifest_path="path/to/job.yaml",
)
job.save("my-k8s-job", overwrite=True)
if __name__ == "__main__":
# run the flow
run_namespaced_job(job)
Generate a resource-specific client from KubernetesClusterConfig
# with minikube / docker desktop & a valid ~/.kube/config this should ~just work~
from prefect_kubernetes.credentials import KubernetesCredentials, KubernetesClusterConfig
k8s_config = KubernetesClusterConfig.from_file('~/.kube/config')
k8s_credentials = KubernetesCredentials(cluster_config=k8s_config)
with k8s_credentials.get_client("core") as v1_core_client:
for namespace in v1_core_client.list_namespace().items:
print(namespace.metadata.name)
List jobs in a specific namespace
from prefect import flow
from prefect_kubernetes.credentials import KubernetesCredentials
from prefect_kubernetes.jobs import list_namespaced_job
@flow
def kubernetes_orchestrator():
v1_job_list = list_namespaced_job(
kubernetes_credentials=KubernetesCredentials.load("k8s-creds"),
namespace="my-namespace",
)
Patch an existing deployment
from kubernetes_asyncio.client.models import V1Deployment
from prefect import flow
from prefect_kubernetes.credentials import KubernetesCredentials
from prefect_kubernetes.deployments import patch_namespaced_deployment
@flow
def kubernetes_orchestrator():
v1_deployment = patch_namespaced_deployment(
kubernetes_credentials=KubernetesCredentials.load("k8s-creds"),
deployment_name="my-deployment",
deployment_updates=yaml.safe_load(...),
namespace="my-namespace"
)
print(v1_deployment)
Feedback
If you encounter any bugs while using prefect-kubernetes
, feel free to open an issue in the prefect repository.
If you have any questions or issues while using prefect-kubernetes
, you can find help in either the Prefect Discourse forum or the Prefect Slack community.
Contributing
If you'd like to help contribute to fix an issue or add a feature to prefect-kubernetes
, please propose changes through a pull request from a fork of the repository.
Here are the steps:
- Fork the repository
- Clone the forked repository
- Install the repository and its dependencies:
pip install -e ".[dev]"
- Make desired changes
- Add tests
- Install
pre-commit
to perform quality checks prior to commit:
pre-commit install
git commit
,git push
, and create a pull request
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file prefect_kubernetes-0.4.0.tar.gz
.
File metadata
- Download URL: prefect_kubernetes-0.4.0.tar.gz
- Upload date:
- Size: 56.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c6b757d80a36c8dc90780a0f9828418b44cb3649d7946606d219230882643ac |
|
MD5 | c7b0768fd04eb0344ee55b31372a04d3 |
|
BLAKE2b-256 | dc10dc4c3eb3bcafddeab50d9ece3994c76a2352b7d94d879cec48142c2fcab7 |
File details
Details for the file prefect_kubernetes-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: prefect_kubernetes-0.4.0-py3-none-any.whl
- Upload date:
- Size: 37.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1673e27abbcc1a93aa01bdaae684da0bff85d41686a3af66325079ba647ca9a8 |
|
MD5 | 3bb6e07b4ef65d1baca0c92436215018 |
|
BLAKE2b-256 | e07a40af044c96681a484f25a8eb6feba33fccb0066fe752e355da86647fb7e0 |