ホットデプロイが流行っているらしいので。
github.com
hakoはECS用のデプロイツールで社内ではいろいろと使われています。
ALBと組み合わせて使うと、頑張っている感じのホットデプロイまわりがシンプルになってよいです。
まだ正式にはALBに対応していないのですが、使う方法を後ろの席の人が教えてくれたので、メモしておきます。
2016/09/07
ALBに対応したようです。
https://github.com/eagletmt/hako/pull/11
hakoのインストール
gemでインストール。
gem install hako
とりあえず適当なインスタンスがあればいいのでecs-cli
でインスタンス群を起動。
ecs-cli configure \
-r ap-northeast-1 \
-c hello-hako
ecs-cli up \
--keypair winebarrel \
--capability-iam \
--size 2 \
--vpc vpc-... \
--subnets subnet-... \
--security-group sg-... \
--instance-type m4.large
おけおけ。
タスク定義
以下のようなタスクを定義したyamlを用意。
だいたいはjsonにそのまま対応しているのんですが、微妙に差異があったりなかったり(リファレンス欲しいなぁ)
- hello-hako.yml(ファイル名がサービス名・タスク名・クラスタ名になる)
scheduler:
type: ecs
region: ap-northeast-1
cluster: hello-hako
desired_count: 2
app:
image: nginx
memory: 300
cpu: 256
essential: true
mount_points:
- container_path: /usr/share/nginx/html
source_volume: vol
- container_path: /var/log/httpd
source_volume: log
port_mappings:
- host_port: 0
container_port: 80
additional_containers:
busybox:
image_tag: busybox
cpu: 64
memory: 256
env:
MSG: hello
volumes_from:
- source_container: app
command:
- /bin/sh
- -c
- |
while true; do
date >> /usr/share/nginx/html/index.html
echo $MSG >> /usr/share/nginx/html/index.html
echo '<br>' >> /usr/share/nginx/html/index.html
sleep 3
done
volumes:
vol: {}
log:
source_path: /var/log/httpd
デプロイ
以下のコマンドをたたいて、タスクをデプロイ。
$ hako deploy hello-hako.yml
I, [2016-09-02T22:42:59.125887
I, [2016-09-02T22:42:59.263683
I, [2016-09-02T22:43:07.825926
I, [2016-09-02T22:43:13.976431
そうするとサービスが作られます。
が、これは削除。(事前にNumber of tasks
を0
にする)
ALBの作成
次にコマンドラインからALBを作成。
aws elbv2 create-load-balancer --name hello-hako --subnets subnet-xxx subnet-yyy --security-group sg-...
サービスの再作成
同じ名前でサービスを再作成。
このときにALBの設定もします。
あと、ターゲットグループのDeregistration delayは短めで。
で、サービス作成完了。
一応、ALBにアクセス。
おけおけ。
yamlを以下のように変更してデプロイ。
command:
- /bin/sh
- -c
- |
while true; do
echo modified >> /usr/share/nginx/html/index.html
date >> /usr/share/nginx/html/index.html
echo $MSG >> /usr/share/nginx/html/index.html
echo '<br>' >> /usr/share/nginx/html/index.html
sleep 3
done
$ hako deploy hello-hako.yml
I, [2016-09-02T23:06:55.847629
...
デプロイと同時にサービスを監視。
$ while true; do echo `date +%X`" | "`curl -s http://hello-hako-1111294014.ap-northeast-1.elb.amazonaws.com/ | head -n 1`; sleep 1; done
23時07分10秒 | Fri Sep 2 14:05:25 UTC 2016
...
しばらく待つとデプロイ完了。
$ hako deploy hello-hako.yml
I, [2016-09-02T23:06:55.847629
I, [2016-09-02T23:06:56.190146
I, [2016-09-02T23:07:29.817474
I, [2016-09-02T23:07:29.817558
I, [2016-09-02T23:07:29.817599
I, [2016-09-02T23:07:43.310115
I, [2016-09-02T23:07:43.310198
I, [2016-09-02T23:07:56.160512
I, [2016-09-02T23:07:56.160670
I, [2016-09-02T23:08:08.035586
I, [2016-09-02T23:08:08.035677
I, [2016-09-02T23:08:18.055483
監視の方は…
23時07分37秒 | Fri Sep 2 14:05:24 UTC 2016
23時07分38秒 | Fri Sep 2 14:05:25 UTC 2016
23時07分39秒 | Fri Sep 2 14:05:25 UTC 2016
23時07分40秒 | Fri Sep 2 14:05:25 UTC 2016
23時07分41秒 | modified
23時07分43秒 | Fri Sep 2 14:05:25 UTC 2016
23時07分44秒 | modified
23時07分45秒 | modified
23時07分46秒 | Fri Sep 2 14:05:25 UTC 2016
23時07分47秒 | modified
23時07分48秒 | modified
23時07分49秒 | Fri Sep 2 14:05:25 UTC 2016
23時07分50秒 | modified
23時07分51秒 | modified
23時07分52秒 | Fri Sep 2 14:05:25 UTC 2016
23時07分54秒 | modified
23時07分55秒 | modified
23時07分56秒 | Fri Sep 2 14:05:25 UTC 2016
23時07分57秒 | modified
23時07分58秒 | modified
23時07分59秒 | Fri Sep 2 14:05:25 UTC 2016
23時08分00秒 | modified
23時08分01秒 | modified
23時08分02秒 | Fri Sep 2 14:05:25 UTC 2016
23時08分03秒 | modified
23時08分04秒 | modified
23時08分06秒 | modified
23時08分07秒 | modified
おけおけ。
ダウンタイムないですね。