使用 Terraform,我尽了最大的努力来找出如何创建 1 个 ECS 集群并在其下运行多个服务。因此,基本上我有 2 个不同的容器想要在这个 1 个 ECS 集群上运行。
我该怎么做?我见过很多例子,但通常 1 个服务搭配 1 个 ECS 集群
所以从这个链接我得到了一个模板https://www.terraform.io/docs/providers/aws/d/ecs_task_definition.html
# Simply specify the family to find the latest ACTIVE revision in that family.
data "aws_ecs_task_definition" "mongo" {
task_definition = "${aws_ecs_task_definition.mongo.family}"
}
resource "aws_ecs_cluster" "foo" {
name = "foo"
}
resource "aws_ecs_task_definition" "mongo" {
family = "mongodb"
container_definitions = <<DEFINITION
[
{
"cpu": 128,
"environment": [{
"name": "SECRET",
"value": "KEY"
}],
"essential": true,
"image": "mongo:latest",
"memory": 128,
"memoryReservation": 64,
"name": "mongodb"
}
]
DEFINITION
}
resource "aws_ecs_service" "mongo" {
name = "mongo"
cluster = "${aws_ecs_cluster.foo.id}"
desired_count = 2
# Track the latest ACTIVE revision
task_definition = "${aws_ecs_task_definition.mongo.family}:${max("${aws_ecs_task_definition.mongo.revision}", "${data.aws_ecs_task_definition.mongo.revision}")}"
}
那么假设我想添加一个节点应用服务,它会是什么样子?并让它们都在同一集群上运行
谢谢!
答案1
您只需创建另一个aws_ecs_service和aws_ecs_task_definition资源。
如果希望它在同一个集群上运行,只需在簇参数。
所以它看起来像这样
# Simply specify the family to find the latest ACTIVE revision in that family.
data "aws_ecs_task_definition" "mongo" {
task_definition = "${aws_ecs_task_definition.mongo.family}"
}
data "aws_ecs_task_definition" "nginx" {
task_definition = "${aws_ecs_task_definition.nginx.family}"
}
resource "aws_ecs_cluster" "foo" {
name = "foo"
}
# ====================== TASKS ===================
resource "aws_ecs_task_definition" "mongo" {
family = "mongodb"
container_definitions = <<DEFINITION
[
{
"cpu": 128,
"environment": [{
"name": "SECRET",
"value": "KEY"
}],
"essential": true,
"image": "mongo:latest",
"memory": 128,
"memoryReservation": 64,
"name": "mongodb"
}
]
DEFINITION
}
resource "aws_ecs_task_definition" "nginx" {
family = "nginx"
container_definitions = <<DEFINITION
[
{
"cpu": 128,
"environment": [{
"name": "SECRET",
"value": "KEY"
}],
"essential": true,
"image": "nginx:latest",
"memory": 128,
"memoryReservation": 64,
"name": "nginx"
}
]
DEFINITION
}
# ====================== SERVICES ===================
resource "aws_ecs_service" "mongo" {
name = "mongo"
cluster = "${aws_ecs_cluster.foo.id}"
desired_count = 2
# Track the latest ACTIVE revision
task_definition = "${aws_ecs_task_definition.mongo.family}:${max("${aws_ecs_task_definition.mongo.revision}", "${data.aws_ecs_task_definition.mongo.revision}")}"
}
resource "aws_ecs_service" "nginx" {
name = "nginx"
cluster = "${aws_ecs_cluster.foo.id}"
desired_count = 2
# Track the latest ACTIVE revision
task_definition = "${aws_ecs_task_definition.nginx.family}:${max("${aws_ecs_task_definition.nginx.revision}", "${data.aws_ecs_task_definition.nginx.revision}")}"
}
还有另一种在 ECS 上运行多个容器的方法。
您可以将 Nginx 和 MongoDB 放在同一个实例上并放在同一个任务/服务下。
它看起来像这样
# Simply specify the family to find the latest ACTIVE revision in that family.
data "aws_ecs_task_definition" "webapp" {
task_definition = "${aws_ecs_task_definition.webapp.family}"
}
resource "aws_ecs_cluster" "foo" {
name = "foo"
}
# ====================== TASKS ===================
resource "aws_ecs_task_definition" "webapp" {
family = "webapp"
container_definitions = <<DEFINITION
[
{
"cpu": 128,
"environment": [{
"name": "SECRET",
"value": "KEY"
}],
"essential": true,
"image": "nginx:latest",
"memory": 128,
"memoryReservation": 64,
"name": "nginx"
},
{
"cpu": 128,
"environment": [{
"name": "SECRET",
"value": "KEY"
}],
"essential": true,
"image": "mongo:latest",
"memory": 128,
"memoryReservation": 64,
"name": "mongodb"
}
]
DEFINITION
}
# ====================== SERVICES ===================
resource "aws_ecs_service" "webapp" {
name = "webapp"
cluster = "${aws_ecs_cluster.foo.id}"
desired_count = 1
# Track the latest ACTIVE revision
task_definition = "${aws_ecs_task_definition.webapp.family}:${max("${aws_ecs_task_definition.webapp.revision}", "${data.aws_ecs_task_definition.webapp.revision}")}"
}
第一种方法的优点是您可以独立扩展和管理每个容器。
第二种方法的优点是容器将放在同一个 EC2 实例上,并且可以链接但不能独立扩展。当您将所需数量设置为 N 时,您将获得 N 个“webapp”(因此 Nginx 和 MongoDB 实例都存在)。使用第一种方法,您可以拥有 N 个 Nginx 和 X 个 MongoDB。
希望这可以帮助。
笔记:Terraform 代码尚未测试。
答案2
@babababa 就您所说的确保容器实例(实际的 AWS 主机)加入正确的集群而言,最佳做法是使用用户数据脚本让它们在创建实例时加入集群。这样可以避免您不得不手动编辑 ECS 代理配置。
user data
因此,无论您如何创建构成 ECS 集群的实际 EC2 实例(手动、自动扩展组或通过 Terraform),都应该为这些实例添加类似于以下内容的内容:
#!/bin/bash
echo ECS_CLUSTER=your_cluster_name >> /etc/ecs/ecs.config
答案3
是的,我想我后来发现了问题所在。我必须编辑文件/etc/ecs/ecs.config
并设置集群名称,以便实例在集群中注册,然后使用不同的任务定义在实例上运行服务。
我之前遇到的问题是实例最终进入集群default
,并且无法在其上运行我的服务,因为任务定义是由我想要的集群设置的,而不是default
集群
因此,解决我的问题的问题是/etc/ecs/ecs.config
适当编辑