【问题标题】:How can I join a Consul agent to a Consul cluster via a Consul server running in a different Fargate task with CloudFormation?如何通过在 CloudFormation 的不同 Fargate 任务中运行的 Consul 服务器将 Consul 代理加入 Consul 集群?
【发布时间】:2021-04-30 09:42:05
【问题描述】:

我目前是一名实习生,必须托管一个微服务应用程序。我选择结合使用 AWS ECS 和 Fargate 任务来托管 Consul Connect 服务网格,为应用程序提供服务发现、意图、Mtls 等。我的设置如下:

  • 1 个带有 Consul 服务器容器的 Fargate 任务(在服务中)。
  • x Fargate 任务(在服务中)使用 Consul 代理、Consul Connect 边车和微服务容器。

我正在使用 CloudFormation 自动部署该基础架构。

问题:

我需要使用 CloudFormation 将在一个 Fargate 任务中运行的 Consul 代理加入到在另一个 Fargate 任务(服务器的任务)中启动的 Consul 集群。我这样做的问题是我没有找到任何方法来获取 IP 地址以将代理加入集群。

有谁知道我如何在 CloudFormation 中获取 Fargate 任务的 IP 地址或执行此操作的最佳实践方式?

如果我没记错的话,您只能使用 IP、DNS 名称或云元数据将 Consul 代理加入 Consul 集群。第一个和第二个我无法使用 CloudFormation 检索,而对于第三个我发现它可能是不可能的(我可能是错的,但这就是我目前所读到的)。

我还尝试了 Consul agent -ui [...] -join 和 -retry-join 标志,但都没有奏效。我还尝试使用我使用 DNS 名称尝试加入集群的 Consul 服务器为任务创建一个内部负载均衡器,但这也不起作用(我还没有在 AWS 上正确设置负载均衡器,所以我可能做错了)。我尝试使用负载均衡器将流量转发到端口 8500(我认为这是错误的端口),然后使用端口 8301(我认为是正确的端口)。但是我一直收到消息说那个地址上没有 Consul Cluster。

谁能告诉我该怎么做?

提前谢谢你!

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation amazon-ecs consul aws-fargate


    【解决方案1】:

    感谢我的一位非常聪明的同事,我发现通过 Consul Server Fargate 任务在 ECS 服务前面放置一个负载均衡器(我之前设置的错误)解决了我的问题。负载平衡器侦听器应侦听 SERF 端口 8301 tcp_udp 并将流量转发到使用该协议和端口的服务。

      ConsulServerTargetGroup8301:
        Type: AWS::ElasticLoadBalancingV2::TargetGroup
        Properties:
          HealthCheckEnabled: true
          HealthCheckIntervalSeconds: 30
          UnhealthyThresholdCount: 3
          HealthyThresholdCount: 3
          Name: ConsulServerTargetGroup8301
          Port: 8301
          Protocol: TCP_UDP
          TargetGroupAttributes:
            - Key: deregistration_delay.timeout_seconds
              Value: 60 # default is 300
          TargetType: ip
          VpcId: !Ref VPC
    
      ConsulServerTargetGroupListener8301:
        Type: AWS::ElasticLoadBalancingV2::Listener
        Properties:
          DefaultActions:
            - TargetGroupArn: !Ref ConsulServerTargetGroup8301
              Type: forward
          Port: 8301
          Protocol: TCP_UDP
          LoadBalancerArn: !Ref ConsulServerLoadbalancer
      
      ConsulServerLoadbalancer:
        Type: AWS::ElasticLoadBalancingV2::LoadBalancer
        Properties:
          Name: ConsulServerLoadbalancer
          IpAddressType: ipv4
          Type: network
          Scheme: internal
          SubnetMappings:
            - SubnetId: !Ref PrivateSubnet1a
            - SubnetId: !Ref PrivateSubnet1b 
    

    然后,您可以使用负载均衡器的 DNS 名称将 Consul 代理加入到 consul 集群,方法是:

            Command:
            - !Sub >-
                consul agent 
                -ui 
                -data-dir consul/data 
                -advertise '{{ GetPrivateInterfaces | include "network" "${pVPCIpRange}" | attr "address" }}'
                -client 0.0.0.0
                -retry-join ${ConsulServerLoadbalancer.DNSName}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-08
      • 1970-01-01
      • 2021-09-21
      • 2022-11-10
      • 1970-01-01
      相关资源
      最近更新 更多