文章目录
NFS共享存储(用于内网)
什么是NFS
NFS是Network File System的缩写,中文意思是网络文件共享系统
它的主要功能是通过网络(一般是局域网)让不同的主机系统之间可以共享文件或目录,只支持Linux与Linux之间数据传输。
共享存储,⽂件服务器
为什么使用NFS
1.为了实现多台服务器之间数据共享
2.实现多台服务器之间数据一致
常见的数据存储方式
网络存储:glusterfs(大文件)、ceph、OSS
分布式:将多台机器,组成像一台机器一样使用
微服务:将一台机器,拆分成多台机器使用
NFS的作用
如果没有NFS会有什么影响?

测试:nginx页面展示
下载nginx
# 准备两台机器
web1 192.168.15.7
web2 192.168.15.8
# 两台机器分别添加nginx.repo : http://nginx.org/en/linux_packages.html#RHEL-CentOS
[root@web1 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@web1 ~]# yum clean all
[root@web1 ~]# yum makecache
[root@web1 ~]# yum install nginx -y
[root@web1 ~]# systemctl start nginx
编辑nginx网页页面内容
cd /usr/share/nginx/html/ nginx网页页面显示指定的目录文件位置/usr/share/nginx/html/index.html
echo 111 > index.html
我们在网页输入web01的网址192.168.15.7,页面是可以显示我们输入的内容111的。
我们只有再次对web02服务器操作命令:echo 111 > /usr/share/nginx/html/index.html才可以实现两台服务器的数据同步。
那怎样才能让web01和web02数据同步显示呢
存储服务器的作用

NFS原理

1.用户进程访问NFS客户端,使用不同的函数对数据进行处理
2.NFS客户端通过TCP/IP连接服务端;
3.NFS服务端接受请求后,会先调用portmap进程进行端口映射;
4.Rpc.nfsd进程用于判断NFS客户端能否连接服务端;
5.Rpc.mount进程用于判断客户端可以对服务端进行哪些操作;
6.最后如果允许操作,客户端可以对服务端磁盘进行修改。
#注意:rpcbind是一个远程调用,使用NFS时必须有rpcbind
- nfs服务端配置
1、关闭防火墙
[root@nfs ~]# systemctl disable --now firewalld
2、关闭seLinux
[root@nfs ~]# sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
[root@nfs ~]# setenforce 0
3、安装nfs
[root@nfs ~]# yum install -y nfs-utils rpcbind
4、修改配置文件
[root@nfs ~]# vim /etc/exports
[nfs存储目录(挂载点)] [监听的IP](权限)
/data 172.16.1.0/24(rw,sync,all_squash)
5、启动NFS
[root@nfs ~]# systemctl start nfs-server
6、查看
[root@nfs ~]# showmount -e
Export list for nfs:
/mnt/data 172.16.1.0/24
[root@nfs ~]# cat /var/lib/nfs/etab
/mnt/data 172.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)
- nfs客户端配置
1、关闭防火墙
[root@nfs ~]# systemctl disable --now firewalld
2、关闭seLinux
[root@nfs ~]# sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
[root@nfs ~]# setenforce 0
查看挂载点
[root@nfs ~]# showmount -e
Export list for nfs:
/mnt/data 172.16.1.0/16
3、挂载
[root@nfs ~]# mount -t nfs 172.16.1.31:/mnt/data /usr/share/nginx/html
4、查看挂载详情
[root@web1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
172.16.1.31:/mnt/data 99G 2.7G 97G 3% /usr/share/nginx/html
NFS配置详解
| NFS共享参数 | 参数作用 |
|---|---|
| rw | 读写权限 |
| ro | 只读权限 |
| all_squash | 当NFS客户端以任意用户访问时,修改权限为NFS服务器的匿名用户(常用 |
| anongid | 配合all_squash,指定匿名用户的gid |
| anonuid | 配合all_squash,指定匿名用户的uid,用户为系统用户,必须存在 |
| sync | 同时将数据写入内存和磁盘(保证数据不丢失) |
开机自动挂载
# 修改/etc/fstab
172.16.1.31:/mnt/data /usr/share/nginx/html nfs defaults 0 0
============================================================================
案例(nginx上传)
1、web服务器访问的页面能够实时的备份到backup服务器
2、web服务器的数据是要共享

backup
用于备份nfs服务器中的/data目录,是rsync的服务端
1、#关闭防火墙
[root@nfs ~]# systemctl disable --now firewalld
2、#关闭seLinux
[root@nfs ~]# sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
[root@nfs ~]# setenforce 0
3、#安装rsync
[root@backup ~]# yum install rsync -y
4、#编写rsync的服务端配置文件
[root@backup ~]# vim /etc/rsyncd.conf
uid = rsync
gid = rsync
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = yeg
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
[backup]
comment = welcome to my backup!
path = /backup
[bak]
comment = "hello world"
path = /bak
5、#添加用户 不能登录 没有家目录
[root@backup ~]# useradd rsync -s /sbin/nologin -M
6、#创建密码文件并授权
[root@backup ~]# vim /etc/rsync.passwd
yeg:1
[root@backup ~]# chmod -R 600 /etc/rsync.passwd
7、#创建备份目录并授权
[root@backup ~]# mkdir /bak
[root@backup ~]# chown -R rsync.rsync /bak
8、#启动服务并端口验证
[root@backup ~]# systemctl start rsyncd
[root@backup ~]# netstat -nutlp
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 6986/rsync tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 6075/rpcbind tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6651/sshd
nfs
用于存放共享的文件,通过inotify+rsync推送给backup,是rsync的服务端
1、#关闭防火墙
[root@nfs ~]# systemctl disable --now firewalld
2、#关闭seLinux
[root@nfs ~]# sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
[root@nfs ~]# setenforce 0
3、#安装nfs rpcbind
[root@nfs data]# yum install nfs-utils rpcbind -y
4、#编辑密码文件
[root@nfs ~]# vim /etc/profile.d/YEG.sh
export RSYNC_PASSWORD=1
5、#重启客户端让文件生效
[root@nfs ~]# source /etc/profile.d/YEG.sh
6、#创建/data目录并统一www用户
[root@nfs ~]# mkdir /data
[root@nfs ~]# groupadd www -g 888
[root@nfs ~]# useradd www -u 888 -g 888
[root@nfs ~]# chown -R www.www /data
7、#编写inotify监控脚本
[root@nfs ~]# cat inotify.sh
#!/bin/bash
#实时推送备份文件脚本
export RSYNC_PASSWORD=1
dir=/data
#监控的文件
inotifywait -mrq --format '%Xe %w %f' -e create,modify,delete,attrib,close_write ${dir} | while read line; do rsync -avz /data yeg@192.168.15.41::bak; done &>/dev/null &
#测试
#nfs端
[root@nfs ~]# bash inotify.sh
[root@nfs ~]# cd /data/
[root@nfs data]# touch q.txt
#backup端
[root@backup /]# cd /bak/
[root@backup bak]# ll
total 0
drwxr-xr-x 2 rsync rsync 19 Apr 21 20:58 data
[root@backup bak]# cd data/
[root@backup data]# ll
total 0
-rw-r--r-- 1 rsync rsync 0 Apr 21 20:58 q.txt
==========================rsync配置完毕,可以同步。以下是NFS当做服务端========================
编写配置文件
[root@nfs ~]# vim /etc/exports
/data 192.168.15.0/24(rw,sync,all_squash,anonuid=888,anongid=888)
重启后检查配置
[root@nfs data]# systemctl restart nfs-server rpcbind
[root@nfs ~]# cat /var/lib/nfs/etab
/data 192.168.15.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=888,anongid=888,sec=sys,rw,secure,root_squash,all_squash)
web01,web02
服务器,是NFS的客户端,两个操作一样
[root@web01 ~]# cd /etc/yum.repos.d/
[root@web01 yum.repos.d]# vim nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@web01 ~]# yum clean all && yum makecache
#安装nginx和nfs rpcbind
yum install nginx nfs-utils rpcbind -y
#启动rpcbind和nfs
[root@web01 ~]# systemctl start rpcbind nfs
#查看挂载点
[root@web01 ~]# showmount -e
Export list for 192.168.15.31:
/data 192.168.15.0/24
#统一用户权限
[root@web01 ~]# groupadd www -g 888
[root@web01 ~]# useradd www -u 888 -g 888
[root@web01 ~]# chown -R www.www /usr/share/nginx/html
#挂载
[root@web01 ~]# mount -t nfs 192.168.15.31:/data /usr/share/nginx/html
#检查挂载
[root@web01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 99G 1.9G 98G 2% /
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.6M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 1014M 133M 882M 14% /boot
tmpfs 98M 0 98M 0% /run/user/0
192.168.15.31:/data 99G 2.1G 97G 3% /usr/share/nginx/html
#测试权限是否满足
#拉取代码到/usr/share/nginx/html下,并解压
[root@web01 ~]# cd /usr/share/nginx/html/
[root@web01 html]# rz
[root@web01 html]# unzip kaoshi\(1\).zip
Archive: kaoshi(1).zip
inflating: info.php
inflating: bg.jpg
inflating: index.html
inflating: upload_file.php
[root@web01 html]# ll
total 80
-rw-r--r-- 1 www www 38772 Apr 27 2018 bg.jpg
-rw-r--r-- 1 www www 2633 May 4 2018 index.html
-rw-r--r-- 1 www www 52 May 10 2018 info.php
-rw-r--r-- 1 www www 26995 Apr 21 16:59 kaoshi(1).zip
-rw-r--r-- 1 root root 0 Apr 21 20:58 q.txt
-rw-r--r-- 1 www www 1192 Jan 10 2020 upload_file.php
web02只需要重复上述步骤到挂载即可
访问并查看两个服务器

=================================================================================
pv uv QPS
pv : 页面浏览量 6千万
uv : 独立用户浏览量 2千
QPS :每秒查询率 10w - 20w
中型公司流量水平
(httpd协议上传)
基于以上web01和web02是通的
部署backup环境
[root@backup ~]# vim /etc/rsyncd.conf
[sersync]
comment="实时同步"
path=/sersync
[root@backup ~]# cat /etc/rsync.passwd
yeg:1
[root@backup ~]# mkdir /sersync
[root@backup ~]# groupadd www -g1000
[root@backup ~]# useradd www -u1000 -g1000
[root@backup ~]# id www
uid=1000(www) gid=1000(www) groups=1000(www)
部署nfs环境
[root@nfs ~]# vim /etc/exports/sersync 172.16.1.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
[root@nfs ~]# mkdir /sersync
[root@nfs ~]# groupadd www -g 1000
[root@nfs ~]# useradd www -g 1000 -u 1000
[root@nfs ~]# chown www.www /sersync/
[root@nfs ~]# systemctl restart nfs-server rpcbind
web01 和 web 2
#挂载
mount -t nfs 172.16.1.31:/sersync /root/web2/
mount -t nfs 172.16.1.31:/sersync /root/web1/
#注:挂载之前web01和web02下的文件内容,在挂载之后不会被覆盖,只有在卸载后才能看到
挂载后显示的内容是/sersync目录下的内容
上传案例–httpd
#安装web服务器(web01和web02两台机器上全需要执行)
[root@web1 ~]# yum install httpd php -y
[root@web1 ~]# systemctl start httpd
[root@web1 ~]# mount -t nfs 172.16.1.31:/sersync /var/www/html/
# 将kaoshi.zip解压至/var/www/html目录(只需要在一台服务器上执行)
[root@web1 ~]# mv kaoshi.zip /var/www/html/
[root@web1 ~]# cd /var/www/html/
[root@web1 html]# unzip kaoshi.zip
[root@web1 ~]# chown -R www.www /var/www/html/

出现以上界面上传视频(不能大于8M)web02上传web01查看验证
[root@web01 ~]# ll /var/www/html/upload
total 220
-rw-r--r-- 1 www www 222097 Apr 22 18:52 4_chenyang_test.mp4

区别
rsync : 但是无法实时传输
innotfy + rsync : 适用于文件比较少的情况
sersync + innotfy + rsync : 大批量文件同步
本文详细介绍了NFS(网络文件系统)的用途、工作原理及配置方法,包括NFS在多台服务器间实现数据共享、同步的意义,以及在Linux环境下的配置步骤。通过案例展示了如何使用NFS进行nginx页面展示、数据备份和同步,讨论了不同同步方案如rsync、inotify的适用场景,并提供了开机自动挂载NFS的配置。此外,还涉及了web服务器的访问统计指标pv、uv和QPS。
 httpd协议 nginx 两种上传方式 区别&spm=1001.2101.3001.5002&articleId=116041309&d=1&t=3&u=4f29f8e41db44b33933503670ac50ba6)
1305

被折叠的 条评论
为什么被折叠?



