docker swarm init
,将本机节点由单引擎模式切换为swarm集群模式。swarm 集群相关概念:
swarm 流程:
swarm 管理节点高可用:
主机 | IP | 作用 |
---|---|---|
node1 | 192.168.27.11 | 安装有Docker(18.09.6) |
node2 | 192.168.27.12 | 安装有Docker(18.09.6) |
node3 | 192.168.27.13 | 安装有Docker(18.09.6) |
通过swarm实现一个集群,拉伸,缩减节点,更新服务内容,并且在主控节点安装一个图像监控visualizer容器
[root@node1 images]# docker load -i ngnix.tar
[root@node1 images]# docker load -i game2048.tar
[root@node1 images]# docker load -i visualizer.tar
[root@node2 images]# docker load -i ngnix.tar
[root@node2 images]# docker load -i game2048.tar
[root@node3 images]# docker load -i ngnix.tar
[root@node3 images]# docker load -i game2048.tar
[root@node1 ~]# docker swarm init
Swarm initialized: current node (v4xx2txjkdysv6ke783820tcd) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-15suzdeu07c9l6uf0pk2up7a3j21evjlnczrg4t6bqekscvccn-4y9np7dcdmrjtklsybyc2zjsm 192.168.27.11:2377
#以woork身份加入集群的命令,在其他需要加入集群的节点执行此命即可
To add a manager to this swarm, run 'docker swarm join-token manager' andfollow the instructions.
#开启swarm集群,node1为管理节点,上面提示中给出了join woker的方式,如果要以manager身份加入集群,查看join-token,命令docker swarm join-token manager
[root@node2 ~]# docker swarm join --token SWMTKN-1-15suzdeu07c9l6uf0pk2up7a3j21evjlnczrg4t6bqekscvccn-4y9np7dcdmrjtklsybyc2zjsm 192.168.27.11:2377
This node joined a swarm as a worker.
[root@node3 ~]# docker swarm join --token SWMTKN-1-15suzdeu07c9l6uf0pk2up7a3j21evjlnczrg4t6bqekscvccn-4y9np7dcdmrjtklsybyc2zjsm 192.168.27.11:2377
This node joined a swarm as a worker.
[root@node1 images]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
v4xx2txjkdysv6ke783820tcd * node1 Ready Active Leader 18.09.6
mv238rores67ue8bc3nocfb6h node2 Ready Active 18.09.6
iiu8b0tw4bzsf848smjrc7r6c node3 Ready Active 18.09.6
[root@node1 images]# docker service create -p 80:80 --replicas 3 --name web_cluster nginx
image nginx:latest could not be accessed on a registry to record
its digest. Each node will access nginx:latest independently,
possibly leading to different nodes running different
versions of the image.
#因为没有使用仓库,报错提示,不影响试验
sb8uuq2ekxghlv8qzlg5csm0j
overall progress: 3 out of 3 tasks
1/3: running
2/3: running
3/3: running
verify: Service converged
#服务建立过程
[root@node1 images]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
sb8uuq2ekxgh web_cluster replicated 3/3 nginx:latest *:80->80/tcp
#查服务是否建立
[root@node1 images]# docker service ps web_cluster
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
qz72v7tujl8r web_cluster.1 nginx:latest node3 Running Running less than a second ago
xa75noxnlsqt web_cluster.2 nginx:latest node1 Running Running 42 seconds ago
ytdr9mg6nu5n web_cluster.3 nginx:latest node2 Running Running 44 seconds ago
#查看建立的服务web_cluster,中具体的task任务分布情况
[root@node1 images]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
f0870e291b8c nginx:latest "nginx -g 'daemon of…" 13 minutes ago Up 13 minutes 80/tcp
#查看容器ID
[root@node1 images]# echo node1 > index.html
[root@node1 images]# docker cp index.html
f0870e291b8c :/usr/share/nginx/html/index.html
#修改index内容
[root@node2 opt]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
999072a92b00 nginx:latest "nginx -g 'daemon of…" 15 minutes ago Up 14 minutes 80/tcp
#查看容器ID
[root@node2 opt]# echo node2 > index.html
[root@node2 opt]# docker cp index.html 999072a92b00:/usr/share/nginx/html/index.html
#修改index内容
[root@node3 opt]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
a0be23d3551a nginx:latest "nginx -g 'daemon of…" 18 minutes ago Up 18 minutes 80/tcpluster.1.qz72v7tujl8r11dzebb7i0ccc
#查看容器ID
[root@node3 opt]# echo node3 > index.html
[root@node3 opt]# docker cp index.html a0be23d3551a:/usr/share/nginx/html/index.html
#修改index内容
[root@node1 images]# for i in {1..10};do curl 192.168.27.11;done
node2
node3
node1
node2
node3
node1
node2
node3
node1
node2
[root@node1 images]# for i in {1..10};do curl 192.168.27.12;done
node3
node1
node2
node3
node1
node2
node3
node1
node2
node3
[root@node1 images]# for i in {1..10};do curl 192.168.27.13;done
node1
node2
node3
node1
node2
node3
node1
node2
node3
node1
[root@node1 images]# docker service scale web_cluster=6
web_cluster scaled to 6
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: Service converged
[root@node1 images]# docker service ps web_cluster
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
qz72v7tujl8r web_cluster.1 nginx:latest node3 Running Running less than a second ago
x9jpdjbdq5r7 web_cluster.2 nginx:latest node1 Running Running 10 minutes ago
ytdr9mg6nu5n web_cluster.3 nginx:latest node2 Running Running 13 minutes ago
u1j1romoes7c web_cluster.4 nginx:latest node1 Running Running 8seconds ago
jy78pio77yoe web_cluster.5 nginx:latest node3 Running Running less than a second ago
1rpve5ncf7l9 web_cluster.6 nginx:latest node2 Running Running 9seconds ago
[root@node1 images]# docker service scale web_cluster=4
web_cluster scaled to 4
overall progress: 4 out of 4 tasks
1/4: running
2/4: running
3/4: running
4/4: running
verify: Service converged
[root@node1 images]# docker service ps web_cluster
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
qz72v7tujl8r web_cluster.1 nginx:latest node3 Running Running less than a second ago
x9jpdjbdq5r7 web_cluster.2 nginx:latest node1 Running Running 12 minutes ago
ytdr9mg6nu5n web_cluster.3 nginx:latest node2 Running Running 14 minutes ago
u1j1romoes7c web_cluster.4 nginx:latest node1 Running Running about a minute ago
[root@node1 images]# docker service create --name=viz --publish=8080:8080/tcp --constraint=node.nager --mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock dockersamples/visualizer
image dockersamples/visualizer:latest could not be accessed on a registry to record
its digest. Each node will access dockersamples/visualizer:latest independently,
possibly leading to different nodes running different
versions of the image.
uusdr62myrnopa7zf44io3obp
overall progress: 1 out of 1 tasks
1/1: running
verify: Service converged
[root@node1 images]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
uusdr62myrno viz replicated 1/1 dockersamples/visualizer:latest *:8080->8080/tcp
sb8uuq2ekxgh web_cluster replicated 9/9 nginx:latest *:80->80/tcp
#服务显示,viz创建
[root@node1 images]# docker service ps viz
ID NAME IMAGE NODE DESIRED CURRENT STATE ERROR PORTS
bupr51oo05yn viz.1 dockersamples/visualizer:latest node1 Running Running 2 minutes ago
#服务具体任务
[root@node1 images]# docker service update --image game2048 --update-delay 5s --update-parallelism 5 web_cluster
#docker service update 更新命令
#--image 更新镜像
#--update-parallelism 5 每次更新容器并行运行的数量,本次设置为5个,也就是每一批更新5个
#--update-delay 5s 每批次更新容器之间的时间间隔,本次设置为5秒
image game2048:latest could not be accessed on a registry to record
its digest. Each node will access game2048:latest independently,
possibly leading to different nodes running different
versions of the image.
web_cluster
overall progress: 9 out of 9 tasks
1/9: running
2/9: running
3/9: running
4/9: running
5/9: running
6/9: running
7/9: running
8/9: running
9/9: running
verify: Service converged
[root@node1 images]# docker service update --image nginx --rollback-parallelism 3 --rollback-delay 2s web_cluster
#docker service update 回滚也属于更新命令下的选项
#--rollback-parallelism 每一批回滚容器的个数,与更新一样
#--rollback-delay 每批次回滚容器之间的时间间隔,与更新一样
image nginx:latest could not be accessed on a registry to record
its digest. Each node will access nginx:latest independently,
possibly leading to different nodes running different
versions of the image.
web_cluster
overall progress: 9 out of 9 tasks
1/9: running
2/9: running
3/9: running
4/9: running
5/9: running
6/9: running
7/9: running
8/9: running
9/9: running
verify: Service converged