简体   繁体   English

将 AWS ECS 放置约束与实例标签结合使用

[英]Using AWS ECS Placement Constraints with instance tags

Is it possible to constrain the placement of ECS tasks based on instance tags?是否可以根据实例标签来约束ECS任务的放置? I have EC2 instances in the ECS cluster that are tagged, and would like to use this to ensure certain tasks run on those instances?我在 ECS 集群中有标记的 EC2 实例,想用它来确保某些任务在这些实例上运行? I don't see how it can be done.我不知道如何做到。

https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-placement-constraints.html https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-placement-constraints.html

Here is a snippet of Terraform config to illustrate how to implement this approach这是 Terraform 配置的片段,用于说明如何实现此方法

data aws_ssm_parameter amazonLinux2 {
  name = "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
}

resource aws_instance elasticsearchInstance {
  ami = data.aws_ssm_parameter.amazonLinux2.value
  instance_type = "t2.medium"
  availability_zone = data.aws_availability_zones.available.names[0]
  subnet_id = aws_subnet.ecsPrivateSubnet.id
  associate_public_ip_address = false
  iam_instance_profile = aws_iam_instance_profile.ecs_agent.name

  user_data = <<EOT
#!/bin/bash
echo 'ECS_CLUSTER=clusterName' >> /etc/ecs/ecs.config
echo 'ECS_INSTANCE_ATTRIBUTES={"type": "elasticsearch"}' >> /etc/ecs/ecs.config
EOT
}

resource "aws_ecs_task_definition" "elasticsearchTask" {
  family = "elasticsearch"
  network_mode = "awsvpc"
  container_definitions = jsonencode([
    {
      name      = "elasticsearch"
      image     = "docker.elastic.co/elasticsearch/elasticsearch:7.15.2"
      cpu       = 2048
      memory    = 3942
      essential = true
      portMappings = [
        {
          containerPort = 9200
        }
      ]
    }
  ])
  placement_constraints {
    type       = "memberOf"
    expression = "attribute:type == elasticsearch"
  }
}

The is not a full configuration, but the core bits are setting the user_data to add ECS_INSTANCE_ATTRIBUTES in this case type=elasticsearch and then adding placement_constraints to the task definition that forces the task onto the instance.这不是一个完整的配置,但核心位正在设置user_data以添加ECS_INSTANCE_ATTRIBUTES在这种情况下type=elasticsearch elasticsearch 然后将placement_constraints添加到任务定义中以将任务强制到实例上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM