一、名词解释
1、什么是NFS?
全称 network file system网络文件系统
通过网络存储和组织文件的一种方法或机制2、什么是文件系统
文件系统是操作系统用于明确存储设备(常见的是磁盘,也有基于NAND Flash的固态硬盘)或分区上的文件的方法和数据结构;即在存储设备上组织文件的方法。操作系统中负责管理和存储文件信息的软件机构称为文件管理系统,简称文件系统。文件系统由三部分组成:文件系统的接口,对对象操纵和管理的软件集合,对象及属性。从系统角度来看,文件系统是对文件存储设备的空间进行组织和分配,负责文件存储并对存入的文件进行保护和检索的系统。具体地说,它负责为用户建立文件,存入、读出、修改、转储文件,控制文件的存取,当用户不再使用时撤销文件等。
3、为什么要用共享存储
前端所有的应用服务器接受到用户上的图片、文件、视频,都会统一的放到后端的存储上。
为什么要共享存储?所有节点服务器都需要将内容存到存储上,取的统一来取
4、共享存储的种类
单点存储系统就是NFS,中小型企业,阿里云服务器的NAS服务。OSS对象存储
大型企业用FastDFS、Ceph、GlsterFS、Mfs
硬件存储:传统企业:稳定、2台 双主机头 几十块硬盘 RAID10
二、原理及实现
1、NFS工作原理
NFS网络文件系统
启动NFS服务,而且还要启动很多端口
NFS功能,需要很多服务。每个服务都有端口,而且经常变化
如何要让客户端找到这些端口呢?就需要一个rpc服务
NFS服务:
1、NFS服务(有很多进程和端口) port:2049
2、RPC服务(对外固定端口111)
客户端请求NFS服务,先找RPC 111端口,找到NFS的端口。
配置文件 /etc/exports
[root@backup backup]# man exports
EXAMPLE
# sample /etc/exports file
/ master(rw) trusty(rw,no_root_squash)
/projects proj*.local.domain(rw)
/usr *.local.domain(ro) @trusted(rw)
/home/joe pc001(rw,all_squash,anonuid=150,anongid=100)
/pub *(ro,insecure,all_squash)
/srv/www -sync,rw server @trusted @external(ro)
/foo 2001:db8:9:e54::/64(rw) 192.0.2.0/24(rw)
/build buildhost[0-9].local.domain(rw)
The first line exports the entire filesystem to machines master and trusty. In addition to write access, all uid squashing is turned off for host trusty.
The second and third entry show examples for wildcard hostnames and netgroups (this is the entry `@trusted'). The fourth line shows the entry for the PC/NFS
client discussed above. Line 5 exports the public FTP directory to every host in the world, executing all requests under the nobody account. The insecure
option in this entry also allows clients with NFS implementations that don't use a reserved port for NFS. The sixth line exports a directory read-write to
the machine 'server' as well as the `@trusted' netgroup, and read-only to netgroup `@external', all three mounts with the `sync' option enabled. The seventh
line exports a directory to both an IPv6 and an IPv4 subnet. The eighth line demonstrates a character class wildcard match.
FILES
/etc/exports /etc/exports.d
1)待共享的目录 存东西的目录 取东西的目录 例如:/date
2)访问的主机
172.16.1.7 (web1) 单个主机
172.16.1.0/24 网段
172.16.1.* 网段
3)()权限
rw 可读 read write 安全 慢
async 异步写到远端缓冲区 不安全 快
实践:
服务器端:
0、安装rpcbind和nfs-utils.x86_64 并启动服务
yum install -y nfs-utils.x86_64 rpcbind
systemctl start nfs-utils.service
systemctl enable nfs-utils.service
systemctl start rpcbind
systemctl enable rpcbind
检查端口注册
rpcinfo -p 127.0.0.1
1、修改exports文件
[root@nfs01 172.16.1.31]# cat /etc/exports
#wushuo shared dir at time
/date 172.16.1.0/24(rw,sync)
2、创建文件
[root@nfs01 172.16.1.31]# mkdir -p /data
[root@nfs01 172.16.1.31]# ll -d /data
drwxr-xr-x 2 root root 6 Apr 17 11:53 /data
修改文件所属主组
[root@nfs01 172.16.1.31]# ll -d /data/
drwxr-xr-x 2 nfsnobody nfsnobody 6 Apr 17 11:53 /data/
3、重启并检查检查服务
[root@nfs01 172.16.1.31]# exportfs -r或
[root@nfs01 172.16.1.31]# systemctl reload nfs.service
[root@nfs01 172.16.1.31]# systemctl status nfs.service
● nfs-server.service - NFS server and services
Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
Active: active (exited) since Wed 2019-04-17 11:56:12 CST; 1min 2s ago
Process: 9740 ExecStopPost=/usr/sbin/exportfs -f (code=exited, status=0/SUCCESS)
Process: 9736 ExecStopPost=/usr/sbin/exportfs -au (code=exited, status=0/SUCCESS)
Process: 9735 ExecStop=/usr/sbin/rpc.nfsd 0 (code=exited, status=0/SUCCESS)
Process: 9834 ExecReload=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
Process: 9766 ExecStartPost=/bin/sh -c if systemctl -q is-active gssproxy; then systemctl restart gssproxy ; fi (code=exited, status=0/SUCCESS)
Process: 9750 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
Process: 9748 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=1/FAILURE)
Main PID: 9750 (code=exited, status=0/SUCCESS)
Apr 17 11:56:12 nfs01 systemd[1]: Starting NFS server and services...
Apr 17 11:56:12 nfs01 exportfs[9748]: exportfs: Failed to stat /date: No such file or directory
Apr 17 11:56:12 nfs01 systemd[1]: Started NFS server and services.
Apr 17 11:56:22 nfs01 systemd[1]: Reloading NFS server and services.
Apr 17 11:56:22 nfs01 exportfs[9789]: exportfs: Failed to stat /date: No such file or directory
Apr 17 11:56:22 nfs01 systemd[1]: Reloaded NFS server and services.
Apr 17 11:57:12 nfs01 systemd[1]: Reloading NFS server and services.
Apr 17 11:57:12 nfs01 systemd[1]: Reloaded NFS server and services.
4、检查并挂载
[root@nfs01 172.16.1.31]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24
[root@nfs01 172.16.1.31]# mount -t nfs 172.16.1.31:/data /mnt
[root@nfs01 172.16.1.31]# touch /mnt/oldboy.test123
客户端:
0、安装rpcbind和nfs-utils.x86_64
yum install -y nfs-utils.x86_64 rpcbind
systemctl start nfs-utils.service
systemctl enable nfs-utils.service
systemctl start rpcbind
systemctl enable rpcbind
检查端口注册
rpcinfo -p 127.0.0.1
1、启动并检查检查服务
[root@web01 172.16.1.31]# exportfs -r或
[root@web01 172.16.1.31]# systemctl reload nfs.service
[root@web01 172.16.1.31]# systemctl status nfs.service
● nfs-server.service - NFS server and services
Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
Active: active (exited) since Wed 2019-04-17 11:56:12 CST; 1min 2s ago
Process: 9740 ExecStopPost=/usr/sbin/exportfs -f (code=exited, status=0/SUCCESS)
Process: 9736 ExecStopPost=/usr/sbin/exportfs -au (code=exited, status=0/SUCCESS)
Process: 9735 ExecStop=/usr/sbin/rpc.nfsd 0 (code=exited, status=0/SUCCESS)
Process: 9834 ExecReload=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
Process: 9766 ExecStartPost=/bin/sh -c if systemctl -q is-active gssproxy; then systemctl restart gssproxy ; fi (code=exited, status=0/SUCCESS)
Process: 9750 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
Process: 9748 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=1/FAILURE)
Main PID: 9750 (code=exited, status=0/SUCCESS)
Apr 17 11:56:12 nfs01 systemd[1]: Starting NFS server and services...
Apr 17 11:56:12 nfs01 exportfs[9748]: exportfs: Failed to stat /date: No such file or directory
Apr 17 11:56:12 nfs01 systemd[1]: Started NFS server and services.
Apr 17 11:56:22 nfs01 systemd[1]: Reloading NFS server and services.
Apr 17 11:56:22 nfs01 exportfs[9789]: exportfs: Failed to stat /date: No such file or directory
Apr 17 11:56:22 nfs01 systemd[1]: Reloaded NFS server and services.
Apr 17 11:57:12 nfs01 systemd[1]: Reloading NFS server and services.
Apr 17 11:57:12 nfs01 systemd[1]: Reloaded NFS server and services.
2、检查并挂载
[root@web01 172.16.1.31]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24
[root@web01 172.16.1.31]# mount -t nfs 172.16.1.31:/data /mnt
[root@web01 172.16.1.31]# touch /mnt/oldboy.test123
[root@web01 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 19911680 2575988 17335692 13% /
devtmpfs 229008 0 229008 0% /dev
tmpfs 239908 0 239908 0% /dev/shm
tmpfs 239908 5664 234244 3% /run
tmpfs 239908 0 239908 0% /sys/fs/cgroup
/dev/sda1 258724 120952 137772 47% /boot
tmpfs 47984 0 47984 0% /run/user/0
172.16.1.31:/data 19911680 2602688 17308992 14% /mnt