DockerSwarm用于搭建Docker集群
简介
首先 我们区分几个概念
- Docker Swarm (老版本的Swarm和DockerMachine集成 需要自己搭建KV和服务发现)
- Swarmkit (一个用于编排分布式系统的项目)
- Docker Swarm Mode (新的DockerSwarm已经集成到DockerEngine)
区别 | Docker Swarm | Swarmkit | Docker Swarm Mode |
---|---|---|---|
安装 | 独立于Docker Engine并可以作为Container运行 | 需要单独编译和运行 | 集成在Docker Engine中(1.12以后的版本) |
外部存储 | 需要额外的KV存储(也可以用Token) | 需要额外的KV存储(例如consul,etcd) | 不需要额外的KV存储 |
服务功能 | 没有服务功能 | 没有服务发现 负载均衡和路由网络 | 支持服务模型以及相应的扩容缩容、服务发现、滚动升级、路由和负载均衡等 |
命令行 | 与DockerMachine集成 | 独立的CLI | 集成在Docker Engine中(1.12以后的版本) |
总之就是一句话 忘记
Docker Swarm
知道Swarmkit
用Docker Swarm Mode
详细区别 comparing-swarm-swarmkit-and-swarm-mode
防火墙开启
1 | firewall-cmd --zone=public --add-port=2376/tcp --permanent |
Docker Swarm(老版本)
安装
需要先安装 DockerMachine 然后按照下列步骤添加 Swarm
创建/添加
查看 DockerMachine 并连接 (可选)
查看已存在的机器
1
2
3
4
5[root@207 ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
206 - generic Running tcp://192.168.2.206:2376 v18.02.0-ce
207 - generic Running tcp://192.168.2.207:2376 v18.02.0-ce
208 - generic Running tcp://192.168.2.208:2376 v18.02.0-ce通过SSH链接到某个节点
1
2
3[root@207 ~]# docker-machine ssh 206
Last login: Mon Mar 12 01:11:54 2018 from 192.168.2.207
[root@206 ~]#
初始化集群
- 使用
docker-machine ssh 206 docker run swarm create
生成Token1
2
3
4
5[root@207 ~]# docker-machine ssh 206 docker run swarm create
651729865f8e56f8ddde11ff9c3499db
Token based discovery is now deprecated and might be removed in the future.
It will be replaced by a default discovery backed by Docker Swarm Mode.
Other mechanisms such as consul and etcd will continue to work as expected.
创建 Swarm 管理者
创建并附带
swarm
的相关参数1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16[root@207 ~]# docker-machine create --driver generic --generic-ip-address=192.168.2.206 --swarm --swarm-master --swarm-discovery token://651729865f8e56f8ddde11ff9c3499db swarm-master
Running pre-create checks...
Creating machine...
(206) No SSH key specified. Assuming an existing key at the default location.
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 centos...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Configuring swarm...
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 machine, run: docker-machine env 206查看节点状态
1
2
3[root@207 ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
swarm-master - generic Running tcp://192.168.2.206:2376 206 (master) v18.02.0-ce
添加工作节点到集群
删除已经添加的非SWARM节点(没有可以忽略)
1
2
3
4
5[root@207 ~]# docker-machine rm 207
About to remove 207
WARNING: This action will delete both local reference and remote instance.
Are you sure? (y/n): y
Successfully removed 206创建并附带
swarm
的相关参数1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16[root@207 ~]# docker-machine create --driver generic --generic-ip-address=192.168.2.207 --swarm --swarm-discovery token://651729865f8e56f8ddde11ff9c3499db swarm-207
Running pre-create checks...
Creating machine...
(207) No SSH key specified. Assuming an existing key at the default location.
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 centos...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Configuring swarm...
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 machine, run: docker-machine env 207同理 添加 208 节点
查看节点状态
1
2
3
4
5[root@207 ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
swarm-master - generic Running tcp://192.168.2.206:2376 206 (master) v18.02.0-ce
swarm-207 - generic Running tcp://192.168.2.207:2376 206 v18.02.0-ce
swarm-208 - generic Running tcp://192.168.2.208:2376 206 v18.02.0-ce
创建 SWARM 网络
1 | eval $(docker-machine env --swarm swarm-master) |
运行服务
此处的服务是个单例
1 | docker run -d --name=alpine1 --net=alpine --net-alias=alpine alpine:edge |
Docker Swarm Mode(新版本)
此处使用了 Docker Machine 进行多机器管理和连接 新版本的可以不需要 但是推荐使用 Docker Machine 方便管理多个 Docker 节点
初始化 SWARM MANAGER 节点
- Docker Machine
1
[root@206 ~]# docker-machine ssh 206 docker swarm init
- 单机执行 SSH连接到主节点机器后执行
1
[root@206 ~]# docker swarm init
- 返回结果 这里提供的命令可以用来添加 Worker 节点
1
2
3
4
5
6
7Swarm initialized: current node (4cngm238bxaq7hnryd20ef8dy) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-4728gc4i30jv9cvdhdk3bguqrk83k6teuejsh272ikvy8yt59f-2fy3kxueb8xlsr0bph0tsqdv1 192.168.2.206:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions. - 如果要添加 Manager 节点 执行
docker swarm join-token manager
这里提供的命令可以添加 Manager 节点1
2
3
4
5[root@206 ~]# docker swarm join-token manager
To add a manager to this swarm, run the following command:
docker swarm join --token SWMTKN-1-4728gc4i30jv9cvdhdk3bguqrk83k6teuejsh272ikvy8yt59f-2kdnx901a0munixenilv97fvw 192.168.2.206:2377
添加 Worker 节点
- Docker Machine
1
2
3
4[root@206 ~]# docker-machine ssh 207 docker swarm join --token SWMTKN-1-4728gc4i30jv9cvdhdk3bguqrk83k6teuejsh272ikvy8yt59f-2fy3kxueb8xlsr0bph0tsqdv1 192.168.2.206:2377
This node joined a swarm as a worker.
[root@206 ~]# docker-machine ssh 208 docker swarm join --token SWMTKN-1-4728gc4i30jv9cvdhdk3bguqrk83k6teuejsh272ikvy8yt59f-2fy3kxueb8xlsr0bph0tsqdv1 192.168.2.206:2377
This node joined a swarm as a worker. - 单机执行 SSH连接到 Worker 节点机器后执行
1
2
3
4[root@207 ~]# docker swarm join --token SWMTKN-1-4728gc4i30jv9cvdhdk3bguqrk83k6teuejsh272ikvy8yt59f-2fy3kxueb8xlsr0bph0tsqdv1 192.168.2.206:2377
This node joined a swarm as a worker.
[root@208 ~]# docker swarm join --token SWMTKN-1-4728gc4i30jv9cvdhdk3bguqrk83k6teuejsh272ikvy8yt59f-2fy3kxueb8xlsr0bph0tsqdv1 192.168.2.206:2377
This node joined a swarm as a worker.
查看节点
1 | [root@206 ~]# docker node ls |
创建SWARM网络
1 | docker network create -d overlay --attachable alpine |
创建服务
1 | docker service create --replicas 1 --name alpine --network alpine |
查看运行的服务
1 | [root@206 ~]# docker service ls |
常见问题
Docker Swarm 模式下创建 Swarm-Master 节点时 提示
Error creating machine: Error running provisioning: Error while creating container: 409 Conflict: Conflict. The container name "/swarm-agent-master" is already in use by container "bafd70d3708ad7b0e3f6d2d554c7550608b84c3561ef358d1b6290f1f2639355". You have to remove (or rename) that container to be able to reuse that name.
- 问题原因: 之前创建过相关节点
- 解决方案:
- 进入
Swarm-Master
节点 - 使用
docker rm bafd70d3708a --force
删除容器即可 - 或者直接使用
DockerMachine
执行docker-machine ssh 206 docker rm bafd70d3708a
hash值(bafd70d3708a)为提示的那个的前12位即可 当然全部也可以
- 进入
创建之后网络不会在子节点及时展示 只要主节点存在即可 子节点会在使用时显示(不知道是不是BUG)
1
2
3
4[root@206 maven]# docker network ls
NETWORK ID NAME DRIVER SCOPE
z4avoqoxil12 open overlay swarm
iamaw8xw4u0r sixi overlay swarm