建立 Docker swarm 與 Node
Env
- Master
- ubuntu 16
- IP:192.168.15.129
- Node
- ubuntu 16
- IP:192.168.15.130
- Node
- ubuntu 16
- IP:192.168.15.131
init Master
itachi@swarm-master:~$ sudo docker swarm init --advertise-addr 192.168.15.129
Swarm initialized: current node (t4eswocpwjpo3479z0w0oectd) is now a manager. # 我是 manager
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-5hmpdgirn7mt0nq7rc9lr2wkpgaj40404h8v4td5as7dz0ltkc-aepc1hnh91ot2dux8dojuh18z 192.168.15.129:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
新增 worker 到 swarm 的 token
docker swarm join --token SWMTKN-1-5hmpdgirn7mt0nq7rc9lr2wkpgaj40404h8v4td5as7dz0ltkc-aepc1hnh91ot2dux8dojuh18z 192.168.15.129:2377
將 Node 加入 token
itachi@swarm-node01:~$ sudo docker swarm join --token SWMTKN-1-5hmpdgirn7mt0nq7rc9lr2wkpgaj40404h8v4td5as7dz0ltkc-3jc1to2cd73rqh921vahc01vs 192.168.15.129:2377
This node joined a swarm as a manager.
itachi@swarm-node2:~$ sudo docker swarm join --token SWMTKN-1-5hmpdgirn7mt0nq7rc9lr2wkpgaj40404h8v4td5as7dz0ltkc-3jc1to2cd73rqh921vahc01vs 192.168.15.129:2377
This node joined a swarm as a manager.
離開 swarm
docker swarm leave -f
查看 swarm 的各個節點
其中 * 號為目前在哪台機器上使用,Leader 只能有一個,但是可以置換。
itachi@swarm-node2:~$ sudo docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
t4eswocpwjpo3479z0w0oectd swarm-master Ready Active Leader 18.04.0-ce
l5ehmx995qoiaw4knawnqz4b4 swarm-node01 Ready Active Reachable 18.04.0-ce
xns8cu2c4db8kdjzk2ur8ecf1 swarm-node01 Down Active Reachable 18.04.0-ce
6wq3zr5i1f0bvgcf5je6sf4tx * swarm-node2 Ready Active Reachable 18.04.0-ce
查看 node 詳細資料
itachi@swarm-master:~$ sudo docker inspect t4eswocpwjpo3479z0w0oectd
demote 節點
執行完後 MANAGER STATUS 狀態會為空
itachi@swarm-master:~$ sudo docker node demote xns8cu2c4db8kdjzk2ur8ecf1
Manager xns8cu2c4db8kdjzk2ur8ecf1 demoted in the swarm.
itachi@swarm-master:~$ sudo docker node demote xns8cu2c4db8kdjzk2ur8ecf1
Manager xns8cu2c4db8kdjzk2ur8ecf1 demoted in the swarm.
移除 node 節點
itachi@swarm-master:~$ sudo docker node rm xns8cu2c4db8kdjzk2ur8ecf1 xns8cu2c4db8kdjzk2ur8ecf1 itachi@swarm-master:~$ sudo docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION t4eswocpwjpo3479z0w0oectd * swarm-master Ready Active Leader 18.04.0-ce l5ehmx995qoiaw4knawnqz4b4 swarm-node01 Ready Active Reachable 18.04.0-ce 6wq3zr5i1f0bvgcf5je6sf4tx swarm-node2 Ready Active 18.04.0-ce
promote 節點
以 master 為範例,當 master demote 時會從新選擇一個 Leader
itachi@swarm-node01:~$ sudo docker node demote swarm-master
Manager swarm-master demoted in the swarm.
itachi@swarm-node01:~$ sudo docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
t4eswocpwjpo3479z0w0oectd swarm-master Ready Active 18.04.0-ce
l5ehmx995qoiaw4knawnqz4b4 * swarm-node01 Ready Active Reachable 18.04.0-ce
6wq3zr5i1f0bvgcf5je6sf4tx swarm-node2 Ready Active Leader 18.04.0-ce
將 master promote,此時 Leader 並不會變回它
itachi@swarm-node01:~$ sudo docker node promote swarm-master
Node swarm-master promoted to a manager in the swarm.
itachi@swarm-node01:~$ sudo docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
t4eswocpwjpo3479z0w0oectd swarm-master Ready Active Reachable 18.04.0-ce
l5ehmx995qoiaw4knawnqz4b4 * swarm-node01 Ready Active Reachable 18.04.0-ce
6wq3zr5i1f0bvgcf5je6sf4tx swarm-node2 Ready Active Leader 18.04.0-ce
Update a node
有時候可能某些節點需要進行維護的工作,所以必須先離線。
此時可以使用 --availability 參數,有三種狀態
- active
- pause
- drain
AVAILABILITY 狀態轉為 Drain
itachi@swarm-node01:~$ sudo docker node update --availability drain swarm-node01 swarm-node01 itachi@swarm-node01:~$ sudo docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION t4eswocpwjpo3479z0w0oectd swarm-master Ready Active Leader 18.04.0-ce l5ehmx995qoiaw4knawnqz4b4 * swarm-node01 Ready Drain Reachable 18.04.0-ce 6wq3zr5i1f0bvgcf5je6sf4tx swarm-node2 Ready Active Reachable 18.04.0-ce
節點維護好使 swarm-node01 上線
AVAILABILITY 狀態轉為 Active
AVAILABILITY 狀態轉為 Active
itachi@swarm-master:~$ sudo docker node update --availability active swarm-node01 [sudo] password for itachi: swarm-node01 itachi@swarm-master:~$ sudo docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION t4eswocpwjpo3479z0w0oectd * swarm-master Ready Active Leader 18.04.0-ce l5ehmx995qoiaw4knawnqz4b4 swarm-node01 Ready Active Reachable 18.04.0-ce 6wq3zr5i1f0bvgcf5je6sf4tx swarm-node2 Ready Active Reachable 18.04.0-ce
離開 swarm 群
以 node01 為例
itachi@swarm-node01:~$ sudo docker swarm leave -f
Node left the swarm.
itachi@swarm-node2:~$ sudo docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
t4eswocpwjpo3479z0w0oectd swarm-master Ready Active Leader 18.04.0-ce
l5ehmx995qoiaw4knawnqz4b4 swarm-node01 Down Active Unreachable 18.04.0-ce
6wq3zr5i1f0bvgcf5je6sf4tx * swarm-node2 Ready Active Reachable 18.04.0-ce
那再把 node01 加入 swarm 群中itachi@swarm-node2:~$ sudo docker swarm join-token worker
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-5hmpdgirn7mt0nq7rc9lr2wkpgaj40404h8v4td5as7dz0ltkc-aepc1hnh91ot2dux8dojuh18z 192.168.15.130:2377
itachi@swarm-node01:~$ sudo docker swarm join --token SWMTKN-1-5hmpdgirn7mt0nq7rc9lr2wkpgaj40404h8v4td5as7dz0ltkc-aepc1hnh91ot2dux8dojuh18z 192.168.15.130:2377
This node joined a swarm as a worker.
https://shpica.com/docker-swarm/
留言
張貼留言