【问题标题】:how to reference existing ELB DNSName in Cloudformation template如何在 Cloudformation 模板中引用现有的 ELB DNSName
【发布时间】:2020-06-23 06:31:00
【问题描述】:

我们有 Cloudformation 模板,通过它我们可以为我们的产品部署基础设施资源。以下是通过 CF 模板创建的 AWS 组件: 1. 网络组件。像 VPC、子网、安全组等。 2. IAM 角色和策略。 3. 电子病历 4.EKS 5.MSK 6.RDS 7. 弹力痛

在我们的 Cloudformation 模板中,我们也很少有自定义资源,例如“Custom::KubeManifest”。我们通过它在 AWS EKS 集群中部署对象。我们的 kubernetes 对象之一是“服务”对象。它为内部服务创建了一个服务端点,以便来自公共网络的请求可以到达我们的 kubernetes 集群。

我们想检查是否可以在 Cloudformation 模板中引用现有的 ELB DNS 名称,以便我们可以在输出中显示 ELB DnsName。

例如,当我们调用“Custom::KubeManifest”资源时,如下模板:

  ServiceDeployment:
    Type: "Custom::KubeManifest"
    Version: '1.0'
    Properties:
      ServiceToken: !Ref KubeManifestLambdaArn
      KubeConfigPath: !Sub "s3://${KubeConfigS3Bucket}/${KubeConfigS3Key}"
      KubeConfigKmsContext: !Ref KmsContext
      Manifest:
        apiVersion: v1
        kind: Service
        metadata:
          name: test
          labels:
            app: client
            tier: master
        spec:
          selector:
            app: client
            tier: master
          ports:
          - name: client-api
            port: 9877
            protocol: TCP
          - name: client-snapshots
            port: 9878
            protocol: TCP
          - name: client-support
            port: 9881
            protocol: TCP
  UiDeployment:
    Type: "Custom::KubeManifest"
    Version: '1.0'
    Properties:
      ServiceToken: !Ref KubeManifestLambdaArn
      KubeConfigPath: !Sub "s3://${KubeConfigS3Bucket}/${KubeConfigS3Key}"
      KubeConfigKmsContext: !Ref KmsContext
      Manifest:
        apiVersion: v1
        kind: Service
        metadata:
          name: client-ui
          annotations:
            service.beta.kubernetes.io/aws-load-balancer-internal: 0.0.0.0/0
            service.beta.kubernetes.io/aws-load-balancer-proxy-protocol: '*'
            service.beta.kubernetes.io/aws-load-balancer-type: nlb
            service.beta.kubernetes.io/aws-load-balancer-backend-protocol: 'tcp'
            service.beta.kubernetes.io/aws-load-balancer-ssl-ports: "tcp"
            service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout: "60"
          labels:
            app: client
            tier: master
        spec:
          type: LoadBalancer
          selector:
            app: client
            tier: master
          ports:
          - name: client-ui
            port: 80
            protocol: TCP
            targetPort: 8800
          - name: client-ui-https
            port: 443
            protocol: TCP
            targetPort: 8800

它在 AWS 账户中创建一个 ELB,并将其与 EKS 集群中的服务端点映射。现在我们想知道是否可以通过任何函数引用新创建的 ELB DnsNames 并将其显示为输出。

【问题讨论】:

    标签: amazon-web-services kubernetes amazon-cloudformation amazon-eks aws-cloudformation-custom-resource


    【解决方案1】:

    这是我的 YAML 示例

    Resources:
      LoadBalancer:
        Type: AWS::ElasticLoadBalancingV2::LoadBalancer
        Properties:
          Name: !Ref EnvironmentName
          Subnets: !Ref Subnets
          SecurityGroups:
            - !Ref SecurityGroup
          Tags:
            - Key: Name
              Value: !Ref EnvironmentName
    
      LoadBalancerListener:
        Type: AWS::ElasticLoadBalancingV2::Listener
        Properties:
          LoadBalancerArn: !Ref LoadBalancer
          Port: 80
          Protocol: HTTP
          DefaultActions:
            - Type: forward
              TargetGroupArn: !Ref DefaultTargetGroup
    

    LoadBalancerLoadBAlanceListener 都必须包含在内。然后,您必须添加声明您希望可用于描述堆栈 API 调用的值的输出。

    Outputs:
      LoadBalancer:
        Description: A reference to the Application Load Balancer
        Value: !Ref LoadBalancer
    
      LoadBalancerUrl:
        Description: The URL of the ALB
        Value: !GetAtt LoadBalancer.DNSName
    
      Listener:
        Description: A reference to a port 80 listener
        Value: !Ref LoadBalancerListener
    

    【讨论】:

    • 这对我不起作用,因为我们没有明确创建 ELB。创建 ELB 实体的是 EKS。因此我不能使用!Ref 或!GetAtt。我相信只有通过 CF 堆栈创建资源时才能使用这两个函数。
    【解决方案2】:

    我们查看了帖子: aws-quickstart-examples-eks

    我们可以在哪里获得新创建的 loadBalancer 的 DnsNames,它通过使用映射到服务端点

    自定义::KubeGet

    资源。

    【讨论】:

      【解决方案3】:

      你可以参考 DNS 名称:

      Fn::GetAtt: [LoadBalancer, DNSName]

      LoadBalancer 是创建的负载均衡器资源。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-12
        • 2021-04-22
        • 2016-04-12
        • 2020-10-22
        • 2017-06-09
        • 2020-09-30
        • 2021-10-27
        • 2013-02-11
        相关资源
        最近更新 更多