如果没有至少 1 个私有子网,则无法在 VPC 中创建 AWS-CDK EKS 集群

AWS-CDK EKS Cluster can not be created in a VPC without at least 1 private subnet

我正在尝试使用 AWS CDK 生成 EKS 集群。问题如下: 当我为不包含私有子网的 EKS 集群定义 VPC 时,cdk synth 抛出以下错误。

Vpc定义及用法:

cluster = eks.Cluster(self, "airflow-eks",
            endpoint_access=eks.EndpointAccess.PUBLIC,
            vpc=ec2.Vpc(self, "airflow-eks-vpc", cidr="172.16.0.0/22", max_azs=2,
                subnet_configuration=[
                    ec2.SubnetConfiguration(
                        name="subnet-1",
                        cidr_mask=27,
                        subnet_type=ec2.SubnetType.PUBLIC
                    ),
                    ec2.SubnetConfiguration(
                        name="subnet-2",
                        cidr_mask=27,
                        subnet_type=ec2.SubnetType.PUBLIC
                    )
                ]
            ),

错误是:

jsii.errors.JSIIError: There are no 'Private' subnet groups in this VPC. Available types: Public

当我向集群定义添加额外的私有子网时,例如

ec2.SubnetConfiguration(
                        name="subnet-3",
                        cidr_mask=27,
                        subnet_type=ec2.SubnetType.PRIVATE
                    )

cdk synth 效果很好。

我想知道是否可以在不创建私有子网的情况下生成 EKS 集群,因为我根本不需要它们,而且使用私有子网会产生额外费用。在 Terraform 中肯定可以做到,那么 AWS CDK 呢?

不确定以下是根本原因,但它有所帮助。所以 eks.Cluster init 方法包含一个参数 - vpcSubnets,这是可选的,默认情况下包括 all public 和私有(!)子网 。因此,当明确定义此参数时,即

const cluster = new eks.Cluster(stack,'my-ts-eks',
    {
        vpc: eks_vpc,
        vpcSubnets:[
            {
                subnetType: ec2.SubnetType.PUBLIC,
                onePerAz: true
            }
        ],
        defaultCapacity: 0,
        version: eks.KubernetesVersion.V1_17,
    });

可以在没有私有子网的情况下创建 EKS 集群。

P.S. The example above is in TypeScript not Python

P.P.S In Python it will look like:

cluster = eks.Cluster(self, "airflow-eks",
           ...
            vpc_subnets = eks_vpc.select_subnets(subnet_type=ec2.SubnetType.PUBLIC, one_per_az=True).subnets,
          ...
        )

您可以通过将 assignPublicIp: true 添加到 FargateServiceProps 来尝试一下吗? 您可以参考github ticket #7062了解更多详情。

在Java中实现如下

ApplicationLoadBalancedFargateService.Builder.create(this, "FargateServiceName").cluster(cluster)
            .assignPublicIp(true)
            .cpu(512) // Default is 256
            .
            .
            .
            .build();