ようへいの日々精進XP

よかろうもん

ecs-cli チュートリアル

おはげようございます。来年の re:Invent は現地で...という目標を胸に英語の勉強を始めたいかっぱです。

tl;dr

re:Invent での発表にて ECS 専用のコマンドラインツールが利用出来るようになったとのことでザクっと試してみたい。

aws.typepad.com

尚、このコマンドラインツールは Docker Compose をサポートしているので Docker Compose で利用する YAML ファイルが直接利用出来るようになっている。(従来は ECS の Task Definition と YAML ファイルの相互変換ツールを利用する方法があった)


参考

docs.aws.amazon.com

github.com


試す

試した環境

% sw_vers
ProductName:    Mac OS X
ProductVersion: 10.11.1
BuildVersion:   15B22c

ecs-cli のインストゥール

ドキュメント通り

% sudo curl -o /usr/local/bin/ecs-cli https://s3.amazonaws.com/amazon-ecs-cli/ecs-cli-darwin-amd64-latest
% sudo chmod +x /usr/local/bin/ecs-cli

簡単。

ECS クラスタの起動

以下のように ECS クラスタを作成する。

% ecs-cli configure --region ap-northeast-1 --cluster my-cluster

開封の儀みたいなもの。

ECS インスタンスコンテナの起動

コンテナインスタンスecs-cli up で起動する。

% ecs-cli up --keypair ${ssh_key_name} \
  --capability-iam \
  --size 2 \
  --vpc vpc-xxxxxxxxx \
  --instance-type t2.micro \
  --subnets subnet-xxxxxxxx,subnet-xxxxxxxxx \
  --azs ap-northeast-1a,ap-northeast-1c
INFO[0000] Created cluster                               cluster=my-cluster
INFO[0001] Waiting for your cluster resources to be created 
INFO[0002] Cloudformation stack status                   stackStatus=CREATE_IN_PROGRESS
INFO[0062] Cloudformation stack status                   stackStatus=CREATE_IN_PROGRESS
INFO[0122] Cloudformation stack status                   stackStatus=CREATE_IN_PROGRESS
INFO[0183] Cloudformation stack status                   stackStatus=CREATE_IN_PROGRESS

以下の通り CloudFormation を利用してコンテナインスタンスが 2 台起動した。

% aws ecs list-container-instances --cluster my-cluster
{
    "containerInstanceArns": [
        "arn:aws:ecs:ap-northeast-1:123456789012:container-instance/094216a6-5a7a-4462-be4c-b25d64254a3f", 
        "arn:aws:ecs:ap-northeast-1:123456789012:container-instance/2b0b743a-9f60-4090-b61a-a24c957a7f7e"
    ]
}

尚、指定出来るオプションは以下の通り。利用の環境に応じて適宜設定する必要があると思ふ。

% ecs-cli up --help
NAME:
   up - Create the ECS Cluster (if it does not already exist) and the AWS resources required to set up the cluster.

USAGE:
   command up [command options] [arguments...]

OPTIONS:
   --keypair            Specify the name of an existing Amazon EC2 key pair to enable SSH access to the EC2 instances in your cluster.
   --capability-iam     Acknowledge that this command may create IAM resources.
   --size               [Optional] Specify the number of instances to register to the cluster. The default is 1.
   --azs                [Optional] Specify a comma-separated list of 2 VPC availability zones in which to create subnets (these AZs must be in the 'available' status). This option is recommended if you do not specify a VPC ID with the --vpc option. WARNING: Leaving this option blank can result in failure to launch container instances if an unavailable AZ is chosen at random.
   --security-group     [Optional] Specify an existing security group to associate it with container instances. Defaults to creating a new one.
   --cidr               [Optional] Specify a CIDR/IP range for the security group to use for container instances in your cluster. Defaults to 0.0.0.0/0 if --security-group is not specified
   --port               [Optional] Specify a port to open on a new security group that is created for your container instances if an existing security group is not specified with the --security-group option. Defaults to port 80.
   --subnets            [Optional] Specify a comma-separated list of existing VPC Subnet IDs in which to launch your container instances. This option is required if you specify a VPC with the --vpc option.
   --vpc                [Optional] Specify the ID of an existing VPC in which to launch your container instances. If you specify a VPC ID, you must specify a list of existing subnets in that VPC with the --subnets option. If you do not specify a VPC ID, a new VPC is created with two subnets.
   --instance-type      [Optional] Specify the EC2 instance type for your container instances.

-security-group に関しては特に指定しない場合には 80 番ポートのみ 0.0.0.0/0 向けに解放されたルールが適用される。又、--capability-iam を指定しておくと以下のような IAM ポリシールールが作成され EC2 インスタンスに適用される。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "ecs:CreateCluster",
        "ecs:DeregisterContainerInstance",
        "ecs:DiscoverPollEndpoint",
        "ecs:Poll",
        "ecs:RegisterContainerInstance",
        "ecs:StartTelemetrySession",
        "ecs:Submit*"
      ],
      "Resource": "*"
    }
  ]
}

Docker Compose を利用してコンテナを起動

以下のような Docker Compose 用の YAML ファイルを用意。

web:
  image: amazon/amazon-ecs-sample
  ports:
   - "80:80"

docker-compose.yml というファイル名で保存しておく。尚、docker-compose.yml というファイル名以外で作成した場合には、以下の ecs-cli compose up を実行する際に --file オプションを利用して YAML ファイルを指定することが出来る。

以下のように実行してコンテナを起動。

% ecs-cli compose up
INFO[0000] Using ECS task definition                     TaskDefinition=ecscompose-ecs-sample:1
INFO[0001] Starting container...                         container=5caf3c9d-efcb-48cc-82ba-757760f0df71/web
INFO[0001] Describe ECS container status                 container=5caf3c9d-efcb-48cc-82ba-757760f0df71/web desiredStatus=RUNNING lastStatus=PENDING taskDefinition=ecscompose-ecs-sample:1
INFO[0013] Describe ECS container status                 container=5caf3c9d-efcb-48cc-82ba-757760f0df71/web desiredStatus=RUNNING lastStatus=PENDING taskDefinition=ecscompose-ecs-sample:1
INFO[0025] Describe ECS container status                 container=5caf3c9d-efcb-48cc-82ba-757760f0df71/web desiredStatus=RUNNING lastStatus=PENDING taskDefinition=ecscompose-ecs-sample:1
INFO[0037] Describe ECS container status                 container=5caf3c9d-efcb-48cc-82ba-757760f0df71/web desiredStatus=RUNNING lastStatus=PENDING taskDefinition=ecscompose-ecs-sample:1
INFO[0049] Describe ECS container status                 container=5caf3c9d-efcb-48cc-82ba-757760f0df71/web desiredStatus=RUNNING lastStatus=PENDING taskDefinition=ecscompose-ecs-sample:1
INFO[0061] Describe ECS container status                 container=5caf3c9d-efcb-48cc-82ba-757760f0df71/web desiredStatus=RUNNING lastStatus=PENDING taskDefinition=ecscompose-ecs-sample:1
INFO[0073] Describe ECS container status                 container=5caf3c9d-efcb-48cc-82ba-757760f0df71/web desiredStatus=RUNNING lastStatus=PENDING taskDefinition=ecscompose-ecs-sample:1
INFO[0080] Started container...                          container=5caf3c9d-efcb-48cc-82ba-757760f0df71/web desiredStatus=RUNNING lastStatus=RUNNING taskDefinition=ecscompose-ecs-sample:1

コンテナが起動していることを確認

ecs-cli ps にて確認。

% ecs-cli ps
Name                                      State    Ports                    TaskDefinition
5caf3c9d-efcb-48cc-82ba-757760f0df71/web  RUNNING  54.65.xxx.xxx:80->80/tcp  ecscompose-ecs-sample:1

ひとまずアクセスしてみる。

% curl -I 54.65.xxx.xxx
HTTP/1.1 200 OK
Date: Fri, 09 Oct 2015 15:34:49 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.3.10-1ubuntu3.15
Vary: Accept-Encoding
Content-Type: text/html

ブラウザからも...

f:id:inokara:20151010061644p:plain

確認。

コンテナをスケールさせてみる

この時点ではコンテナは 1 コンテナのみ起動しているので、もう一つコンテナを起動してみる。コンテナをスケールさせるには ecs-cli compose scale N を利用する。

% ecs-cli compose scale 2
INFO[0000] Starting container...                         container=1e0fc3d7-c9da-4853-bdf5-e7c64277e31d/web
INFO[0000] Describe ECS container status                 container=1e0fc3d7-c9da-4853-bdf5-e7c64277e31d/web desiredStatus=RUNNING lastStatus=PENDING taskDefinition=ecscompose-ecs-sample:1
INFO[0012] Describe ECS container status                 container=1e0fc3d7-c9da-4853-bdf5-e7c64277e31d/web desiredStatus=RUNNING lastStatus=PENDING taskDefinition=ecscompose-ecs-sample:1
INFO[0025] Describe ECS container status                 container=1e0fc3d7-c9da-4853-bdf5-e7c64277e31d/web desiredStatus=RUNNING lastStatus=PENDING taskDefinition=ecscompose-ecs-sample:1
INFO[0037] Describe ECS container status                 container=1e0fc3d7-c9da-4853-bdf5-e7c64277e31d/web desiredStatus=RUNNING lastStatus=PENDING taskDefinition=ecscompose-ecs-sample:1
INFO[0049] Describe ECS container status                 container=1e0fc3d7-c9da-4853-bdf5-e7c64277e31d/web desiredStatus=RUNNING lastStatus=PENDING taskDefinition=ecscompose-ecs-sample:1
INFO[0055] Started container...                          container=1e0fc3d7-c9da-4853-bdf5-e7c64277e31d/web desiredStatus=RUNNING lastStatus=RUNNING taskDefinition=ecscompose-ecs-sample:1

確認。

% ecs-cli ps
Name                                      State    Ports                      TaskDefinition
1e0fc3d7-c9da-4853-bdf5-e7c64277e31d/web  RUNNING  54.178.xxx.xxx:80->80/tcp  ecscompose-ecs-sample:1
5caf3c9d-efcb-48cc-82ba-757760f0df71/web  RUNNING  54.65.xxx.xxx:80->80/tcp    ecscompose-ecs-sample:1

2 コンテナ起動しているようなので確認。

% curl -I 54.178.xxx.xxx
HTTP/1.1 200 OK
Date: Fri, 09 Oct 2015 15:34:41 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.3.10-1ubuntu3.15
Vary: Accept-Encoding
Content-Type: text/html

% curl -I 54.65.xxx.xxx
HTTP/1.1 200 OK
Date: Fri, 09 Oct 2015 15:34:49 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.3.10-1ubuntu3.15
Vary: Accept-Encoding
Content-Type: text/html

後片付け

一通り試したら全てのリソースを削除する必要がある。これを後片付けと呼ぶ。以下のように実行することで CloudFromation のスタックがザクっと削除されて、全てのリソースが削除される。尚、コンテナが稼働している場合には --force を付ける

% ecs-cli down --force
INFO[0001] Waiting for your cluster resources to be deleted 
INFO[0001] Cloudformation stack status                   stackStatus=DELETE_IN_PROGRESS
INFO[0062] Cloudformation stack status                   stackStatus=DELETE_IN_PROGRESS
INFO[0122] Deleted cluster                               cluster=my-cluster

当たり前だのクラッカーだが...コンテナインスタンス、ECS クラスタ、IAM ロールのポリシー等ザクっと消えてしまうので消し忘れ防止は嬉しいが、実行の際にはくれぐれもご注意を...。


ということで...

  • Docker Compose が利用出来るようになったのは嬉しい
  • コンテナインスタンスから一気通貫で構築出来るのは嬉しい(マネジメントコンソールを拝む必要が「ほぼ」無い)
  • 少し CloudFormation のスキルが必要かもしれない
  • ELB に繋いだりするのも ecs-cli から出来ると嬉しいと思ったり

以上。