Machine を使って Swarm クラスタを構築する


## 概要
Docker公式ツールである Machine を使って同じく公式ツールである Swarm のクラスタを作るという話。

## Machine を使って Swarm クラスタを構築する
### クラスタトークンの作成
`swarm`コンテナを走らせてトークンを発行するために、まず一時的にDockerホストを立てる

```bash
$ docker-machine create -d virtualbox local

INFO[0001] Creating SSH key...
INFO[0001] Creating VirtualBox VM...
INFO[0009] Starting VirtualBox VM...
INFO[0009] Waiting for VM to start...
INFO[0042] "local" has been created and is now the active machine.
INFO[0042] To point your Docker client at it, run this in your shell: $(docker-machine env local)
```

```
$ $(docker-machine env local)
$ docker run swarm create

Unable to find image 'swarm:latest' locally
511136ea3c5a: Pull complete
ae115241d78a: Pull complete
f49087514537: Pull complete
fff73787bd9f: Pull complete
97c8f6e912d7: Pull complete
33f9d1e808cf: Pull complete
62860d7acc87: Pull complete
bf8b6923851d: Pull complete
swarm:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.

Status: Downloaded newer image for swarm:latest
b9d430858ebdb9b079eba25664b1399b
```

トークン `b9d430858ebdb9b079eba25664b1399b` を手に入れる。

local というマシンは捨ててしまおう

```
$ docker-machine rm local
```

### Swarm Master の 構築

```bash
$ docker-machine create \
    -d virtualbox \
    --swarm \
    --swarm-master \
    --swarm-discovery token://b9d430858ebdb9b079eba25664b1399b \
    swarm-master

INFO[0000] Creating SSH key...
INFO[0001] Creating VirtualBox VM...
INFO[0009] Starting VirtualBox VM...
INFO[0009] Waiting for VM to start...
INFO[0042] Configuring Swarm...
INFO[0062] "swarm-master" has been created and is now the active machine.
INFO[0062] To point your Docker client at it, run this in your shell: $(docker-machine env swarm-master)
```

### Swarm Node の構築
3 台 Swarm Node を作る

```bash
$ docker-machine create \
    -d virtualbox \
    --swarm \
    --swarm-discovery token://b9d430858ebdb9b079eba25664b1399b \
    swarm-node-00

INFO[0001] Creating SSH key...
INFO[0001] Creating VirtualBox VM...
INFO[0009] Starting VirtualBox VM...
INFO[0009] Waiting for VM to start...
INFO[0042] Configuring Swarm...
INFO[0052] "swarm-node-00" has been created and is now the active machine.
INFO[0052] To point your Docker client at it, run this in your shell: $(docker-machine env swarm-node-00)
```

```bash
$ docker-machine create \
    -d virtualbox \
    --swarm \
    --swarm-discovery token://b9d430858ebdb9b079eba25664b1399b \
    swarm-node-01
    
INFO[0000] Creating SSH key...
INFO[0001] Creating VirtualBox VM...
INFO[0009] Starting VirtualBox VM...
INFO[0009] Waiting for VM to start...
INFO[0042] Configuring Swarm...
INFO[0059] "swarm-node-01" has been created and is now the active machine.
INFO[0059] To point your Docker client at it, run this in your shell: $(docker-machine env swarm-node-01)
```

```bash
$ docker-machine create \
    -d virtualbox \
    --swarm \
    --swarm-discovery token://b9d430858ebdb9b079eba25664b1399b \
    swarm-node-02
    
    INFO[0000] Creating SSH key...
INFO[0001] Creating VirtualBox VM...
INFO[0009] Starting VirtualBox VM...
INFO[0010] Waiting for VM to start...
INFO[0043] Configuring Swarm...
INFO[0053] "swarm-node-02" has been created and is now the active machine.
INFO[0053] To point your Docker client at it, run this in your shell: $(docker-machine env swarm-node-02)
```

マシンリストを見てみる

```bash
$ docker-machine ls
NAME            ACTIVE   DRIVER       STATE     URL                         SWARM
swarm-master             virtualbox   Running   tcp://192.168.99.100:2376   swarm-master (master)
swarm-node-00            virtualbox   Running   tcp://192.168.99.102:2376   swarm-master
swarm-node-01            virtualbox   Running   tcp://192.168.99.103:2376   swarm-master
swarm-node-02   *        virtualbox   Running   tcp://192.168.99.104:2376   swarm-master
```

### Swarm の利用

```bash
$(docker-machine env --swarm swarm-master)
$ docker info
Containers: 5
Nodes: 4
 swarm-node-00: 192.168.99.102:2376
  └ Containers: 1
  └ Reserved CPUs: 0 / 4
  └ Reserved Memory: 0 B / 999.9 MiB
 swarm-node-01: 192.168.99.103:2376
  └ Containers: 1
  └ Reserved CPUs: 0 / 4
  └ Reserved Memory: 0 B / 999.9 MiB
 swarm-node-02: 192.168.99.104:2376
  └ Containers: 1
  └ Reserved CPUs: 0 / 4
  └ Reserved Memory: 0 B / 999.9 MiB
 swarm-master: 192.168.99.100:2376
  └ Containers: 2
  └ Reserved CPUs: 0 / 4
  └ Reserved Memory: 0 B / 999.9 MiB
```

## REF

* [Using Docker Machine with Docker Swarm](https://docs.docker.com/machine/#using-docker-machine-with-docker-swarm)
* [Docker Machine リファレンス](http://qiita.com/spesnova/items/073dd64a8a1d580d9ae9)
* [Docker Swarm リファレンス](http://qiita.com/spesnova/items/f4b5f7ee57c9271c9dbd)