docker swarm 集群构建:

swarm 在docker 1.12 版本后内置

#h官方文档tps://docs.docker.com/engine/swarm/#feature-highlights


系统环境:

  centos7.3

  docker17.06



1、初始化manager节点:

  docker swarm init

  为实现高可用,manager节点需要有奇数个,类似zookerper选举制度。


2、复制生成的命令,在node节点运行。


#docker swarm join-token manager

  To add a manager to this swarm, run the following command:


docker swarm join --token SWMTKN-1-1hwdongcnsvzh92s1wnd33w9p08k1o18vobvkzu7z509xoduif-2ezofrbnkaamwuuby9d1w0wn5 10.39.3.25:2377


# docker swarm join-token worker

   To add a worker to this swarm, run the following command:


docker swarm join --token SWMTKN-1-1hwdongcnsvzh92s1wnd33w9p08k1o18vobvkzu7z509xoduif-1j43vxwnsrc3cyz2y7ips3d3g 10.39.3.25:2377



示例:

   如目前有 10.39.3.25--29 集群, 选其中3个25、26、27 做为manager节点,用于管理swarm集群和实现高可用。

# docker node ls

ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS

  dga3oelektv4tbofub2ii9d36     cephnode5           Ready               Active

  j0hmbmvn9pwnpr1n9zm20j67k *   cephnode3           Ready               Active              Reachable

  k7pb8msn85c6xzflbe4r57itd     cephnode1           Ready               Active              Reachable

  mmlszdi5lcr0zt2nk44pcjvya     cephnode4           Ready               Active

  t9f91au60k3cgf406cqe1pn2t     cephnode2           Ready               Active              Leader


2个manager是无法实现高可用的,主节点异常后,备用节点运行命令将报错如下:

#docker node ls

Error response from daemon: rpc error: code = 2 desc = The swarm does not have a leader. It's possible that too few managers are online. Make sure more than half of the managers are online.