血と汗となみだを流す

個人の思ったこと、やったことの吐き出し口です。

Developers.ioの記事を読んでやってみる「ECS CLIでAmazon ECSを操作してみた」(2018/02/27)

対象

やってみる

ECS CLI

  • ecs-cliのインストール
$ sudo curl -o /usr/local/bin/ecs-cli https://s3.amazonaws.com/amazon-ecs-cli/ecs-cli-linux-amd64-latest
  • 実行権限の付与
$ sudo chmod +x /usr/local/bin/ecs-cli
$ which ecs-cli
$ ecs-cli --version
ecs-cli version 1.3.0 (*UNKNOWN)

ECSクラスタを作成する

  • キーペアを事前に作っておく
  • region/clusterオプションは環境変数にセットでいけた
    • region: AWS_DEFAULT_REGION
    • cluster: ECS_CLUSTER
  • ecs-cliでECSクラスタ起動
$ ecs-cli up --keypair [キーペア名] --capability-iam --size 2 --instance-type t2.micro
INFO[0000] Created cluster                               cluster=test-cluster region=ap-northeast-1
INFO[0001] Waiting for your cluster resources to be created...
INFO[0001] Cloudformation stack status                   stackStatus="CREATE_IN_PROGRESS"
INFO[0063] Cloudformation stack status                   stackStatus="CREATE_IN_PROGRESS"
INFO[0124] Cloudformation stack status                   stackStatus="CREATE_IN_PROGRESS"
VPC created: vpc-xxxxxxxx
Security Group created: sg-xxxxxxxx
Subnet created: subnet-xxxxxxx1
Subnet created: subnet-xxxxxxx2
Cluster creation succeeded.

Composeでタスクを動かす

  • docker-compose用のYAMLを作る
$ vi wordpress.yml
wordpress:
  image: wordpress
  ports:
    - "80:80"
  links:
    - mysql
mysql:
  image: mysql
  mem_limit: 268435456
  environment:
    MYSQL_ROOT_PASSWORD: password
  • ecs-cli compose でupする
$ ecs-cli compose --file wordpress.yml up
INFO[0000] Using ECS task definition                     TaskDefinition="ecs:2"
INFO[0000] Starting container...                         container="60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/wordpress"
INFO[0000] Starting container...                         container="60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/mysql"
INFO[0000] Describe ECS container status                 container="60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/wordpress" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecs:2"
INFO[0000] Describe ECS container status                 container="60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/mysql" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecs:2"
INFO[0012] Describe ECS container status                 container="60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/wordpress" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecs:2"
INFO[0012] Describe ECS container status                 container="60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/mysql" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecs:2"
INFO[0025] Describe ECS container status                 container="60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/wordpress" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecs:2"
INFO[0025] Describe ECS container status                 container="60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/mysql" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecs:2"
INFO[0037] Describe ECS container status                 container="60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/wordpress" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecs:2"
INFO[0037] Describe ECS container status                 container="60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/mysql" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecs:2"
INFO[0043] Started container...                          container="60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/wordpress" desiredStatus=RUNNING lastStatus=RUNNING taskDefinition="ecs:2"
INFO[0043] Started container...                          container="60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/mysql" desiredStatus=RUNNING lastStatus=RUNNING taskDefinition="ecs:2"
  • AWSコンソールでタスクを確認 f:id:Anorlondo448:20180227013523p:plain
  • コンテナ確認
$ ecs-cli ps
Name                                            State    Ports                     TaskDefinition
60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/wordpress  RUNNING  54.95.194.136:80->80/tcp  ecs:2
60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/mysql      RUNNING                            ecs:2

タスクをスケールさせる

  • scaleオプションを2にする
$ ecs-cli compose --file wordpress.yml scale 2
INFO[0000] Starting container...                         container="13dba493-325d-49b6-8493-890621830da4/wordpress"
INFO[0000] Starting container...                         container="13dba493-325d-49b6-8493-890621830da4/mysql"
INFO[0000] Describe ECS container status                 container="13dba493-325d-49b6-8493-890621830da4/mysql" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecs:2"
INFO[0000] Describe ECS container status                 container="13dba493-325d-49b6-8493-890621830da4/wordpress" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecs:2"
INFO[0012] Describe ECS container status                 container="13dba493-325d-49b6-8493-890621830da4/mysql" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecs:2"
INFO[0012] Describe ECS container status                 container="13dba493-325d-49b6-8493-890621830da4/wordpress" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecs:2"
INFO[0024] Describe ECS container status                 container="13dba493-325d-49b6-8493-890621830da4/mysql" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecs:2"
INFO[0024] Describe ECS container status                 container="13dba493-325d-49b6-8493-890621830da4/wordpress" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecs:2"

INFO[0037] Describe ECS container status                 container="13dba493-325d-49b6-8493-890621830da4/mysql" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecs:2"
INFO[0037] Describe ECS container status                 container="13dba493-325d-49b6-8493-890621830da4/wordpress" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecs:2"
INFO[0043] Started container...                          container="13dba493-325d-49b6-8493-890621830da4/mysql" desiredStatus=RUNNING lastStatus=RUNNING taskDefinition="ecs:2"
INFO[0043] Started container...                          container="13dba493-325d-49b6-8493-890621830da4/wordpress" desiredStatus=RUNNING lastStatus=RUNNING taskDefinition="ecs:2"
  • コンテナ確認。2台ずつにスケールしている
$ ecs-cli ps
Name                                            State    Ports                     TaskDefinition
13dba493-325d-49b6-8493-890621830da4/mysql      RUNNING                            ecs:2
13dba493-325d-49b6-8493-890621830da4/wordpress  RUNNING  54.249.30.126:80->80/tcp  ecs:2
60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/wordpress  RUNNING  54.95.194.136:80->80/tcp  ecs:2
60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/mysql      RUNNING                            ecs:2

ECSサービスとして定義する

  • ECSサービスとして登録する前に、今上がっているタスクをダウンさせる
$ ecs-cli compose --file wordpress.yml down
INFO[0000] Stopping container...                         container="13dba493-325d-49b6-8493-890621830da4/mysql"
INFO[0000] Stopping container...                         container="13dba493-325d-49b6-8493-890621830da4/wordpress"
INFO[0000] Stopping container...                         container="60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/wordpress"
INFO[0000] Stopping container...                         container="60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/mysql"
INFO[0000] Describe ECS container status                 container="13dba493-325d-49b6-8493-890621830da4/mysql" desiredStatus=STOPPED lastStatus=RUNNING taskDefinition="ecs:2"
INFO[0000] Describe ECS container status                 container="13dba493-325d-49b6-8493-890621830da4/wordpress" desiredStatus=STOPPED lastStatus=RUNNING taskDefinition="ecs:2"
INFO[0000] Describe ECS container status                 container="60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/wordpress" desiredStatus=STOPPED lastStatus=RUNNING taskDefinition="ecs:2"
INFO[0000] Describe ECS container status                 container="60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/mysql" desiredStatus=STOPPED lastStatus=RUNNING taskDefinition="ecs:2"
INFO[0006] Stopped container...                          container="13dba493-325d-49b6-8493-890621830da4/mysql" desiredStatus=STOPPED lastStatus=STOPPED taskDefinition="ecs:2"
INFO[0006] Stopped container...                          container="13dba493-325d-49b6-8493-890621830da4/wordpress" desiredStatus=STOPPED lastStatus=STOPPED taskDefinition="ecs:2"
INFO[0006] Stopped container...                          container="60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/wordpress" desiredStatus=STOPPED lastStatus=STOPPED taskDefinition="ecs:2"
INFO[0006] Stopped container...                          container="60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/mysql" desiredStatus=STOPPED lastStatus=STOPPED taskDefinition="ecs:2"
  • 停止確認
$ ecs-cli ps
Name                                            State                Ports                     TaskDefinition
13dba493-325d-49b6-8493-890621830da4/mysql      STOPPED ExitCode: 0                            ecs:2
13dba493-325d-49b6-8493-890621830da4/wordpress  STOPPED ExitCode: 0  54.249.30.126:80->80/tcp  ecs:2
60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/wordpress  STOPPED ExitCode: 0  54.95.194.136:80->80/tcp  ecs:2
60ca60aa-c320-43e2-8659-ad7d4e8d9cf2/mysql      STOPPED ExitCode: 0                            ecs:2
  • サービスとして起動
$ ecs-cli compose --file wordpress.yml service up
INFO[0000] Using ECS task definition                     TaskDefinition="ecs:3"
INFO[0000] Created an ECS service                        service=ecs taskDefinition="ecs:3"
INFO[0000] Updated ECS service successfully              desiredCount=1 serviceName=ecs
INFO[0015] Service status                                desiredCount=1 runningCount=1 serviceName=ecs
INFO[0015] (service ecs) has started 1 tasks: (task 1f26eaa3-940a-4482-8c03-334621156a05).  timestamp=2018-02-26 17:09:54 +0000 UTC
INFO[0015] ECS Service has reached a stable state        desiredCount=1 runningCount=1 serviceName=ecs
  • コンテナ確認
$ ecs-cli ps
Name                                            State                Ports                     TaskDefinition
1f26eaa3-940a-4482-8c03-334621156a05/mysql      RUNNING                                        ecs:3
1f26eaa3-940a-4482-8c03-334621156a05/wordpress  RUNNING              54.249.30.126:80->80/tcp  ecs:3

ECSサービスを削除する

  • サービスを削除する
$ ecs-cli compose --file wordpress.yml service rm
INFO[0000] Updated ECS service successfully              desiredCount=0 serviceName=ecs
INFO[0000] Service status                                desiredCount=0 runningCount=1 serviceName=ecs
INFO[0030] Service status                                desiredCount=0 runningCount=0 serviceName=ecs
INFO[0030] (service ecs) has stopped 1 running tasks: (task 1f26eaa3-940a-4482-8c03-334621156a05).  timestamp=2018-02-26 17:13:16 +0000 UTC
INFO[0030] (service ecs) has reached a steady state.     timestamp=2018-02-26 17:13:26 +0000 UTC
INFO[0030] ECS Service has reached a stable state        desiredCount=0 runningCount=0 serviceName=ecs
INFO[0030] Deleted ECS service                           service=ecs
INFO[0030] ECS Service has reached a stable state        desiredCount=0 runningCount=0 serviceName=ecs
  • サービス削除確認 f:id:Anorlondo448:20180227021441p:plain

ECSクラスタ停止

$ ecs-cli down --force
INFO[0000] Waiting for your cluster resources to be deleted...
INFO[0000] Cloudformation stack status                   stackStatus="DELETE_IN_PROGRESS"
INFO[0066] Cloudformation stack status                   stackStatus="DELETE_IN_PROGRESS"
INFO[0126] Cloudformation stack status                   stackStatus="DELETE_IN_PROGRESS"
INFO[0187] Deleted cluster                               cluster=test-cluster
プライバシーポリシー