准备三个Docker环境
这里我们叫它他为 manager,node1,node2
docker环境可以用docker-machine( https://www.nongtianlang.com/post/docker-machine/ )来搭建
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
manager
开一个终端
$ docker-machine create manager -d virtualbox --virtualbox-boot2docker-url=$HOME/.docker/machine/cache/boot2docker.iso
进入主机
$ docker-machine ssh manager
node1
再开一个终端
$ docker-machine create node1 -d virtualbox --virtualbox-boot2docker-url=$HOME/.docker/machine/cache/boot2docker.iso
进入主机
$ docker-machine ssh node1
node2
再开一个终端
$ docker-machine create node2 -d virtualbox --virtualbox-boot2docker-url=$HOME/.docker/machine/cache/boot2docker.iso
进入主机
$ docker-machine ssh node2
查看所有docker主机
再开一个终端
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
manager - virtualbox Running tcp://192.168.99.100:2376 v18.09.0
node1 - virtualbox Running tcp://192.168.99.101:2376 v18.09.0
node2 - virtualbox Running tcp://192.168.99.102:2376 v18.09.0
可以看到三个主机的一些信息
|
swarm集群
manager上创建swarm
1
2
3
4
5
6
7
8
9
10
|
manager主机
$ docker swarm init --advertise-addr 192.168.99.100
Swarm initialized: current node (2g055v9ayzjbabi4kazdlz36r) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-2qipc1iivmfmuxnsq293a8nlmsxo0fb6kmdvufxtne0uz5rnkw-brabjkyps2rp9vbo95k8b5r2i 192.168.99.100:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
|
node1加入swarm
1
2
3
4
|
node1主机,使用manager上swarm创建时生成的命令
$ docker swarm join --token SWMTKN-1-2qipc1iivmfmuxnsq293a8nlmsxo0fb6kmdvufxtne0uz5rnkw-brabjkyps2rp9vbo95k8b5r2i 192.168.99.100:2377
This node joined a swarm as a worker.
|
node2加入swarm
1
2
3
4
|
node2主机,使用manager上swarm创建时生成的命令
$ docker swarm join --token SWMTKN-1-2qipc1iivmfmuxnsq293a8nlmsxo0fb6kmdvufxtne0uz5rnkw-brabjkyps2rp9vbo95k8b5r2i 192.168.99.100:2377
This node joined a swarm as a worker.
|
在manager上查看所有节点
1
2
3
4
5
6
7
|
manager主机
$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
2g055v9ayzjbabi4kazdlz36r * manager Ready Active Leader 18.09.0
j71an2e98s6wbalkju97ulb9m node1 Ready Active 18.09.0
kw97y6khnnvfu5wq7yb7n79yw node2 Ready Active 18.09.0
|
swarm集群创建成功
用host方式部署服务(mode=host)
1
2
3
4
5
6
7
8
|
manager主机
$ docker service create --name my_web --replicas 3 --publish mode=host,published=8080,target=80 nginx
现在可以在浏览器中正常访问以下三个网址:
http://192.168.99.100:8080/
http://192.168.99.101:8080/
http://192.168.99.102:8080/
|