目录
1、Swarm中使用Volume和NFS服务
1.1、为什么需要nfs服务器?nfs是什么?nfs解决了什么问题?
2、布置一台nfs服务器
2.1、客户机上能否有写的权限要看两种权限
2.2、sync(同步)和async(异步)
2.1、如果我们在启动了NFS之后又修改了/etc/exports,是不是还要重新启动nfs呢?
2.2、实现开机自动挂载
2.3、Create a service which creates an NFS volume(创建NFS卷的服务)
注意:本文中使用的机器都是上个文章中提到的,若是想要了解可以看这篇文章
[docker]十、docker swarm是什么?以及创建docker swarm_FanMY_71的博客-CSDN博客
1、Swarm中使用Volume和NFS服务
目的:随便访问任何一台节点服务器上的web服务,看到的页面都是一样的,即满足volume被swarm里的service使用。所有的节点服务器都到同一台文件服务器上拿网页数据就可以了。
这台文件服务器就是nfs服务,另外这台机器里也需要安装docker
1.1、为什么需要nfs服务器?nfs是什么?nfs解决了什么问题?
廉价的解决方法:nfs;有钱的解决方法:san。
2、布置一台nfs服务器
第一步:安装nfs的相关软件
[root@nfs-service ~]# yum install nfs-utils -y # 这个步骤在所有需要用nfs服务的机器都要安装,但是下面的操作只需要在作为nfs服务的机器上操作就行
第二步:启动nfs-servcer服务
[root@nfs-service ~]# service nfs-server start Redirecting to /bin/systemctl start nfs-server.service [root@nfs-service ~]# ps aux|grep nfs root 2980 0.0 0.0 0 0 ? S< 17:12 0:00 [nfsd4_callbacks] root 2986 0.0 0.0 0 0 ? S 17:12 0:00 [nfsd] root 2987 0.0 0.0 0 0 ? S 17:12 0:00 [nfsd] root 2988 0.0 0.0 0 0 ? S 17:12 0:00 [nfsd] root 2989 0.0 0.0 0 0 ? S 17:12 0:00 [nfsd] root 2990 0.0 0.0 0 0 ? S 17:12 0:00 [nfsd] root 2991 0.0 0.0 0 0 ? S 17:12 0:00 [nfsd] root 2992 0.0 0.0 0 0 ? S 17:12 0:00 [nfsd] root 2993 0.0 0.0 0 0 ? S 17:12 0:00 [nfsd] root 3005 0.0 0.0 112824 988 pts/0 S+ 17:13 0:00 grep --color=auto nfs
第三步:编辑/etc/exports文件
# 限制挂载文件的权限 [root@nfs-service web]# cat /etc/exports /web 192.168.29.0/24(ro,all_squash,sync) /download 192.168.29.140/24(rw,all_squash,sync) [root@nfs-service ~]# mkdir /web [root@nfs-service web]# ls fan.gif index.html myself.jpg study.html # 往index.html输入东西能让你觉得这个网页改变了就行
2.1、客户机上能否有写的权限要看两种权限
- 共享权限。如/etc/exports文件里的权限,例如ro、rw
- 文件系统里的权限。/web在linux里的权限
第三步:修改/web文件的权限
[root@nfs-service web]# chmod o+w /web /download [root@nfs-service web]# ll -d /web/ drwxr-xrwx. 2 root root 75 4月 15 12:09 /web/ [root@nfs-service web]# ll -d /download/ drwxr-xrwx. 2 root root 6 4月 28 17:43 /download/ [root@nfs-service web]# exportfs -rv # 刷新输出文件的列表,并输出详细信息
2.2、sync(同步)和async(异步)
sync:当容器往宿主机里文件写东西,会同时往另一台机器的内存和硬盘中写入。这样保证了数据的不丢失。
async:当容器往宿主机里文件写东西,要等到宿主机里的cache缓存满了之后,才会往另一台机器的内存和硬盘中写入。
第四步:建议关闭防火墙和selinux
[root@nfs-server download]# service firewalld stop Redirecting to /bin/systemctl stop firewalld.service [root@nfs-server download]# systemctl disable firewalld [root@nfs-server download]# getenforce Disabled
第五步: 在客户机上挂载nfs服务共享的/web和/download目录
# 安装nfs-tils软件,方便客户机上挂载,具有了相关命令,例如:showmount [root@worker138 ~]# yum install nfs-utils -y # 查看nfs服务器上共享输出了哪些文件夹 [root@worker138 ~]# showmount -e 192.168.29.30 Export list for 192.168.29.30: /download 192.168.29.140/24 /web 192.168.29.0/24
[挂载nfs服务器上的目录到本机上]
若是使用的docker容器开启的nginx,那么这一步不用跟着敲,因为后边有命令会帮助所有客户机挂载nfs服务。若是是真实的nginx服务就要使用mount在 本机上挂载。
注意:因为是copy出来的服务器,所以文件内容都是一样的,我们要把除了nfs服务的机器以外的里的/web和/download清空。但是想要本机的/web或者/download文件夹必须存在,或者你用其他的文件夹接收挂载的文件也行。
[root@worker140 web]# mount 192.168.29.30:/web /web [root@worker140 web]# mount 192.168.29.30:/download /download [root@worker140 web]# df -Th |grep nfs4 192.168.29.30:/web nfs4 17G 8.1G 9.0G 48% /web 192.168.29.30:/download nfs4 17G 8.1G 9.0G 48% /download
# 若是在/web文件中挂载之后,要退出再进入之后才会显示挂载的内容 [root@worker140 web]# cd .. [root@worker140 /]# cd - /web [root@worker140 web]# ls fan.gif index.html myself.jpg study.html
注意:所有的docker宿主机都要挂载nfs服务器的/web和/download
[取消挂载]
取消挂载要在不是那个文件夹中取消。例如,我们现在要取消挂载/web
[root@worker138 /]# df -Th|grep web 192.168.29.30:/web nfs4 17G 8.1G 9.0G 48% /web [root@worker138 web]# cd / # 可以是除了/web的任何文件 [root@worker138 /]# umount /web [root@worker138 /]# df -Th|grep web
2.1、如果我们在启动了NFS之后又修改了/etc/exports,是不是还要重新启动nfs呢?
这个时候我们就可以用exportfs 命令来使改动立刻生效,该命令格式如下:
# exportfs [-aruv]
-a 全部挂载或卸载 /etc/exports中的内容
-r 重新读取/etc/exports 中的信息 ,并同步更新/etc/exports、/var/lib/nfs/xtab
-u 卸载单一目录(和-a一起使用为卸载所有/etc/exports文件中的目录)
-v 在export的时候,将详细的信息输出到屏幕上。
具体例子:
# exportfs -au 卸载所有共享目录
# exportfs -rv 重新共享所有目录并输出详细信息
[root@worker140 web]# mkdir ww # 这是因为挂载的时候,对/etc/exports里的文件做出权限的限制 mkdir: 无法创建目录"ww": 只读文件系统 [root@worker140 download]# mkdir hello [root@worker140 download]# ls hello
2.2、实现开机自动挂载
第一种方法:修改/etc/rc.local文件
在文件中加入"mount 192.168.29.30:/web /web"
第二种方法:修改/etc/fstab文件
/etc/fstab是linux系统开机会自动根据这个文件里的内容挂载磁盘分区
加入"192.168.29.30:/web /web nfs defaults 0 0"这行代码
192.168.29.30:/web 是挂载的分区,nfs的文件系统;后边这个/web是本地的挂载点;defaults,是挂载选项,使用默认;0,是否支持dump命令进行备份;0,是否开机的时候进行分区的文件系统的检查,分区的文件系统是否有问题
推荐使用第二种方法
2.3、Create a service which creates an NFS volume(创建NFS卷的服务)
NFSv3型
$ docker service create -d --name nfs-service --mount 'type=volume,source=nfsvolume,target=/app,volume-driver=local,volume-opt=type=nfs,volume-opt=device=:/var/docker-nfs,volume-opt=o=addr=10.0.0.10' nginx:latest
NFSv4型
# 这个命令要在manager里边执行,-d,放在后台执行,建议不要放在后台执行
$ docker service create -d
--name nfs-service
--mount 'type=volume,source=nfsvolume,target=/app,volume-driver=local,volume-opt=type=nfs,volume-opt=device=:/var/docker-nfs,"volume-opt=o=addr=10.0.0.10,rw,nfsvers=4,async"'
--replicas 10 -p 8089:80 nginx:latest
"""
--name,指定名字
source=nfsvolume docker宿主机上的卷的名字,若是没有可以自己创建,按自己的修改。另外这个卷名字只能使用一次
/app 容器里存放网页的目录,按自己的修改
volume-driver=local 访问本地的某个目录
volume-opt=type=nfs volume对nfs的支持选项
volume-opt=device=:/var/docker-nfs 是nfs服务器共享的目录
volume-opt=o=addr=10.0.0.10,rw,nfsvers=4,async 挂载具体的nfs服务器的IP地址和选项
--replicas 10 副本数量
-p 8089:90 映射端口
"""
# 我执行的代码
[root@manager _data]# docker service create --name fan-service --mount 'type=volume,source=nfsvolume,target=/usr/share/nginx/html,volume-driver=local,volume-opt=type=nfs,volume-opt=device=:/web,"volume-opt=o=addr=192.168.29.30,rw,nfsvers=4,async"' --replicas 10 -p 8089:80 nginx:latest
udx5rhb9uy89n1j3vibj9x4j8
[root@manager _data]# docker volume inspect nfsvolume
[
{
"CreatedAt": "2022-05-01T17:43:06+08:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/nfsvolume/_data",
"Name": "nfsvolume",
"Options": {
"device": ":/web",
"o": "addr=192.168.29.30,rw,nfsvers=4,async",
"type": "nfs"
},
"Scope": "local"
}
]
[root@manager ~]# cd /var/lib/docker/volumes/nfsvolume/_data
[root@manager _data]# ls
fan.gif index.html myself.jpg study.html
[查看在运行的容器]
[root@manager ~]# docker service ps fan-service ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR vvb9sgkpz1wy fan-service.1 nginx:latest nfs-service Running Running 26 seconds ago 5woz1nvox22l fan-service.2 nginx:latest worker138 Running Running 24 seconds ago q8mdxskerc30 fan-service.3 nginx:latest manager Running Running 27 seconds ago 5exfmv4lndfl fan-service.4 nginx:latest worker138 Running Running 24 seconds ago u930lx73v6g1 fan-service.5 nginx:latest nfs-service Running Running 26 seconds ago 9w4o6xqi1rry fan-service.6 nginx:latest manager Running Running 27 seconds ago nex07xqefokt fan-service.7 nginx:latest nfs-service Running Running 26 seconds ago y74k2dr0n6t2 fan-service.8 nginx:latest worker138 Running Running 24 seconds ago zi74iqx5zrfp fan-service.9 nginx:latest manager Running Running 27 seconds ago r1o4b32cx46a fan-service.10 nginx:latest worker138 Running Running 24 seconds ago
Share data among machines(在机器之间数据共享)
参考官方文献: Use volumes | Docker Documentation


![[docker]十一、docker nfs服务 [docker]十一、docker nfs服务](http://www.mshxw.com/aiimages/31/849654.png)
