気づいたら面白そうなことが出来そうな事をつまみ食いしていたので組み合わせて試してみました。
概要
絵が一番わかりやすし。

ECS で登録したタスクで起動したコンテナアプリケーションを registrator で consul にサービスを自動登録して consul-template で haproxy の設定を書き出す。
参考
- Service Discovery via Consul with Amazon ECS | AWS Compute Blog
- Consul Template + Registrator で、コンテナ上のWebサーバをHAProxyへリアルタイムに登録してみる - log.fstn
準備
Amazon ECS 環境の用意
上の記事でちょこっと ECS を触ってみました。
registrator の用意
上の記事でちょこっと registrator を触ってみました。
haproxy と consul と consul-template の用意
事前に...
consul と consul-template はダウンロードしておこう。
haproxy の用意
sudo yum -y install haproxy
consul の起動
consul agent -server -bootstrap -data-dir=/tmp/consul &
haproxy のテンプレートファイルを用意
- /tmp/haproxy.cfg.ctmpl
global
daemon
maxconn 256
defaults
mode tcp
timeout connect 5000ms
timeout client 60000ms
timeout server 60000ms
listen http-in
bind *:8000{{range service "centos"}}
server {{.ID}} {{.Address}}:{{.Port}} check{{end}}
consul-template の起動
sudo consul-template -consul 127.0.0.1:8500 -template "/tmp/haproxy.cfg.ctmpl:/etc/haproxy/haproxy.cfg:service haproxy restart" &
やっと本題
ECS の task
- simple-web-service.json
[
{
"environment": [],
"name": "centos",
"image": "centos:centos6",
"cpu": 10,
"portMappings": [{
"containerPort": 8000
}],
"memory": 10,
"command": [
"/bin/sh", "-c", "hostname -s > /tmp/index.html ; cd /tmp/ ; python -m SimpleHTTPServer 8000"
],
"essential": true
}
]
task を定義。
aws ecs register-task-definition --family kappa-test --container-definitions file://simple-web-service.json --output json
task の実行
aws ecs run-task --cluster kappa-test-cluster --task-definition kappa-test:5 --count 2 --output json
コンテナインスタンス側では以下のようなログが出力される。
[ec2-user@ip-xx-x-x-xxx consul-template_0.10.0_linux_amd64]$ 2015/06/24 15:07:51 [INFO] agent: Synced service 'ip-xx-x-x-xxx:ecs-kappa-test-5-centos-e4cdd7a4c7e180d5cb01:8000' 2015/06/24 15:07:51 registrator: added: 25a2be61b30e ip-xx-x-x-xxx:ecs-kappa-test-5-centos-e4cdd7a4c7e180d5cb01:8000 Stopping haproxy: [ OK ] Starting haproxy: [ OK ] 2015/06/24 15:07:53 [INFO] agent: Synced service 'ip-xx-x-x-xxx:ecs-kappa-test-5-centos-9890fa90b5a8f4e29a01:8000' 2015/06/24 15:07:53 registrator: added: 3c98ceec24b4 ip-xx-x-x-xxx:ecs-kappa-test-5-centos-9890fa90b5a8f4e29a01:8000 Stopping haproxy: [ OK ] Starting haproxy: [ OK ] [ec2-user@ip-xx-x-x-xxx consul-template_0.10.0_linux_amd64]$
確認もろもろ
- コンテナ
[ec2-user@ip-xx-x-x-xxx consul-template_0.10.0_linux_amd64]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3c98ceec24b4 centos:centos6 "/bin/sh -c 'hostnam 2 minutes ago Up 2 minutes 0.0.0.0:32782->8000/tcp ecs-kappa-test-5-centos-9890fa90b5a8f4e29a01 25a2be61b30e centos:centos6 "/bin/sh -c 'hostnam 2 minutes ago Up 2 minutes 0.0.0.0:32781->8000/tcp ecs-kappa-test-5-centos-e4cdd7a4c7e180d5cb01 9cdaec7ca88b amazon/amazon-ecs-agent:latest "/agent" About an hour ago Up About an hour 127.0.0.1:51678->51678/tcp ecs-agent
- consul に登録されているサービス
[ec2-user@ip-xx-x-x-xxx consul-template_0.10.0_linux_amd64]$ curl -s localhost:8500/v1/catalog/services?pretty
{
"amazon-ecs-agent": [],
"centos": [],
"consul": []
}
[ec2-user@ip-xx-x-x-xxx consul-template_0.10.0_linux_amd64]$ curl -s localhost:8500/v1/catalog/service/centos?pretty=true
[
{
"Node": "ip-xx-x-x-xxx",
"Address": "xx.x.x.xxx",
"ServiceID": "ip-xx-x-x-xxx:ecs-kappa-test-5-centos-e4cdd7a4c7e180d5cb01:8000",
"ServiceName": "centos",
"ServiceTags": null,
"ServiceAddress": "",
"ServicePort": 32781
},
{
"Node": "ip-xx-x-x-xxx",
"Address": "xx.x.x.xxx",
"ServiceID": "ip-xx-x-x-xxx:ecs-kappa-test-5-centos-9890fa90b5a8f4e29a01:8000",
"ServiceName": "centos",
"ServiceTags": null,
"ServiceAddress": "",
"ServicePort": 32782
}
]
- haproxy へのアクセス
[ec2-user@ip-xx-x-x-xxx consul-template_0.10.0_linux_amd64]$ curl localhost:8000 3c98ceec24b4 [ec2-user@ip-xx-x-x-xxx consul-template_0.10.0_linux_amd64]$ curl localhost:8000 25a2be61b30e [ec2-user@ip-xx-x-x-xxx consul-template_0.10.0_linux_amd64]$ curl localhost:8000 3c98ceec24b4 [ec2-user@ip-xx-x-x-xxx consul-template_0.10.0_linux_amd64]$ curl localhost:8000 25a2be61b30e
気づいてみたら
以下の記事と似たようなことをやっていましたとさ。
上記の記事では registrator も Docker コンテナでやっているようだけど、ECS のコンテナインスタンスがシンプルな Amazon Linux なので registrator をそのままインストールしてもいいかなあと思ったりした。自腹が続く限りは...今度は複数台のコンテナインスタンスでやってみたい。