Docker 1.12新功能探索(4):1分钟创建集群

时间:2021-12-25 09:01:38

Docker 1.12新功能探索(4):1分钟创建集群

在这篇文章里面我们将会通过创建一个极其简单的3个节点的集群来学习一下docker swarm 模式的使用方法。这个1.12版本中最引人关注的部分到底使用的感觉如何。因为没有在大型的生产环境中进行严格的比较,单从研究性质或者小规模的集群的创建的感觉来说和kubernetes的集群创建不相上下,使用方便,在准备妥当的情况下,手速快的朋友1分钟之内完全能够创建完毕。

事前确认

[root@host31 ~]# docker info |egrep 'Manager|Version'
WARNING: Usage of loopback devices is strongly discouraged for production use. Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
Server Version: 1.12.0
Library Version: 1.02.107-RHEL7 (2015-10-14)
Kernel Version: 3.10.0-327.el7.x86_64
[root@host31 ~]#

创建集群的Manager(Leader)

[root@host31 ~]# docker swarm init --advertise-addr 192.168.32.31:2377
Swarm initialized: current node (7hldumh3aj43tt5n95etu2zu5) is now a manager.

To add a worker to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-2ve6usua6ala8oqio497w7pjhqij6o6ew10ym6qoak303ct0an-3cnugfn7jkd62l8lp0p3ci5cm \
192.168.32.31:2377

To add a manager to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-2ve6usua6ala8oqio497w7pjhqij6o6ew10ym6qoak303ct0an-a82d0cp1hp5wser9m6uj5vyfo \
192.168.32.31:2377
[root@host31 ~]#

结果确认: 从docker info里我们可以看到,Is Manager 和Managers在创建集群之前是不存在的一个Item,创建完成之后,Is Manager被设定成了true,表明了本台node的身份。

[root@host31 ~]# docker info |egrep 'Manager|Version'
WARNING: Usage of loopback devices is strongly discouraged for production use. Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.
WARNING: bridge-nf-call-ip6tables is disabled
Server Version: 1.12.0
Library Version: 1.02.107-RHEL7 (2015-10-14)
Is Manager: true
Managers: 1
Kernel Version: 3.10.0-327.el7.x86_64
[root@host31 ~]#

确认集群节点信息

[root@host31 ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
7hldumh3aj43tt5n95etu2zu5 * host31 Ready Active Leader
[root@host31 ~]#

加入一个节点

[root@host33 ~]# docker swarm join \
> --token SWMTKN-1-2ve6usua6ala8oqio497w7pjhqij6o6ew10ym6qoak303ct0an-3cnugfn7jkd62l8lp0p3ci5cm \
> 192.168.32.31:2377
This node joined a swarm as a worker.
[root@host33 ~]#

结果确认:加入之后同样增加了Is Manager的Item,当然因为是普通节点,此处被设定为false,同时没有Managers这个Item。

[root@host32 ~]# docker info |egrep 'Manager|Version'
WARNING: Usage of loopback devices is strongly discouraged for production use. Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.
Server Version: 1.12.0
Library Version: 1.02.107-RHEL7 (2015-10-14)
Is Manager: false
Kernel Version: 3.10.0-327.el7.x86_64
[root@host32 ~]#

确认集群节点信息

已经可以看到host33成功的加入到集群之中了

[root@host31 ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
7hldumh3aj43tt5n95etu2zu5 * host31 Ready Active Leader
9c58j9yl5zektje7o4391uq1p host33 Ready Active
[root@host31 ~]#

再加入一个节点

[root@host34 ~]# docker swarm join \
> --token SWMTKN-1-2ve6usua6ala8oqio497w7pjhqij6o6ew10ym6qoak303ct0an-3cnugfn7jkd62l8lp0p3ci5cm \
> 192.168.32.31:2377
This node joined a swarm as a worker.
[root@host34 ~]#

确认集群节点信息

host34也成功地加入到集群当中了

[root@host31 ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
09je641kruls9fxb8112vhz7v host34 Ready Active
7hldumh3aj43tt5n95etu2zu5 * host31 Ready Active Leader
9c58j9yl5zektje7o4391uq1p host33 Ready Active
[root@host31 ~]#