2025年4月4日 星期五 乙巳(蛇)年 正月初五 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 云技术 > Docker

创建 Docker Swarm 集群

时间:12-14来源:作者:点击数:10
城东书院 www.cdsy.xyz

Docker Swarm 是由 Docker 提供的原生集群工具,通过它可以很方便的将多台主机组成一个集群。 其中,每个集群都可以有多台 manager 和多台 worker。

创建 Swarm 集群

创建 Swarm 集群的动作其实就是把一台主机 Node 初始化成 manager 的行为,方法超级简单,只需要在 manager node 主机上执行

  • docker swarm init --advertise-addr 172.27.0.8
  • Swarm initialized: current node (wrjpsf0ms32ioem9ozw5xu27r) is now a manager.
  • To add a worker to this swarm, run the following command:
  • docker swarm join --token SWMTKN-1-0brpw02md8cpm97xld6go5tat7fuj7hggdvp7an5rm49ybhp8v-76mcm21ak8phkez4ktf8wa5b0 172.27.0.8:2377
  • To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

docker swarm init 会随机生成两个 token,一个 worker token,一个 manager token。

比如上面输出中的 SWMTKN-1-0brpw02md8cpm97xld6go5tat7fuj7hggdvp7an5rm49ybhp8v-76mcm21ak8phkez4ktf8wa5b0 就是Worker Token

后面的 172.27.0.8:2377 就是当前 cluster 的 IP 和监听的端口

而根据提示,获取 manager token 通过执行下面命令来获取

  • docker swarm join-token manager
  • To add a manager to this swarm, run the following command:
  • docker swarm join --token SWMTKN-1-0brpw02md8cpm97xld6go5tat7fuj7hggdvp7an5rm49ybhp8v-0zyncobrt5vfcms9r8f1exyok 172.27.0.8:2377

manager Token 为 SWMTKN-1-0brpw02md8cpm97xld6go5tat7fuj7hggdvp7an5rm49ybhp8v-0zyncobrt5vfcms9r8f1exyok

我们可以用 docker info 查一下 Swarm 状态

  • docker info |sed -n "/^Swarm/,+5 p"
  • WARNING: bridge-nf-call-ip6tables is disabled
  • Swarm: active
  • NodeID: wrjpsf0ms32ioem9ozw5xu27r
  • Is Manager: true
  • ClusterID: 9pz2p3d57im2yo2v3vayb5w8n
  • Managers: 1
  • Nodes: 1

其中 Is Manager: true 说明本节点是作为 Manager, Managers: 1 说明集群中有一个Manager, Nodes: 1 说明集群中只有一个节点。

我们还可以通过 docker node ls 命令来查看集群中各节点的信息

  • docker node ls
  • ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
  • wrjpsf0ms32ioem9ozw5xu27r * VM_0_8_centos Ready Active Leader 18.03.1-ce

ID后面的 * 号表示这个记录就是当前NODE的信息。

向集群中添加节点

现在我们往这个集群中添加一个 worker 节点,方法很简单,跟着 docker swarm init 输出中的命令来执行就行了

这次我们在 worker node 主机上执行

  • docker swarm join --token SWMTKN-1-0brpw02md8cpm97xld6go5tat7fuj7hggdvp7an5rm49ybhp8v-76mcm21ak8phkez4ktf8wa5b0 118.24.148.238:2377
  • This node joined a swarm as a worker.

注意到我这里 docker swarm join 的IP是 118.24.148.238, 而不是前面输出的 172.27.0.8, 这是因为 172.27.0.8 是我云主机的内网IP,而 118.24.148.238 才是映射出来的公网IP地址。

而我的 worker node 主机是通过公网IP来访问 manager node 主机的,因此这里对IP进行了修改。

另外,有时候可能会遇到需要把一个 work 节点提升到 manager 节点的可能,则可以在 join 命令中添加 --listen-addr ${WORKER_IP}:2377 作为监听准备

增加节点后,再来看一下集群中各节点的信息:

在 manager node 上执行

  • docker node ls
  • ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
  • qpinc63kw5ta2t1b5ojhi8r1o T520 Ready Active 18.05.0-ce
  • wrjpsf0ms32ioem9ozw5xu27r * VM_0_8_centos Ready Active Leader 18.03.1-ce

可以看到多了一个NODE信息。

另外值得一说的是,类似 docker node ls 这些管理类的命令只能在 manager node 上执行,若在 worker node 上执行则会失败:

  • [lujun9972@T520 ~]$ docker node ls
  • Error response from daemon: This node is not a swarm manager. Worker nodes can't be used to view or modify cluster state. Please run this command on a manager node or promote the current node to a manager.

部署服务到集群中

我们在 manager node 上使用 docker service create 命令来创建一个服务.

但是在部署服务前,先需要保证集群中所有节点都预先有了要使用的镜像

docker service create 跟 docker start 的命令接口有点像:

  • docker service create --name web --publish 8080:80 nginx:alpine
  • rttpyxf8wbajmsr312j4r20ub
  • overall progress: 0 out of 1 tasks 
  • 1/1: overall progress: 0 out of 1 tasks 1/1: assigned overall progress: 0 out of 1 tasks 1/1: starting overall progress: 0 out of 1 tasks 1/1: starting overall progress: 0 out of 1 tasks 1/1: starting overall progress: 0 out of 1 tasks 1/1: running overall progress: 1 out of 1 tasks 
  • verify: Waiting 5 seconds to verify that tasks are stable... verify: Waiting 5 seconds to verify that tasks are stable... verify: Waiting 5 seconds to verify that tasks are stable... verify: Waiting 5 seconds to verify that tasks are stable... verify: Waiting 5 seconds to verify that tasks are stable... verify: Waiting 4 seconds to verify that tasks are stable... verify: Waiting 4 seconds to verify that tasks are stable... verify: Waiting 4 seconds to verify that tasks are stable... verify: Waiting 4 seconds to verify that tasks are stable... verify: Waiting 4 seconds to verify that tasks are stable... verify: Waiting 3 seconds to verify that tasks are stable... verify: Waiting 3 seconds to verify that tasks are stable... verify: Waiting 3 seconds to verify that tasks are stable... verify: Waiting 3 seconds to verify that tasks are stable... verify: Waiting 3 seconds to verify that tasks are stable... verify: Waiting 2 seconds to verify that tasks are stable... verify: Waiting 2 seconds to verify that tasks are stable... verify: Waiting 2 seconds to verify that tasks are stable... verify: Waiting 2 seconds to verify that tasks are stable... verify: Waiting 2 seconds to verify that tasks are stable... verify: Waiting 1 seconds to verify that tasks are stable... verify: Waiting 1 seconds to verify that tasks are stable... verify: Waiting 1 seconds to verify that tasks are stable... verify: Waiting 1 seconds to verify that tasks are stable... verify: Waiting 1 seconds to verify that tasks are stable... verify: Service converged 

这里 --name web 表示创建的服务名称为 web, 这个服务是基于 nginx:alpine 这个镜像的,而且容器的HTTP端口发布在主机的8080端口上。

而上面命令中的输出 rttpyxf8wbajmsr312j4r20ub 则是这个服务的ID号

我们可以使用 docker service ls 来查看集群中的服务

  • docker service ls
  • ID NAME MODE REPLICAS IMAGE PORTS
  • rttpyxf8wbaj web replicated 1/1 nginx:alpine *:8080->80/tcp

通过上面的 REPLICAS 可以知道,这个服务由单个容器提供,而且目前有一个容器在提供服务。

我们可以通过 docker service create 的 --replicas 参数来设置创建服务时启动的副本数,比如

  • docker service create --name ping --replicas 2 alpine ping www.baidu.com
  • k2cvjo3nb4o7d1cqr4thv4c51
  • overall progress: 0 out of 2 tasks 
  • 1/2: 
  • 2/2: overall progress: 0 out of 2 tasks 1/2: assigned 2/2: assigned overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 1/2: preparing 2/2: preparing overall progress: 0 out of 2 tasks 2/2: preparing 1/2: preparing overall progress: 0 out of 2 tasks 1/2: starting 2/2: starting overall progress: 0 out of 2 tasks 2/2: starting 1/2: starting overall progress: 0 out of 2 tasks 1/2: running 2/2: starting overall progress: 1 out of 2 tasks 1/2: running 2/2: running overall progress: 2 out of 2 tasks 
  • verify: Waiting 5 seconds to verify that tasks are stable... verify: Waiting 5 seconds to verify that tasks are stable... verify: Waiting 5 seconds to verify that tasks are stable... verify: Waiting 5 seconds to verify that tasks are stable... verify: Waiting 5 seconds to verify that tasks are stable... verify: Waiting 4 seconds to verify that tasks are stable... verify: Waiting 4 seconds to verify that tasks are stable... verify: Waiting 4 seconds to verify that tasks are stable... verify: Waiting 4 seconds to verify that tasks are stable... verify: Waiting 4 seconds to verify that tasks are stable... verify: Waiting 3 seconds to verify that tasks are stable... verify: Waiting 3 seconds to verify that tasks are stable... verify: Waiting 3 seconds to verify that tasks are stable... verify: Waiting 3 seconds to verify that tasks are stable... verify: Waiting 3 seconds to verify that tasks are stable... verify: Waiting 2 seconds to verify that tasks are stable... verify: Waiting 2 seconds to verify that tasks are stable... verify: Waiting 2 seconds to verify that tasks are stable... verify: Waiting 2 seconds to verify that tasks are stable... verify: Waiting 2 seconds to verify that tasks are stable... verify: Waiting 1 seconds to verify that tasks are stable... verify: Waiting 1 seconds to verify that tasks are stable... verify: Waiting 1 seconds to verify that tasks are stable... verify: Waiting 1 seconds to verify that tasks are stable... verify: Waiting 1 seconds to verify that tasks are stable... verify: Service converged 

这里创建了一个名为 ping 的服务,这个服务启动了两个alpine容器,执行 ping www.baidu.com 这个命令

我们再来看一下这个集群中的服务

  • docker service ls
  • ID NAME MODE REPLICAS IMAGE PORTS
  • k2cvjo3nb4o7 ping replicated 2/2 alpine:latest
  • rttpyxf8wbaj web replicated 1/1 nginx:alpine *:8080->80/tcp

调整服务规模

使用命令 docker service scale ${SERVICE}=${NUMBER} 可以调整指定服务的规模,比如

  • docker service scale web=5
  • web scaled to 5
  • overall progress: 0 out of 5 tasks 
  • 1/5: 
  • 2/5: 
  • 3/5: 
  • 4/5: 
  • 5/5: 1/5: running overall progress: 1 out of 5 tasks 2/5: assigned 3/5: assigned 4/5: assigned 5/5: assigned 1/5: running overall progress: 1 out of 5 tasks 2/5: preparing 3/5: ready 4/5: starting 5/5: preparing 1/5: running overall progress: 1 out of 5 tasks 2/5: starting 3/5: starting 4/5: starting 5/5: starting 1/5: running overall progress: 1 out of 5 tasks 1/5: running 2/5: starting 3/5: starting 4/5: starting 5/5: starting overall progress: 1 out of 5 tasks 2/5: starting 3/5: starting 4/5: starting 5/5: starting 1/5: running overall progress: 1 out of 5 tasks 2/5: starting 3/5: starting 4/5: starting 5/5: starting 1/5: running overall progress: 1 out of 5 tasks 4/5: starting 5/5: starting 1/5: running 2/5: starting 3/5: starting overall progress: 1 out of 5 tasks 2/5: starting 3/5: starting 4/5: starting 5/5: starting 1/5: running overall progress: 1 out of 5 tasks 2/5: starting 3/5: starting 4/5: running 5/5: starting 1/5: running overall progress: 2 out of 5 tasks 2/5: starting 3/5: starting 4/5: running 5/5: starting 1/5: running overall progress: 2 out of 5 tasks 2/5: starting 3/5: starting 4/5: running 5/5: starting 1/5: running overall progress: 2 out of 5 tasks 2/5: running 3/5: running 4/5: running 5/5: running 1/5: running overall progress: 5 out of 5 tasks 
  • verify: Waiting 5 seconds to verify that tasks are stable... verify: Waiting 5 seconds to verify that tasks are stable... verify: Waiting 5 seconds to verify that tasks are stable... verify: Waiting 5 seconds to verify that tasks are stable... verify: Waiting 5 seconds to verify that tasks are stable... verify: Waiting 4 seconds to verify that tasks are stable... verify: Waiting 4 seconds to verify that tasks are stable... verify: Waiting 4 seconds to verify that tasks are stable... verify: Waiting 4 seconds to verify that tasks are stable... verify: Waiting 4 seconds to verify that tasks are stable... verify: Waiting 3 seconds to verify that tasks are stable... verify: Waiting 3 seconds to verify that tasks are stable... verify: Waiting 3 seconds to verify that tasks are stable... verify: Waiting 3 seconds to verify that tasks are stable... verify: Waiting 3 seconds to verify that tasks are stable... verify: Waiting 2 seconds to verify that tasks are stable... verify: Waiting 2 seconds to verify that tasks are stable... verify: Waiting 2 seconds to verify that tasks are stable... verify: Waiting 2 seconds to verify that tasks are stable... verify: Waiting 2 seconds to verify that tasks are stable... verify: Waiting 1 seconds to verify that tasks are stable... verify: Waiting 1 seconds to verify that tasks are stable... verify: Waiting 1 seconds to verify that tasks are stable... verify: Waiting 1 seconds to verify that tasks are stable... verify: Waiting 1 seconds to verify that tasks are stable... verify: Service converged 

这个命令将启动5个容器来提供web服务,我们再来看一下这个集群中的服务

  • docker service ls
  • ID NAME MODE REPLICAS IMAGE PORTS
  • k2cvjo3nb4o7 ping replicated 2/2 alpine:latest
  • rttpyxf8wbaj web replicated 5/5 nginx:alpine *:8080->80/tcp

我们可以看到 web 服务的 REPLICAS 变成了 5

查看服务详细信息

我们可以使用 docker service inspect 命令来查看某项服务的详细内容,比如

  • docker service inspect --pretty web
  • ID: rttpyxf8wbajmsr312j4r20ub
  • Name: web
  • Service Mode: Replicated
  • Replicas: 5
  • Placement:
  • UpdateConfig:
  • Parallelism: 1
  • On failure: pause
  • Monitoring Period: 5s
  • Max failure ratio: 0
  • Update order: stop-first
  • RollbackConfig:
  • Parallelism: 1
  • On failure: pause
  • Monitoring Period: 5s
  • Max failure ratio: 0
  • Rollback order: stop-first
  • ContainerSpec:
  • Image: nginx:alpine@sha256:56a9367b64eaef37894842a6f7a19a0ef8e7bd5de964aa844a70b3e2d758033c
  • Resources:
  • Endpoint Mode: vip
  • Ports:
  • PublishedPort = 8080
  • Protocol = tcp
  • TargetPort = 80
  • PublishMode = ingress

我们还可以使用 docker service ps ${SERVICE} 来查看服务运行在那几个node主机上

  • docker service ps web
  • ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
  • xsldf1trr9ea web.1 nginx:alpine VM_0_8_centos Running Running 35 minutes ago
  • rf7s8r2e44xy web.2 nginx:alpine VM_0_8_centos Running Running 5 minutes ago
  • g0fc0edmriig web.3 nginx:alpine VM_0_8_centos Running Running 5 minutes ago
  • 2y39megirflj web.4 nginx:alpine VM_0_8_centos Running Running 5 minutes ago
  • on0k4qnv7d5v web.5 nginx:alpine VM_0_8_centos Running Running 5 minutes ago

可以看到所有的容器都是运行在 VM_0_8_centos 这个节点上的,我们在这个节点上直接运行 docker ps 命令来看看是不是有5个nginx容器在运行

  • docker ps |grep nginx:alpine
  • e93f8e412c4d nginx:alpine "nginx -g 'daemon of…" 11 minutes ago Up 11 minutes 80/tcp web.2.rf7s8r2e44xyvmqdno7442oez
  • abecc4812055 nginx:alpine "nginx -g 'daemon of…" 11 minutes ago Up 11 minutes 80/tcp web.3.g0fc0edmriigwx4crz64zv3id
  • cc7d82708eec nginx:alpine "nginx -g 'daemon of…" 11 minutes ago Up 11 minutes 80/tcp web.4.2y39megirflj5nwko3kvhl8yu
  • 4d695daec1ef nginx:alpine "nginx -g 'daemon of…" 11 minutes ago Up 11 minutes 80/tcp web.5.on0k4qnv7d5v6b0g6z8s361s2
  • fa08b0567c13 nginx:alpine "nginx -g 'daemon of…" 42 minutes ago Up 42 minutes 80/tcp web.1.xsldf1trr9eavsdsh9efpne55

可以看到确实是5个容器在跑,与结果温和

删除服务

删除服务就特别简单了,直接在manager node上运行 docker service rm ${SERVICE} 就行了,比如

  • docker service rm ping
  • ping

然后我们再用查看一下集群中的服务

  • docker service ls
  • ID NAME MODE REPLICAS IMAGE PORTS
  • rttpyxf8wbaj web replicated 5/5 nginx:alpine *:8080->80/tcp

现在只剩下 web 这一个服务了

服务升级

当提供服务容器的镜像发生更改后,可以使用 docker service update --image ${NEW_IMAGE} ${SERVICE} 来进行服务升级。

  • docker service update --image nginx:1.15.1-alpine-perl web

我们再来看一下集群中的服务

  • docker service ls
  • ID NAME MODE REPLICAS IMAGE PORTS
  • rttpyxf8wbaj web replicated 5/5 nginx:1.15.1-alpine-perl *:8080->80/tcp

可以看到,服务镜像已经发生了改变。

NODE退出集群

要将某个NODE退出集群十分简单,只需要在那个node上执行 docker swarm leave 即可。

比如我在 worker node 上执行

  • docker swarm leave
  • Node left the swarm.

再在 manager node 上查看一下 node 信息

  • docker node ls
  • ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
  • qpinc63kw5ta2t1b5ojhi8r1o T520 Down Active 18.05.0-ce
  • wrjpsf0ms32ioem9ozw5xu27r * VM_0_8_centos Ready Active Leader 18.03.1-ce

可以看到集群中 T520 的状态已经变成 Down 了,要想把这个节点完全从集群中移除,则可以在 manager node 上执行 docker node rm 命令

  • docker node rm T520
  • T520

再查看一下 node 信息

  • docker node ls
  • ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
  • wrjpsf0ms32ioem9ozw5xu27r * VM_0_8_centos Ready Active Leader 18.03.1-ce

好了, T520 这个 node 已经被彻底移除了。

城东书院 www.cdsy.xyz
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐