【Docker】公式チュートリアルをやってみた 【Part4】

# **はじめに**
- ConohaのVPSを借りたのでDockerを入れようと思い、[公式のチュートリアル](https://docs.docker.com/get-started/)をやってみようと思ったのでございます

# **環境**
- Windows10
- VirtualBox 5.1.20 r114628 (Qt5.6.2)
- Docker version 17.05.0-ce, build 89658be
- docker-machine.exe version 0.11.0, build 5b27455

# **チュートリアル**
- [Part 1: Orientation and Setup](https://docs.docker.com/get-started/)
- [Part 2: Containers](https://docs.docker.com/get-started/part2/)
    - [【Docker】公式チュートリアルをやってみた 【Part1/Part2】](https://qiita.com/Tocyuki/items/420c3afa25bd28e58159)
- [Part 3: Services](https://docs.docker.com/get-started/part3/)
    - [【Docker】公式チュートリアルをやってみた 【Part3】](https://qiita.com/Tocyuki/items/2b04a0fcf25c07c1b367)
- [**Part 4: Swarms**](https://docs.docker.com/get-started/part4/)
    - [【Docker】公式チュートリアルをやってみた 【Part4】](https://qiita.com/Tocyuki/items/dade81596400b80b7ef3)
- [Part 5: Stacks](https://docs.docker.com/get-started/part5/)
- [Part 6: Deploy your app](https://docs.docker.com/get-started/part6/)

# **Part 4: Swarms**
## クラスターの作成
- VirtualBoxドライバーを使用してVMを2台作成

```shell-session
$ docker-machine create --driver virtualbox myvm1
$ docker-machine create --driver virtualbox myvm2
```

こんな感じで`myvm1`と`myvm2`が作成されている

![WS000524.png](https://qiita-image-store.s3.amazonaws.com/0/79134/f1f83111-f41a-ffa6-c3a0-df337d9ddd44.png)

- `docker-machine ls`で確認

```shell-session
$ docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376           v17.03.2-ce
myvm1     -        virtualbox   Running   tcp://192.168.99.103:2376           v17.03.2-ce
myvm2     -        virtualbox   Running   tcp://192.168.99.104:2376           v17.03.2-ce
```

## `myvm1`をSwarmマネージャに設定
- 下記コマンドを送ると、以下のように出力される

```shell-session
$ docker-machine ssh myvm1 "docker swarm init"
Error response from daemon: could not choose an IP address to advertise since this system has multiple addresses on different interfaces (10.0.2.15 on eth0 and 192.168.99.103 on eth1) - specify one with --advertise-addr
exit status 1
```

- `--advertise-addr`オプションを使用して再度実行

```shell-session
$ docker-machine ssh myvm1 "docker swarm init --advertise-addr 192.168.99.103:2377"
Swarm initialized: current node (yfbk30ncvzp8gn60oeye6lxmn) is now a manager.

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

    docker swarm join \
    --token SWMTKN-1-5kwikrks7ukdmn02lpxu19ybpdh2xgsporxf3twyqhgnlm5sxt-6xuh5cjwfwmncu4n4aeghvfqw \
    192.168.99.103:2377

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

## `myvm2`をSwamワーカーとして参加
```shell-session
$ docker-machine ssh myvm2 "docker swarm join \
--token SWMTKN-1-5kwikrks7ukdmn02lpxu19ybpdh2xgsporxf3twyqhgnlm5sxt-6xuh5cjwfwmncu4n4aeghvfqw \
192.168.99.103:2377"
This node joined a swarm as a worker.
```

## `docker-machine ssh`コマンドでノードを確認
```shell-session
$ docker-machine ssh myvm1 "docker node ls"
ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
8l9qvm98lmu2k4gyhnfpx83zs    myvm2     Ready   Active
yfbk30ncvzp8gn60oeye6lxmn *  myvm1     Ready   Active        Leader
```

## クラスターへアプリをデプロイする
- [Part3]()で使用した`docker-compose.yml`を`myvm1`へコピー

```shell-session
$ docker-machine scp docker-compose.yml myvm1:~
docker-compose.yml                                                                                       100%  299     0.3KB/s   00:00
```

- `docker stack deploy`コマンドでアプリのデプロイ

```shell-session
$ docker-machine ssh myvm1 "docker stack deploy -c docker-compose.yml getstartedlab"
Creating network getstartedlab_webnet
Creating service getstartedlab_web
```

- クラスタへのデプロイ状況の確認

```shell-session
$ docker-machine ssh myvm1 "docker stack ps getstartedlab"
ID            NAME                 IMAGE                   NODE   DESIRED STATE  CURRENT STATE               ERROR  PORTS
2nzqlikufmv7  getstartedlab_web.1  tocyuki/repository:tag  myvm2  Running        Running about a minute ago
vnx6ytymitk8  getstartedlab_web.2  tocyuki/repository:tag  myvm1  Running        Running about a minute ago
d77gpo4op9l8  getstartedlab_web.3  tocyuki/repository:tag  myvm2  Running        Running about a minute ago
0bs7md3ffa5l  getstartedlab_web.4  tocyuki/repository:tag  myvm1  Running        Running about a minute ago
vml7gktru257  getstartedlab_web.5  tocyuki/repository:tag  myvm1  Running        Running about a minute ago
```

## 各VM毎の起動状況
```shell-session:myvm1
$ docker-machine ssh myvm1 "docker ps"
CONTAINER ID        IMAGE                                                                                        COMMAND             CREATED             STATUS              PORTS               NAMES
bd811d21d9b1        tocyuki/repository@sha256:1ae82dcb5528f752c9c1fc57a55ec0dde097ee2b119011ed2d99b3b9e81228d8   "python app.py"     32 minutes ago      Up 32 minutes       80/tcp              getstartedlab_web.5.vml7gktru257n7p49g9typkgl
b17c0abc447c        tocyuki/repository@sha256:1ae82dcb5528f752c9c1fc57a55ec0dde097ee2b119011ed2d99b3b9e81228d8   "python app.py"     32 minutes ago      Up 32 minutes       80/tcp              getstartedlab_web.4.0bs7md3ffa5l9jx9422lmc67c
ff50c29c1aa6        tocyuki/repository@sha256:1ae82dcb5528f752c9c1fc57a55ec0dde097ee2b119011ed2d99b3b9e81228d8   "python app.py"     32 minutes ago      Up 32 minutes       80/tcp              getstartedlab_web.2.vnx6ytymitk84stdi4f901ha8
```

```shell-session:myvm2
$ docker-machine ssh myvm2 "docker ps"
CONTAINER ID        IMAGE                                                                                        COMMAND             CREATED             STATUS              PORTS               NAMES
c6060d9c56e2        tocyuki/repository@sha256:1ae82dcb5528f752c9c1fc57a55ec0dde097ee2b119011ed2d99b3b9e81228d8   "python app.py"     34 minutes ago      Up 34 minutes       80/tcp              getstartedlab_web.3.d77gpo4op9l8s00u9ish4c00z
833c7cbc9ede        tocyuki/repository@sha256:1ae82dcb5528f752c9c1fc57a55ec0dde097ee2b119011ed2d99b3b9e81228d8   "python app.py"     34 minutes ago      Up 34 minutes       80/tcp              getstartedlab_web.1.2nzqlikufmv7bwtcq2wtydzrg
```

## アクセスイメージ
- `myvm1`、`myvm2`のどちらにアクセスしてもすべてのコンテナへ負荷分散される

![Untitled (1).png](https://qiita-image-store.s3.amazonaws.com/0/79134/4c8ffd67-5bbc-25b1-ab27-66e1bbc715ef.png)


## スタックの破棄
- 以下のコマンドでDocker stackを破棄することができる

```shell-session
$ docker-machine ssh myvm1 "docker stack rm getstartedlab"
Removing service getstartedlab_web
Removing network getstartedlab_webnet
```

- 破棄されたことを確認

```shell-session
$ docker-machine ssh myvm1 "docker stack ps getstartedlab"
Nothing found in stack: getstartedlab
```

## Part4で学習したコマンドのチートシート
```bash
# Create a VM (Mac, Win7, Linux)                                           
docker-machine create --driver virtualbox myvm1                            
# Win10                                                                    
docker-machine create -d hyperv --hyperv-virtual-switch "myswitch" myvm1   
# View basic information about your node                                   
docker-machine env myvm1                                                   
# List the nodes in your swarm                                             
docker-machine ssh myvm1 "docker node ls"                                  
# Inspect a node                                                           
docker-machine ssh myvm1 "docker node inspect <node ID>"                   
# View join token                                                          
docker-machine ssh myvm1 "docker swarm join-token -q worker"               
# Open an SSH session with the VM; type "exit" to end                      
docker-machine ssh myvm1                                                   
# Make the worker leave the swarm                                          
docker-machine ssh myvm2 "docker swarm leave"                              
# Make master leave, kill swarm                                            
docker-machine ssh myvm1 "docker swarm leave -f"                           
# Start a VM that is currently not running                                 
docker-machine start myvm1                                                 
# Stop all running VMs                                                     
docker-machine stop $(docker-machine ls -q)                                
# Delete all VMs and their disk images                                     
docker-machine rm $(docker-machine ls -q)                                  
# Copy file to node's home dir                                             
docker-machine scp docker-compose.yml myvm1:~                              
# Deploy an app                                                            
docker-machine ssh myvm1 "docker stack deploy -c <file> <app>"                                                                      
```