微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何通过使用 CloudFormation 在不同 Fargate 任务中运行的 Consul 服务器将 Consul 代理加入 Consul 集群?

如何解决如何通过使用 CloudFormation 在不同 Fargate 任务中运行的 Consul 服务器将 Consul 代理加入 Consul 集群?

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

  • 1 个 Fargate 任务(在一个服务中)和一个 Consul 服务器容器。
  • x Fargate 任务(在服务中),带有 Consul 代理、Consul Connect sidecar 和微服务容器。

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

问题:

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

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

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

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

谁能告诉我如何继续?

先谢谢你!

解决方法

感谢我的一个非常聪明的同事,我发现在 ECS 服务前面放置一个负载均衡器(我之前设置错误)与 Consul Server Fargate 任务解决了我的问题。负载平衡器侦听器应侦听 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}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。