1. 使用 docker swarm init 在本机初始化一个 Swarm 集群
( 192.168.132.207为manager节点 192.168.132.208为worker)
[root@192 project-name]# docker swarm init --advertise-addr 192.168.132.207
Swarm initialized: current node (d6kjtfjfm5jn8e94lvczp16f4) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-2ifl7hz1t2dpfn65p8d9ihjygdp4101q7621duukn7hxq3t9z2-7nyce3eofor2i45fp96qpus9t \
192.168.132.207:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
[root@192 project-name]#
2. 查看管理节点令牌环
[root@192 project-name]# docker swarm join-token manager
To add a manager to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-2ifl7hz1t2dpfn65p8d9ihjygdp4101q7621duukn7hxq3t9z2-6gnrnpyy7j8blxvp0fzmzunn6 \
192.168.132.207:2377
[root@192 project-name]#
3. 查看work的令牌环
[root@192 project-name]# docker swarm join-token worker
To add a worker to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-2ifl7hz1t2dpfn65p8d9ihjygdp4101q7621duukn7hxq3t9z2-7nyce3eofor2i45fp96qpus9t \
192.168.132.207:2377
[root@192 project-name]#
4.再在 192.168.132.208 执行操作
[root@localhost project-name]# docker swarm join \
> --token SWMTKN-1-2ifl7hz1t2dpfn65p8d9ihjygdp4101q7621duukn7hxq3t9z2-7nyce3eofor2i45fp96qpus9t \
> 192.168.132.207:2377
This node joined a swarm as a worker.
[root@localhost project-name]#
5,离开节点操作如下:(worker节点如下)
[root@localhost project-name]# docker swarm leave
Node left the swarm.
[root@localhost project-name]#
管理节点只能强制性推出:
[root@192 project-name]# docker swarm leave -f
Node left the swarm.
[root@192 project-name]#
6,docker swarm之service,必须先开启swarm
[root@192 project-name]# docker service create --replicas 2 --name test docker.io/nginx
k636cx4vt7ebxce9vexx3m0zb
[root@192 project-name]# docker service ls
ID NAME MODE REPLICAS IMAGE
k636cx4vt7eb test replicated 2/2 docker.io/nginx:latest
[root@192 project-name]#
[root@192 ~]# docker service create --replicas 3 --name test docker.io/nginx
uw3usyh4567hjrzvf9t08a1gy
[root@192 ~]# docker service ls
ID NAME MODE REPLICAS IMAGE
uw3usyh4567h test replicated 3/3 docker.io/nginx:latest
[root@192 ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
901w1l5rhsnx67nk0ra0nd70n * localhost.localdomain Ready Active Leader
kws017dtx5qs3xqscf2wf6lxx 192.168.132.206 Ready Active
[root@192 ~]# docker service ps test
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
f2fogyogih3l test.1 docker.io/nginx:latest 192.168.132.206 Running Running about a minute ago
tn0ma5q8o1dh test.2 docker.io/nginx:latest localhost.localdomain Running Running 2 minutes ago
fum6amocz7re test.3 docker.io/nginx:latest 192.168.132.206 Running Running about a minute ago