创建Task Defination

本节我们将创建三个Task Defination(Web, Cats, Dogs)

Task Defination使用JSON格式定义了如何启动一个docker容器。里面包括了以下信息:

  • 使用的镜像
  • 端口映射(Host端口和Container端口)
  • 需要的内存和cpu
  • 环境变量
  • 网络配置(bridge或awsvpc)

创建 Web Task Defination

进入Task definations页面,创建一个新的Task Defination:

image-20231018093544654

命名为webdef, Launch Type选择EC2;网络模式选择bridge类型,设置CPU和内存大小:

image-20231018094107641

在Container部署,使用第一节创建完成后的web应用的ECR地址,其他保持默认:

image-20231018093823042

最后点击创建

创建Cats Task Defination

上面创建的Task Defination是通过控制台操作的,这里我们使用JSON的方式,来避免重复的手工操作:

image-20231018094334520

JSON定义如下,替换image, awslogs-region, account id变量

{:
    "requiresCompatibilities": [
        "EC2"
    ],
    "family": "catsdef",
    "containerDefinitions": [
        {
            "name": "cats",
            "image": "<YOUR CATS ECR IMAGE URI>",
            "portMappings": [
                {
                    "name": "cats-80-tcp",
                    "containerPort": 80,
                    "hostPort": 0,
                    "protocol": "tcp",
                    "appProtocol": "http"
                }
            ],
            "essential": true,
            "environment": [],
            "environmentFiles": [],
            "mountPoints": [],
            "volumesFrom": [],
            "logConfiguration": {
                "logDriver": "awslogs",
                "options": {
                    "awslogs-create-group": "true",
                    "awslogs-group": "/ecs/catsdef",
                    "awslogs-region": "<YOUR REGION>",
                    "awslogs-stream-prefix": "ecs"
                }
            }
        }
    ],
    "volumes": [],
    "networkMode": "bridge",
    "memory": "1024",
    "cpu": "512",
    "runtimePlatform": {
        "cpuArchitecture": "X86_64",
        "operatingSystemFamily": "LINUX"
    },
    "executionRoleArn": "arn:aws:iam::<YOUR ACCOUNT ID>:role/ecsTaskExecutionRole"
}

创建Dogs Task Defination

通过JSON创建Dogs Task Defination,同样替换image, awslogs-region, account id变量:

{
    "requiresCompatibilities": [
        "FARGATE"
    ],
    "family": "dogsdef",
    "containerDefinitions": [
        {
            "name": "dogs",
            "image": "<YOUR DOGS ECR IMAGE URI>",
            "portMappings": [
                {
                    "name": "dogs-80-tcp",
                    "containerPort": 80,
                    "hostPort": 80,
                    "protocol": "tcp",
                    "appProtocol": "http"
                }
            ],
            "essential": true,
            "environment": [],
            "environmentFiles": [],
            "mountPoints": [],
            "volumesFrom": [],
            "logConfiguration": {
                "logDriver": "awslogs",
                "options": {
                    "awslogs-create-group": "true",
                    "awslogs-group": "/ecs/dogsdef",
                    "awslogs-region": "<YOUR REGION>",
                    "awslogs-stream-prefix": "ecs"
                }
            }
        }
    ],
    "volumes": [],
    "networkMode": "awsvpc",
    "memory": "1024",
    "cpu": "512",
    "runtimePlatform": {
        "cpuArchitecture": "X86_64",
        "operatingSystemFamily": "LINUX"
    },
    "executionRoleArn": "arn:aws:iam::<YOUR ACCOUNT ID>:role/ecsTaskExecutionRole"
}

注意Dogs Task Defination使用的是Fargate,而前面两个Defination使用的是EC2