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


はじめに

環境

  • 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 4: Swarms

クラスターの作成

  • VirtualBoxドライバーを使用してVMを2台作成
$ docker-machine create --driver virtualbox myvm1
$ docker-machine create --driver virtualbox myvm2

こんな感じでmyvm1myvm2が作成されている

  • docker-machine lsで確認
$ 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マネージャに設定

  • 下記コマンドを送ると、以下のように出力される
$ 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オプションを使用して再度実行
$ 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ワーカーとして参加

$ 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コマンドでノードを確認

$ 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.ymlmyvm1へコピー
$ docker-machine scp docker-compose.yml myvm1:~
docker-compose.yml                                                                                       100%  299     0.3KB/s   00:00
  • docker stack deployコマンドでアプリのデプロイ
$ docker-machine ssh myvm1 "docker stack deploy -c docker-compose.yml getstartedlab"
Creating network getstartedlab_webnet
Creating service getstartedlab_web
  • クラスタへのデプロイ状況の確認
$ 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毎の起動状況

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
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

アクセスイメージ

  • myvm1myvm2のどちらにアクセスしてもすべてのコンテナへ負荷分散される

スタックの破棄

  • 以下のコマンドでDocker stackを破棄することができる
$ docker-machine ssh myvm1 "docker stack rm getstartedlab"
Removing service getstartedlab_web
Removing network getstartedlab_webnet
  • 破棄されたことを確認
$ docker-machine ssh myvm1 "docker stack ps getstartedlab"
Nothing found in stack: getstartedlab

Part4で学習したコマンドのチートシート

# 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>"