Elaboration in, Garbage out

Twitt*r ではメモできない何かそれ的なモノ・コトを

Docker 1.12/1.13 でクラスタ機能が付いたらしい。

はじめに

以前 JupyterHub を Swarm/Docker 上にデプロイしようとするために、 証明書周りをがっつりこねこねする Ansible を書いていており、リファクタしないとなと思いはや一年。。 忙しさにかまけると何もできないですね。。 で、久々に手を入れようと思ったのですが、 次の2つをしってびっくり。

  1. RHEL, CentOS7 系は Linux Kernel 3.12 未満であるのみ関わらず VXLAN を利用可能
  2. Docker 3.12 からオーケストレーション機能が標準で入る。

一つ目は、調べたらふつーに出てきましたね。。はい。 二つ目は、証明書周りがかなり楽になっていて、証明書周りを Ansible で触らなくてすみそうです。 しかも consul や etcd も不要になったとのことなので、余計な管理もしなくて済みそうです。

https://pocketstudio.net/2016/06/21/docker-1-12-build-in-orchestration-translate/

で、実際に触ってみましょう。

参考

https://docs.docker.com/engine/swarm/swarm-tutorial/

やはり公式ドキュメントが一番です。

Vagrant/Ansible の準備

大したモノじゃないけど、あると色々便利な vagrant/ansible のスクリプトをベースに、vagrant up で初期設定ながします。

  • Docker の repository file を /etc/yum.repos.d/docker.repo に格納して
  • Docker のインストール・vagrant で実行可能にしておく程度です。

github.com

Docker Swarm の構築

まずは Docker Swarm の構築 *1

[vagrant@mgmt ~]$ docker -v
Docker version 1.13.0, build 49bf474
[vagrant@mgmt ~]$ docker swarm init  --advertise-addr  192.168.33.41:2377
Swarm initialized: current node (t98hh0yrs83yny7n2gkuphwrn) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join \
    --token SWMTKN-1-1obl48uxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-2yyyyyyyyyyyyyyyyyyyyyyyy \
    192.168.33.41:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

続いて、Docker ワーカー用の VM に上述のコマンドを実行してあげます。

[vagrant@dc01 ~]$  docker swarm join \
    --token SWMTKN-1-1obl48uxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-2yyyyyyyyyyyyyyyyyyyyyyyy \
    192.168.33.41:2377
[vagrant@dc02 ~]$  docker swarm join \
    --token SWMTKN-1-1obl48uxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-2yyyyyyyyyyyyyyyyyyyyyyyy \
    192.168.33.41:2377

ノードが追加されているか確認しましょう。

[vagrant@mgmt ~]$ docker node ls
ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
f76rx5jootznd1w8kjhj9palz *  mgmt      Ready   Active        Leader
i9be3upvx5x76l7a7yajko619    dc01      Ready   Active
kkt3lk0bf37cqr5snz7jnni6f    dc02      Ready   Active

次に、公式ドキュメントでは docker.com に ping を打つだけのコンテナを動かしてます。 ping of death 対策してるのかな。。と思い、ここでは割愛。 とりあえず nginx を一つのコンテナで動かしてみましょう。

[vagrant@mgmt ~]$ docker service create \
  --name my-web \
  --publish 8080:80 \
  --replicas 1 \
  nginx

https://docs.docker.com/engine/swarm/ingress/ にある通り、manger, worker ノードすべて 8080 番をリッスンします。 ちなみに、ローカル環境では全ての VM が 8080 を Listen するまでに 3 分ぐらいかかりました。

[vagrant@mgmt ~]$ curl -I 192.168.33.41:8080
HTTP/1.1 200 OK
Server: nginx/1.11.9
(略)

[vagrant@mgmt ~]$ curl -I 192.168.33.42:8080
HTTP/1.1 200 OK
Server: nginx/1.11.9
(略)

[vagrant@mgmt ~]$ curl -I 192.168.33.43:8080
HTTP/1.1 200 OK
Server: nginx/1.11.9
(略)

ほんとに全台 Listen し始めてる。あとはコンテナ数を増やしたり、HAProxy で設定すれば手軽にかんたん冗長構成ですね。

*1:どうやら swarm init のオプションが 1.12 と 1.13 で違うようです。