docker@manager1:~$ docker swarm init --advertise-addr 192.168.99.100
Swarm initialized: current node (j0o7sykkvi86xpc00w71ew5b6) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-47z6jld2o465z30dl7pie2kqe4oyug4fxdtbgkfjqgybsy4esl-8r55lxhxs7ozfil45gedd5b8a 192.168.99.100:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
docker@worker1:~$ docker swarm join \
--token SWMTKN-1-47z6jld2o465z30dl7pie2kqe4oyug4fxdtbgkfjqgybsy4esl-8r55lxhxs7ozfil45gedd5b8a \
192.168.99.100:2377
This node joined a swarm as a worker.
退出虚拟主机
docker@worker1:~$ exit
创建虚拟主机 worker2
创建
$ docker-machine create -d virtualbox worker2
入虚拟主机worker2
$ docker-machine ssh worker2
加入 swarm 集群
docker@worker2:~$ docker swarm join \
--token SWMTKN-1-47z6jld2o465z30dl7pie2kqe4oyug4fxdtbgkfjqgybsy4esl-8r55lxhxs7ozfil45gedd5b8a \
192.168.99.100:2377
This node joined a swarm as a worker.
退出虚拟主机
docker@worker2:~$ exit
两个工作节点添加完成
查看集群
进入管理节点:
docker-machine ssh manager1
宿主机子上查看虚拟主机
docker@manager1:~$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
manager1 * virtualbox Running tcp://192.168.99.100:2376 v17.12.1-ce
worker1 - virtualbox Running tcp://192.168.99.101:2376 v17.12.1-ce
worker2 - virtualbox Running tcp://192.168.99.102:2376 v17.12.1-ce
在主节点上面执行 docker node ls 查询集群主机信息
docker@manager1:~$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
1ipck4z2uuwf11f4b9mnon2ul * manager1 Ready Active Leader
rtcpqgcn2gytnvufwfveukgrv worker1 Ready Active
te2e9tr0qzbetjju5gyahg6f7 worker2 Ready Active
这样我们就创建了一个最小的 Swarm 集群,包含一个管理节点和两个工作节点。
部署服务
我们使用 docker service 命令来管理 Swarm 集群中的服务,该命令只能在管理节点运行。
docker@manager1:~$ docker service create --replicas 1 --name helloworld alpine ping ityouknow.com
rwpw7eij4v6h6716jvqvpxbyv
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged
命令解释:
docker service create 命令创建一个服务
--name 服务名称命名为 helloworld
--replicas 设置启动的示例数
alpine 指的是使用的镜像名称,ping ityouknow.com指的是容器运行的bash
使用命令 docker service ps rwpw7eij4v6h6716jvqvpxbyv 可以查看服务进展
docker@manager1:~$ docker service ps rwpw7eij4v6h6716jvqvpxbyv
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
rgroe3s9qa53 helloworld.1 alpine:latest worker1 Running Running about a minute ago
使用 docker service ls 来查看当前 Swarm 集群运行的服务。
docker@manager1:~$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
yzfmyggfky8c helloworld replicated 0/1 alpine:latest
监控集群状态
登录管理节点 manager1
docker-machine ssh manager1
运行 docker service inspect --pretty <SERVICE-ID> 查询服务概要状态,以 helloworld 服务为例:
docker@manager1:~$ docker service ps helloworld
NAME IMAGE NODE DESIRED STATE LAST STATE
helloworld.1.8p1vev3fq5zm0mi8g0as41w35 alpine worker1 Running Running 3 minutes
在工作节点查看任务的执行情况
docker-machine ssh worker1
在节点执行docker ps 查看容器的运行状态。
docker@worker1:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
96bf5b1d8010 alpine:latest "ping ityouknow.com" 4 minutes ago Up 4 minutes helloworld.1.rgroe3s9qa53lf4u4ky0tzcb8
docker service ps helloworld
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
rgroe3s9qa53 helloworld.1 alpine:latest manager1 Running Running 8 minutes ago
a61nqrmfhyrl helloworld.2 alpine:latest worker2 Running Running 9 seconds ago
调整 helloworld 的服务实例数为1个
docker service update --replicas 1 helloworld
再次查看节点运行情况:
docker service ps helloworld
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
a61nqrmfhyrl helloworld.2 alpine:latest worker2 Running Running about a minute ago
再次调整 helloworld 的服务实例数为3个
docker service update --replicas 3 helloworld
helloworld
overall progress: 3 out of 3 tasks
1/3: running [==================================================>]
2/3: running [==================================================>]
3/3: running [==================================================>]
verify: Service converged
查看节点运行情况:
docker@manager1:~$ docker service ps helloworld
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
mh7ipjn74o0d helloworld.1 alpine:latest worker2 Running Running 40 seconds ago
1w4p9okvz0xw helloworld.2 alpine:latest manager1 Running Running 2 minutes ago
snqrbnh4k94y helloworld.3 alpine:latest worker1 Running Running 32 seconds ago
删除集群服务
docker service rm helloworld
调整集群大小
动态调整 Swarm 集群的工作节点。
添加集群
创建虚拟主机 worker3
$ docker-machine create -d virtualbox worker3
入虚拟主机 worker3
$ docker-machine ssh worker3
加入swarm 集群
docker@worker3:~$ docker swarm join \
--token SWMTKN-1-47z6jld2o465z30dl7pie2kqe4oyug4fxdtbgkfjqgybsy4esl-8r55lxhxs7ozfil45gedd5b8a \
192.168.99.100:2377
This node joined a swarm as a worker.
退出虚拟主机
docker@worker3:~$exit
在主节点上面执行 docker node ls 查询集群主机信息
登录主节点
docker-machine ssh manager1
查看集群节点
docker@manager1:~$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
j0o7sykkvi86xpc00w71ew5b6 * manager1 Ready Active Leader
xwv8aixasqraxwwpox0d0bp2i worker1 Ready Active
ij3z1edgj7nsqvl8jgqelrfvy worker2 Ready Active
i31yuluyqdboyl6aq8h9nk2t5 worker3 Ready Active
docker@manager1:~$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
j0o7sykkvi86xpc00w71ew5b6 * manager1 Ready Active Leader
xwv8aixasqraxwwpox0d0bp2i worker1 Ready Active
ij3z1edgj7nsqvl8jgqelrfvy worker2 Down Active
i31yuluyqdboyl6aq8h9nk2t5 worker3 Ready Active
可以看出集群节点 worker2 状态已经下线
也可以再次加入
docker@worker2:~$ docker swarm join \
> --token SWMTKN-1-47z6jld2o465z30dl7pie2kqe4oyug4fxdtbgkfjqgybsy4esl-8r55lxhxs7ozfil45gedd5b8a \
> 192.168.99.100:2377
This node joined a swarm as a worker.
再次查看
docker@manager1:~$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
j0o7sykkvi86xpc00w71ew5b6 * manager1 Ready Active Leader
xwv8aixasqraxwwpox0d0bp2i worker1 Ready Active
0agpph1vtylm421rhnx555kkc worker2 Ready Active
ij3z1edgj7nsqvl8jgqelrfvy worker2 Down Active
i31yuluyqdboyl6aq8h9nk2t5 worker3 Ready Active