swarm的快速入門的中文資料(姿勢(shì)正確的)很少脚作。本文簡(jiǎn)單介紹swarm(偽)集群的搭建球涛。使用docker-machine開一些虛擬機(jī)作為docker主機(jī)。
準(zhǔn)備工作
首先怒開4個(gè)虛擬機(jī)亿扁,一個(gè)Manager,三個(gè)worker
docker-machine create manager (ip: 103)
docker-machine create worker1 (ip: 104)
docker-machine create worker2 (ip: 105)
docker-machine create worker3 (ip: 106)
docker-machine ls #查看虛擬機(jī)
創(chuàng)建manager節(jié)點(diǎn)
docker-machine ssh manager # 登錄到manager
docker swarm init --advertise-addr 192.168.99.103:2377 # 集群manager
docker node list # 查看集群節(jié)點(diǎn)
下面登錄worker機(jī)器并將它們加入集群
docker-machine ssh worker1
docker swarm join --token SWMTKN-1-1gcvu7m59jfo8jgmjeu44jypfl2pywwn7l8slscs4eix28x8
v7-1jm8o9wf8ghhcrit7hkkxvpd4 192.168.99.103:2377
回到103上看看集群節(jié)點(diǎn)情況从祝。
docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
afd3jflix35ykn3tju9853kxm * manager Ready Active Leader
aqb10743e0w278f99p8xg1njc worker3 Ready Active
ib651vgesr5aqre0z2bx2trq5 worker1 Ready Active
wxvwuuzou9q54ikez6gjjnoo6 worker2 Ready Active
在集群上創(chuàng)建服務(wù)牍陌,服務(wù)的工作就是ping manager機(jī)器
docker service create --name ping00 alpine ping 192.168.99.103
docker service ls # 查看集群中的服務(wù)
docker service ps ping00 # 查看服務(wù)的任務(wù)(容器)
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
72fk7w8j4sdd ping00.1 alpine:latest worker2 Running Running 47 seconds ago
我們看到這個(gè)task運(yùn)行在了worker2機(jī)器上毒涧,登錄到worker2運(yùn)行docker ps可以看到這個(gè)運(yùn)行的容器
路由轉(zhuǎn)發(fā)
docker service create --name web --publish 80:80 nginx
我們啟動(dòng)了一個(gè)nginx服務(wù),使用docker service ps web查看到其跑在了manager(103)上仿吞,但是捡偏,我們可以從103,104,105,106任何一臺(tái)機(jī)器訪問到這個(gè)服務(wù)。
事實(shí)上霹琼,這是因?yàn)榧荷系娜魏喂?jié)點(diǎn)在接到請(qǐng)求后如果處理不了就交給manager,manager會(huì)看哪一臺(tái)機(jī)器可以處理并轉(zhuǎn)發(fā)過去枣申。
真正的集群
下面讓我們的集群跑起10個(gè)Nginx
docker service update --replicas 10 web
docker service ls
ID NAME MODE REPLICAS IMAGE
mfljbz7x2w0u web replicated 10/10 nginx:latest
十個(gè)容器分布在了四臺(tái)機(jī)器上
docker service ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
du3vjyuwotu7 web.1 nginx:latest manager Running Running 6 minutes ago
wr6ncubcolhx web.2 nginx:latest worker1 Running Running 39 seconds ago
xizys7b4n0cf web.3 nginx:latest worker2 Running Running 39 seconds ago
sv7sk9f3gdwn web.4 nginx:latest worker3 Running Running 39 seconds ago
w8aeu4qk3rhb web.5 nginx:latest worker1 Running Running 39 seconds ago
inl9wxvv3051 web.6 nginx:latest manager Running Running 39 seconds ago
q5nczw8oyk3m web.7 nginx:latest worker3 Running Running 39 seconds ago
708m289ztdpp web.8 nginx:latest worker3 Running Running 39 seconds ago
qr2bx5n3x7gn web.9 nginx:latest manager Running Running 39 seconds ago
4p8gz2qg2z7s web.10 nginx:latest worker2 Running Running 39 seconds ago
下面我們關(guān)掉worker2,我們可以看到忠藤,worker2上的容器被跑到了其他節(jié)點(diǎn)上。