感覺昨天講了一堆廢話,沒真正實作的話我自己是看不懂拉,今天將應用Docker官方的範例來做Swarm的使用並做擴展。
首先先將官方實例clone下來再bulid就好
git clone https://github.com/dockersamples/node-bulletin-board
cd node-bulletin-board/bulletin-board-app
docker build --tag bulletinboard:1.0 .
接下來再建立bb-stack.yaml,包含以下的內容(此檔案就如同compose-file)
version: '3.7'
services:
bb-app:
image: bulletinboard:1.0
ports:
- "8000:8080"
在使用之前要先確認Swarm功能是有運行的,如果沒有就初始Swarm,Docker就會自動建立Swarm manager能先體驗一下功能
docker swarm init
一切就緒之後就可以deploy來使用我們的stack
$ docker stack deploy -c bb-stack.yaml mystack
Creating network mystack_default
Creating service mystack_bb-app
部屬好我們的stack之後可以使用service命令來觀察我們建立的service,可以看到service ID還有一些詳細資訊
$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
27g4pcawle5o mystack_bb-app replicated 1/1 bulletinboard:1.0 *:8000->8080/tcp
也可以觀察node這昨天提過
再來是做擴展的功能,擴展也是使用service指令,格式如下
docker service scale <SERVICE-ID>=<NUMBER-OF-TASKS>
再搭配我們剛剛觀察到的service ID建立擴展十個,就會增加十個task
$ docker service scale 27g4pcawle5o=1027g4pcawle5o scaled to 10
27g4pcawle5o scaled to 10
overall progress: 10 out of 10 tasks
1/10: running [==================================================>]
2/10: running [==================================================>]
3/10: running [==================================================>]
4/10: running [==================================================>]
5/10: running [==================================================>]
6/10: running [==================================================>]
7/10: running [==================================================>]
8/10: running [==================================================>]
9/10: running [==================================================>]
10/10: running [==================================================>]
verify: Service converged
再回去查看我們的service,REPLICAS就變成了十個
$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
27g4pcawle5o mystack_bb-app replicated 10/10 bulletinboard:1.0 *:8000->8080/tcp
也能透過ps來查看對應service的資訊
$ docker service ps 27g4pcawle5o
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
qphzkfmn5c7t mystack_bb-app.1 bulletinboard:1.0 docker-desktop Running Running 26 minutes ago
8qna1q65sdsb mystack_bb-app.2 bulletinboard:1.0 docker-desktop Running Running 5 minutes ago
xuqussywhffl mystack_bb-app.3 bulletinboard:1.0 docker-desktop Running Running 5 minutes ago
ex0dg88uu5gy mystack_bb-app.4 bulletinboard:1.0 docker-desktop Running Running 5 minutes ago
2h8prgkgpolm mystack_bb-app.5 bulletinboard:1.0 docker-desktop Running Running 5 minutes ago
7y6wtubgi9om mystack_bb-app.6 bulletinboard:1.0 docker-desktop Running Running 5 minutes ago
6vuncarl1is4 mystack_bb-app.7 bulletinboard:1.0 docker-desktop Running Running 5 minutes ago
yjroyvbvkhke mystack_bb-app.8 bulletinboard:1.0 docker-desktop Running Running 5 minutes ago
yru1cx4rbu7z mystack_bb-app.9 bulletinboard:1.0 docker-desktop Running Running 5 minutes ago
zfutz1757xpf mystack_bb-app.10 bulletinboard:1.0 docker-desktop Running Running 5 minutes ago
這十個擴展就能提供給Swarm自動做負載平衡
刪除的語法則是使用docker service rm <stack-name>
就好
$ docker service rm 27g4pcawle5o
27g4pcawle5o
看了兩天終於看懂manager跟worker到底是在說甚麼了,不過由於時間已經太晚就只稍微講一下,manager跟worker都是各自的虛擬機或是實體機拉,所以我每次Swarm join都在manager自然會報錯,真是搞笑。好啦不過我們今天已經將Swarm基本功能玩過了。接下來來研究怎麼部署我們的container image到cloud吧,是跟Docker hub有關嗎?
參考文獻:
docker官方文件