我无法使用Terraform将自动扩展组分配给ECS集群

时间:2019-03-25 08:42:46

标签: amazon-web-services terraform amazon-ecs

我正在尝试使用服务创建ECS集群,但是我无法设置自动扩展,因此集群中不会启动任何实例:

  

service my_service无法放置任务,因为没有容器   实例满足其所有要求。原因:没有容器实例   在您的集群中找到。有关更多信息,请参见   故障排除部分。

这是我的Terraform配置(仅相关配置):

集群和服务

resource "aws_ecs_cluster" "my_cluster" {
  name = "my_cluster"
}

resource "aws_ecs_service" "my_service" {
  name            = "my_service"
  cluster         = "${aws_ecs_cluster.my_cluster.id}"
  task_definition = "${aws_ecs_task_definition.my_tf.arn}"
  desired_count   = 1
  iam_role        = "${aws_iam_role.ecs-service-role.id}"
}

自动缩放

resource "aws_launch_configuration" "launch_config" {
  name_prefix          = "my_lc"
  image_id             = "${data.aws_ami.ubuntu.id}"
  instance_type        = "t2.micro"
  user_data            = "${data.template_file.user_data.rendered}"
  security_groups      = ["${aws_security_group.my_sg.id}"]
  iam_instance_profile = "${aws_iam_instance_profile.ecs-instance-profile.id}"

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_autoscaling_group" "autoscaling_group" {
  name                 = "my_autoscaling_group"
  max_size             = 2
  min_size             = 1
  launch_configuration = "${aws_launch_configuration.launch_config.name}"
  vpc_zone_identifier  = ["${aws_subnet.public.id}"]
}


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

  vars {
    cluster_name = "${aws_ecs_cluster.my_cluster.name}"
  }
}

templates / user-data.sh

#!/bin/bash

# ECS config
{
  echo "ECS_CLUSTER=${cluster_name}"
} >> /etc/ecs/ecs.config

start ecs

AMI

data "aws_ami" "ubuntu" {
  most_recent = true

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["099720109477"]
}

据我所知,群集和自动伸缩组通过启动配置中的user_data链接,但似乎不起作用。

我想念什么?

2 个答案:

答案 0 :(得分:0)

为使此功能正常运行,您缺少一个 key 组件。 Cloudwatch警报

Cloudwatch将提醒自动伸缩组在需要时采取措施。配置示例:

这将创建必要的警报,以在未充分利用计算机时按比例缩小。通过var调整您的需求。

resource "aws_cloudwatch_metric_alarm" "alarm-cpu-down" {
  alarm_name          = "ecs-down"
  comparison_operator = "LessThanOrEqualToThreshold"
  evaluation_periods  = "${var.evaluation_periods}"
  metric_name         = "${var.metric_name}"
  namespace           = "${var.namespace}"
  period              = "${var.period_down}"
  statistic           = "${var.statistic}"
  threshold           = "${var.threshold_down}"
  dimensions          = "${map(var.dimension_name, var.dimension_value == "false" ? var.autoscaling_group_name[count.index] : var.dimension_value)}"

  alarm_description = "This metric monitors CPU utilization down"
  alarm_actions     = ["${aws_autoscaling_policy.down-scale.*.arn}"]
}

这将创建必要的警报,以在需要计算机时扩大规模(CPUReservation(=您的服务需要什么)大于可用的机器)

resource "aws_cloudwatch_metric_alarm" "alarm-cpu-up" {
  alarm_name          = "${var.environment}-${var.project}-${var.name}-${var.metric_name}-up${count.index}"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  evaluation_periods  = "${var.evaluation_periods}"
  metric_name         = "${var.metric_name}"
  namespace           = "${var.namespace}"
  period              = "${var.period_up}"
  statistic           = "${var.statistic}"
  threshold           = "${var.threshold_up}"
  dimensions          = "${map(var.dimension_name, var.dimension_value == "false" ? var.autoscaling_group_name[count.index] : var.dimension_value)}"

  alarm_description = "This metric monitors CPU utilization up"
  alarm_actions     = ["${aws_autoscaling_policy.up-scale.*.arn}"]
}

这些是实际的自动缩放策略。触发警报后,这是cloudwatch警报需要执行的操作。

resource "aws_autoscaling_policy" "up-scale" {   
    count = "${var.num_asg}"   
    name  = "${var.environment}-${var.project}-${var.name}-${var.metric_name}-up${count.index}" 
    autoscaling_group_name = "${var.autoscaling_group_name[count.index]}"  
    adjustment_type        = "${var.adjustment_type}"   
    policy_type            = "${var.policy_type}"   
    cooldown               = "${var.cooldown_up}"   
    scaling_adjustment     = "${var.adjustment_up}" 
}

resource "aws_autoscaling_policy" "down-scale" {   
    count = "${var.num_asg}"   
    name  = "${var.environment}-${var.project}-${var.name}-${var.metric_name}-down${count.index}" 
    autoscaling_group_name = "${var.autoscaling_group_name[count.index]}"  
    adjustment_type        = "${var.adjustment_type}"   
    policy_type            = "${var.policy_type}"   
    cooldown               = "${var.cooldown_down}"   
    scaling_adjustment     = "${var.adjustment_down}" 
}

答案 1 :(得分:0)

您需要一种经ECS优化的AMI:https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-optimized_AMI.html

您需要创建自己的AMI,这并不容易。