AWS ECS 私有和 Public 服务

AWS ECS Private and Public Services

我有一个场景,我必须在 AWS ECS 上部署多个微服务。我想让服务能够通过在每个微服务中开发的 API 相互通信。我想在 AWS ECS 上部署前端,可以公开访问,还可以与部署在 AWS ECS 上的其他微服务进行通信。我怎样才能做到这一点?我是否可以通过将所有服务置于私有子网中以启用它们之间的通信来使用 AWS ECS 服务发现?我能否使用 Elastic Load Balancer 让最终用户仅通过 HTTP/HTTPS 协议通过互联网访问前端微服务,同时将其保留在私有子网中?

根据文档,"Amazon ECS does not support registering services into public DNS namespaces"

换句话说,当它注册 DNS 时,它只使用服务的 私有 IP 地址,这可能会出现问题。 "public" 服务的 DNS 将注册到私有 IP 地址,这仅在例如您使用 VPN 连接到私有网络时有效,无论您的子网规则是什么。

我认为更好的解决方案是将服务附加到两个负载平衡器之一......一个面向互联网,一个内部。我认为这对于扩展服务来说更自然。服

I want to deploy the front-end on AWS ECS as well that can be accessed publicly and can also communicate with other micro-services deployed on AWS ECS.

我会使用 Service Discovery 在内部连接服务,并使用 Elastic Load Balancer 集成让 public 可以访问它们。

负载均衡器可以在一侧进行负载均衡,DNS SRV 记录可以在内部为您的 API 进行负载均衡。

Stack Overflow 上有一个类似的问题,它的答案 [1] 概述了在 ECS 中使用负载均衡器和服务发现集成的可能解决方案。

Can I use Elastic Load Balancer to make front-end micro-service accessible to end-users over the internet only via HTTP/HTTPS protocols while keeping it in a private subnet?

是的,负载均衡器可以在私有子网中注册目标。

参考资料

[1]

AWS 负载均衡器(用于 public 访问)和 Amazon ECS 服务发现(用于内部通信)的组合是 Web 应用程序的完美选择。

Built-in service discovery in ECS is another feature that makes it easy to develop a dynamic container environment without needing to manage as many resources outside of your application. ECS and Route 53 combine to provide highly available, fully managed, and secure service discovery

服务发现是一种使用容器直接 IP 地址而不是像负载均衡器这样的中介将流量从一个容器获取到另一个容器的技术。它适用于多种用例:

  • 私有的内部服务发现
  • 服务之间的低延迟通信
  • 长期存在的双向连接,例如 gRPC。

是的,您可以使用 AWS ECS 服务发现 将所有服务置于私有子网中以实现它们之间的通信。

This makes it possible for an ECS service to automatically register itself with a predictable and friendly DNS name in Amazon Route 53. As your services scale up or down in response to load or container health, the Route 53 hosted zone is kept up to date, allowing other services to lookup where they need to make connections based on the state of each service.

是的,您可以使用 Load Balancer 让最终用户可以通过 Internet 访问前端微服务。您可以查看此图表,其中显示了 ECS 中 Web 应用程序的 AWS LB 和服务发现。

您可以看到位于私有子网中的后端容器,通过 ALB 服务 public 请求,而容器的其余部分使用 AWS 服务发现。

Amazon ECS 服务发现

Let’s launch an application with service discovery! First, I’ll create two task definitions: “flask-backend” and “flask-worker”. Both are simple AWS Fargate tasks with a single container serving HTTP requests. I’ll have flask-backend ask worker.corp to do some work and I’ll return the response as well as the address Route 53 returned for worker. Something like the code below:

@app.route("/")
namespace = os.getenv("namespace")
worker_host = "worker" + namespace
def backend():
    r = requests.get("http://"+worker_host)
    worker = socket.gethostbyname(worker_host)
    return "Worker Message: {]\nFrom: {}".format(r.content, worker)

请注意,在这个 私有架构 中没有 public 子网,只有一个私有子网。子网内的容器可以使用其内部 IP 地址相互通信。但是他们需要一些方法来发现彼此的 IP 地址。

AWS 服务发现提供两种方法:

  • 基于 DNS(Route 53 创建并维护一个自定义 DNS 名称,该名称 解析为其他容器的一个或多个 IP 地址,例如 例如,http://nginx.service.production 那么其他容器可以 只需使用打开连接即可将流量发送到目的地 这个 DNS 名称)
  • 基于
  • API(容器可以查询 API 以获取 IP 地址列表 目标可用,然后直接打开连接到其中一个 其他容器。)

您可以阅读有关 AWS 服务发现和用例的更多信息amazon-ecs-service-discovery and here