前言
Swarm是Docker官方提供的一款集群管理工具,其主要作用是把若干台Docker主机抽象为一个整体,并且通过一个入口统一管理这些Docker主机上的各种Docker资源。
目录
一、基本概念
节点
服务和任务
二、初始化集群
三、增加工作节点
四、查看集群
五、部署、查看和移除服务
Swarm 是使⽤SwarmKit构建的 Docker 引擎内置(原⽣)的集群管理和编排⼯具。Docker Swarm 是 Docker 官⽅三剑客项⽬之⼀,提供 Docker 容器集群服务,是 Docker 官⽅对容器云⽣态进⾏⽀持的核⼼⽅案。
使⽤它,⽤户可以将多个 Docker 主机封装为单个⼤型的虚拟 Docker 主机,快速打造⼀套容器云平 台。Swarm mode 内置 kv 存储功能,提供了众多的新特性,⽐如:具有容错能⼒的去中⼼化设计、内置服务发现、负载均衡、路由⽹格、动态伸缩、滚动更新、安全传输等。使得 Docker 原⽣的 Swarm 集群具备与 Mesos 、 Kubernetes 竞争的实⼒。使⽤ Swarm 集群之前需要了解以下⼏个概念。
运⾏ Docker 的主机可以主动初始化⼀个 Swarm 集群或者加⼊⼀个已存在的 Swarm 集群,这样这个 运⾏ Docker 的主机就成为⼀个 Swarm 集群的节点 (node) 。节点分为 管理 (manager) 节点和⼯作 (worker) 节点 。
管理节点⽤于 Swarm 集群的管理, docker swarm 命令基本只能在管理节点执⾏(节点退出集群命令 docker swarm leave 可以在⼯作节点执⾏)。⼀个 Swarm 集群可以有多个管理节点,但只有⼀个管理节点可以成为 leader ,leader 通过 raft 协议实现。
⼯作节点是任务执⾏节点,管理节点将服务 ( service ) 下发⾄⼯作节点执⾏。管理节点默认也作为⼯作节点。你也可以通过配置让服务只运⾏在管理节点。来⾃ Docker 官⽹的这张图⽚形象的展示了集群中管理节点与⼯作节点的关系。
任务(Task) 是 Swarm 中的最⼩的调度单位,⽬前来说就是⼀个单⼀的容器; 服务(Services) 是 指⼀组任务的集合,服务定义了任务的属性。服务有两种模式:
① replicated services :按照⼀定规则在各个⼯作节点上运⾏指定个数的任务。
② global services :每个⼯作节点上运⾏⼀个任务 。
两种模式通过 docker service create 的 --mode 参数指定。来⾃ Docker 官⽹的这张图⽚形象的展示了容器、任务、服务的关系。
我们这⾥利⽤上⼀节的 docker machine 来充当集群的主机,⾸先先创建⼀个 manager 节点,然后在该节点上执⾏初始化集群命令:
☁ ~ docker-machine create -d virtualbox manager
Running pre-create checks...
Creating machine...
(manager) Copying /Users/ych/.docker/machine/cache/boot2docker.iso to /Users/ych/.docker/m
achine/machines/manager/boot2docker.iso...
(manager) Creating VirtualBox VM...
(manager) Creating SSH key...
(manager) Starting the VM...
(manager) Check network to re-create if needed...
(manager) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual mach
ine, run: docker-machine env manager
☁ ~ docker-machine env manager
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.101:2376"
export DOCKER_CERT_PATH="/Users/ych/.docker/machine/machines/manager"
export DOCKER_MACHINE_NAME="manager"
# Run this command to configure your shell:
# eval $(docker-machine env manager)
☁ ~ eval $(docker-machine env manager)
☁ ~ docker-machine ssh manager
## .
## ## ## ==
## ## ## ## ## ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/
_ _ ____ _ _
| |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 18.03.1-ce, build HEAD : cb77972 - Thu Apr 26 16:40:36 UTC 2018
Docker version 18.03.1-ce, build 9ee9f40
docker@manager:~$ docker swarm init --advertise-addr 192.168.99.101
Swarm initialized: current node (3gsjpckj5ag1vvdg44fgzylow) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-1aqikkhsz91l4n7k9ig3xinjz0iv0fh4gcrlhp9mk3643rblca-
aqgqldlrw33k8heiao7yx27w5 192.168.99.101:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instr
uctions.
执⾏ docker swarm init 命令的节点⾃动成为管理节点。
管理节点初始化完成后,然后同样的⽤ docker-machine 创建⼯作节点,然后将其加⼊到管理节点之中去即可:
☁ ~ docker-machine create -d virtualbox worker1
Running pre-create checks...
Creating machine...
(worker1) Copying /Users/ych/.docker/machine/cache/boot2docker.iso to /Users/ych/.docker/m
achine/machines/worker1/boot2docker.iso...
(worker1) Creating VirtualBox VM...
(worker1) Creating SSH key...
(worker1) Starting the VM...
(worker1) Check network to re-create if needed...
(worker1) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual mach
ine, run: docker-machine env worker1
☁ ~ docker-machine ssh worker1
## .
## ## ## ==
## ## ## ## ## ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/
_ _ ____ _ _
| |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 18.03.1-ce, build HEAD : cb77972 - Thu Apr 26 16:40:36 UTC 2018
Docker version 18.03.1-ce, build 9ee9f40
docker@worker1:~$ docker swarm join --token SWMTKN-1-1aqikkhsz91l4n7k9ig3xinjz0iv0fh4gcrlh
p9mk364
3rblca-aqgqldlrw33k8heiao7yx27w5 192.168.99.101:2377
This node joined a swarm as a worker.
我们可以看到上⾯的提示信息:This node joined a swarm as a worker.,表明节点已经加⼊到 swarm 集群之中了。
经过上边的两步,我们已经拥有了⼀个最⼩的 Swarm 集群,包含⼀个管理节点和两个⼯作节点。 管理节点使⽤ docker node ls 查看集群:
☁ ~ docker node ls
ID HOSTNAME STATUS AVAILABILITY
MANAGER STATUS ENGINE VERSION
3gsjpckj5ag1vvdg44fgzylow * manager Ready Active
Leader 18.03.1-ce
cxmj5lr0vbwo1em9y9oang5m8 worker1 Ready Active
18.03.1-ce
ksruum3uc1c265ywm4kn9a88g worker2 Ready Active
18.03.1-ce
☁ ~ docker service ls
ID NAME MODE REPLICAS IMAGE
PORTS
☁ ~ docker service create --replicas 3 -p 80:80 --name nginx nginx:1.13.7-alpine
4k9cbna8ive87p4or9mny9kfs
overall progress: 3 out of 3 tasks
1/3: running [==================================================>]
2/3: running [==================================================>]
3/3: running [==================================================>]
verify: Service converged
☁ ~ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER
ERRORS
manager * virtualbox Running tcp://192.168.99.101:2376 v18.03.1-ce
worker1 - virtualbox Running tcp://192.168.99.102:2376 v18.03.1-ce
worker2 - virtualbox Running tcp://192.168.99.103:2376 v18.03.1-ce
☁ ~ docker service ls
ID NAME MODE REPLICAS IMAGE
PORTS
4k9cbna8ive8 nginx replicated 3/3 nginx:1.13
.7-alpine *:80->80/tcp
☁ ~ docker service ps nginx
ID NAME IMAGE NODE DESIRED
STATE CURRENT STATE ERROR PORTS
r7hmzkqsri8p nginx.1 nginx:1.13.7-alpine worker1 Running
Running about a minute ago
y0xgrfwmjfrj nginx.2 nginx:1.13.7-alpine worker2 Running
Running about a minute ago
j8k7be8xkbg3 nginx.3 nginx:1.13.7-alpine manager Running
Running about a minute ago
使⽤ docker service logs 来查看某个服务的⽇志。
☁ ~ docker service logs nginx
☁ ~ docker service rm nginx
nginx
正如之前使⽤ docker-compose.yml 来⼀次配置、启动多个容器,在 Swarm 集群中也可以使 ⽤ compose ⽂件(docker-compose.yml)来配置、启动多个服务。
上⼀节中,我们使⽤ docker service create ⼀次只能部署⼀个服务,使⽤ docker-compose.yml 我们 可以⼀次启动多个关联的服务。
我们以在 Swarm 集群中部署 WordPress 为例进⾏说明:(docker-compose.yml)
version: "3"
services:
wordpress:
image: wordpress
ports:
- 80:80
networks:
- overlay
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
deploy:
mode: replicated
replicas: 3
db:
image: mysql
networks:
- overlay
volumes:
- db-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
deploy:
placement:
constraints: [node.role == manager]
visualizer:
image: dockersamples/visualizer:stable
ports:
- "8080:8080"
stop_grace_period: 1m30s
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
deploy:
placement:
constraints: [node.role == manager]
volumes:
db-data:
networks:
overlay:
其中constraints: [node.role == manager]是调度策略,⽂档地址:https://docs.docker.com/swarm/scheduler/filter/
在 Swarm 集群管理节点新建该⽂件,其中的 visualizer 服务提供⼀个可视化⻚⾯,我们可以从浏览器 中很直观的查看集群中各个服务的运⾏节点。
在 Swarm 集群中使⽤ docker-compose.yml 我们⽤ docker stack 命令,下⾯我们对该命令进⾏详细讲解。
部署服务
使⽤ docker stack deploy ,其中 -c 参数指定 compose ⽂件名。
$ docker stack deploy -c docker-compose.yml wordpress
查看服务
$ docker stack ls
NAME SERVICES
wordpress 3
移除服务
要移除服务,使⽤ docker stack down :
$ docker stack down wordpress
Removing service wordpress_db
Removing service wordpress_visualizer
Removing service wordpress_wordpress
Removing network wordpress_overlay
Removing network wordpress_default
注:该命令不会移除服务所使⽤的 数据卷 ,如果你想移除数据卷请使⽤ docker volume rm