NFS介紹與基本安裝

1NFS介紹

NFS是Network File System的縮寫(分佈式文件系統協議),==它最大的功能就是可以透過網絡,讓不同的機器、不同的操作系統、可以彼此分享個別的檔案 (share files)==
NFS最早由Sun公司開發,分2,3,4三個版本,2和3由Sun起草開發,4.0開始Netapp公司參與並主導開發,最新爲4.1版本
NFS數據傳輸基於RPC協議,RPC爲Remote Procedure Call的簡寫。

《客戶端NFS和服務端NFS通訊過程》
1)首先服務器端啓動RPC服務,並開啓111端口
2)啓動NFS服務,並向RPC註冊端口信息
3)客戶端啓動RPC(portmap服務),向服務端的RPC(portmap)服務請求服務端的NFS端口
4)服務端的RPC(portmap)服務反饋NFS端口信息給客戶端。
5)客戶端通過獲取的NFS端口來建立和服務端的NFS連接並進行數據的傳輸。
mark
(注:rpcbind是centos6及以後稱之爲rpcbind,portmap是centos5及以前的版本)

場景1:有3臺機器A,B,C,需求是這3臺機器的數據是一樣的,當A的機器負載高時客戶想要訪問A服務器上的數據時,這時有2臺機器B,C的負載不是很高,將A與B,C的數據共享,可是怎麼樣能時實的更新B,與C的機器上呢。
此時用NFS服務即可實現,時實的更新的數據。(NFS不監聽端口,portmap監聽111端口)

2nfs服務端安裝配置

服務端的配置:

[root@chy01 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.212.11  netmask 255.255.255.0  broadcast 192.168.212.255
        inet6 fe80::243b:ddac:7a2b:a5b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:13:b3:3b  txqueuelen 1000  (Ethernet)
        RX packets 5269  bytes 2572740 (2.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1167  bytes 157323 (153.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
(服務端的ip地址)
[root@chy01 ~]# yum install -y nfs-utils rpcbind
[root@chy01 ~]# vim /etc/exports
(nfs的配置文件)
/home/nfstestdir 192.168.212.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
(/home/nfstestdir 需要共享的目錄,這個目錄後續需要創建,192.168.212.0/24(給192.168.212.0這個網絡段共享目錄。(rw,sync,all_squash,anonuid=1000,anongid=1000)如此是
[root@chy01 ~]# mkdir /home/nfstestdir
[root@chy01 ~]# chmod 777 /home/nfstestdir
(創建目錄並且給777的權限)
[root@chy01 ~]# 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      1/systemd           
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1275/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2240/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      1744/mysqld         
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      1275/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2240/master       
(可以查看到rpc服務已經啓動)
[root@chy01 ~]# ps aux |grep nfs
root       3502  0.0  0.0 112664   976 pts/0    S+   07:06   0:00 grep --color=auto nfs
(雖然rpc已經啓動,但是nfs服務並沒有啓動)
[root@chy01 ~]# systemctl start nfs
(手動啓動nfs)
[root@chy01 ~]# ps aux |grep nfs
root       3550  0.0  0.0      0     0 ?        S<   07:07   0:00 [nfsd4_callbacks]
root       3556  0.0  0.0      0     0 ?        S    07:07   0:00 [nfsd]
root       3557  0.0  0.0      0     0 ?        S    07:07   0:00 [nfsd]
root       3558  0.0  0.0      0     0 ?        S    07:07   0:00 [nfsd]
root       3559  0.0  0.0      0     0 ?        S    07:07   0:00 [nfsd]
root       3560  0.0  0.0      0     0 ?        S    07:07   0:00 [nfsd]
root       3561  0.0  0.0      0     0 ?        S    07:07   0:00 [nfsd]
root       3562  0.0  0.0      0     0 ?        S    07:07   0:00 [nfsd]
root       3563  0.0  0.0      0     0 ?        S    07:07   0:00 [nfsd]
root       3568  0.0  0.0 112664   976 pts/0    S+   07:07   0:00 grep --color=auto nfs
(查看nfs服務)
[root@chy01 ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
(nfs開機啓動)

客戶端簡單配置:

[root@chy ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.212.10  netmask 255.255.255.0  broadcast 192.168.212.255
        inet6 fe80::338e:589c:fa07:65e5  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:70:77:62  txqueuelen 1000  (Ethernet)
        RX packets 8880  bytes 8055224 (7.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1205  bytes 139708 (136.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
(客戶端的IP地址)
[root@chy ~]# yum install -y nfs-utils rpcbind
(同樣需要安裝nfs-utils包)

3nfs配置選項

nfs配置選項

[root@chy01 ~]# cat /etc/exports
/home/nfstestdir 192.168.212.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
rw 讀寫
 ro 只讀
 sync 同步模式,內存數據實時寫入磁盤
 async 非同步模式
 no_root_squash 客戶端掛載NFS共享目錄後,root用戶不受約束,權限很大
 root_squash 與上面選項相對,客戶端上的root用戶收到約束,被限定成某個普通用戶
 all_squash 客戶端上所有用戶在使用NFS共享目錄時都被限定爲一個普通用戶
 anonuid/anongid 和上面幾個選項搭配使用,定義被限定用戶的uid和gid

掛載客戶端

在此就不需要在安裝nfs,因爲之前就有安裝nfs安裝包(需要關閉防火牆)
[root@chy ~]# showmount -e 192.168.212.11
Export list for 192.168.212.11:
/home/nfstestdir 192.168.212.0/24
(可以看到共享的目錄/home/nfstestdir)
[root@chy ~]# mount -t nfs 192.168.212.11:/home/nfstestdir /mnt/ (掛載時需要加-t
[root@chy ~]# df -h 
文件系統                         容量  已用  可用 已用% 掛載點
/dev/mapper/cl-root              8.8G  5.4G  2.9G   66% /
devtmpfs                         737M     0  737M    0% /dev
tmpfs                            748M     0  748M    0% /dev/shm
tmpfs                            748M  8.6M  739M    2% /run
tmpfs                            748M     0  748M    0% /sys/fs/cgroup
/dev/sda1                        190M  107M   70M   61% /boot
/dev/mapper/cl-var               8.4G  281M  7.7G    4% /var
/dev/mapper/cl-home              497M   26M  472M    6% /home
tmpfs                            150M     0  150M    0% /run/user/0
192.168.212.11:/home/nfstestdir  497M   26M  472M    6% /mnt
[root@chy mnt]# touch chy.111
[root@chy mnt]# ls -l 
總用量 0
-rw-r--r-- 1 mysql mysql 0 8月  24 08:06 chy.111
(在客戶端創建一個chy.111)
[root@chy mnt]# id mysql
uid=1000(mysql) gid=1000(mysql) 組=1000(mysql)

在服務端測試

[root@chy01 ~]# ls -l /home/nfstestdir/
總用量 0
-rw-r--r-- 1 mysql mysql 0 8月  24 08:06 chy.111
[root@chy01 ~]# id mysql
uid=1000(mysql) gid=1000(mysql) 組=1000(mysql)

也許有個疑問?爲什麼授權用戶和組是mysql呢?

[root@chy01 ~]# cat /etc/exports
/home/nfstestdir 192.168.212.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
我們看到,我們設置的權限爲anonuid=1000,anongid=1000,那麼1000就是mysql
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章