首页 > 解决方案 > 在服务部署上启动实例

问题描述

我正在使用 Terraform 在 AWS 上部署 ECS EC2 集群。

我的管道正在从 docker-compose 创建一个新的任务定义,然后更新服务以使用此任务定义。所需计数为 1,deployment_minimum_healthy_percent 为 100,deployment_maximum_percent 为 200。

预期的行为是自动缩放组将启动一个新的 EC2 实例来部署新任务,然后终止旧的。发生的情况是我收到一条错误消息:“服务 X 无法放置任务,因为没有容器实例满足其所有要求。最接近的匹配容器实例没有足够的可用内存。”

没有创建实例并且回滚部署。如何确保在部署我的服务时创建了一个额外的实例?

这是我的 Terraform 代码:

resource "aws_ecs_cluster" "main_cluster" {
  name        = var.application_name

  tags = {
    Environment = var.environment_name
  }
}

data "aws_ecs_task_definition" "main_td" {
  task_definition = var.application_name
}

resource "aws_ecs_service" "main_service" {
  name            = var.application_name
  cluster         = aws_ecs_cluster.main_cluster.id
  launch_type     = "EC2"
  scheduling_strategy = "REPLICA"
  task_definition = "${data.aws_ecs_task_definition.main_td.family}:${data.aws_ecs_task_definition.main_td.revision}"
  desired_count   = var.target_capacity
  deployment_minimum_healthy_percent = 100
  deployment_maximum_percent         = 200
  health_check_grace_period_seconds  = 10
  wait_for_steady_state = false
  force_new_deployment = true

  load_balancer {
    target_group_arn = aws_lb_target_group.main_tg.arn
    container_name   = var.container_name
    container_port   = var.container_port
  }

  ordered_placement_strategy {
    type = "binpack"
    field = "memory"
  }

  deployment_circuit_breaker {
    enable = true
    rollback = true
  }

  lifecycle {
    ignore_changes = [desired_count]
  }

  tags = {
    Environment = var.environment_name
  }
}

自动缩放组:

data "template_file" "user_data" {
  template = "${file("${path.module}/user_data.sh")}"

  vars = {
    ecs_cluster = "${aws_ecs_cluster.main_cluster.name}"
  }
}

resource "aws_launch_configuration" "main_lc" {
  name          = var.application_name
  image_id      = var.ami_id
  instance_type = var.instance_type
  associate_public_ip_address = true
  iam_instance_profile = "arn:aws:iam::812844034365:instance-profile/ecsInstanceRole"
  security_groups = ["${aws_security_group.main_sg.id}"]

  root_block_device {
    volume_size = "30"
    volume_type = "gp3"
  }

  user_data = "${data.template_file.user_data.rendered}"
}

resource "aws_autoscaling_policy" "main_asg_policy" {
  name                   = "${var.application_name}-cpu-scale-policy"
  policy_type            = "TargetTrackingScaling"
  autoscaling_group_name = aws_autoscaling_group.main_asg.name
  estimated_instance_warmup = 10

  target_tracking_configuration {
    predefined_metric_specification {
      predefined_metric_type = "ASGAverageCPUUtilization"
    }

    target_value = 40.0
  }
}

resource "aws_autoscaling_group" "main_asg" {
  name                 = var.application_name
  launch_configuration = aws_launch_configuration.main_lc.name
  min_size             = var.target_capacity
  max_size             = var.target_capacity * 2
  health_check_type    = "EC2"
  health_check_grace_period = 10
  default_cooldown     = 30
  desired_capacity     = var.target_capacity
  vpc_zone_identifier  = data.aws_subnet_ids.subnets.ids
  wait_for_capacity_timeout = "3m"

  instance_refresh {
    strategy = "Rolling"
    preferences {
      min_healthy_percentage = 100
    }
  }
}

模块在这里发布https://registry.terraform.io/modules/hboisgibault/ecs-cluster/aws/latest

标签: terraformamazon-ecsautoscalingaws-auto-scaling

解决方案


推荐阅读