如何针对单个 ECS 服务附加两个目标组
Posted
技术标签:
【中文标题】如何针对单个 ECS 服务附加两个目标组【英文标题】:How to attach Two target group against single ECS services 【发布时间】:2019-11-28 14:37:16 【问题描述】:我正在寻找一种将两个目标组附加到单个 ECS 服务的方法,在其他情况下,我的容器公开了两个端口,但我只能将一个端口针对我的服务映射到 LB。
到目前为止,我能够创建一个新的侦听器和目标组,但是在创建目标组之后,我可以按预期看到所有内容,但目标组显示 There are no targets registered to this target group
这是我的目标组和监听器配置
target_group:
resource "aws_lb_target_group" "e_admin"
name = "$var.env_prefix_name-admin"
port = 5280
protocol = "HTTP"
vpc_id = "$aws_vpc.VPC.id"
health_check
path = "/admin"
healthy_threshold = 2
unhealthy_threshold = 10
port = 5280
timeout = 90
interval = 100
matcher = "401,200"
听众:'
resource "aws_lb_listener" "admin"
load_balancer_arn = "$aws_lb.admin_lb.arn"
port = "5280"
protocol = "HTTP"
default_action
target_group_arn = "$aws_lb_target_group.e_admin.id"
type = "forward"
我的问题是如何添加 ECS 集群 Autoscaling 组或如何将 ECS 集群中运行的所有实例添加到此目标组?
【问题讨论】:
【参考方案1】:AWS recently announced 支持 ECS 服务的多个目标组。
当前未发布的2.22.0 version of the AWS provider 包含对此的支持,方法是向aws_ecs_service
资源添加更多load_balancer
块。来自the acceptance tests的示例:
resource "aws_ecs_service" "with_alb"
name = "example"
cluster = "$aws_ecs_cluster.main.id"
task_definition = "$aws_ecs_task_definition.with_lb_changes.arn"
desired_count = 1
iam_role = "$aws_iam_role.ecs_service.name"
load_balancer
target_group_arn = "$aws_lb_target_group.test.id"
container_name = "ghost"
container_port = "2368"
load_balancer
target_group_arn = "$aws_lb_target_group.static.id"
container_name = "ghost"
container_port = "4501"
depends_on = [
"aws_iam_role_policy.ecs_service",
]
【讨论】:
【参考方案2】:根据https://docs.aws.amazon.com/AmazonECS/latest/developerguide/service-load-balancing.html,
每个服务有一个负载均衡器或目标组的限制。
如果要将自动缩放组附加到目标组,请使用aws_autoscaling_attachment
,
https://www.terraform.io/docs/providers/aws/r/autoscaling_attachment.html
resource "aws_autoscaling_attachment" "asg_attachment_bar"
autoscaling_group_name = "$aws_autoscaling_group.your_asg.id"
alb_target_group_arn = "$aws_alb_target_group.e_admin.arn"
【讨论】:
但是附加自动伸缩组的其他方式呢 使用aws_autoscaling_attachment
, terraform.io/docs/providers/aws/r/autoscaling_attachment.html
我将它用于 EKS 的 Auto Scaling 组,将多个端口连接到 nlb 目标组。您是否为每个端口创建了两个目标组?
是的,正如我提到的,这是第二个
你能分享一下你的配置吗以上是关于如何针对单个 ECS 服务附加两个目标组的主要内容,如果未能解决你的问题,请参考以下文章