简介
Docker Swarm 是一个为 IT 运维提供集群和调度能力的编排工具。
官方文档:https://docs.docker.com/swarm/
运行 Docker 的主机可以主动初始化一个 Swarm 集群或者加入一个已存在的 Swarm 集群,这样这个运行 Docker 的主机就成为一个 Swarm 集群的节点 (node) 。节点分为管理 (manager) 节点和工作 (worker) 节点。
管理节点用于 Swarm 集群的管理,docker swarm 命令基本只能在管理节点执行(节点退出集群命令 docker swarm leave 可以在工作节点执行)。一个 Swarm 集群可以有多个管理节点,但只有一个管理节点可以成为 leader,leader 通过 raft 协议实现。
工作节点是任务执行节点,管理节点将服务 (service) 下发至工作节点执行。管理节点默认也作为工作节点。你也可以通过配置让服务只运行在管理节点。下图展示了集群中管理节点与工作节点的关系。
特点
-
服务的高用性
Docker Swarm 由 Swarm Manager 提供高可用性,通过创建多个 Swarm master 节点和制定主 master 节点宕机时的备选策略。如果一个 master 节点宕机,那么一个 slave 节点就会被升格为 master 节点,直到原来的 master 节点恢复正常。
此外,节点无法加入集群,Swarm 会继续尝试加入,并提供错误警报和日志。在节点出错时,Swarm 现在可以尝试把容器重新调度到正常的节点上去。 -
灵活的容器调度
-
Docker API 及整合支持的兼容性
Swarm 对 Docker API 完全支持,如 Docker CLI,Compose, Registry,Hub
创建swarm集群
初始化集群
manager节点
docker swarm init --advertise-addr 192.168.9.36
Swarm initialized: current node (16qchih428i98wwwmrw59n9v3) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-1ryl337idbwyt8dpuka4czpugzwnc0j3dft4arx9ntzpa2uqqk-dft3756xs301nqz59qt4f7q4g 192.168.9.36:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
加入集群
worker节点
docker swarm join --token SWMTKN-1-1ryl337idbwyt8dpuka4czpugzwnc0j3dft4arx9ntzpa2uqqk-dft3756xs301nqz59qt4f7q4g 192.168.9.36:2377
This node joined a swarm as a worker.
查看集群
docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
16qchih428i98wwwmrw59n9v3 * manager1 Ready Active Leader
le63qciq6gu1noetf3zq1p7zl worker1 Ready Active
tlg277myvmwglt2w7pzfphuoo worker2 Ready Active
这样创建了一个最小的 Swarm 集群,包含一个管理节点和两个工作节点。
部署服务
docker pull alpine
进入管理节点
docker service create --replicas 3 --name helloworld alpine ls
qgl0azd3o8o6pclqps4jocgyn
Since --detach=false was not specified, tasks will be created in the background.
In a future release, --detach=false will become the default.
docker service ps qgl0azd3o8o6pclqps4jocgyn
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
kcb6mcyiwwh9 helloworld.1 alpine:latest worker1 Running Running 12 seconds ago
w7iwsflxt0k5 helloworld.2 alpine:latest worker2 Running Running 12 seconds ago
m6veai1pfq8y helloworld.3 alpine:latest manager1 Running Running 16 seconds ago
docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
qgl0azd3o8o6 helloworld replicated 3/3 alpine:latest
监控集群
docker service inspect helloworld
集群伸缩
docker service update --replicas 5 --detach=false helloworld
overall progress: 6 out of 6 tasks
1/6: running
2/6: running
3/6: running
4/6: running
5/6: running
6/6: running
verify: Waiting 1 seconds to verify that tasks are stable...
docker service ps helloworld
docker service ps helloworld
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
kcb6mcyiwwh9 helloworld.1 alpine:latest worker1 Running Running 12 minutes ago
mcukad5gbsrm helloworld.2 alpine:latest worker1 Running Running about a minute ago
m6veai1pfq8y helloworld.3 alpine:latest manager1 Running Running 12 minutes ago
ytjqxyj05qna helloworld.4 alpine:latest worker2 Running Running about a minute ago
s77f45s7anu8 helloworld.5 alpine:latest worker2 Running Running about a minute ago
xjodt65mctki helloworld.6 alpine:latest manager1 Running Running about a minute ago