04.@nfs基础实战

1|0NFS

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

网络存储:glusterfs、ceph、OSS。

分布式:将多台机器,组成像一台机器一样使用。

微服务:将一台机器,拆分成多台。

1|1配置测试

# 准备两台机器 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

1|2NFS实际体验

  • 服务端配置
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,anonuid=1000,anongid=1000) 5、启动NFS [root@nfs ~]# systemctl start nfs-server rpcbind 6、查看 [root@nfs ~]# showmount -e #动服务检查共享目录状态 Export list for nfs: /mnt/data 172.16.1.0/16 [root@nfs ~]# cat /var/lib/nfs/etab #使用文件查看共享状态 /mnt/data 172.16.1.0/16(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)
  • 客户端配置
1、关闭防火墙 [root@nfs ~]# systemctl disable --now firewalld 2、关闭seLinux [root@nfs ~]# sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config [root@web01 html]# setenforce 0 setenforce: SELinux is disabled 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 5、使用浏览器进行查看共享nfs状态

1|3参数详解

rw 读写权限 ro 只读权限 all_squash 当NFS客户端以任意⽤户访问时,修改权限为NFS服务器的匿名⽤户(常⽤) anonuid 配合all_squash,指定匿名⽤户的uid,⽤户为系统⽤户,必须存在 anongid 配合all_squash,指定匿名⽤户的gid sync 同时将数据写⼊内存和磁盘(保证数据不丢失) async 优先将数据写⼊内存,再写⼊硬盘;效率⾼,但是会丢失数据 root_squash 当NFS客户端以root访问时,修改权限为NFS服务器的匿名⽤户(不常⽤) no_root_squash 当NFS客户端以root访问时,修改权限为NFS服务器的root⽤户(⼏乎不⽤) no_all_squash 当NFS客户端以任意⽤户访问时,不修改权限(不常⽤)

1|4开机自动挂载设置

# 开机自启动脚本(局限性:) echo "mount -t nfs 172.16.1.31:/mnt/data /usr/share/nginx/html" >> /etc/rc.local # 修改/etc/fstab 172.16.1.31:/mnt/data /usr/share/nginx/html nfs defaults 0 0 # 测试 mount -a

1|5案例

1、web服务器访问的页面能够实时的备份到backup服务器 2、web服务器的数据是要共享 # web1和web2 nginx /usr/share/nginx/html # nfs 配置一个挂载点 建立一个监控 # backup 备份仓库

1|0Backup

# backup服务端 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 = hzl secrets file = /etc/rsync.passwd log file = /var/log/rsyncd.log ##################################### [backup] comment = "备份" path = /backup 5、创建用户 [root@backup ~]# useradd rsync 6、设置密码文件 [root@backup ~]# echo "hzl:1" > /etc/rsync.passwd [root@backup ~]# chmod 600 /etc/rsync.passwd 7、启动 [root@backup ~]# systemctl start rsyncd 8、建立仓库 [root@backup ~]# mkdir /backup [root@backup ~]# chown rsync.rsync /backup/ # 客户端 1、创建密码文件 [root@nfs ~]# vim /etc/rsync.passwd 1 [root@nfs ~]# chmod 600 /etc/rsync.passwd 2、测试链接 [root@nfs ~]# rsync -avz ./* hzl@backup::backup --password-file=/etc/rsync.passwd 3、安装监控软件 [root@nfs ~]# yum install -y inotify-tools 4、监控软件配合rsync /usr/bin/inotifywait -mrq --format '%w %f' -e create,delete,attrib,close_write /data | while read line;do cd /data && rsync -avz --delete ./* wzh@172.16.1.41::backup --password-file=/etc/rsync.passwd done

1|0NFS服务端

  • 服务端
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、编写nfs配置文件 [root@nfs data]# vim /etc/exports /data 172.16.1.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000) 5、启动服务 [root@nfs data]# systemctl restart nfs-server rpcbind 6、查看挂载点 [root@nfs data]# showmount -e Export list for nfs: /data 172.16.1.0/24 /mnt/data 172.16.1.0/24 7、根据配置文件进行检查挂载点 [root@nfs data]# cat /var/lib/nfs/etab /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=1000,anongid=1000,sec=sys,rw,secure,root_squash,all_squash) /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=1000,anongid=1000,sec=sys,rw,secure,root_squash,all_squash)
  • 客户端
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、挂载 mount -t nfs 172.16.1.31:/data /usr/share/nginx/html/

1|0web端

1、配置nginx的yum安装源 [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 [root@web1 ~]# mount -t nfs 172.16.1.31:/data /usr/share/nginx/html/

在这里插入图片描述

1|0backup端

1、配置nginx的yum安装源 [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 [root@web1 ~]# mount -t nfs 172.16.1.31:/data /usr/share/nginx/html/

在这里插入图片描述


__EOF__

本文作者ଲ小何才露煎煎饺
本文链接https://www.cnblogs.com/zeny/p/15121581.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   ଲ小何才露煎煎饺  阅读(30)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
点击右上角即可分享
微信分享提示