docker_swarm发布服务挂载本地卷:
存储卷应用:
持久化存储应用:(最大的一个需求点)
本地卷:
[root@swarm-1 ~]# docker service create --replicas 1 --mount "type=bind,source=$PWD,target=/abc" --publish 8081:80 192.168.17.10/library/centos-nginx:v1 --name cnginxsvc
--replicas: 副本越多,说明服务越稳定。一个容器掉了,我还有其他容器。
--mount: 把我们的数据保存在本地。
[root@swarm-1 ~]# docker service create --replicas 1 --mount "type=bind,source=$PWD,target=/abc" --publish 8081:80 --name cnginxsvc 192.168.17.10/library/centos-nginx:v1
image 192.168.17.10/library/centos-nginx:v1 could not be accessed on a registry to record
its digest. Each node will access 192.168.17.10/library/centos-nginx:v1 independently,
possibly leading to different nodes running different
versions of the image.
ea0i4c6s33upj4cfqsoyn4lp1
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged
我们只创建了一个副本:我们通过docker service ps cnginxsvc命令检查在那确认service运行在那个主机上。
[root@swarm-1 ~]# docker service ps cnginxsvc
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
jkqa86uc84qq cnginxsvc.1 192.168.17.10/library/centos-nginx:v1 swarm-3 Running Running about a minute ago
然后我们到swarm-3这台主机上检查容器:
[root@swarm-3 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
296905d7ca7d 192.168.17.10/library/centos-nginx:v1 "/bin/sh -c /usr/sbi…" 33 seconds ago Up 32 seconds 80/tcp cnginxsvc.1.jkqa86uc84qqormhdi8bgp5vo
44b66067405e nginx:latest "/docker-entrypoint.…" 2 hours ago Up 2 hours 80/tcp stack-demo_nginx.2.jvwpf1um0f5m70alx52cp5oa0
You have new mail in /var/spool/mail/root
[root@swarm-3 ~]#
[root@swarm-3 ~]# docker exec cnginxsvc.1.jkqa86uc84qqormhdi8bgp5vo ls /abc
abc
bin
dev
etc
home
lib
lib64
lost+found
我们执行在容器中ls /目录,检查abc目录。这个应该是在创建容器的时候产生的。
另外还有一个问题:我是在swarm-1 node(manager node)创建的service,这个service是运行在swarm-3这个节点node上。那么在swarm-3的这个节点上挂载的是本地目录,abc目录里显示的是swarm-3这个节点的内容,而不是swarm-1节点下的内容。
这点跟老师视频解决有区别。
服务scaling扩大和缩小:
[root@swarm-1 ~]# docker service scale cnginxsvc=3
cnginxsvc scaled to 3
overall progress: 3 out of 3 tasks
1/3: running [==================================================>]
2/3: running [==================================================>]
3/3: running [==================================================>]
verify: Service converged
那么,三台服务器节点都有service:
manager node会进行调度将service可以运行到三台服务器各一个,也可以一台服务器上有两个。
[root@swarm-1 ~]# docker service ps cnginxsvc
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
jkqa86uc84qq cnginxsvc.1 192.168.17.10/library/centos-nginx:v1 swarm-3 Running Running 17 minutes ago
sg1spuurjjmu cnginxsvc.2 192.168.17.10/library/centos-nginx:v1 swarm-2 Running Running 56 seconds ago
ny1cx7ud7j3a cnginxsvc.3 192.168.17.10/library/centos-nginx:v1 swarm-1 Running Running 56 seconds ago
[root@swarm-1 ~]#
然后我们再检查下挂载情况:文章来源:https://www.toymoban.com/news/detail-538694.html
[root@swarm-1 ~]# docker exec cnginxsvc.3.ny1cx7ud7j3afpyrcfnxbi5tr ls /abc
anaconda-ks.cfg
docker-compose.yaml
harbor
harbor-offline-installer-v2.8.2.tgz
nginxtest
[root@swarm-1 ~]#
[root@swarm-2 ~]# docker exec cnginxsvc.2.sg1spuurjjmuzg5uj5fp4ee65 ls /abc
anaconda-ks.cfg
node2.txt
[root@swarm-3 ~]# docker exec cnginxsvc.1.jkqa86uc84qqormhdi8bgp5vo ls /abc
anaconda-ks.cfg
node3.txt
可以得出结论:通过--mount 数据卷。将容器/abc目录的内容映射到各自本机的/root中。所有主机挂载都是本地的。这种方式有局限性。文章来源地址https://www.toymoban.com/news/detail-538694.html
到了这里,关于Docker学习笔记27的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!