blank
blank
发布于 2019-10-27 / 438 阅读 / 0 评论 / 0 点赞

docker 组建 swarm 集群

创建 swarm 集群前的准备

  • 安装完 docker-ce
  • 开放 2377/tcp 7946/tcp 集群通信端口

在 manager 机上运行 初始化 swarm manager

docker swarm init --advertise-addr <MASTER IP>

Swarm initialized: current node (7cbp689s0w3zcyrasqf1amsd2) is now a manager.

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

    docker swarm join --token SWMTKN-1-5flxc94h2ngj2joaiosggsxo0kid146z1efprjojnf78jc069r-d6pn95krm0z062u3c71vzcyfk 192.168.1.11:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

添加 manager (管理)节点

# 先在 manager 节点运行以下命令 得到在要成为 manager 节点中运行的命令
docker swarm join-token manager
To add a manager to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-5flxc94h2ngj2joaiosggsxo0kid146z1efprjojnf78jc069r-d6pn95krm0z062u3c71vzcyfk 192.168.1.11:2377
# 在要成为 manager 的节点上运行
docker swarm join --token SWMTKN-1-5flxc94h2ngj2joaiosggsxo0kid146z1efprjojnf78jc069r-d6pn95krm0z062u3c71vzcyfk 192.168.1.11:2377

添加 worker (工作) 节点

# 先在 manager 节点运行以下命令 得到在要成为 worker 节点中运行的命令
docker swarm join-token worker
To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-5flxc94h2ngj2joaiosggsxo0kid146z1efprjojnf78jc069r-9wgyeecb6l8yj24a0md8dw6xz 192.168.1.11:2377
# 在 所有 node 节点上运行
docker swarm join --token SWMTKN-1-5flxc94h2ngj2joaiosggsxo0kid146z1efprjojnf78jc069r-9wgyeecb6l8yj24a0md8dw6xz 192.168.1.11:2377
This node joined a swarm as a worker.

当你忘记哪台是 manager 机是 可以输入 docker info 查看

找到 Manager Addresses: 192.168.1.11:2377 的这台就是 manager 机 IP

docker info
Client:
 Debug Mode: false

Server:
 Containers: 2
  Running: 0
  Paused: 0
  Stopped: 2
 Images: 9
 Server Version: 19.03.4
 Swarm: active
  NodeID: kdvdlbhq244vlso72y39tls8k
  Is Manager: false
  Node Address: 192.168.1.12
  Manager Addresses:
   192.168.1.11:2377

在 manager 机中 运行 docker node ls 查看 集群状态

docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
495g4mx3nsy2080nmescol8gt *   node11              Ready               Active              Leader              19.03.4
kdvdlbhq244vlso72y39tls8k     node12              Ready               Active                                  19.03.4
fqfm9czf1cfr6egipv5lj90zy     node13              Ready               Active                                  19.03.4
r469p6twzrkianryfa3xx96nx     node14              Ready               Active                                  19.03.4
wsdss4gzjv3l03iyjn701ktyn     node15              Ready               Active                                  19.03.4
w8lmk90lcqxd7dz3nsc2fcbmg     node16              Ready               Active                                  19.03.4

移除节点 在 manager 节点运行 docker node remove $HOSTNAME

# docker node ls 中显示 的 HOSTNAME
docker node remove [HOSTNAME]

离开集群

# worker 节点运行
docker swarm leave
# 如果是 manager 节点要离开集群 需要在命令后 加 -f
docker swarm leave -f
Node left the swarm.

评论