文件服务-NFS
NFS介绍
简介
-
NFS(Network File System)网络文件系统,主要用于Linux/Unix上实现文件共享的一种协议
-
NFS的客户端主要为Linux
-
支持多节点同时挂载以及并发写入
-
为集群中的web server配置后端存储(做静态资源服务器)
-
没有用户认证机制,且数据在网络上传送的时候是明文传送,一般只能在局域网中使用
RPC远程过程调度协议
介绍
-
RPC:Remote Procedure Call Protocol
-
NFS协议本身并没有网络传输功能,而是基于远程过程调用协议实现的
-
提供一个面向过程的远程服务的接口
-
可以通过网络从远程主机程序上请求服务,而不需要了解底层网络技术的协议
-
工作在OSI模型的会话层,它可以为遵从RPC协议应用层协议提供端口注册功能
-
很多服务(NFS和NIS等)都可以向RPC注册端口
-
RPC使用网络端口111来监听客户端的请求
RPC协议模型
-
服务(此处是指nfs服务,在别处有可能是代表其他服务)在启动时向portmapper注册端口
-
客户端联系服务端portmapper询问服务的端口号
-
portmapper告知客户端nfs服务端口号
-
客户端访问该端口
-
nfs服务响应客户端的请求
NFS配置文件
rpcbind : 负责NFS的数据传输
nfs-utils :控制共享哪些文件,权限管理
[root@nas ~]# yum install rpcbind.x86_64 nfs-utils.x86_64
[root@nas ~]# vim /etc/exports 配置文件
共享目录 共享选项
/share *(ro,sync) #所有主机
/public 192.168.0.0/24(rw,async)
/public 192.168.0.0/24(rw) 192.168.1.0/24(ro)
/share 192.168.123.254(ro,sync)
NFS配置参数
选项 | 说明 |
---|---|
ro | 只读 |
rw | 读写 |
sync | 实时同步,直接写入磁盘 |
async | 异步,先缓存数据在内存然后再周期性同步入磁盘 |
anonuid | 指定匿名用户的uid,uid需要在/etc/passwd中存在 |
anongid | 指定匿名用户的gid |
all_squash | 所有用户,访问nfs共享目录,权限都被压缩到等同匿名用户 |
root_squash | root,访问nfs共享目录,权限被压缩到等同匿名用户 |
no_root_squash | 保持root用户不被压缩 |
基于NFS搭建web后端NAS存储
主机名 | ip地址 |
---|---|
web1 | 192.168.226.10 |
web2 | 192.168.226.20 |
nas | 192.168.226.30 |
backup | 192.168.226.40 |
环境准备
[root@server1 ~]# hostnamectl set-hostname web01
[root@server1 ~]# su
[root@server2 ~]# hostnamectl set-hostname web2
[root@server2 ~]# su
[root@server3 ~]# hostnamectl set-hostname nas
[root@server3 ~]# su
[root@server4 ~]# hostnamectl set-hostname backup
[root@server4 ~]# su
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl status firewalld.service
setenforce 0
sed -i 's/enforced/disabled/' /etc/selinux/config
getenforce
搭建web
[root@web1 ~]# yum install -y httpd
[root@web1 ~]# systemctl start httpd.service
[root@web2 ~]# yum install -y httpd
[root@web2 ~]# systemctl start httpd.service
安装
[root@nas ~]# yum install rpcbind.x86_64 nfs-utils.x86_64 -y
创建共享目录,准备文件
[root@nas ~]# mkdir /share
[root@nas ~]# mv Mac_vs._PC_vs._Linux南方公園.mp4 /share
[root@nas ~]# ls /share
Mac_vs._PC_vs._Linux南方公園.mp4
修改配置文件
[root@nas ~]# vim /etc/exports
/share 192.168.226.0/24(rw,async)
启动服务
[root@nas ~]# systemctl start rpcbind
[root@nas ~]# systemctl start nfs
[root@nas ~]# ss -tnalp |grep 111
LISTEN 0 128 *:111 *:* users:(("rpcbind",pid=11878,fd=8))
LISTEN 0 128 [::]:111 [::]:* users:(("rpcbind",pid=11878,fd=11))
创建用于挂载的目录
[root@web1 ~]# mkdir /var/www/html/media
[root@web2 ~]# mkdir /var/www/html/media
#一旦目录被挂载后,只读!!!
在Web服务器中挂载后端nas存储
[root@web1 ~]# mount -t nfs 192.168.226.30:/share /var/www/html/media/
mount: 文件系统类型错误、选项错误、192.168.226.30:/share 上有坏超级块、
缺少代码页或助手程序,或其他错误
(对某些文件系统(如 nfs、cifs) 您可能需要
一款 /sbin/mount.<类型> 助手程序)
有些情况下在 syslog 中可以找到一些有用信息- 请尝试
dmesg | tail 这样的命令看看。
报错:mount: 文件系统类型错误、选项错误、192.168.226.30:/share 上有坏超级块、
缺少代码页或助手程序,或其他错误 (对某些文件系统(如 nfs、cifs) 您可能需要
一款 /sbin/mount.<类型> 助手程序)原因:没有安装nfs客户端工具
解决:yum install rpcbind.x86_64 nfs-utils.x86_64 -y
[root@web1 ~]# yum install rpcbind.x86_64 nfs-utils.x86_64 -y
[root@web1 ~]# mount -t nfs 192.168.226.30:/share /var/www/html/media/
[root@web1 ~]# df -Th
文件系统 类型 容量 已用 可用 已用% 挂载点
devtmpfs devtmpfs 899M 0 899M 0% /dev
tmpfs tmpfs 910M 0 910M 0% /dev/shm
tmpfs tmpfs 910M 9.6M 901M 2% /run
tmpfs tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 17G 1.9G 16G 11% /
/dev/sda1 xfs 1014M 194M 821M 20% /boot
tmpfs tmpfs 182M 0 182M 0% /run/user/0
192.168.226.30:/share nfs4 17G 1.9G 16G 11% /var/www/html
[root@web1 ~]# ls /var/www/html/
Mac_vs._PC_vs._Linux南方公園.mp4
[root@web2 ~]# yum install rpcbind.x86_64 nfs-utils.x86_64 -y
[root@web2 ~]# mount -t nfs 192.168.226.30:/share /var/www/html/
[root@web2 ~]# df -Th
文件系统 类型 容量 已用 可用 已用% 挂载点
devtmpfs devtmpfs 899M 0 899M 0% /dev
tmpfs tmpfs 910M 0 910M 0% /dev/shm
tmpfs tmpfs 910M 9.6M 901M 2% /run
tmpfs tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 17G 1.9G 16G 11% /
/dev/sda1 xfs 1014M 194M 821M 20% /boot
tmpfs tmpfs 182M 0 182M 0% /run/user/0
192.168.226.30:/share nfs4 17G 1.9G 16G 11% /var/www/html
[root@web2 ~]# ls /var/www/html
Mac_vs._PC_vs._Linux南方公園.mp4
在HTML网页中调用文件
[root@web1 ~]# cd /var/www/html/
[root@web1 html]# vim index.html
<video width="800" height="450" controls>
<source src="media/Mac_vs._PC_vs._Linux南方公園.mp4">
</video>
[root@web2 ~]# cd /var/www/html/
[root@web2 html]# vim index.html
<video width="800" height="450" controls>
<source src="media/Mac_vs._PC_vs._Linux南方公園.mp4">
</video>
实时备份(rsync部分详细介绍)
[root@nas ~]# yum install -y lrzsz
[root@nas ~]# rz
#传入监控软件
[root@nas ~]# ls
anaconda-ks.cfg inotify-tools-3.14.tar.gz
[root@nas ~]# tar -xzf inotify-tools-3.14.tar.gz
[root@nas ~]# cd inotify-tools-3.14/
[root@nas inotify-tools-3.14]# yum install -y make gcc gcc-c++
[root@nas inotify-tools-3.14]# ./configure && make && make install
[root@backup ~]# mkdir /backup
[root@nas ~]# yum install rsync.x86_64 -y
[root@nas ~]# systemctl start rsyncd
#backup端也要启动rsync服务
[root@backup ~]# yum install -y rsync.x86_64
[root@backup ~]# systemctl start rsyncd
[root@nas ~]# vim rsync.sh
#!/bin/bash
/usr/local/bin/inotifywait -mrq -e modify,delete,create,attrib,move /share |while read events
do
/usr/bin/rsync -av --delete /share/ 192.168.226.40:/backup/
echo "`date +%F\ %T`出现事件$events" >> /var/log/rsync.log 2>&1
done
[root@nas ~]# chmod +x rsync.sh
[root@nas ~]# ssh-keygen
[root@nas ~]# ssh-copy-id root@192.168.226.40
[root@nas ~]# nohup ./rsync.sh &
[1] 14668
[root@nas ~]# nohup: 忽略输入并把输出追加到"nohup.out"
#回车
测试
[root@nas ~]# touch /share/photos.jpg
[root@backup ~]# ls /backup/
Mac_vs._PC_vs._Linux南方公園.mp4 photos.jpg