繁体   English   中英

AWS EC2 实例未加入 ECS 集群

[英]AWS EC2 instance not joining ECS cluster

我对一个与此线程中描述的问题非常相似的问题感到非常绝望。

https://github.com/OpenDroneMap/opendronemap-ecs/issues/14#issuecomment-432004023

当我将网络接口附加到我的 EC2 实例时,以便使用我的自定义 VPC 而不是默认 VPC,EC2 实例不再加入 ECS 集群。

这是我的地形定义。

provider "aws" {}

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
  enable_dns_support = true
  enable_dns_hostnames = true
  assign_generated_ipv6_cidr_block = true
}

resource "aws_internet_gateway" "main" {
  vpc_id = aws_vpc.main.id
}

resource "aws_subnet" "main" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.0.0/16"
  availability_zone = "us-west-2a"
  map_public_ip_on_launch = true
}

resource "aws_route_table" "main" {
  vpc_id = aws_vpc.main.id

}

resource "aws_route_table_association" "rta1" {

  subnet_id      = aws_subnet.main.id
  route_table_id = aws_route_table.main.id
}

resource "aws_route_table_association" "rta2" {
  gateway_id     = aws_internet_gateway.main.id
  route_table_id = aws_route_table.main.id
}

resource "aws_security_group" "sg-jenkins" {
  name        = "sg_jenkins"
  description = "Allow inbound traffic for Jenkins instance"
  vpc_id      = aws_vpc.main.id

  ingress = [
    {
      description      = "inbound all"
      from_port        = 0
      to_port          = 0
      protocol         = "-1"
      cidr_blocks      = ["0.0.0.0/0"]
      ipv6_cidr_blocks = ["::/0"]
      self            = null
      prefix_list_ids = null
      security_groups = null
    }
  ]

  egress = [
    {
      description      = "outbound all"
      from_port        = 0
      to_port          = 0
      protocol         = "-1"
      cidr_blocks      = ["0.0.0.0/0"]
      ipv6_cidr_blocks = ["::/0"]
      self            = null
      prefix_list_ids = null
      security_groups = null
    }
  ]

}

resource "aws_network_interface" "main" {
  subnet_id   = aws_subnet.main.id
  security_groups = [aws_security_group.sg-jenkins.id]
}

resource "aws_instance" "ec2_instance" {
  ami           = "ami-07764a7d8502d36a2"
  instance_type = "t2.micro"
  iam_instance_profile = "ecsInstanceRole"
  key_name = "fran"

  network_interface {
    device_index         = 0
    network_interface_id = aws_network_interface.main.id
  }

  user_data = <<EOF
  #!/bin/bash
  echo ECS_CLUSTER=cluster >> /etc/ecs/ecs.config
  EOF

  depends_on = [aws_internet_gateway.main]
}

### Task definition

resource "aws_ecs_task_definition" "jenkins-task" {
  family = "namespace"
  container_definitions = jsonencode([
    {
      name      = "jenkins"
      image     = "cnservices/jenkins-master"
      cpu       = 10
      memory    = 512
      essential = true
      portMappings = [
        {
          containerPort = 8080
          hostPort      = 8080
        }
      ]
    }
  ])

#  network_mode = "awsvpc"

  volume {
    name      = "service-storage"
    host_path = "/ecs/service-storage"
  }

  placement_constraints {
    type       = "memberOf"
    expression = "attribute:ecs.availability-zone in [us-west-2a]"
  }
}


### Cluster

resource "aws_ecs_cluster" "cluster" {
  name = "cluster"

  setting {
    name  = "containerInsights"
    value = "enabled"
  }
}

### Service

resource "aws_ecs_service" "jenkins-service" {
  name            = "jenkins-service"
  cluster         = aws_ecs_cluster.cluster.id
  task_definition = aws_ecs_task_definition.jenkins-task.arn
  desired_count   = 1
  #  iam_role        = aws_iam_role.foo.arn
  #  depends_on      = [aws_iam_role_policy.foo]

#  network_configuration {
#    security_groups = [aws_security_group.sg-jenkins.id]
#    subnets = [aws_subnet.main.id]
#  }

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

  placement_constraints {
    type       = "memberOf"
    expression = "attribute:ecs.availability-zone in [us-west-2a]"
  }
}

您尚未创建通往 IGW路线 因此,您的实例无法连接到 ECS 服务以向您的集群注册。 所以删除rta2并添加一个路由:

# not needed. to be removed.
# resource "aws_route_table_association" "rta2" {
#   gateway_id     = aws_internet_gateway.main.id
#   route_table_id = aws_route_table.main.id
# }

# add a missing route to the IGW
resource "aws_route" "r" {
  route_table_id              = aws_route_table.main.id
  gateway_id                  = aws_internet_gateway.main.id
  destination_cidr_block      = "0.0.0.0/0"
}

暂无
暂无

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

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