Linux基础(day56)

14.1 NFS介绍

NFS介绍

  • NFS是Network File System的缩写;这个文件系统是基于网路层面,经过网络层面实现数据同步mysql

  • NFS最先由Sun公司开发,分2,3,4三个版本,2和3由Sun起草开发,4.0开始Netapp公司参与并主导开发,目前最新为4.1版本——>4.1版本是2010年出来还没更新过linux

  • NFS数据传输基于RPC协议,RPC为Remote Procedure Call的简写,意思为 远程过程调用sql

    • 例子:
      • 服务端和客户端通讯,A机器和B机器之间不能直接通讯,须要借助RPC协议来实现
  • NFS应用场景是:A,B,C三台机器上须要保证被访问到的文件是同样的,A共享数据出来,B和C分别去挂载A共享的数据目录,从而B和C访问到的数据和A上的一致vim

  • NFS架构centos

    输入图片说明

    • 例子:
      • 跑了一个网站,上面传输了不少图片,用户访问一个图片时,须要从A机器上去请求,但A机器负载高,为了分担负载,就多弄了两台机器,B机器C机器同时提供服务;正常的话,须要到A机器上才能拿到数据,可是B机器和C机器作了负载均衡,分担了相同的服务器,那么用户也有可能到B机器或者C机器上;那么用户请求到B机器上的时候,如何才能获取到A机器上的数据呢;要么把A机器的数据传输到B机器上,同时传输到C机器上,可是这个不能时时更新,(用户上传的数据是存放在A机器上,但用户请求的时候数据是请求到B机器上)这样A上的数据还没到B上面去,就会致使用户请求获取的数据访问不到,访问为空,为404;那么NFS服务就能够解决这个问题,将A机器的数据共享到B机器、C机器,经过NFS来实现。有NFS服务之后,上传到A机器上的数据,B机器或C机器上就能立刻看到和调用
    • 总结,NFS就是实时同步

NFS原理图

  • 服务端须要启动一个NFS服务,服务端要想给客户端提供服务,须要借助RPC协议,RPC协议是由rpcbind服务所实现的;在centos 5或者以前的版本叫portmap服务,centos6及以后的版本叫rpcbind服务,这两个都是一个服务,最终实现了RPC协议的通讯,NFS服务默认不会监放任何端口(启动服务,但不会监听端口),最终监听端口,实现RPC通讯的过程是由rpcbind服务产生的RPC协议实现的,RPC协议 默认监听 的 端口是111 端口;
  • 整个流程为:服务端的NFS服务监听一个端口经过RPC协议监听的端口,再去告诉客户端RPC协议,而后NFS客户端经过本机的RPC端口回传数据信息到服务端NFS监听的端口,最终实现通讯

输入图片说明

  • NFS服务须要借助RPC协议实现通讯

14.2 NFS服务端安装配置

NFS服务端安装配置目录概要

  • yum install -y nfs-utils rpcbind
  • vim /etc/exports //加入以下内容
/home/nfstestdir 192.168.133.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
  • 保存配置文件后,执行以下准备操做
  • mkdir /home/nfstestdir
  • chmod 777 /home/nfstestdir
  • systemctl start rpcbind
  • systemctl start nfs
  • systemctl enable rpcbind
  • systemctl enable nfs

NFS服务端安装配置

  1. 首先准备两台机器,我这里准备两台虚拟机,A机器IP分别为192.168.202.130,B机器IP为192.168.202.131
  • A机器,查看IP
[root@hanfeng ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.202.130  netmask 255.255.255.0  broadcast 192.168.202.255
        inet6 fe80::20c:29ff:feff:458f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ff:45:8f  txqueuelen 1000  (Ethernet)
        RX packets 684  bytes 68341 (66.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 226  bytes 26200 (25.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eno16777736:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.202.150  netmask 255.255.255.0  broadcast 192.168.202.255
        ether 00:0c:29:ff:45:8f  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 8  bytes 536 (536.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 536 (536.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@hanfeng ~]#
  • B机器,查看IP
[root@hf-01 ~]# ifconfig
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.202.131  netmask 255.255.255.0  broadcast 192.168.202.255
        inet6 fe80::baa:c015:db72:1d5c  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:b6:4d:41  txqueuelen 1000  (Ethernet)
        RX packets 63  bytes 7905 (7.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 79  bytes 11615 (11.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 68  bytes 5524 (5.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 68  bytes 5524 (5.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@hf-01 ~]#
  1. 将A机器做为服务端,并安装两个包,分别为 nfs-utils 和 rpcbind
  • yum install -y nfs-utils rpcbind
[root@hanfeng ~]# yum install -y nfs-utils rpcbind
  1. 将B机器做为客户端,安装 nfs-utils 包
  • 每次刚开机运行yum的时候都会很慢,由于须要从新生成一个缓存文件
  • 有时yum安装的时候很慢,咱们能够先禁掉,进入到/etc/yum.repos.d/目录下,将目录下的epel.repo文件改个名字,从新安装便可
  • yum install -y nfs-utils
[root@hf-01 ~]# yum install -y nfs-utils
  1. 在安装完成后,须要去A机器中 /etc/exports 编辑配置文件
  • vim /etc/exports //加入以下内容
[root@hanfeng ~]# vim /etc/exports

/home/nfstestdir     
要分享出去的目录是哪个目录,这个目录是不存在的,后期还须要建立的
192.168.202.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
指定你要给哪一个机器去分享这个目录,首先定义IP或IP段

保存退出
  1. 下面就能够启动服务了,启动服务前须要先建立目录,并设置权限
  2. 在A机器上建立分享的目录
[root@hanfeng ~]# mkdir /home/nfstestdir
[root@hanfeng ~]#
  1. 并设置成777的权限,这里设置777权限是为了方便接下里的实验
[root@hanfeng ~]# chmod 777 /home/nfstestdir
[root@hanfeng ~]#
  1. 启动 rpcbind ,再启动前查看A机器上监听的端口,就会看到启动了1/systemd,这是centos7系统的一个特性——>111端口是rpcbind服务监听的
  • 用ps能够查看服务已经启动,由于有这个服务,因此监听了111端口
[root@hanfeng ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2172/master         
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1115/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2172/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      2264/mysqld         
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      1115/sshd           
[root@hanfeng ~]# ps aux |grep rpc
rpc       2393  0.0  0.1  64964  1044 ?        Ss   15:40   0:00 /sbin/rpcbind -w
root      2537  0.0  0.0 112680   972 pts/0    R+   16:17   0:00 grep --color=auto rpc
  1. 再到B机器上查看下端口,会看到也启动了rpcbind
[root@hf-01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      2393/rpcbind        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1334/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2146/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      2029/mysqld         
tcp6       0      0 :::111                  :::*                    LISTEN      2393/rpcbind        
tcp6       0      0 :::22                   :::*                    LISTEN      1334/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2146/master         
[root@hf-01 ~]# ps aux |grep rpc
root     16644  0.0  0.0 112656   992 pts/2    R+   04:46   0:00 grep --color=auto rpc
[root@hf-01 ~]#
  1. 在A机器启动了rpcbind,下面就能够启动nfs服务,在使用ps查看
  • systemctl start nfs
[root@hanfeng ~]# systemctl start nfs 
[root@hanfeng ~]# ps aux |grep nfs
root      4560  0.0  0.0      0     0 ?        S<   04:52   0:00 [nfsd4]
root      4561  0.0  0.0      0     0 ?        S<   04:52   0:00 [nfsd4_callbacks]
root      4565  0.0  0.0      0     0 ?        S    04:52   0:00 [nfsd]
root      4566  0.0  0.0      0     0 ?        S    04:52   0:00 [nfsd]
root      4567  0.0  0.0      0     0 ?        S    04:52   0:00 [nfsd]
root      4568  0.0  0.0      0     0 ?        S    04:52   0:00 [nfsd]
root      4569  0.0  0.0      0     0 ?        S    04:52   0:00 [nfsd]
root      4570  0.0  0.0      0     0 ?        S    04:52   0:00 [nfsd]
root      4571  0.0  0.0      0     0 ?        S    04:52   0:00 [nfsd]
root      4572  0.0  0.0      0     0 ?        S    04:52   0:00 [nfsd]
root      4576  0.0  0.0 112676   984 pts/0    R+   04:52   0:00 grep --color=auto nfs
[root@hanfeng ~]#
  1. 同时也能够在A机器上ps aux |grep rpc查询,在启动nfs服务的时候,它会自动帮你启动rpc相关的一些服务
[root@hanfeng ~]# ps aux |grep rpc
rpc       2957  0.0  0.1  64908  1416 ?        Ss   03:05   0:00 /sbin/rpcbind -w
rpcuser   4530  0.0  0.1  42380  1744 ?        Ss   04:52   0:00 /usr/sbin/rpc.statd
root      4531  0.0  0.0      0     0 ?        S<   04:52   0:00 [rpciod]
root      4539  0.0  0.0  19324   652 ?        Ss   04:52   0:00 /usr/sbin/rpc.idmapd
root      4550  0.0  0.0  42556   948 ?        Ss   04:52   0:00 /usr/sbin/rpc.mountd
root      4602  0.0  0.0 112676   984 pts/0    R+   05:00   0:00 grep --color=auto rpc
[root@hanfeng ~]#
  1. 而在客户端B机器上是没有的这些服务的
[root@hf-01 ~]# ps aux |grep rpc
root     16644  0.0  0.0 112656   992 pts/2    R+   04:46   0:00 grep --color=auto rpc
[root@hf-01 ~]#
  1. 若是想让nfs开机启动,还须要执行systemctl enable nfs 命令,在服务端A机器上调用
  • systemctl enable nfs 开机启动nfs
  • systemctl disable nfs 关闭开机启动
[root@hanfeng ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@hanfeng ~]#

14.3 NFS配置选项

NFS配置选项

  • rw 读写
  • ro 只读
  • sync 同步模式,内存数据实时写入磁盘,相应的就会下降磁盘效率
  • async 非同步模式,它会每隔一段时间才会将数据刷新到磁盘
    • 优点:可以保证磁盘的效率
    • 劣势:万一断电,就会有可能丢失一部门数据
  • no_root_squash 客户端挂载NFS共享目录后,root用户不受约束,权限很大
    • NFS,要想在客户端上去使用服务端上共享的目录,须要去把它挂载到客户端上的一个挂载点,那就跟本地上的目录是同样的,在操做本地的目录时候,确定会有一些权限设置,若是加上no_root_squash,这样root用户去共享目录下读写文件的时候,就不会受到限制(就至关于root用户在本地上读写)
  • root_squash 与上面选项相对,客户端上的root用户收到约束,被限定成某个普通用户
  • all_squash 客户端上全部用户(包括root用户)在使用NFS共享目录时都被限定为一个普通用户
  • anonuid/anongid 和上面几个选项搭配使用,定义被限定用户的uid和gid

客户端上挂载

  1. 首先在客户端B机器上安装了rpcbind,它是由nfs-utils 这个包自动安装的
yum install -y nfs-utils
  1. 在B机器上安装完成后不须要启动任何服务
  2. 在B机器上执行 showmount -e 命令
  • showmount -e 192.168.74.129 //查看在NFS服务端是否有权限
    • 该ip为NFS服务端ip
[root@hf-02 ~]# showmount -e 192.168.74.129
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
[root@hf-02 ~]#
  1. 这时候会看到报错了,RPC: Port mapper failure说明网络不通,没法与192.168.74.129端口通讯
  • 不能通讯的两种状况
    • 要么是对方没有开启rpcbind的服务,没有监听111端口
    • 要么是防火墙致使的缘由
  1. 由于以前在服务端已经开启了rpcbind服务,那这时只能说明是防火墙致使的,因此要想让NFS实现正常的通讯,还须要把防火墙关闭,由于NFS服务比较特殊,虽然rpc能够通讯,就算iptables把111端口放行但NFS也不必定能正常通讯,由于它们使用了一个不固定的端口
  2. 这时先把A机器服务端和B机器客户端,防火墙关闭,命令systemctl stop firewalld
A机器关闭防火墙
[root@hf-01 ~]# systemctl stop firewalld
[root@hf-01 ~]# getenforce
Disabled
[root@hf-01 ~]# 

B机器关闭防火墙
[root@hf-02 ~]# systemctl stop firewalld
[root@hf-02 ~]# 
[root@hf-02 ~]# getenforce
Enforcing
[root@hf-02 ~]# setenforce 0
[root@hf-02 ~]# getenforce
Permissive
[root@hf-02 ~]#
  1. 这时再去B机器客户端来showmount -e 192.168.74.129 ,这时会看到能够能show到远程服务端192.168.74.129这台机器,它共享的目录是/home/nfstestdir
[root@hf-02 ~]# showmount -e 192.168.202.130
Export list for 192.168.74.129:
/home/nfstestdir 192.168.74.0/24
[root@hf-02 ~]#
  1. 这时候在B机器客户端上来挂载了,而后查看——>挂载的时间可能有点慢
[root@hf-02 ~]#  mount -t nfs 192.168.74.129:/home/nfstestdir /mnt/
[root@hf-02 ~]# df -h
文件系统                         容量  已用  可用 已用% 挂载点
/dev/sda3                         18G  873M   17G    5% /
devtmpfs                         489M     0  489M    0% /dev
tmpfs                            494M     0  494M    0% /dev/shm
tmpfs                            494M   13M  481M    3% /run
tmpfs                            494M     0  494M    0% /sys/fs/cgroup
/dev/sda1                        197M   75M  122M   39% /boot
192.168.74.129:/home/nfstestdir   18G  7.4G   11G   42% /mnt
[root@hf-02 ~]#
  1. 这个就是远程NFS服务端共享的目录
  2. 这时到B机器客户端共享的目录下建立文件
[root@hf-02 ~]# cd /mnt/
[root@hf-02 mnt]# ls
[root@hf-02 mnt]# touch aminglinux.111
[root@hf-02 mnt]# ls -l
总用量 0
-rw-r--r--. 1 1000 1000 0 8月  24 00:34 aminglinux.111
[root@hf-02 mnt]#
  1. 到A机器的服务端查看,能够看到文件的属主和属组都为1000
[root@hf-01 ~]# ls -l /home/nfstestdir/
总用量 0
-rw-r--r-- 1 mysql mysql 0 8月  24 00:34 aminglinux.111
[root@hf-01 ~]#
  • 缘由是配置 /etc/export 文件时,配置了anonuid=1000,anongid=1000
  • 客户端显示 1000 用户 是由于客户端机器上并无1000权限的用户
  • 一样,服务端显示mysql,是由于服务端上的机器mysql 就是1000权限的用户

总结

  • 在/etc/exports文件中的/home/nfstestdir 192.168.74.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000) ,其中ip与(rw之间不能有空格, 不然客户端挂载的目录会变成只读..
相关文章
相关标签/搜索