14.1 NFS介绍
NFS是Network File System的缩写 (网络层面,实现数据同步)
NFS最早由Sun公司开发,分2,3,4三个版本,2和3由Sun起草开发,4.0开始Netapp公司参与并主导开发,最新为4.1版本(2010年)
NFS数据传输基于RPC协议,RPC为Remote Procedure Call的简写(远程过程调用)。
NFS应用场景是:A,B,C三台机器上需要保证被访问到的文件是一样的,A共享数据出来,B和C分别去挂载A共享的数据目录,从而B和C访问到的数据和A上的一致
深入解析:
A/B/C三台服务器数据要一致,比如现在跑了一个网站,网站上传输很多图片,用户访问某个图片时需要去A服务器去请求,但是A服务器负载过高,为了分担负载增加两台服务器B和C,正常用户需要去A服务器查看到需要的图片,但是B和C作为A的负载均衡,分担相同的服务器,用户也有可能请求到B和C服务器,当请求到B上时本来数据在A上,需要在B上获取到A上数据,A上数据传输到B上同时传输到C上,但是不能实时去更新,假如客户传了图片到网站,马上想访问图片,但是请求到了B服务器上,结果A上数据还没来得及同步至BC服务器,此时访问不正常,访问空、404状态码;NFS服务可以解决问题,A上数据可以共享给B和C服务器,NFS服务实现,同时A上更新了数据、文件B、C服务器马上能看到,B上更新数据、文件A、C也马上能看到,同理C,这就是NFS应用场景
右边NFS服务端、左边NFS客户端
服务端需要启动NFS服务,NFS服务要想给NFS客户端提供服务需要借助于RPC协议(RPC协议是由rpcbind服务所实现,portmap在centos5以及之前版本叫法,centos6以及之后版本叫做rpcbind,两者是一样的,启动服务实现RPC协议通信),NFS服务默认不监听任何端口,最终实现监听端口,实现TCP/IP通信的过程是由rpcbind服务产生的RPC协议实现,rpcbind会监听111端口
首先TCP/IP通信服务端和客户端先通信,服务端NFS会在服务端RPC协议中去注册一个端口,服务端NFS告知服务端RPC它通信端口是什么,然后服务端RPC再告诉客户端rpcbind,NFS客户端的服务器再和服务端的NFS端口去通信,最终实现数据传输
NFS服务需要借助RPC协议实现通信
14.2 NFS服务端安装配置 14.3 NFS配置选项
服务端:
yum install -y nfs-utils rpcbind
vim /etc/exports //加入如下内容
/home/nfstestdir (服务端分享出去的目录绝对路径,AB两台机子访问的文件目录要一致一样)
192.168.133.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000) (
ip与(rw之间不能有空格, 否则客户端挂载的目录会变成只读..) (指定哪个机子共享ip或者ip段)
rw 读写
ro 只读
sync 同步模式,内存数据实时写入磁盘(很快把数据写到磁盘中,但是会降低磁盘效率)
async 非同步模式 (不实时写入磁盘,每隔一段时间将数据刷入磁盘中,保障磁盘效率,坏处:意外断电等可能会丢失部分数据)
下面三种模式解析:nfs在客户端使用服务端的共享目录,需要挂载到客户端的挂载点,就和本地目录一样,在操作本地目录时一定会有权限设置,下面就是指定用户权限
no_root_squash 客户端挂载NFS共享目录后,root用户不受约束,权限很大(相当于root用户在本地的磁盘上读写)
root_squash 与上面选项相对,客户端上的root用户收到约束,被限定成某个普通用户(限制root用户,当root用户切入至共享目录,身份就改变了,不再是root用户)
all_squash 客户端上所有用户在使用NFS共享目录时都被限定为一个普通用户(包括root用户)
anonuid/anongid 和上面几个选项搭配使用,定义被限定用户的uid和gid(此参数来规定上面三种模式用户被限定成什么用户)
保存配置文件后,执行如下准备操作
mkdir /home/nfstestdir
chmod 777 /home/nfstestdir
systemctl start rpcbind
systemctl start nfs
systemctl enable rpcbind
systemctl enable nfs
服务端:
[root
@localhost src]# yum install -y nfs-utils rpcbind
创建共享目录
编辑配置
/home/nfstestdir 192.168.1.3(rw,sync,all_squash,anonuid=1000,anongid=1000)
启动rpcbind(安装完后会自动启动rpcbind)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd(情况特殊,系统特性)
[root@localhost ~]# systemctl start rpcbind
[root@localhost ~]# ps aux|grep rpcbind
rpc 18778 0.0 0.0 68872 1668 ? Ss 12:31 0:00 /sbin/rpcbind -w
root 19041 0.0 0.0 112664 972 pts/1 S+ 12:46 0:00 grep --color=auto rpcbind
启动nfs服务
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# ps aux|grep nfs
root 19009 0.0 0.0 0 0 ? S< 12:46 0:00 [nfsd4_callbacks]
root 19015 0.0 0.0 0 0 ? S 12:46 0:00 [nfsd]
root 19016 0.0 0.0 0 0 ? S 12:46 0:00 [nfsd]
root 19017 0.0 0.0 0 0 ? S 12:46 0:00 [nfsd]
root 19018 0.0 0.0 0 0 ? S 12:46 0:00 [nfsd]
root 19019 0.0 0.0 0 0 ? S 12:46 0:00 [nfsd]
root 19020 0.0 0.0 0 0 ? S 12:46 0:00 [nfsd]
root 19021 0.0 0.0 0 0 ? S 12:46 0:00 [nfsd]
root 19022 0.0 0.0 0 0 ? S 12:46 0:00 [nfsd]
root 19039 0.0 0.0 112664 968 pts/1 S+ 12:46 0:00 grep --color=auto nfs
开机启动服务
[root@localhost ~]# systemctl enable rpcbind
[root@localhost ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
客户端
yum install -y nfs-utils
showmount -e 192.168.133.130 //该ip为NFS服务端ip
mount -t nfs 192.168.133.130:/home/nfstestdir /mnt
df -h
touch /mnt/aminglinux.txt
ls -l /mnt/aminglinux.txt //可以看到文件的属主和属组都为1000
客户端安装
[root@test1 ~]# yum install -y nfs-utils
服务端关闭防火墙并且关闭selinux
[root@localhost ~]# systemctl stop firewalld
客户端关闭防火墙并且关闭selinux
[root@test1 ~]# systemctl stop firewalld
客户端查看服务端是否有权限(正常通信需要防火墙关闭)
[root@test1 ~]# showmount -e 192.168.1.3
Export list for 192.168.1.2:
/home/nfstestdir 192.168.1.3
挂载 -t指定类型 IP:共享目录 挂载点
[root@test1 ~]# mount -t nfs 192.168.1.3:/home/nfstestdir /mnt
[root@test1 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/cl-root 96G 3.3G 93G 4% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 25M 1.9G 2% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 197M 156M 42M 79% /boot
tmpfs 380M 0 380M 0% /run/user/0
192.168.1.3:/home/nfstestdir 40G 4.2G 36G 11% /mnt
客户创建
[root@test1 ~]# cd /mnt/
[root@test1 ~]# touch nfs_test.txt
[root@test1 ~]]# ls -l nfs_test.txt
-rw-r--r-- 1 mysql mysql 0 12月 29 13:07 nfs_test.txt
服务端查看
[root@localhost ~]# ls -l /home/nfstestdir/
总用量 0
-rw-r--r-- 1 mysql mysql 0 12月 29 13:07 nfs_test.txt
[root@localhost ~]# id mysql
uid=1000(mysql) gid=1000(mysql) 组=1000(mysql)