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 Relationships AmazonEKSClusterPolicy IAM Policy must be attached

airflow/providers/amazon/aws/example_dags/example_eks_with_nodegroups.pyView Source

    # 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.

airflow/providers/amazon/aws/example_dags/example_eks_with_nodegroups.pyView Source

    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.

airflow/providers/amazon/aws/example_dags/example_eks_using_defaults.pyView Source

    # 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 Relationships AmazonEC2ContainerRegistryReadOnly IAM Policy must be attached AmazonEKSWorkerNodePolicy IAM Policy must be attached

airflow/providers/amazon/aws/example_dags/example_eks_with_nodegroups.pyView Source

    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.

airflow/providers/amazon/aws/example_dags/example_eks_with_nodegroups.pyView Source

    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 Relationships eks.amazonaws.com must be added to the Trusted Relationships AmazonEC2ContainerRegistryReadOnly IAM Policy must be attached AmazonEKSClusterPolicy IAM Policy must be attached AmazonEKSWorkerNodePolicy IAM Policy must be attached

airflow/providers/amazon/aws/example_dags/example_eks_using_defaults.pyView Source

    # 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.

airflow/providers/amazon/aws/example_dags/example_eks_with_nodegroups.pyView Source

    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,
    )

Reference

For further information, look at:

Was this entry helpful?