To launch Pods in external clusters from a local Airflow environment, you must have valid authentication for the external cluster so that your local Airflow environment has permissions to launch a Pod in the external cluster. For managed Kubernetes services from public cloud providers, authentication is federated through the native IAM service. To grant the Astro role permissions to launch pods on your cluster, you can either include static credentials or use workload identity to authorize the Astro role to your cluster.
Prerequisites
- Network connectivity between your Airflow execution environment and the external Kubernetes cluster:
- Hosted execution mode: A network connection between your Astro Deployment and the external cluster.
- Remote execution mode: Network connectivity between the environment where your Remote Execution Agent runs and the external cluster. You are responsible for managing this connectivity. A direct network connection between Astro and the external cluster is not required.
Setup
1
Set up your external cluster
-
Create an EKS cluster IAM role with a unique name and add the following permission policies:
AmazonEKSWorkerNodePolicyAmazonEKS_CNI_PolicyAmazonEC2ContainerRegistryReadOnly
-
Update the trust policy of this new role to include the workload identity of your Deployment. This step ensures that the role can be assumed by your Deployment.
- If you don’t already have a cluster, create a new EKS cluster and assign the new role to it.
2
Retrieve the KubeConfig file from the EKS cluster
-
Use a
KubeConfigfile to remotely connect to your new cluster. On AWS, you can run the following command to retrieve it:This command creates a newKubeConfigfile calledmy_kubeconfig.yaml. -
Ensure that the file below matches your generated KubeConfig. The newly generated KubeConfig must be edited to instruct the AWS IAM Authenticator for Kubernetes to assume your new IAM Role created in Step 1. Replace
<your assume role arn>with the IAM Role ARN from Step 1.my_kubeconfig.yaml
3
Create a Kubernetes cluster connection
Astronomer recommends creating a Kubernetes cluster connection because it’s more secure than adding an unencrypted
kubeconfig file directly to your Astro project.- Convert the
kubeconfigconfiguration you retrieved from your cluster to JSON format. - In either the Airflow UI or the Astro environment manager, create a new Kubernetes Cluster Connection connection. In the Kube config (JSON format) field, paste the
kubeconfigconfiguration you retrieved from your cluster after converting it fromyamltojsonformat. - Click Save.
KubernetesPodOperator task that needs to access your external cluster.4
Install the AWS CLI in your Astro environment
To connect to your external EKS cluster, you need to install the AWS CLI in your Astro project.
-
Add the following to your
Dockerfileto install the AWS CLI:Dockerfile -
Add the
unzippackage to yourpackages.txtfile to make theunzipcommand available in your Docker container:packages.txt
5
Configure your task
In your
KubernetesPodOperator task configuration, ensure that you set cluster-context and namespace for your remote cluster. In the following example, the task launches a Pod in an external cluster based on the configuration defined in the k8s connection.Example dag
The following dag uses several classes from the Amazon provider package to dynamically spin up and delete Pods for each task in a newly created node group. If your remote Kubernetes cluster already has a node group available, you only need to define your task in theKubernetesPodOperator itself.
The example dag contains 5 consecutive tasks:
- Create a node group according to the user’s specifications (For the example that uses GPU resources).
- Use a sensor to check that the cluster is running correctly.
- Use the
KubernetesPodOperatorto run any valid Docker image in a Pod on the newly created node group on the remote cluster. The example dag uses the standardUbuntuimage to print “hello” to the console using abashcommand. - Delete the node group.
- Verify that the node group has been deleted.