KubernetesPodOperator is one of the most customizable Apache Airflow operators. A task using the KubernetesPodOperator runs in a dedicated, isolated Kubernetes Pod that terminates after the task completes. To learn more about the benefits and usage of the KubernetesPodOperator, see the KubernetesPodOperator Learn guide.
This guide explains how to complete specific goals using the KubernetesPodOperator on Astro Private Cloud.
Prerequisites
- A running Airflow Deployment on Astro Private Cloud
Set up the KubernetesPodOperator
Install the operator
Run the following command to install theapache-airflow-providers-cncf-kubernetes package:
Specify parameters
Instantiate the operator based on your image and setup:KubernetesPodOperator, you must specify the following values:
namespace = conf.get("kubernetes", "NAMESPACE"): Every Deployment runs on its own Kubernetes namespace. Information about this namespace can be programmatically imported as long as you set this variable.image: This is the Docker image that the operator will use to run its defined task, commands, and arguments. The value you specify is assumed to be an image tag that’s publicly available on a public registry. To pull an image from a private registry, read Pull images from a Private Registry.in_cluster=True: When this value is set, your task will run within the cluster from which it’s instantiated. This ensures that the Kubernetes Pod running your task has the correct permissions within the cluster.is_delete_operator_pod=True: This setting ensures that once aKubernetesPodOperatortask is complete, the Kubernetes Pod that ran that task is terminated. This ensures that there are no unused pods in your cluster taking up resources.
Add resources to your Deployment on Astro Private Cloud
TheKubernetesPodOperator is entirely powered by the resources allocated to the Extra Capacity slider of your deployment’s Configure page in the Software UI in lieu of needing a Celery worker (or scheduler resources for those running the Local Executor). Raising the slider will increase your namespace’s resource quota such that Airflow has permissions to successfully launch pods within your deployment’s namespace.
Note: Your Airflow scheduler and webserver will remain necessary fixed resources that ensure the rest of your tasks can execute and that your deployment stays up and running.In terms of resource allocation, Astronomer recommends starting with 1 CPU and 3840 Mi memory in
Extra Capacity and scaling up from there as needed. If it’s set too low, you may get a permissions error:
Note: If you need to increase your limit range on Astro Private Cloud, contact your system admin.
Define resources per task
A notable advantage of leveraging Airflow’sKubernetesPodOperator is that you can control compute resources in the task definition.
Note: If you’re using the Kubernetes Executor, note that this value is separate from theexecutor_configparameter. In this case, theexecutor_configwould only define the Airflow worker that is launching your Kubernetes task.
Example task definition
V1ResourceRequirements object:
hello-world image, which is pulled from Docker Hub, in your Airflow Deployment’s namespace with the resource requests defined above. Once the task finishes, the Pod will terminate gracefully.
Pull images from a private registry
By default, theKubernetesPodOperator will look for images hosted publicly on Docker Hub. If you want to pull images from a private registry, you may do so.
To pull images from a private registry on Astro Private Cloud:
-
Retrieve a
config.jsonfile that contains your Docker credentials by following the Docker documentation. The generated file should look something like this: - Follow the Kubernetes documentation to create a secret based on your credentials.
-
In your Dag code, import
modelsfromkubernetes.clientand specifyimage_pull_secretswith your Kubernetes secret. After configuring this value, you can pull an image as you would from a public registry like in the following example.
Local testing
Astronomer recommends testing your Dags locally before pushing them to a Deployment on Astro Private Cloud. For more information, read How to run theKubernetesPodOperator locally. That guide provides information on how to use MicroK8s or Docker for Kubernetes to run tasks with the KubernetesPodOperator in a local environment.
To pull images from a private registry locally, you’ll have to create a secret in your local namespace and similarly call it in your operator following the guidelines above.