ECS和应用程序负载均衡器 [英] ECS and Application Load Balancer

查看:93
本文介绍了ECS和应用程序负载均衡器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找有关云形成的信息,有关使用 ECS ELB(应用程序负载平衡器)创建堆栈的信息,但无法

Ive been looking for some information on Cloud Formation with regards to creating a stack with ECS and ELB (Application Load Balancer) but unable to do so.

我创建了两个Docker映像,每个映像都包含一个Node.js微服务,该服务在端口 3000 4000 。如上所述,如何使用 ECS ELB 创建堆栈?我假设可以将Application Load Balancer配置为侦听这两个端口?

I have created two Docker images each containing a Node.js microservice that listens on ports 3000 and 4000. How do I go about creating my stack with ECS and ELB as mentioned ? I assume the Application Load Balancer can be configured to listen to both these ports ?

示例云形成模板确实有帮助。

A sample Cloud Formation template would really help.

推荐答案

应用程序负载平衡器可用于跨服务中的ECS任务加载流量。应用程序负载平衡器具有两个很不错的功能,您可以利用它们: 动态端口映射(主机上的端口由ECS / Docker自动分配),可让您在单个EC2实例上运行同一服务的多个任务,并基于路径进行路由允许您根据URL路径中的模式将传入的请求路由到不同的服务。

The Application Load Balancer can be used to load traffic across the ECS tasks in your service(s). The Application Load Balancer has two cool features that you can leverage; dynamic port mapping (port on host is auto-assigned by ECS/Docker) allowing you to run multiple tasks for the same service on a single EC2 instance and path-based routing allowing you to route incoming requests to different services depending on patterns in the URL path.

要进行连接,首先需要定义一个TargetGroup这样的

To wire it up you need first to define a TargetGroup like this

"TargetGroupService1" : {
  "Type" : "AWS::ElasticLoadBalancingV2::TargetGroup",
  "Properties" : {
    "Port": 10,
    "Protocol": "HTTP",
    "HealthCheckPath": "/service1",
    "VpcId": {"Ref" : "Vpc"}
  }
}

如果您使用动态端口映射,目标组中指定的端口是无关紧要的,因为它会被为每个目标动态分配的端口覆盖。

If you are using dynamic port mapping, the port specified in the target group is irrelevant since it will be overridden by the dynamically allocated port for each target.

接下来,您定义一个ListenerRule,该路径定义了路由到目标组:

Next you define a ListenerRule that defines the path that shall be routed to the TargetGroup:

"ListenerRuleService1": {
  "Type" : "AWS::ElasticLoadBalancingV2::ListenerRule",
  "Properties" : {
    "Actions" : [
      {
        "TargetGroupArn" : {"Ref": "TargetGroupService1"},
        "Type" : "forward"
      }
    ],
    "Conditions" : [
      {
        "Field" : "path-pattern",
        "Values" : [ "/service1" ]
      }
    ],
    "ListenerArn" : {"Ref": "Listener"},
    "Priority" : 1
  }
}

最后,将ECS服务与TargetGroup相关联。这样,ECS可以将您的任务容器自动注册为目标组中的目标(使用您在TaskDefinition中配置的主机端口)

Finally you associate your ECS Service with the TargetGroup. This enable ECS to automatically register your task containers as targets in the target group (with the host port that you have configured in your TaskDefinition)

"Service1": {
  "Type" : "AWS::ECS::Service",
  "DependsOn": [
    "ListenerRuleService1"
  ],
  "Properties" : {
    "Cluster" : { "Ref" : "ClusterName" },
    "DesiredCount" : 2,
    "Role" : "/ecsServiceRole",
    "TaskDefinition" : {"Ref":"Task1"},
    "LoadBalancers": [
      {
        "ContainerName": "Task1",
        "ContainerPort": "8080",
        "TargetGroupArn" : { "Ref" : "TargetGroupService1" }
      }
    ]
  }
}  

您可以在我写过的一篇博客文章中找到更多详细信息,请参见 Amazon ECS和应用程序负载平衡器

You can find more details in a blog post I have written about this, see Amazon ECS and Application Load Balancer

这篇关于ECS和应用程序负载均衡器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆