Amazon Elastic Kubernetes Service (EKS) Operators¶
Amazon Elastic Kubernetes Service (Amazon EKS) is a managed service that makes it easy for you to run Kubernetes on AWS without needing to stand up or maintain your own Kubernetes control plane. Kubernetes is an open-source system for automating the deployment, scaling, and management of containerized applications.
Airflow provides operators to create and interact with the EKS clusters and compute infrastructure.
Manage Amazon EKS Clusters¶
Create an Amazon EKS Cluster¶
To create an Amazon EKS Cluster you can use
EKSCreateClusterOperator
.
- Note: An AWS IAM role with the following permissions is required:
eks.amazonaws.com
must be added to the Trusted RelationshipsAmazonEKSClusterPolicy
IAM Policy must be attached
# Create an Amazon EKS Cluster control plane without attaching a compute service.
create_cluster = EKSCreateClusterOperator(
task_id='create_eks_cluster',
cluster_name=CLUSTER_NAME,
cluster_role_arn=ROLE_ARN,
resources_vpc_config=VPC_CONFIG,
compute=None,
)
Delete an Amazon EKS Cluster¶
To delete an existing Amazon EKS Cluster you can use
EKSDeleteClusterOperator
.
delete_cluster = EKSDeleteClusterOperator(task_id='delete_eks_cluster', cluster_name=CLUSTER_NAME)
Note: If the cluster has any attached resources, such as a nodegroup, the cluster can not be deleted.
Using the force
parameter will attempt to delete any attached resources first.
# An Amazon EKS cluster can not be deleted with attached resources.
# Setting the `force` to `True` will delete any attached resources before deleting the cluster.
delete_all = EKSDeleteClusterOperator(
task_id='delete_nodegroup_and_cluster', cluster_name=CLUSTER_NAME, force_delete_compute=True
)
Manage Amazon EKS Managed Nodegroups¶
Create an Amazon EKS Managed NodeGroup¶
To create an Amazon EKS Managed Nodegroup you can use
EKSCreateNodegroupOperator
.
- Note: An AWS IAM role with the following permissions is required:
ec2.amazon.aws.com
must be in the Trusted RelationshipsAmazonEC2ContainerRegistryReadOnly
IAM Policy must be attachedAmazonEKSWorkerNodePolicy
IAM Policy must be attached
create_nodegroup = EKSCreateNodegroupOperator(
task_id='create_eks_nodegroup',
cluster_name=CLUSTER_NAME,
nodegroup_name=NODEGROUP_NAME,
nodegroup_subnets=SUBNETS,
nodegroup_role_arn=ROLE_ARN,
)
Delete an Amazon EKS Managed Nodegroup¶
To delete an existing Amazon EKS Managed Nodegroup you can use
EKSDeleteNodegroupOperator
.
delete_nodegroup = EKSDeleteNodegroupOperator(
task_id='delete_eks_nodegroup', cluster_name=CLUSTER_NAME, nodegroup_name=NODEGROUP_NAME
)
Create an Amazon EKS Cluster and Nodegroup in one step¶
To create an Amazon EKS Cluster and an EKS Managed Nodegroup in one command, you can use
EKSCreateClusterOperator
.
- Note: An AWS IAM role with the following permissions is required:
ec2.amazon.aws.com
must be in the Trusted Relationshipseks.amazonaws.com
must be added to the Trusted RelationshipsAmazonEC2ContainerRegistryReadOnly
IAM Policy must be attachedAmazonEKSClusterPolicy
IAM Policy must be attachedAmazonEKSWorkerNodePolicy
IAM Policy must be attached
# Create an Amazon EKS cluster control plane and an EKS nodegroup compute platform in one step.
create_cluster_and_nodegroup = EKSCreateClusterOperator(
task_id='create_eks_cluster_and_nodegroup',
cluster_name=CLUSTER_NAME,
nodegroup_name=NODEGROUP_NAME,
cluster_role_arn=ROLE_ARN,
nodegroup_role_arn=ROLE_ARN,
# Opting to use the same ARN for the cluster and the nodegroup here,
# but a different ARN could be configured and passed if desired.
resources_vpc_config=VPC_CONFIG,
# Compute defaults to 'nodegroup' but is called out here for the purposed of the example.
compute='nodegroup',
)
Perform a Task on an Amazon EKS Cluster¶
To run a pod on an existing Amazon EKS Cluster, you can use
EKSPodOperator
.
Note: An Amazon EKS Cluster with underlying compute infrastructure is required.
start_pod = EKSPodOperator(
task_id="run_pod",
cluster_name=CLUSTER_NAME,
image="amazon/aws-cli:latest",
cmds=["sh", "-c", "ls"],
labels={"demo": "hello_world"},
get_logs=True,
# Delete the pod when it reaches its final state, or the execution is interrupted.
is_delete_operator_pod=True,
)