サーバ構築

Docker-Machineでswarmクラスタを作ってみる

更新日:

イメージ図

  • docker-hubにホスト情報などを保存(default)  

Swarmクラスタ作る

TOKENをローカルマシンで作成

$ SWARM_TOKEN=$(docker run swarm create)
Unable to find image 'swarm:latest' locally
latest: Pulling from library/swarm
d85c18077b82: Pulling fs layer
1e6bb16f8cb1: Pulling fs layer
85bac13497d7: Pulling fs layer
85bac13497d7: Verifying Checksum
85bac13497d7: Download complete
1e6bb16f8cb1: Verifying Checksum
1e6bb16f8cb1: Download complete
d85c18077b82: Verifying Checksum
d85c18077b82: Download complete
d85c18077b82: Pull complete
1e6bb16f8cb1: Pull complete
85bac13497d7: Pull complete
Digest: sha256:406022f04a3d0c5ce4dbdb60422f24052c20ab7e6d41ebe5723aa649c3833975
Status: Downloaded newer image for swarm:latest
Token based discovery is now deprecated and might be removed in the future.
It will be replaced by a default discovery backed by Docker Swarm Mode.
Other mechanisms such as consul and etcd will continue to work as expected.

$ echo $SWARM_TOKEN
5b06bdca9e59148988d01cbe7fbdd6f1

swarmマスターをVirtualBoxに作成

$ docker-machine create -d virtualbox --engine-label dc=a --swarm --swarm-master --swarm-discovery token://$SWARM_TOKEN swarm-master
Running pre-create checks...
Creating machine...
(swarm-master) Copying /Users/hironori_megumi/.docker/machine/cache/boot2docker.iso to /Users/hironori_megumi/.docker/machine/machines/swarm-master/boot2docker.iso...
(swarm-master) Creating VirtualBox VM...
(swarm-master) Creating SSH key...
(swarm-master) Starting the VM...
(swarm-master) Check network to re-create if needed...
(swarm-master) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Configuring swarm...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env swarm-master

swarmノード2台をVirtualBoxに作成

$ docker-machine create -d virtualbox --engine-label dc=a --swarm --swarm-discovery token://$SWARM_TOKEN swarm-1
Running pre-create checks...
Creating machine...
(swarm-1) Copying /Users/hironori_megumi/.docker/machine/cache/boot2docker.iso to /Users/hironori_megumi/.docker/machine/machines/swarm-1/boot2docker.iso...
(swarm-1) Creating VirtualBox VM...
(swarm-1) Creating SSH key...
(swarm-1) Starting the VM...
(swarm-1) Check network to re-create if needed...
(swarm-1) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Configuring swarm...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env swarm-1

$ docker-machine create -d virtualbox --engine-label dc=b --swarm --swarm-discovery token://$SWARM_TOKEN swarm-2
Running pre-create checks...
Creating machine...
(swarm-2) Copying /Users/hironori_megumi/.docker/machine/cache/boot2docker.iso to /Users/hironori_megumi/.docker/machine/machines/swarm-2/boot2docker.iso...
(swarm-2) Creating VirtualBox VM...
(swarm-2) Creating SSH key...
(swarm-2) Starting the VM...
(swarm-2) Check network to re-create if needed...
(swarm-2) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Configuring swarm...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env swarm-2

一覧取得

$ docker run swarm list token://$SWARM_TOKEN
192.168.99.102:2376
192.168.99.103:2376
192.168.99.101:2376

ノード詳細

  • swarmマスタから docker info を実行
$ eval $(docker-machine env --swarm swarm-master)
$ docker info
Containers: 4
 Running: 4
 Paused: 0
 Stopped: 0
Images: 3
Server Version: swarm/1.2.9
Role: primary
Strategy: spread
Filters: health, port, containerslots, dependency, affinity, constraint, whitelist
Nodes: 3
 swarm-1: 192.168.99.102:2376
  └ ID: E4OI:DT77:3BMS:VLHF:4ORJ:PBA6:5R2M:BBLT:MAX6:LDYP:P7QP:TBKJ|192.168.99.102:2376
  └ Status: Healthy
  └ Containers: 1 (1 Running, 0 Paused, 0 Stopped)
  └ Reserved CPUs: 0 / 1
  └ Reserved Memory: 0 B / 1.021 GiB
  └ Labels: dc=a, kernelversion=4.9.93-boot2docker, operatingsystem=Boot2Docker 18.06.0-ce (TCL 8.2.1); HEAD : 1f40eb2 - Thu Jul 19 18:48:09 UTC 2018, ostype=linux, provider=virtualbox, storagedriver=aufs
  └ UpdatedAt: 2018-08-20T13:24:18Z
  └ ServerVersion: 18.06.0-ce
 swarm-2: 192.168.99.103:2376
  └ ID: ZM42:R3UW:FDVE:V3T5:52LB:WLTQ:VNUN:IYWO:ECR3:HKVV:WOLF:K3RW|192.168.99.103:2376
  └ Status: Healthy
  └ Containers: 1 (1 Running, 0 Paused, 0 Stopped)
  └ Reserved CPUs: 0 / 1
  └ Reserved Memory: 0 B / 1.021 GiB
  └ Labels: dc=b, kernelversion=4.9.93-boot2docker, operatingsystem=Boot2Docker 18.06.0-ce (TCL 8.2.1); HEAD : 1f40eb2 - Thu Jul 19 18:48:09 UTC 2018, ostype=linux, provider=virtualbox, storagedriver=aufs
  └ UpdatedAt: 2018-08-20T13:24:14Z
  └ ServerVersion: 18.06.0-ce
 swarm-master: 192.168.99.101:2376
  └ ID: Q24C:3Z66:CDD2:DYBQ:BKDB:43HT:MDIC:GNC6:3OGW:JX2L:GUMD:PN57|192.168.99.101:2376
  └ Status: Healthy
  └ Containers: 2 (2 Running, 0 Paused, 0 Stopped)
  └ Reserved CPUs: 0 / 1
  └ Reserved Memory: 0 B / 1.021 GiB
  └ Labels: dc=a, kernelversion=4.9.93-boot2docker, operatingsystem=Boot2Docker 18.06.0-ce (TCL 8.2.1); HEAD : 1f40eb2 - Thu Jul 19 18:48:09 UTC 2018, ostype=linux, provider=virtualbox, storagedriver=aufs
  └ UpdatedAt: 2018-08-20T13:24:24Z
  └ ServerVersion: 18.06.0-ce
Plugins:
 Volume: 
 Network: 
 Log: 
Swarm: 
 NodeID: 
 Is Manager: false
 Node Address: 
Kernel Version: 4.9.93-boot2docker
Operating System: linux
Architecture: amd64
CPUs: 3
Total Memory: 3.063GiB
Name: 2647bab7d744
Docker Root Dir: 
Debug Mode (client): false
Debug Mode (server): false
Experimental: false
Live Restore Enabled: false

WARNING: No kernel memory limit support

各ノードでは

  • swarm-agent コンテナが起動
  • swarmマスタでは swarm-agent-master コンテナも起動してる
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                     NAMES
55449bc242a7        swarm:latest        "/swarm join --adver…"   32 minutes ago      Up 31 minutes       2375/tcp                                  swarm-2/swarm-agent
ff73bab61883        swarm:latest        "/swarm join --adver…"   36 minutes ago      Up 36 minutes       2375/tcp                                  swarm-1/swarm-agent
4beaf34e64f2        swarm:latest        "/swarm join --adver…"   39 minutes ago      Up 39 minutes       2375/tcp                                  swarm-master/swarm-agent
2647bab7d744        swarm:latest        "/swarm manage --tls…"   39 minutes ago      Up 39 minutes       2375/tcp, 192.168.99.101:3376->3376/tcp   swarm-master/swarm-agent-master

コンテナを起動してみる

  • debianコンテナ起動すると、swarm-1ノードで起動するようにスケジュールされる。
$ docker run -d debian sleep 10
64c49d34463dfd9b98796e55d82c65d3f01e77e884fc383df7f4e1706b79fb2c

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                      PORTS                                     NAMES
64c49d34463d        debian              "sleep 10"               About a minute ago   Exited (0) 54 seconds ago                                             swarm-1/friendly_mestorf
55449bc242a7        swarm:latest        "/swarm join --adver…"   35 minutes ago       Up 35 minutes               2375/tcp                                  swarm-2/swarm-agent
ff73bab61883        swarm:latest        "/swarm join --adver…"   39 minutes ago       Up 39 minutes               2375/tcp                                  swarm-1/swarm-agent
4beaf34e64f2        swarm:latest        "/swarm join --adver…"   42 minutes ago       Up 42 minutes               2375/tcp                                  swarm-master/swarm-agent
2647bab7d744        swarm:latest        "/swarm manage --tls…"   42 minutes ago       Up 42 minutes               2375/tcp, 192.168.99.101:3376->3376/tcp   swarm-master/swarm-agent-master
  • nginxをポート指定して各ノードで起動させる
$ docker run -d -p 80:80 nginx
640533ad5ea04bead2bb7435f0ec0df63dd443748360016ac4b9b6e9d42c982f

$ docker run -d -p 80:80 nginx
729f064a9979d12064370f0686ff00c7e6cde1e1ec17fc6c7bdc844ff68babbd

$ docker run -d -p 80:80 nginx
0fd9b5d44f1e65d5dcba2fb8fe017dd8751edd1ed4e31b3a6e96f100e0359d7b

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                       NAMES
0fd9b5d44f1e        nginx               "nginx -g 'daemon of…"   3 minutes ago       Up 3 minutes        192.168.99.102:80->80/tcp   swarm-1/modest_heisenberg
729f064a9979        nginx               "nginx -g 'daemon of…"   4 minutes ago       Up 4 minutes        192.168.99.101:80->80/tcp   swarm-master/zen_leakey
640533ad5ea0        nginx               "nginx -g 'daemon of…"   5 minutes ago       Up 5 minutes        192.168.99.103:80->80/tcp   swarm-2/compassionate_euler

`

  • 4回目はノード上に空いてるポートがない(80は↑で起動してるため)のでエラーになる
$ docker run -d -p 80:80 nginx
docker: Error response from daemon: Unable to find a node that satisfies the following conditions 

[port 80 (Bridge mode)]

. See 'docker run --help'.

フィルタ指定してコンテナ起動

  • dc=bというラベルを持つノード(swarm-2)でコンテナを起動
$ docker run -d -e constraint:dc==b postgres
585d9c69c7ddd3c5af5c06863b1f20957a2dcef9f1405fae53c138d73025edf4

$ docker run -d -e constraint:dc==b postgres
5f757a1e54dc893057d5fb567c51a0ef10fa1e590fa21d360eaffe0a47691c0c

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                       NAMES
5f757a1e54dc        postgres            "docker-entrypoint.s…"   6 seconds ago       Up 6 seconds        5432/tcp                    swarm-2/friendly_swanson
585d9c69c7dd        postgres            "docker-entrypoint.s…"   8 minutes ago       Up 8 minutes        5432/tcp                    swarm-2/cocky_chaplygin
0fd9b5d44f1e        nginx               "nginx -g 'daemon of…"   15 minutes ago      Up 15 minutes       192.168.99.102:80->80/tcp   swarm-1/modest_heisenberg
729f064a9979        nginx               "nginx -g 'daemon of…"   15 minutes ago      Up 15 minutes       192.168.99.101:80->80/tcp   swarm-master/zen_leakey
640533ad5ea0        nginx               "nginx -g 'daemon of…"   17 minutes ago      Up 17 minutes       192.168.99.103:80->80/tcp   swarm-2/compassionate_euler

-サーバ構築
-

Copyright© 明日から頑張ります。 , 2025 All Rights Reserved Powered by STINGER.