参考リンク
- チュートリアル: Amazon ECS CLI を使用して Fargate タスクのクラスターを作成する
- Amazon ECS コマンドラインインターフェイスの使用
- 運用ベースのecs-cli利用手引き
環境
$ aws --version aws-cli/2.0.0 Python/3.8.1 Darwin/18.7.0 botocore/2.0.0dev4 $ ecs-cli --version ecs-cli version 1.18.0 (*UNKNOWN)
ステップ 1: タスク実行 IAM ロールを作成する
タスク実行ロールのjsonを作成する。
cat <<EOF >task-execution-assume-role.json { "Version": "2012-10-17", "Statement": [ { "Sid": "", "Effect": "Allow", "Principal": { "Service": "ecs-tasks.amazonaws.com" }, "Action": "sts:AssumeRole" } ] } EOF
タスク実行ロールを作成する。
aws iam create-role --role-name ecsTaskExecutionRole --assume-role-policy-document file://task-execution-assume-role.json
タスク実行ロールにAWS管理ポリシーをアタッチする。
aws iam attach-role-policy --role-name ecsTaskExecutionRole --policy-arn arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy
ステップ 2: Amazon ECS CLI の設定
クラスター設定を作成する。
ecs-cli configure --cluster tutorial --default-launch-type FARGATE --config-name tutorial --region ap-northeast-1
以下のようなファイルができている。
$ cat ~/.ecs/config version: v1 default: tutorial clusters: tutorial: cluster: tutorial region: ap-northeast-1 default_launch_type: FARGATE
チュートリアルにしたがってECS CLI用のプロファイルを作ってもよいが、以下のリンク先で認証情報の優先順位を確認すると、AWS CLIのプロファイルを使ってくれるようなので、作らないで進める。
ステップ 3: クラスターを作成してセキュリティグループを設定する
クラスターを作成する。
ecs-cli up --cluster-config tutorial
$ ecs-cli up --cluster-config tutorial INFO[0010] Created cluster cluster=tutorial region=ap-northeast-1 INFO[0011] Waiting for your cluster resources to be created... INFO[0011] Cloudformation stack status stackStatus=CREATE_IN_PROGRESS INFO[0071] Cloudformation stack status stackStatus=CREATE_IN_PROGRESS VPC created: vpc-043a040708519ced9 Subnet created: subnet-0c4f0a5623cadd6dc Subnet created: subnet-0a38218b69be4acd1 Cluster creation succeeded.
CloudFormationでVPCが作成されているが、CloudFormationのリソースにはクラスターは含まれていない。
VPCデフォルトのセキュリティグループIDを取得する。
aws ec2 describe-security-groups --filters Name=vpc-id,Values=vpc-043a040708519ced9 --output yaml
$ aws ec2 describe-security-groups --filters Name=vpc-id,Values=vpc-043a040708519ced9 --output yaml SecurityGroups: - Description: default VPC security group GroupId: sg-02daae0c6a0e014e2 GroupName: default IpPermissions: - IpProtocol: '-1' IpRanges: [] Ipv6Ranges: [] PrefixListIds: [] UserIdGroupPairs: - GroupId: sg-02daae0c6a0e014e2 UserId: '600147515981' IpPermissionsEgress: - IpProtocol: '-1' IpRanges: - CidrIp: 0.0.0.0/0 Ipv6Ranges: [] PrefixListIds: [] UserIdGroupPairs: [] OwnerId: '600147515981' VpcId: vpc-043a040708519ced9
デフォルトのセキュリティグループに80番ポートの穴を開ける。
aws ec2 authorize-security-group-ingress --group-id sg-02daae0c6a0e014e2 --protocol tcp --port 80 --cidr 0.0.0.0/0
ステップ 4: 構成ファイルを作成する
docker-composeのファイルを作成する。
cat <<EOF >docker-compose.yml version: '3' services: web: image: amazon/amazon-ecs-sample ports: - "80:80" logging: driver: awslogs options: awslogs-group: tutorial awslogs-region: ap-northeast-1 awslogs-stream-prefix: web EOF
サービスのECSパラメータファイルを作成する。
cat <<EOF >ecs-params.yml version: 1 task_definition: task_execution_role: ecsTaskExecutionRole ecs_network_mode: awsvpc task_size: mem_limit: 0.5GB cpu_limit: 256 run_params: network_configuration: awsvpc_configuration: subnets: - "subnet-0c4f0a5623cadd6dc" - "subnet-0a38218b69be4acd1" security_groups: - "sg-02daae0c6a0e014e2" assign_public_ip: ENABLED EOF
ステップ 5: クラスターに設定ファイルをデプロイする
サービスをデプロイする。compose up
だとタスクが、compose service up
だとサービスが作成される。
ecs-cli compose --project-name tutorial service up --create-log-groups --cluster-config tutorial
$ ecs-cli compose --project-name tutorial service up --create-log-groups --cluster-config tutorial INFO[0005] Using ECS task definition TaskDefinition="tutorial:1" INFO[0005] Created Log Group tutorial in ap-northeast-1 INFO[0006] Created an ECS service service=tutorial taskDefinition="tutorial:1" INFO[0006] Updated ECS service successfully desiredCount=1 force-deployment=false service=tutorial INFO[0021] (service tutorial) has started 1 tasks: (task 3d152bf3-9beb-4b96-b5d2-10df121092c5). timestamp="2020-02-16 15:00:56 +0000 UTC" INFO[0062] Service status desiredCount=1 runningCount=1 serviceName=tutorial INFO[0062] (service tutorial) has reached a steady state. timestamp="2020-02-16 15:01:26 +0000 UTC" INFO[0062] ECS Service has reached a stable state desiredCount=1 runningCount=1 serviceName=tutorial
ステップ 6: クラスターで実行中のコンテナを確認する
コンテナを確認する。
ecs-cli compose --project-name tutorial service ps --cluster-config tutorial --ecs-profile tutorial-profile
$ ecs-cli compose --project-name tutorial service ps --cluster-config tutorial Name State Ports TaskDefinition Health 3d152bf3-9beb-4b96-b5d2-10df121092c5/web RUNNING 52.193.39.152:80->80/tcp tutorial:1 UNKNOWN
ステップ 7: コンテナログを表示する
コンテナログを確認する。
ecs-cli logs --task-id 3d152bf3-9beb-4b96-b5d2-10df121092c5 --follow --cluster-config tutorial
ステップ 8: クラスターのタスクをスケーリングする
タスクの数を増やす。
ecs-cli compose --project-name tutorial service scale 2 --cluster-config tutorial
$ ecs-cli compose --project-name tutorial service scale 2 --cluster-config tutorial INFO[0005] Updated ECS service successfully desiredCount=2 force-deployment=false service=tutorial INFO[0005] Service status desiredCount=2 runningCount=1 serviceName=tutorial INFO[0026] (service tutorial) has started 1 tasks: (task 9e726b32-73c3-4930-b581-4ef0306f826a). timestamp="2020-02-16 15:24:33 +0000 UTC" INFO[0046] Service status desiredCount=2 runningCount=2 serviceName=tutorial INFO[0046] (service tutorial) has reached a steady state. timestamp="2020-02-16 15:24:54 +0000 UTC" INFO[0046] ECS Service has reached a stable state desiredCount=2 runningCount=2 serviceName=tutorial $ ecs-cli compose --project-name tutorial service ps --cluster-config tutorial Name State Ports TaskDefinition Health 3d152bf3-9beb-4b96-b5d2-10df121092c5/web RUNNING 52.193.39.152:80->80/tcp tutorial:1 UNKNOWN 9e726b32-73c3-4930-b581-4ef0306f826a/web RUNNING 13.113.189.21:80->80/tcp tutorial:1 UNKNOWN
ステップ 9: (オプション) ウェブアプリケーションを表示する
ブラウザでアクセスして確認する。
ステップ 10: クリーンアップ
サービスを削除する。
ecs-cli compose --project-name tutorial service down --cluster-config tutorial
$ ecs-cli compose --project-name tutorial service down --cluster-config tutorial INFO[0000] Updated ECS service successfully desiredCount=0 force-deployment=false service=tutorial INFO[0000] Service status desiredCount=0 runningCount=2 serviceName=tutorial INFO[0020] Service status desiredCount=0 runningCount=0 serviceName=tutorial INFO[0020] (service tutorial) has stopped 2 running tasks: (task 3d152bf3-9beb-4b96-b5d2-10df121092c5) (task 9e726b32-73c3-4930-b581-4ef0306f826a). timestamp="2020-02-16 15:28:28 +0000 UTC" INFO[0020] ECS Service has reached a stable state desiredCount=0 runningCount=0 serviceName=tutorial INFO[0020] Deleted ECS service service=tutorial INFO[0021] ECS Service has reached a stable state desiredCount=0 runningCount=0 serviceName=tutorial
クラスターを削除する。
ecs-cli down --force --cluster-config tutorial
ローカルで起動する
docker-compose up
すると、ログドライバーがawslogs
なためエラーとなってしまう。
この設定だけ上書きするファイルを作成する。
cat <<EOF >docker-compose-local.yml version: '3' services: web: logging: driver: json-file EOF
設定ファイルを上書きしながら起動する。
docker-compose -f docker-compose.yml -f docker-compose-local.yml up -d
$ docker-compose -f docker-compose.yml -f docker-compose-local.yml up -d Creating network "poc-env_default" with the default driver Pulling web (amazon/amazon-ecs-sample:)... latest: Pulling from amazon/amazon-ecs-sample 72d97abdfae3: Pull complete 9db40311d082: Pull complete 991f1d4df942: Pull complete 9fd8189a392d: Pull complete Digest: sha256:36c7b282abd0186e01419f2e58743e1bf635808231049bbc9d77e59e3a8e4914 Status: Downloaded newer image for amazon/amazon-ecs-sample:latest Creating poc-env_web_1 ... done
停止する。
docker-compose down
$ docker-compose down Stopping poc-env_web_1 ... done Removing poc-env_web_1 ... done Removing network poc-env_default