準備三臺主機
A:192.168.1.5
B:192.168.1.7
C:192.168.1.10
Docker Swarm集羣中的節點主機開放如下三個端口
2377端口, 用於集羣管理通訊
7946端口, 用於集羣節點之間的通訊
4789端口, 用於overlay網絡流量
關閉防火牆
systemctl stop firewalld.service
systemctl disable firewalld.service
在A上執行
建立集羣並加入集羣
$ docker swarm init --advertise-addr 192.168.1.5:2377 --listen-addr 192.168.1.5:2377
initialized: current node (sf4zgbesw21ko536rrgpxspv4) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-3mlkj1pouzqm8mjdqdnj5gw30i4ahjw5aayfu177vzernbzmod-1svbkyjwzhv0swzojp6xh3shm 192.168.1.5:2377
查看集羣中的節點
$ docker node ls
查看 manager 角色的 token
$ docker swarm join-token manager
To add a manager to this swarm, run the following command:
docker swarm join --token SWMTKN-1-3mlkj1pouzqm8mjdqdnj5gw30i4ahjw5aayfu177vzernbzmod-0ib338l48ybx8csrlpr3yn7ug 192.168.1.5:2377
查看 worker 角色的 token
$ docker swarm join-token worker
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-3mlkj1pouzqm8mjdqdnj5gw30i4ahjw5aayfu177vzernbzmod-1svbkyjwzhv0swzojp6xh3shm 192.168.1.5:2377
在B上執行
把 B 以 manager 角色加入集羣 (上面的 token 那段命令,再接上當前的監聽的IP )
$ docker swarm join --token SWMTKN-1-3mlkj1pouzqm8mjdqdnj5gw30i4ahjw5aayfu177vzernbzmod-0ib338l48ybx8csrlpr3yn7ug 192.168.1.5:2377 --advertise-addr 192.168.1.7:2377 --listen-addr 192.168.1.7:2377
查看一下節點
$ docker node ls
在C上執行
把 C 以 worker 角色加入集羣(上面的 token 那段命令,再接上當前的監聽的IP )
$ docker swarm join --token SWMTKN-1-3mlkj1pouzqm8mjdqdnj5gw30i4ahjw5aayfu177vzernbzmod-1svbkyjwzhv0swzojp6xh3shm 192.168.1.5:2377 --advertise-addr 192.168.1.10:2377 --listen-addr 192.168.1.10:2377
在 worker 節點查看集羣中的節點會報沒有權限的錯誤
$ docker node ls
Error response from daemon: This node is not a swarm manager. Worker nodes can't be used to view or modify cluster state.
集羣建好了,就開始建立服務,能夠在任意 manager 角色的主機上建立
這個建立容器幾乎同樣,--replicas 5 在集羣中運行五個實例副本 -p 80:8080 服務綁定 80 端口
$ docker service create --name web-fa -p 80:8080 --replicas 5 nigelpoulton/pluralsight-docker-ci
k4qpts36rterzzsy0ys0f7rjw
overall progress: 5 out of 5 tasks
1/5: running [==================================================>]
2/5: running [==================================================>]
3/5: running [==================================================>]
4/5: running [==================================================>]
5/5: running [==================================================>]
打開瀏覽器,輸入 A B C 任意主機的 IP 訪問一下,看看可否打開一個web頁面
查看服務列表
$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
k4qpts36rter web-fa replicated 5/5 nigelpoulton/pluralsight-docker-ci:latest *:80->8080/tcp
查看具體服務中運行的任務
$ docker service ps web-fa
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
94thjgudevf0 web-fa.1 nigelpoulton/pluralsight-docker-ci:latest host Running Running 7 minutes ago
o0ll8yuo5w0x web-fa.2 nigelpoulton/pluralsight-docker-ci:latest host Running Running 7 minutes ago
nkonamy3qa97 web-fa.3 nigelpoulton/pluralsight-docker-ci:latest host Running Running 7 minutes ago
k311fuxvvz0c web-fa.4 nigelpoulton/pluralsight-docker-ci:latest aaa Running Running 7 minutes ago
sttj7tt70k99 web-fa.5 nigelpoulton/pluralsight-docker-ci:latest host Running Running 7 minutes ago
查看服務的詳細信息
docker service inspect --pretty web-fa
還有一些實用命令
退出集羣: docker swarm leave -f
刪除服務: docker service rm <service-name>
總結一下,當任務副本數大於集羣中的節點數時,勢必有節點得運行兩個容器實例。經過 docker ps 能夠看到多個容器實例並無綁定到宿主機的端口,若是綁定同一個端口,勢必會衝突。
建議,若是想學得深刻點,仍是得買書呀,網上都是教程都近似於筆記,不少細節沒有講。《深刻淺出 Docker》還能夠,書也不厚,最煩那種外文翻譯出來厚厚地書了,有些真是一半都是廢話,浪費生命啊。。。
查看節點爲例:
[zbseoag@host ~]$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
ioi5usennpdgjpb6ehxmkifgm host Down Active 19.03.4
ozeybjcrl9z913qtx2xqkexiq host Ready Active Reachable 19.03.4
sf4zgbesw21ko536rrgpxspv4 * host Ready Active Leader 19.03.4
x5yvbr44s6294qjf1bx9fhcro host Ready Active Reachable 19.03.4
說明:
sf4zgbesw21ko536rrgpxspv4 * : 這個 * 表示當前執行命令的主機
HOSTNAME:因爲我是虛擬機副本建立的,都是同樣的名字。你能夠在其中一臺上用 sodu hostname aaa 來臨時改變主機名,而後再運行一下 docker node ls 會看到列表中主機名變了
MANAGER STATUS:直譯就叫管理員狀態,這一欄爲空就表示 worker 節點, Leader 領導者,Reachable 追隨者