部署swarm mode集群

环境准备

主机名(角色) IP
swarm-manager 172.16.100.20
swarm-node1 172.16.100.22
swarm-node2 172.16.100.22

加入swarm mode集群后不允许修改主机名

前提条件

  1. 安装Docker Engine 1.12或更新版本
  2. 允许2377的tcp端口和7946的TCP/UDP端口(overlay网络类型需要允许4789的UDP端口)

创建swarm mode集群

1
2
3
4
5
6
7
8
#  docker swarm init --advertise-addr 172.16.100.20
Swarm initialized: current node (sc21k9597zasfjaf6cfpuyvy6) is now a manager.

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

docker swarm join --token SWMTKN-1-10rnutvx6cpja7wv88k7ydywpvjjz1xsj88on00s43te740xca-1pwd9juzgpwnxlxne7p2g93va 172.16.100.20:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
1
2
3
4
5
6
7
8
9
10
11
12
13
# docker info |grep -A5 Swarm
Swarm: active
NodeID: pg6fteetxsezu2ygyd3b0joye
Is Manager: true
ClusterID: yb5c85p7o054sxp1hb8ieqw43
Managers: 1
Nodes: 1
# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
pg6fteetxsezu2ygyd3b0joye * swarm-manager Ready Active Leader
# netstat -lntp|grep docker
tcp6 0 0 :::2377 :::* LISTEN 1249/dockerd
tcp6 0 0 :::7946 :::* LISTEN 1249/dockerd

添加节点到swarm mode集群

swarm mode集群有manager和worker节点,可通过docker swarm join-token [manager|worker]命令获取节点添加命令

1
2
3
4
5
6
7
8
9
# docker swarm join-token manager
To add a manager to this swarm, run the following command:

docker swarm join --token SWMTKN-1-5vp5axn28a2cbrtzxlirktbhpnluayacuw81zqacooe3ooe2o3-6ys543fe4zkeagkoaacgaqe3e 172.16.100.20:2377

# docker swarm join-token worker
To add a worker to this swarm, run the following command:

docker swarm join --token SWMTKN-1-5vp5axn28a2cbrtzxlirktbhpnluayacuw81zqacooe3ooe2o3-64gphy50682jszwc19nn0onpc 172.16.100.20:2377

分别在node1和node2节点上执行如下的docker swarm join命令添加worker节点

1
# docker swarm join --token SWMTKN-1-5vp5axn28a2cbrtzxlirktbhpnluayacuw81zqacooe3ooe2o3-64gphy50682jszwc19nn0onpc 172.16.100.20:2377

管理swarm mode集群节点

1
2
3
4
5
# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
lati2179dcwgkvvkc0qcieoim swarm-node2 Ready Active
pg6fteetxsezu2ygyd3b0joye * swarm-manager Ready Active Leader
y83k6khc3vxmch1qd3j8kl4ak swarm-node1 Ready Active

AVAILABILITY

  • Active :scheduler可分配任务到该节点;
  • Pause:scheduler无法分配新任务到该节点,但有任务仍在运行;
  • Drain:scheduler无法分配新任务到该节点,scheduler停止已存在的任务并调度到其他可用的节点上。

MANAGER STATUS

  • 空值:表示worker节点
  • Leader:表示主manager节点,负责swarm mode集群的管理和编排
  • Reachable: 表示manager节点并参与raft一致性算法的仲裁(如果leader不可用,该节点可被选举为新的leader)
  • Unavailable:表示该manager节点无法与其他manager节点进行交互(如果manager变成unavailable,必须加入新的manager节点到swarm mode集群或者将worker节点提升为manager)

升/降级节点

  • 升级worker节点为manager节点

    1
    2
    3
    4
    5
    6
    7
    8
    # docker node promote swarm-node1 swarm-node2
    Node swarm-node1 promoted to a manager in the swarm.
    Node swarm-node2 promoted to a manager in the swarm.
    # docker node ls
    ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
    lati2179dcwgkvvkc0qcieoim swarm-node2 Ready Active Reachable
    pg6fteetxsezu2ygyd3b0joye * swarm-manager Ready Active Leader
    y83k6khc3vxmch1qd3j8kl4ak swarm-node1 Ready Active Reachable
  • 降级manager节点为worker节点

    1
    2
    3
    4
    5
    6
    7
    8
    # docker node demote swarm-node1 swarm-node2
    Manager swarm-node1 demoted in the swarm.
    Manager swarm-node2 demoted in the swarm.
    # docker node ls
    ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
    lati2179dcwgkvvkc0qcieoim swarm-node2 Ready Active
    pg6fteetxsezu2ygyd3b0joye * swarm-manager Ready Active Leader
    y83k6khc3vxmch1qd3j8kl4ak swarm-node1 Ready Active

移除节点

移除节点时需要先在worker节点上执行docker swarm leave命令将节点状态设为Down后,在manager节点上执行docker node rm <node-name>移除。如果要移除manager节点,不建议使用--force强制移除,而应该先进行降级后再移除。

1
2
3
4
# docker swarm leave
Error response from daemon: You are attempting to leave the swarm on a node that is participating as a manager. Removing the last manager erases all current state of the swarm. Use `--force` to ignore this message.
# docker node rm swarm-node1
Error response from daemon: rpc error: code = 9 desc = node y83k6khc3vxmch1qd3j8kl4ak is not down and can't be removed
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@swarm-node2 ~]# docker swarm leave
Node left the swarm.
[root@swarm-manager ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
lati2179dcwgkvvkc0qcieoim swarm-node2 Down Active
pg6fteetxsezu2ygyd3b0joye * swarm-manager Ready Active Leader
y83k6khc3vxmch1qd3j8kl4ak swarm-node1 Ready Active
[root@swarm-manager ~]# docker node rm swarm-node2
swarm-node2
[root@swarm-manager ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
pg6fteetxsezu2ygyd3b0joye * swarm-manager Ready Active Leader
y83k6khc3vxmch1qd3j8kl4ak swarm-node1 Ready Active
坚持原创技术分享,您的支持将鼓励我继续创作!
0%