ECS に登場する概念
ECS ( EC2 Container Service ) では Service 、Task 、Cluster 等の様々な概念が登場しますが、とりあえず以下の記事がわかりやすいので、一旦読めば良いと思います。
Amazon EC2 Container Service(ECS)の概念整理
今回は以下の公式ドキュメントにある ECS CLI を用いたチュートリアルで少しでも ECS の理解を深める方向で進めたいと思います。
Amazon ECS CLI チュートリアル
ステップ 0: ECS CLI のインストール
まずは ECS CLI をインストールしましょう。
例のごとく Amazon Linux AMI を使います。
$ cat /etc/os-release
NAME="Amazon Linux AMI"
VERSION="2017.09"
ID="amzn"
ID_LIKE="rhel fedora"
VERSION_ID="2017.09"
PRETTY_NAME="Amazon Linux AMI 2017.09"
ANSI_COLOR="0;33"
CPE_NAME="cpe:/o:amazon:linux:2017.09:ga"
HOME_URL="http://aws.amazon.com/amazon-linux-ami/"
以下のコマンドでちゃちゃっとインストールしちゃいましょう。
$ sudo curl -o /usr/local/bin/ecs-cli https://s3.amazonaws.com/amazon-ecs-cli/ecs-cli-linux-amd64-latest
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 15.2M 100 15.2M 0 0 2423k 0 0:00:06 0:00:06 --:--:-- 3711k
$ sudo chmod +x /usr/local/bin/ecs-cli
$ ecs-cli --version
ecs-cli version 0.6.6 (fe47016)
ステップ 1: クラスターを作成する
さっそく ECS CLI を用いてクラスターを立ち上げます。 と、その前に、ecs-cli のサブコマンドである configure を実施します。
$ ecs-cli configure --region ap-northeast-1 --cluster WordPress
$ cat .ecs/config
[ecs]
cluster = WordPress
aws_profile =
region = ap-northeast-1
aws_access_key_id =
aws_secret_access_key =
compose-project-name-prefix = ecscompose-
compose-service-name-prefix = ecscompose-service-
cfn-stack-name-prefix = amazon-ecs-cli-setup-
で、以下のコマンドで、クラスターを作成。
$ ecs-cli up --keypair WorkKey --capability-iam --size 2 --vpc vpc-f8e4769c --subnets subnet-85bfe5f3,subnet-8580e4dd --instance-type t2.medium
INFO[0000] Created cluster cluster=WordPress region=ap-northeast-1
INFO[0000] Waiting for your cluster resources to be created...
INFO[0000] Cloudformation stack status stackStatus="CREATE_IN_PROGRESS"
INFO[0061] Cloudformation stack status stackStatus="CREATE_IN_PROGRESS"
INFO[0121] Cloudformation stack status stackStatus="CREATE_IN_PROGRESS"
INFO[0181] Cloudformation stack status stackStatus="CREATE_IN_PROGRESS"
マネジメントコンソールからクラスターが上がったことが確認できます。
ステップ 2: 構成ファイルを作成する
まずは以下の YAML ファイルを作成します。 Docker Compose で言うところの docker-compose.yml 的なファイルですね。
$ cat wordpress.yml
version: '2'
services:
wordpress:
image: wordpress
cpu_shares: 100
mem_limit: 524288000
ports:
- "80:80"
links:
- mysql
mysql:
image: mysql
cpu_shares: 100
mem_limit: 524288000
environment:
MYSQL_ROOT_PASSWORD: password
ちなみに Docker Compose における compose ファイルの Version 1 と 2 がサポートされているみたいです。
ステップ 3: クラスターに設定ファイルをデプロイする
以下のコマンドで一発デプロイ!
$ ecs-cli compose --file wordpress.yml up
WARN[0000] Skipping unsupported YAML option... option name=networks
WARN[0000] Skipping unsupported YAML option for service... option name=networks service name=mysql
WARN[0000] Skipping unsupported YAML option for service... option name=networks service name=wordpress
INFO[0000] Using ECS task definition TaskDefinition="ecscompose-WordPress:1"
INFO[0000] Starting container... container="88f5871b-4c0a-43f8-8e21-9c2248fb4084/mysql"
INFO[0000] Starting container... container="88f5871b-4c0a-43f8-8e21-9c2248fb4084/wordpress"
INFO[0000] Describe ECS container status container="88f5871b-4c0a-43f8-8e21-9c2248fb4084/wordpress" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecscompose-WordPress:1"
INFO[0000] Describe ECS container status container="88f5871b-4c0a-43f8-8e21-9c2248fb4084/mysql" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecscompose-WordPress:1"
INFO[0012] Describe ECS container status container="88f5871b-4c0a-43f8-8e21-9c2248fb4084/wordpress" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecscompose-WordPress:1"
INFO[0012] Describe ECS container status container="88f5871b-4c0a-43f8-8e21-9c2248fb4084/mysql" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecscompose-WordPress:1"
INFO[0024] Describe ECS container status container="88f5871b-4c0a-43f8-8e21-9c2248fb4084/wordpress" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecscompose-WordPress:1"
INFO[0024] Describe ECS container status container="88f5871b-4c0a-43f8-8e21-9c2248fb4084/mysql" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecscompose-WordPress:1"
INFO[0036] Describe ECS container status container="88f5871b-4c0a-43f8-8e21-9c2248fb4084/wordpress" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecscompose-WordPress:1"
INFO[0036] Describe ECS container status container="88f5871b-4c0a-43f8-8e21-9c2248fb4084/mysql" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecscompose-WordPress:1"
INFO[0042] Started container... container="88f5871b-4c0a-43f8-8e21-9c2248fb4084/wordpress" desiredStatus=RUNNING lastStatus=RUNNING taskDefinition="ecscompose-WordPress:1"
INFO[0042] Started container... container="88f5871b-4c0a-43f8-8e21-9c2248fb4084/mysql" desiredStatus=RUNNING lastStatus=RUNNING taskDefinition="ecscompose-WordPress:1"
簡単!
ステップ 4: クラスターの実行中のコンテナを確認する
ECS CLI にも docker ps 的なコマンドが存在し、クラスターで実行されているコンテナの状態を確認できる。
Swarm Manager を eval で指定した後の docker ps に近い気がする。
$ ecs-cli ps
Name State Ports TaskDefinition
88f5871b-4c0a-43f8-8e21-9c2248fb4084/wordpress RUNNING 13.115.235.240:80->80/tcp ecscompose-WordPress:1
88f5871b-4c0a-43f8-8e21-9c2248fb4084/mysql RUNNING ecscompose-WordPress:1
ちなみにそれぞれのコンテナインスタンスにログインして docker ps コマンドを実行すると、片方のインスタンスでのみコンテナが起動されていることがわかる。
# コンテナインスタンス A
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ea008bc7f03 amazon/amazon-ecs-agent:latest "/agent" 14 minutes ago Up 14 minutes ecs-agent
# コンテナインスタンス B
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5f60d2e6e1b3 wordpress "docker-entrypoint..." 7 minutes ago Up 7 minutes 0.0.0.0:80->80/tcp ecs-ecscompose-WordPress-1-wordpress-ee80b09cefea869e2b00
66d4f5a42a0a mysql "docker-entrypoint..." 8 minutes ago Up 8 minutes 3306/tcp ecs-ecscompose-WordPress-1-mysql-e892d3ddf7f88ca42000
f79b40504500 amazon/amazon-ecs-agent:latest "/agent" 14 minutes ago Up 14 minutes ecs-agent
あと、どちらも ecs-agent というコンテナが起動してますね。
ステップ 5: クラスターのタスクをスケーリングする
ECS CLI の scale サブコマンドにより、タスクを実行するコンテナインスタンスの数をスケールできる。
$ ecs-cli compose --file wordpress.yml scale 2
WARN[0000] Skipping unsupported YAML option... option name=networks
WARN[0000] Skipping unsupported YAML option for service... option name=networks service name=mysql
WARN[0000] Skipping unsupported YAML option for service... option name=networks service name=wordpress
INFO[0000] Starting container... container="f73f27e6-cb46-4199-aeb5-4747c4778c0f/mysql"
INFO[0000] Starting container... container="f73f27e6-cb46-4199-aeb5-4747c4778c0f/wordpress"
INFO[0000] Describe ECS container status container="f73f27e6-cb46-4199-aeb5-4747c4778c0f/wordpress" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecscompose-WordPress:1"
INFO[0000] Describe ECS container status container="f73f27e6-cb46-4199-aeb5-4747c4778c0f/mysql" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecscompose-WordPress:1"
INFO[0012] Describe ECS container status container="f73f27e6-cb46-4199-aeb5-4747c4778c0f/wordpress" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecscompose-WordPress:1"
INFO[0012] Describe ECS container status container="f73f27e6-cb46-4199-aeb5-4747c4778c0f/mysql" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecscompose-WordPress:1"
INFO[0024] Describe ECS container status container="f73f27e6-cb46-4199-aeb5-4747c4778c0f/wordpress" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecscompose-WordPress:1"
INFO[0024] Describe ECS container status container="f73f27e6-cb46-4199-aeb5-4747c4778c0f/mysql" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecscompose-WordPress:1"
INFO[0036] Describe ECS container status container="f73f27e6-cb46-4199-aeb5-4747c4778c0f/wordpress" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecscompose-WordPress:1"
INFO[0036] Describe ECS container status container="f73f27e6-cb46-4199-aeb5-4747c4778c0f/mysql" desiredStatus=RUNNING lastStatus=PENDING taskDefinition="ecscompose-WordPress:1"
INFO[0042] Started container... container="f73f27e6-cb46-4199-aeb5-4747c4778c0f/wordpress" desiredStatus=RUNNING lastStatus=RUNNING taskDefinition="ecscompose-WordPress:1"
INFO[0042] Started container... container="f73f27e6-cb46-4199-aeb5-4747c4778c0f/mysql" desiredStatus=RUNNING lastStatus=RUNNING taskDefinition="ecscompose-WordPress:1"
それじゃあ確認しましょう。
$ ecs-cli ps
Name State Ports TaskDefinition
88f5871b-4c0a-43f8-8e21-9c2248fb4084/wordpress RUNNING 13.115.235.240:80->80/tcp ecscompose-WordPress:1
88f5871b-4c0a-43f8-8e21-9c2248fb4084/mysql RUNNING ecscompose-WordPress:1
f73f27e6-cb46-4199-aeb5-4747c4778c0f/wordpress RUNNING 54.65.189.188:80->80/tcp ecscompose-WordPress:1
f73f27e6-cb46-4199-aeb5-4747c4778c0f/mysql RUNNING ecscompose-WordPress:1
# コンテナインスタンス A
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c1af8a66556 wordpress "docker-entrypoint..." About a minute ago Up About a minute 0.0.0.0:80->80/tcp ecs-ecscompose-WordPress-1-wordpress-ccdcbf95a9dfa1d5a501
dbe43830e839 mysql "docker-entrypoint..." About a minute ago Up About a minute 3306/tcp ecs-ecscompose-WordPress-1-mysql-e091e1a3f7dfad973200
5ea008bc7f03 amazon/amazon-ecs-agent:latest "/agent" 20 minutes ago Up 20 minutes ecs-agent
# コンテナインスタンス B
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5f60d2e6e1b3 wordpress "docker-entrypoint..." 14 minutes ago Up 14 minutes 0.0.0.0:80->80/tcp ecs-ecscompose-WordPress-1-wordpress-ee80b09cefea869e2b00
66d4f5a42a0a mysql "docker-entrypoint..." 14 minutes ago Up 14 minutes 3306/tcp ecs-ecscompose-WordPress-1-mysql-e892d3ddf7f88ca42000
f79b40504500 amazon/amazon-ecs-agent:latest "/agent" 20 minutes ago Up 20 minutes ecs-agent
どちらのコンテナインスタンスでも同様のコンテナが立ち上がってますね。
ステップ 6: 設定ファイルから ECS サービスを作成する
ECS CLI の service サブコマンドで service を新たに作成できます。 まずはクラスターを空っぽにしましょう。
$ ecs-cli compose --file wordpress.yml down
WARN[0000] Skipping unsupported YAML option... option name=networks
WARN[0000] Skipping unsupported YAML option for service... option name=networks service name=mysql
WARN[0000] Skipping unsupported YAML option for service... option name=networks service name=wordpress
INFO[0000] Stopping container... container="88f5871b-4c0a-43f8-8e21-9c2248fb4084/wordpress"
INFO[0000] Stopping container... container="88f5871b-4c0a-43f8-8e21-9c2248fb4084/mysql"
INFO[0000] Stopping container... container="f73f27e6-cb46-4199-aeb5-4747c4778c0f/wordpress"
INFO[0000] Stopping container... container="f73f27e6-cb46-4199-aeb5-4747c4778c0f/mysql"
INFO[0000] Describe ECS container status container="88f5871b-4c0a-43f8-8e21-9c2248fb4084/wordpress" desiredStatus=STOPPED lastStatus=RUNNING taskDefinition="ecscompose-WordPress:1"
INFO[0000] Describe ECS container status container="88f5871b-4c0a-43f8-8e21-9c2248fb4084/mysql" desiredStatus=STOPPED lastStatus=RUNNING taskDefinition="ecscompose-WordPress:1"
INFO[0000] Describe ECS container status container="f73f27e6-cb46-4199-aeb5-4747c4778c0f/wordpress" desiredStatus=STOPPED lastStatus=RUNNING taskDefinition="ecscompose-WordPress:1"
INFO[0000] Describe ECS container status container="f73f27e6-cb46-4199-aeb5-4747c4778c0f/mysql" desiredStatus=STOPPED lastStatus=RUNNING taskDefinition="ecscompose-WordPress:1"
INFO[0006] Stopped container... container="88f5871b-4c0a-43f8-8e21-9c2248fb4084/wordpress" desiredStatus=STOPPED lastStatus=STOPPED taskDefinition="ecscompose-WordPress:1"
INFO[0006] Stopped container... container="88f5871b-4c0a-43f8-8e21-9c2248fb4084/mysql" desiredStatus=STOPPED lastStatus=STOPPED taskDefinition="ecscompose-WordPress:1"
INFO[0006] Stopped container... container="f73f27e6-cb46-4199-aeb5-4747c4778c0f/wordpress" desiredStatus=STOPPED lastStatus=STOPPED taskDefinition="ecscompose-WordPress:1"
INFO[0006] Stopped container... container="f73f27e6-cb46-4199-aeb5-4747c4778c0f/mysql" desiredStatus=STOPPED lastStatus=STOPPED taskDefinition="ecscompose-WordPress:1"
で、以下のコマンドを実行。
$ ecs-cli compose --file wordpress.yml service up
WARN[0000] Skipping unsupported YAML option... option name=networks
WARN[0000] Skipping unsupported YAML option for service... option name=networks service name=mysql
WARN[0000] Skipping unsupported YAML option for service... option name=networks service name=wordpress
INFO[0000] Using ECS task definition TaskDefinition="ecscompose-WordPress:1"
INFO[0000] Updated ECS service successfully desiredCount=1 serviceName=ecscompose-service-WordPress
INFO[0015] (service ecscompose-service-WordPress) has started 1 tasks: (task b3334c02-3ff6-463a-b5b3-aa13a25d63ab). timestamp=2017-11-13 16:09:13 +0000 UTC
INFO[0030] Service status desiredCount=1 runningCount=1 serviceName=ecscompose-service-WordPress
INFO[0030] (service ecscompose-service-WordPress) has reached a steady state. timestamp=2017-11-13 16:09:24 +0000 UTC
INFO[0030] ECS Service has reached a stable state desiredCount=1 runningCount=1 serviceName=ecscompose-service-WordPress
ステップ 7: クリーンアップ
全部終わったらお掃除。
サービスを削除して、クラスタを停止。
# service 停止
$ ecs-cli compose --file wordpress.yml service rm
WARN[0000] Skipping unsupported YAML option... option name=networks
WARN[0000] Skipping unsupported YAML option for service... option name=networks service name=mysql
WARN[0000] Skipping unsupported YAML option for service... option name=networks service name=wordpress
INFO[0000] Updated ECS service successfully desiredCount=0 serviceName=ecscompose-service-WordPress
INFO[0000] Service status desiredCount=0 runningCount=1 serviceName=ecscompose-service-WordPress
INFO[0015] Service status desiredCount=0 runningCount=0 serviceName=ecscompose-service-WordPress
INFO[0015] (service ecscompose-service-WordPress) has stopped 1 running tasks: (task b3334c02-3ff6-463a-b5b3-aa13a25d63ab). timestamp=2017-11-13 16:11:35 +0000 UTC
INFO[0015] (service ecscompose-service-WordPress) has reached a steady state. timestamp=2017-11-13 16:11:47 +0000 UTC
INFO[0015] ECS Service has reached a stable state desiredCount=0 runningCount=0 serviceName=ecscompose-service-WordPress
INFO[0015] Deleted ECS service service=ecscompose-service-WordPress
INFO[0015] ECS Service has reached a stable state desiredCount=0 runningCount=0 serviceName=ecscompose-service-WordPress
# クラスタ削除
$ ecs-cli down --force
NFO[0000] Waiting for your cluster resources to be deleted...
INFO[0000] Cloudformation stack status stackStatus="DELETE_IN_PROGRESS"
INFO[0060] Cloudformation stack status stackStatus="DELETE_IN_PROGRESS"
INFO[0120] Cloudformation stack status stackStatus="DELETE_IN_PROGRESS"
INFO[0151] Deleted cluster cluster=WordPress