<返回更多

NFS服务

2021-05-18  givenchy_yzl  
加入收藏

什么是NFS?

NFS本质就是一个共享存储,文件服务器。

一、NFS基本概述

NFS是Network File System的缩写即网络文件系统。NFS主要功能是通过局域网络让不同的主机系统之间可以共享文件或目录。

NFS系统和windows网络共享、网络驱动器类似, 只不过windows用于局域网, NFS用于企业集群架构中, 如果是大型网站, 会用到更复杂的分布式文件系统glusterfs(大文件ISO镜像等),ceph,oss(阿里云的存储系统)

为什么使用NFS

1.实现多台服务器之间数据共享

2.实现多台服务器之间数据一致

二、NFS应用

1)没有NFS时:

1.A用户上传图片经过负载均衡,负载均衡将上传请求调度至WEB1服务器上。

2.B用户访问A用户上传的图片,此时B用户被负载均衡调度至WEB2上,因为WEB2上没有这张图片,所以B用户无法看到A用户传的图片。

 

知识点---NFS服务

 

 

2)如果有NFS

1.A用户上传图片无论被负载均衡调度至WEB1还是WEB2, 最终数据都被写入至共享存储

2.B用户访问A用户上传图片时,无论调度至WEB1还是WEB2,最终都会上共享存储访问对应的文件,这样就可以访问到资源了

知识点---NFS服务

 

3)NFS工作原理
 

知识点---NFS服务

 

1.用户访问NFS客户端,将请求转化为函数

2.NFS通过tcp/ip连接服务器

3.NFS服务端接收请求,会先调用portmap进程进行端口映射

4.Rpc.nfsd进程用于判断NFS客户端能否连接到服务端

5.Rpc.mount进程用于判断客户端对服务端的操作权限

6.如果通过权限验证,可以对服务端进行操作,修改或读取等

三、NFS实践

1、环境准备:

主机 ip 角色

web01 172.16.1.7 NFS客户端

NFS 172.16.1.31 NFS服务端

2、服务端(172.16.1.31)

1)关闭防火墙与selinux

2)安装NFS和rpcbind

[root@nfs ~]# yum install -y nfs-utils rpcbind

注意:

centos6需要安装rpcbind

centos7默认已经安装好了rpcbind,并且默认开机自启动

3)配置nfs

#nfs的默认配置文件是:/etc/exports
#配置nfs配置文件
vim /etc/exports
/data 172.16.1.0/24(rw sync all_squash)

语法拆分:

/data #nfs服务端的共享目录

172.16.1.0/24 nfs允许连接的客户端ip

(rw sync all_squash) 允许操作的权限

4)创建共享目录

[root@nfs ~]# mkdir /data

5)启动服务

#centos7启动

[root@nfs ~]# systemctl start rpcbind nfs

#centos6启动时一定要先启动rpcbind,在启动nfs

[root@nfs ~]# /etc/init.d/rpcbind start

[root@nfs ~]# /etc/init.d/nfs start

[root@nfs ~]# service rpcbind start

[root@nfs ~]# service nfs start

#验证启动

[root@nfs ~]# netstat -ntlp | grep rpc

tcp 0 0 0.0.0.0:36234 0.0.0.0:* LISTEN 2041/rpc.statd

tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 2030/rpcbind

tcp 0 0 0.0.0.0:20048 0.0.0.0:* LISTEN 2067/rpc.mountd

tcp6 0 0 :::46149 :: LISTEN 2041/rpc.statd

tcp6 0 0 :::111 :: LISTEN 2030/rpcbind

tcp6 0 0 :::20048 :: LISTEN 2067/rpc.mountd

6)验证nfs配置

[root@nfs ~]# cat /var/lib/nfs/etab

/data172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,all_squash)

#出现以上内容即为配置成功

3、客户端

1)关闭防火墙与selinux

2)安装rpcbind服务

[root@web01 ~]# yum install -y rpcbind nfs-utils

#注意:

centos6需要安装rpcbind

centos7默认已经安装好了rpcbind,并且默认开机自启动

安装rpcbind是为了连接服务端的进程,安装nfs-utils是为了使用showmount命令

3)查看挂载点

[root@web01 ~]# showmount -e 172.16.1.31

Export list for 172.16.1.31:

/data 172.16.1.0/24

4)挂载

[root@web01 ~]# mount -t nfs

172.16.1.31:/data /backup

命令拆分:

mount #挂载命令

-t #指定挂载的文件类型

nfs #nfs文件类型

172.16.1.31: #服务端的IP地址

/data 服务端提供的可供挂载的目录

/backup 本地要挂载到服务端的目录

[root@web01 ~]# df -h #查看挂载结果

Filesystem Size Used Avail Use% Mounted on

devtmpfs 979M 0 979M 0% /dev

tmpfs 991M 0 991M 0% /dev/shm

tmpfs 991M 9.5M 981M 1% /run

tmpfs 991M 0 991M 0% /sys/fs/cgroup

/dev/mApper/centos-root 18G 3.1G 15G 17% /

/dev/sda1 1014M 163M 852M 17% /boot

tmpfs 199M 0 199M 0% /run/user/0

172.16.1.31:/data 18G 3.1G 15G 18% /backup

5.写入数据进行测试

#若没有授权,没有客户端权限写入

[root@web01 ~]# touch /backup/123.txt

touch: cannot touch ‘/backup/123.txt’: Permission denied

#服务端授权

[root@nfs ~]# chown -R nfsnobody.nfsnobody /data

#客户端再次写入即可

[root@web01 ~]# touch /backup/123.txt

#服务端查看,若文件相同同步完成

[root@nfs ~]# ls /data

123.txt

四、nfs挂载与卸载

NFS客户端的配置步骤也十分简单。先使用showmount命令,查询NFS服务器的远程共享信息,其输出格式为“共享的目录名称 允许使用客户端地址

NFS挂载:客户端的目录仅仅是服务端共享目录的一个入口,真正的数据全都是在服务端的目录,客户顿写入的数据也是在服务器存储的

注意事项:

1.挂载目录后,原文件里的内容并没有消失,只是被遮盖(像藏起来了)取消挂载后仍然存在

2.取消挂载时,不要在挂载目录下操作目录本身,否则会提示操作忙碌,切换到其他目录在进行卸载即可

3.挂载时如果在挂载目录下,还是可以看到挂载前的目录下文件

,需要重新进入目录才会显示挂在后的目录内容

开机挂载(此处一定要仔细,初学者慎用)

如果希望NFS文件共享服务能一直有效,则需要将其写入到客户端fstab文件中

#编辑fstab文件

[root@web01 ~]# vim /etc/fstab

172.16.1.31:/data /nfsdir nfs defaults 0 0

#验证fstab是否写正确

[root@web01 ~]# mount -a

1

2

3

4

5

6

卸载:

#卸载的两种方式

[root@web01 ~]# umount /backup

[root@web01 ~]# umount 172.16.1.31:/data

#强制卸载(一般不建议使用,特定场景下使用)

[root@web01 ~]# umount -lf /backup

nfs配置详解

[root@nfs ~]# cat /etc/exports

/data 172.16.1.0/24(rw,sync,all_squash)

知识点---NFS服务

 

五、NFS案例

1、backup

#安装rsync
[root@backup ~]# yum install rsync -y
[root@backup ~]# yum install -y nfs-utils rpcbind

#编写rsync服务端配置文件
[root@backup ~]# vim /etc/rsyncd.conf
uid = rsync
gid = rsync 
port = 873 
use chroot = no
fake super = yes
max connections = 200
timeout = 600
ignore errors     
read only = false
list = false      
auth users = yzl
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
[backup]
comment = welcome to oldboyedu backup!
path = /backup
#创建rsync服务需要使用的普通用户
[root@backup ~]# useradd rsync
#将用户名和密码写入rsync密码文件
[root@backup ~]# echo "yzl:1" >/etc/rsync.passwd
#将服务端密码文件权限设置为600
[root@backup ~]# chmod 600 !$
#创建模块目录,并将其属组、属主更改为普通用户
[root@backup ~]# mkdir /backup
#授权
[root@backup ~]# chown -R rsync.rsync !$


[root@backup ~]# vim /etc/exports
/backup 172.16.1.0/24(rw,sync,all_squash)
[root@backup ~]# systemctl start nfs
#启动rsync服务端
[root@backup ~]# systemctl start rsyncd

2、nfs


#创建rsync客户端密码文件为其设置600权限
[root@nfs ~]# echo "1" >>/etc/rsync.passwd
[root@nfs ~]# chmod 600 !$

#安装nfs、rpcbind工具
[root@nfs ~]# yum install -y nfs-utils rpcbind

#编辑nfs配置文件
[root@nfs ~]# vim /etc/exports
/sersync 172.16.1.0/24(rw,sync,all_squash)

#创建可供挂载的nfs目录
[root@nfs ~]# mkdir /sersync
[root@nfs ~]# chown -R nfsnobody.nfsnobody /sersync
[root@nfs ~]# systemctl start nfs

#上传sersync压缩包
[root@nfs sersync]# rz -E
rz waiting to receive.
[root@nfs sersync]# tar -xf sersync.gz 
[root@nfs sersync]# mv GNU-Linux-x86/* ./



[root@nfs GNU-Linux-x86]# vim confxml.xml 
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" port="8008"></host>
    <debug start="false"/>
    <fileSystem xfs="false"/>
    <filter start="false">
	<exclude expression="(.*).svn"></exclude>
	<exclude expression="(.*).gz"></exclude>
	<exclude expression="^info/*"></exclude>
	<exclude expression="^static/*"></exclude>
    </filter>
    <inotify>
	<delete start="true"/>
	<createFolder start="true"/>
	<createFile start="true"/>
	<closeWrite start="true"/>
	<moveFrom start="true"/>
	<moveTo start="true"/>
	<attrib start="true"/>
	<modify start="true"/>
    </inotify>

    <sersync>
	<localpath watch="/backup">
	    <remote ip="172.16.1.41" name="backup"/>
	    <!--<remote ip="192.168.8.39" name="tongbu"/>-->
	    <!--<remote ip="192.168.8.40" name="tongbu"/>-->
	</localpath>
	<rsync>
	    <commonParams params="-az"/>
	    <auth start="true" users="yzl" passwordfile="/etc/rsync.passwd"/>
	    <userDefinedPort start="false" port="873"/><!-- port=874 -->
	    <timeout start="false" time="100"/><!-- timeout=100 -->
	    <ssh start="false"/>
	</rsync>
	<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
	<crontab start="false" schedule="600"><!--600mins-->
	    <crontabfilter start="false">
		<exclude expression="*.php"></exclude>
		<exclude expression="info/*"></exclude>
	    </crontabfilter>
	</crontab>
	<plugin start="false" name="command"/>
    </sersync>

    <plugin name="command">
	<param prefix="/bin/sh" suffix="" ignoreError="true"/>	<!--prefix /opt/tongbu/mmm.sh suffix-->
	<filter start="false">
	    <include expression="(.*).php"/>
	    <include expression="(.*).sh"/>
	</filter>
    </plugin>

    <plugin name="socket">
	<localpath watch="/opt/tongbu">
	    <deshost ip="192.168.138.20" port="8009"/>
	</localpath>
    </plugin>
    <plugin name="refreshCDN">
	<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
	    <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
	    <sendurl base="http://pic.xoyo.com/cms"/>
	    <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
	</localpath>
    </plugin>
</head>
[root@nfs sersync]# ./sersync2 -dro confxml.xml 

web1和web2


[root@web01 ~]#  yum install -y rpcbind nfs-utils
[root@web01 ~]#  yum install -y httpd php
[root@web01 ~]#  systemctl start httpd
[root@web01 ~]#  cd /var/www/html
[root@web01 ~]# mount -t nfs 172.16.1.31:/sersync /var/www/html
[root@web01 html]# rz -E
rz waiting to receive.
[root@web01 html]# unzip kaoshi.zip 

[root@web01 html]# systemctl restart httpd
网站测试并访问

[root@web01 ~]# vim ping.sh
#!/bin/bash

ping -c1 172.16.1.31 >/dev/null 

if [ $? -ne 0 ];then
        umount -lf /var/www/html && mount -t nfs 172.16.1.41:/backup /va
r/www/html/
else

        df -h | grep 172.16.1.31 >/dev/null

        if [ $? -ne 0 ];then
                umount -lf /var/www/html && mount -t nfs 172.16.1.31:/ser
sync /var/www/html     
        fi
fi
[root@web01 ~]# chmod +x ping.sh 
[root@web01 ~]# crontab -e
* * * * * /var/www/html/ping.sh

NFS小结

1.NFS存储优点

1)NFS文件系统简单易用、方便部署、数据可靠、服务稳定、满足中小企业需求。

2)NFS文件系统内存放的数据都在文件系统之上,所有数据都是能看得见。

2.NFS存储局限

1)存在单点故障, 如果构建高可用维护麻烦web -> nfs -> backup

2)NFS数据明文, 并不对数据做任何校验。

3)客户端挂载NFS服务没有密码验证, 安全性一般(内网使用)

3.NFS应用建议

1)生产场景应将静态数据尽可能往前端推, 减少后端存储压力

2)必须将存储里的静态资源通过CDN缓存jpgpngmp4avicssjs

3)如果没有缓存或架构本身历史遗留问题太大, 在多存储也无用

摘录来源:givenchy_yzl博主

声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>