Swarm集群中有三台机器,分别是
192.168.126.134
,192.168.126.136
,192.168.126.137
初始化一个swarm集群(current machine:192.168.126.134
)
# 初始化一个docker swarm集群,并且当前节点为manager节点 dockerlee@lee-PC:~$ docker swarm init --advertise-addr 192.168.126.134 Swarm initialized: current node (adpp7lpsu7j6rrz7c9j9wj68v) is now a manager. To add a worker to this swarm, run the following command: docker swarm join \ --token SWMTKN-1-22tdl0iv7zgh3acdsxert42mcmrfbe7fg5lzxdmegd480p0rqj-5byurq37c024gb5vnaw0qhd2n \ 192.168.126.134:2377 To add a manager to this swarm, run the following command: docker swarm join \ --token SWMTKN-1-22tdl0iv7zgh3acdsxert42mcmrfbe7fg5lzxdmegd480p0rqj-36dl2p9w73gjvh0kuhwozb3rw \ 192.168.126.134:2377 # 展示所有节点 dockerlee@lee-PC:~$ docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS adpp7lpsu7j6rrz7c9j9wj68v * lee-PC Ready Active Leader
加入swarm集群
使用manager的token(current machine:192.168.126.136
)
# 136机器已manager身份(使用的token是manager的)加入swarm集群 dockerlee136@lee-PC:~$ docker swarm join --token \ SWMTKN-1-22tdl0iv7zgh3acdsxert42mcmrfbe7fg5lzxdmegd480p0rqj-36dl2p9w73gjvh0kuhwozb3rw 192.168.126.134:2377 This node joined a swarm as a manager. # 展示所有节点 dockerlee136@lee-PC:~$ docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS adpp7lpsu7j6rrz7c9j9wj68v lee-PC Ready Active Leader en91vjoyfns9b0697sgj6trpc * lee-PC Ready Active Reachable
使用worker的方式加入(current machine:192.168.126.137
)
# 137的机器已worker身份(使用的token是worker的)加入swarm集群 dockerlee137@localhost:~$ docker swarm join --token \ SWMTKN-1-22tdl0iv7zgh3acdsxert42mcmrfbe7fg5lzxdmegd480p0rqj-5byurq37c024gb5vnaw0qhd2n 192.168.126.134:2377 This node joined a swarm as a worker. # 展示所有节点,因为是worker节点,所以没有权限 dockerlee137@localhost:~$ 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. \ Please run this command on a manager node or promote the current node to a manager.
创建Docker service (current machine:192.168.126.134
)
# 展示service dockerlee@lee-PC:~$ docker service ls ID NAME REPLICAS IMAGE COMMAND # 创建一个service dockerlee@lee-PC:~$ docker service create -p 8000:8000 \ --replicas 3 --name simple imekaku/simple-web python /work/simple.py 1s4azb89d0jni57iafcce8d2i # 展示service dockerlee@lee-PC:~$ docker service ls ID NAME REPLICAS IMAGE COMMAND 1s4azb89d0jn simple 0/3 imekaku/simple-web python /work/simple.py # 展示service,此时replicas3个地方都正常工作 dockerlee@lee-PC:~$ docker service ls ID NAME REPLICAS IMAGE COMMAND 1s4azb89d0jn simple 3/3 imekaku/simple-web python /work/simple.py
这里使用的是我自己做的一个十分粗糙的镜像(大约400Mb)
如果希望使用,可以docker pull imekaku/simple-web
开启后,在三台机器上curl http://127.0.0.1:8000
可以得到Hello, friendly user!
字样
示意图
更改节点角色
To promote a node or set of nodes, run docker node promote from a manager node:
$ docker node promote node-3 node-2
Node node-3 promoted to a manager in the swarm.
Node node-2 promoted to a manager in the swarm.
To demote a node or set of nodes, run docker node demote from a manager node:
$ docker node demote node-3 node-2
Manager node-3 demoted in the swarm.
Manager node-2 demoted in the swarm.
https://docs.docker.com/engine/swarm/manage-nodes/#promote-or-demote-a-node