1. 程式人生 > >Docker學習筆記(六)Swarms

Docker學習筆記(六)Swarms

       Swarm就是一組docker宿主機組成的cluster,它有個管理節點叫作swarm manager,這個swarm裡的docker命令都在這個swarm manager上執行,其他的節點都叫作worker。一個docker宿主機通過執行docker swarm init命令將自己設定為swarm manager,而其他的宿主機通過執行docker swarm join命令並指定swarm manager的IP和埠(預設埠2377)來加入swarm(join)。

       在筆記四中部署hello-service時,其實已經用到了swarm,下面還是通過demo來更直觀的瞭解swarm吧。


Note

本文demo參考docker官方文件,有條件的朋友建議直接看原文Link:https://docs.docker.com/get-started/part4/#create-a-cluster

Demo

Swarm manager

       我先用一臺IP是10.200.218.124的宿主機來做swarm manager。執行docker swarm init,會提示我們怎麼將其他的宿主機join到這個swarm中。

[email protected]:/home/hunk/docker# docker swarm init
Swarm initialized: current node (pxjmqahdfyqp2m2znlaj4kwc2) is now a manager.

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

    docker swarm join \
    --token SWMTKN-1-51yl2q26jn2za02mcb5id3f3etkx38s6js19xnfzg1ldnnq6ft-9bgk59pk5f95m53y7w65gl1mi \
    10.200.218.124:2377

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

note:swarm manager節點和worker節點都要先login。

Swarm node

       我用另一臺IP是10.200.218.125的宿主機來join到上面的swarm中。

[email protected]:/home/hunk# docker swarm join \
>     --token SWMTKN-1-51yl2q26jn2za02mcb5id3f3etkx38s6js19xnfzg1ldnnq6ft-9bgk59pk5f95m53y7w65gl1mi \
>     10.200.218.124:2377
This node joined a swarm as a worker.

View node

        在swarm manager上檢視所有的node

[email protected]:/home/hunk/docker# docker node ls
ID                           HOSTNAME              STATUS  AVAILABILITY  MANAGER STATUS
j6s1lwlgq8d6o7mxq5azg0gxh    hunk-virtual-machine  Ready   Active        
pxjmqahdfyqp2m2znlaj4kwc2 *  hunk-virtual-machine  Ready   Active        Leader

Deploy service

       在筆記四里面,我們用yml配置來部署了一個hello-service服務,其實當時已經用到了swarm,只是我們只有一個swarm manager,沒有worker,service所有的container replicas都執行在swarm manager上。而當前我們多了一個worker,那service的container replicas其實會被均勻的分配到所有的worker上(包括swarm manager)。

      先用同樣的方式部署hello-service。

[email protected]:/home/hunk/docker# docker stack deploy -c hello-service.yml hello-service
Creating network hello-service_webnet
Creating service hello-service_web

      在swarm manager上檢視container replicas的分佈情況

[email protected]:/home/hunk/docker# docker stack ps hello-service --no-resolve
ID            NAME                         IMAGE                   NODE                       DESIRED STATE  CURRENT STATE                   ERROR  PORTS
q0wv31fthg3c  y46ique6nw7taei64dfm44c6s.1  hebostary/gohead:demo1  pxjmqahdfyqp2m2znlaj4kwc2  Running        Running 53 seconds ago                 
u22hl65sgyd6  y46ique6nw7taei64dfm44c6s.2  hebostary/gohead:demo1  j6s1lwlgq8d6o7mxq5azg0gxh  Running        Running less than a second ago         
vjdgx00ga2k5  y46ique6nw7taei64dfm44c6s.3  hebostary/gohead:demo1  pxjmqahdfyqp2m2znlaj4kwc2  Running        Running 53 seconds ago                 
tdi3fbp2sn3f  y46ique6nw7taei64dfm44c6s.4  hebostary/gohead:demo1  j6s1lwlgq8d6o7mxq5azg0gxh  Running        Running less than a second ago         
n9vokwj46sbi  y46ique6nw7taei64dfm44c6s.5  hebostary/gohead:demo1  pxjmqahdfyqp2m2znlaj4kwc2  Running        Running 53 seconds ago
[email protected]:/home/hunk/docker# docker container ls
CONTAINER ID        IMAGE                                                                                      COMMAND             CREATED             STATUS              PORTS               NAMES
8bedc58f8933        hebostary/[email protected]:969c4f12d7c1d9e3f167498e1779aceefc631a158ec4e18730d16f5602569d03   "python app.py"     5 minutes ago       Up 5 minutes        80/tcp              hello-service_web.3.vjdgx00ga2k5jo0757wmmw8fg
5bcaa11079d0        hebostary/[email protected]:969c4f12d7c1d9e3f167498e1779aceefc631a158ec4e18730d16f5602569d03   "python app.py"     5 minutes ago       Up 5 minutes        80/tcp              hello-service_web.5.n9vokwj46sbi68wozrmkkvx8v
6c52c7c28e71        hebostary/[email protected]:969c4f12d7c1d9e3f167498e1779aceefc631a158ec4e18730d16f5602569d03   "python app.py"     5 minutes ago       Up 5 minutes        80/tcp              hello-service_web.1.q0wv31fthg3cwqffrm2ocgkur

檢視worker節點上的container

[email protected]:/home/hunk# docker container ls
CONTAINER ID        IMAGE                                                                                      COMMAND             CREATED             STATUS              PORTS               NAMES
9fdc525604b7        hebostary/[email protected]:969c4f12d7c1d9e3f167498e1779aceefc631a158ec4e18730d16f5602569d03   "python app.py"     7 minutes ago       Up 7 minutes        80/tcp              hello-service_web.2.u22hl65sgyd6y9w3effadl37u
a21c29caead8        hebostary/[email protected]:969c4f12d7c1d9e3f167498e1779aceefc631a158ec4e18730d16f5602569d03   "python app.py"     7 minutes ago       Up 7 minutes        80/tcp              hello-service_web.4.tdi3fbp2sn3f528jdvg3a35x2

curl訪問service

[email protected]:/home/hunk# curl http://10.200.218.124:4000
<h3>Hello World!</h3><b>Hostname:</b> 9fdc525604b7<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>[email protected]:/home/hunk# curl http://10.200.218.124:4000
<h3>Hello World!</h3><b>Hostname:</b> 5bcaa11079d0<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>[email protected]:/home/hunk# curl http://10.200.218.124:4000
<h3>Hello World!</h3><b>Hostname:</b> 8bedc58f8933<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>
[email protected]:/home/hunk# curl http://10.200.218.125:4000
<h3>Hello World!</h3><b>Hostname:</b> 6c52c7c28e71<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>[email protected]:/home/hunk# curl http://10.200.218.125:4000
<h3>Hello World!</h3><b>Hostname:</b> 8bedc58f8933<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>[email protected]:/home/hunk# curl http://10.200.218.125:4000
<h3>Hello World!</h3><b>Hostname:</b> 5bcaa11079d0<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>

每次請求返回的hostname都不同,說明這些請求確實被分發到了不同的task中,有loadbalance的處理。另外,除了swarm manager,我們用worker節點的IP地址也可以訪問這個web服務。這裡和普通的服務可能有些區別了,每個node上都能接收處理外部請求,並且都有loadbalance的能力。這裡我們直接引用官方demo的ingress network圖,它說明swarm裡所有的node都能對外提供api,並且都有loadbalance的能力。

routing mesh diagram