[toc]前端
什麼是文件系統,NFS文件系統又是什麼? 簡單的說,文件系統就是經過軟件對磁盤上的數據進行組織和管理的一種機制,對其的一種封裝或透視。linux
NFS,Network File System。顧名思義,網絡文件系統,即經過網絡,對在不一樣主機上的文件進行共享。web
NFS最先由Sun公司開發,分2,3,4三個版本,2和3由Sun起草開發,4.0開始Netapp公司參與並主導開發,最新爲4.1版本,更新比較慢其實4.1是從2010年就開始使用了。vim
NFS數據傳輸基於RPC協議,RPC爲Remote Procedure Call的簡寫。服務器
NFS應用場景是:A,B,C三臺機器上須要保證被訪問到的文件是同樣的,A共享數據出來,B和C分別去掛載A共享的數據目錄,從而B和C訪問到的數據和A上的一致
網絡
爲何須要使用NFS呢? 在生產環境,咱們不多使用單機部署應用,由於單機存在單點故障,一旦宕機,將沒法爲前端業務提供服務,這是客戶沒法容忍的。如今提倡的高可用及7*24服務,是咱們追求的目標。 對於一些小的應用,雙機成爲了主流。咱們來看一種簡單集羣場景, 應用分別部署在A及B上, 前端經過F5或着web服務器訪問應用。以下圖:架構
那麼當客戶端發出一個請求時,F5根據必定的機制進行轉發,有可能有A服務器進行響應,也有可能 由B服務器進行響應。 而在web應用中上傳一些靜態文件是很常見的一種功能,如圖片。假設用戶在某一時間,上傳了一張照片到A服務器上,那麼下次訪問時,被F5路由到了B服務器上,因爲B服務器上並無存儲上傳的照片,因此將形成用戶沒法看到本身上傳的照片。併發
那麼很容易想到,是否是能夠把文件上傳到一個公用的服務器上呢? 這樣無論訪問的是A仍是B,讀、取文件都只存在一份。答案是確定的,這個公用的服務器咱們也稱之爲文件服務器。上面的架構就演變成了下面的架構:
app
注:此處固然也能夠經過sftp或ftp進行文件上傳和下載。dom
首先:NFS包括兩部分,服務端及客戶端
因爲NFS服務功能不少,會有不少端口,這些端口還有可能不固定,那麼客戶端就沒法與服務器進行通訊,由於程序間通訊必須經過端口(tcp/udp都是端到端通訊),那麼就須要一箇中間的橋接機制,RPC進程即充當這樣一個角色,RPC的端口是必定的(111),當NFS啓動時,會向RPC進行註冊, 那麼客戶端PRC就能與服務器RPC進行通訊, 從而進行文件的傳輸。
當客戶端用戶打開一個文件或目錄時,內核會判斷,該文件是本地文件仍是遠程共享目錄文件,若是是遠程文件則經過RPC進程訪問遠程NFS服務端的共享目錄,若是是本地文件,則直接打開。 爲了更好的併發,RPC進程及NFS進程都有多個。
# yum install -y nfs-utils //yum工具安裝nfs-utils時會一併安裝recbind
如下以epel爲例
[root@xavi ~]# cd /etc/yum.repos.d/ [root@xavi yum.repos.d]# ls CentOS7-Base-163.repo CentOS-fasttrack.repo CentOS-Vault.repo CentOS-CR.repo CentOS-Media.repo epel.repo CentOS-Debuginfo.repo CentOS-Sources.repo epel-testing.repo [root@xavi yum.repos.d]# mv epel.repo epel.repo.1
[root@xavi ~]# vim /etc/exports /home/nfstestdir 192.168.72.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
[root@xavi ~]# chmod 777 /home/nfstestdir/
[root@xavi ~]# netstat -lntp //111端口說明rpcbind已啓動 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 192.168.122.1:53 0.0.0.0:* LISTEN 1866/dnsmasq tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1050/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1048/cupsd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1693/master tcp6 0 0 :::111 :::* LISTEN 1/systemd tcp6 0 0 :::22 :::* LISTEN 1050/sshd tcp6 0 0 ::1:631 :::* LISTEN 1048/cupsd tcp6 0 0 ::1:25 :::* LISTEN 1693/master [root@xavi ~]# ps aux |grep rpc //rpcbind服務已啓動 root 385 0.0 0.0 0 0 ? S< 10:00 0:00 [rpciod] rpcuser 2657 0.0 0.0 44464 1824 ? Ss 10:11 0:00 /usr/sbin/rpc.statd rpc 2659 0.0 0.0 64964 1348 ? Ss 10:11 0:00 /sbin/rpcbind -w root 2660 0.0 0.0 42564 940 ? Ss 10:11 0:00 /usr/sbin/rpc.mountd root 2661 0.0 0.0 43812 536 ? Ss 10:11 0:00 /usr/sbin/rpc.idmapd root 2700 0.0 0.0 112684 976 pts/0 S+ 10:12 0:00 grep --color=auto rpc
[root@xavi ~]# systemctl start nfs [root@xavi ~]# ps aux |grep nfs root 2672 0.0 0.0 0 0 ? S< 10:11 0:00 [nfsd4_callbacks] root 2678 0.0 0.0 0 0 ? S 10:11 0:00 [nfsd] root 2679 0.0 0.0 0 0 ? S 10:11 0:00 [nfsd] root 2680 0.0 0.0 0 0 ? S 10:11 0:00 [nfsd] root 2681 0.0 0.0 0 0 ? S 10:11 0:00 [nfsd] root 2682 0.0 0.0 0 0 ? S 10:11 0:00 [nfsd] root 2683 0.0 0.0 0 0 ? S 10:11 0:00 [nfsd] root 2684 0.0 0.0 0 0 ? S 10:11 0:00 [nfsd] root 2685 0.0 0.0 0 0 ? S 10:11 0:00 [nfsd] root 2690 0.0 0.0 112680 976 pts/0 S+ 10:11 0:00 grep --color=auto nfs [root@xavi ~]# ps aux |grep rpc root 385 0.0 0.0 0 0 ? S< 10:00 0:00 [rpciod] rpcuser 2657 0.0 0.0 44464 1824 ? Ss 10:11 0:00 /usr/sbin/rpc.statd rpc 2659 0.0 0.0 64964 1348 ? Ss 10:11 0:00 /sbin/rpcbind -w root 2660 0.0 0.0 42564 940 ? Ss 10:11 0:00 /usr/sbin/rpc.mountd root 2661 0.0 0.0 43812 536 ? Ss 10:11 0:00 /usr/sbin/rpc.idmapd root 2700 0.0 0.0 112684 976 pts/0 S+ 10:12 0:00 grep --color=auto rpc
[root@xavi ~]# 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-utils包
使用showmount -e IP能夠查看NFS的共享狀況
[root@xavi-002 ~]# showmount -e 192.168.72.130 Export list for 192.168.72.130: /home/nfstestdir 192.168.241.0/24 //共享目錄爲/home/nfstestdir,信任主機爲192.168.241.0/24網段
[root@xavi ~]# setenforce 0 setenforce: SELinux is disabled [root@xavi-002 ~]# getenforce Disabled
[root@xavi-002 ~]# mount -t nfs 192.168.72.130:/home/nfstestdir /mnt/ [root@xavi-002 ~]# df -h 文件系統 容量 已用 可用 已用% 掛載點 /dev/sda3 17G 3.6G 13G 23% / devtmpfs 479M 0 479M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 7.1M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda1 197M 109M 88M 56% /boot tmpfs 98M 0 98M 0% /run/user/0 192.168.72.130:/home/nfstestdir 17G 10G 6.2G 63% /mnt
[root@xavi-002 ~]# cd /mnt/ [root@xavi-002 mnt]# ls [root@xavi-002 mnt]# touch xavilinux.123 [root@xavi-002 mnt]# ls -l 總用量 0 -rw-r--r-- 1 xavi xavi 0 3月 27 10:55 xavilinux.123 [root@xavi-002 mnt]# id xavi uid=1000(xavi) gid=1000(xavi) 組=1000(xavi),10(wheel)
[root@xavi ~]# ls -l /home/nfstestdir/ 總用量 0 -rw-r--r-- 1 xavi xavi 0 3月 27 10:55 xavilinux.123 [root@xavi ~]# id xavi uid=1000(xavi) gid=1000(xavi) 組=1000(xavi),1004(grptest1)
能夠看到建立的新文件xavilinux.txt全部者和所屬組爲xavi,其uid和gid都爲1000.
假如A的NFS服務停掉了,可是B和C還在掛載着A分享的分區,這就沒法正常工做了,因爲B和C上面的進程是d狀態(不可中斷進程,在ps一節中學習到的),一旦A中止了,B和C必定會出問題,沒法服務,既不能夠重啓,也不能夠關閉,就會把系統整掛掉。因此NFS必定不能夠隨便重啓! 若是必須重啓怎麼辦?先把B和C上面的目錄卸載下來!
[root@xavi ~]# umount /mnt
如上這種方法只適合掛載了少許的機器,若是掛載的機器不少怎麼辦呢?
經常使用選項:
在服務器端操做:
# exportfs -avr exporting 192.168.72.0/24:/home/nfssharedir
[root@xavi ~]# vi /etc/exports /home/nfstestdir 192.168.72.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000) /tmp 192.168.72.133(rw,sync,no_root_squash)
[root@xavi ~]# exportfs -avr exporting 192.168.72.133:/tmp exporting 192.168.72.0/24:/home/nfstestdir
[root@xavi-002 ~]# showmount -e 192.168.72.130 Export list for 192.168.72.130: /home/nfstestdir 192.168.72.0/24 /tmp 192.168.72.133
[root@xavi-002 ~]# mount -t nfs 192.168.72.130:/tmp /tmp/xavi/ [root@xavi-002 ~]# df -h 文件系統 容量 已用 可用 已用% 掛載點 /dev/sda3 17G 3.6G 13G 23% / devtmpfs 479M 0 479M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 7.1M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda1 197M 109M 88M 56% /boot tmpfs 98M 0 98M 0% /run/user/0 192.168.72.130:/tmp 17G 10G 6.2G 63% /tmp/xavi
[root@xavi-002 ~]# vim /tmp/xavi/1212.txt [root@xavi-002 ~]# ls -l /tmp/xavi/1212.txt -rw-r--r-- 1 root root 22 3月 29 00:05 /tmp/xavi/1212.txt
NFS 4版本會有該問題
咱們在分享一個目錄且權限設置爲no_root_squash,客戶端看到的文件屬主、組不是root,倒是nobody!這個不是設置錯誤,是軟件自己自帶的BUG。
[root@xavi-002 ~]# mount -t nfs -o,nfsvers=3 192.168.72.130:/tmp /tmp/xavi/
[root@xavi-002 ~]# vim /etc/idmapd.conf [General] #Verbosity = 0 # The following should be set to the local NFSv4 domain name # The default is the host's DNS domain name. #Domain = local.domain.edu # The following is a comma-separated list of Kerberos realm # names that should be considered to be equivalent to the "/etc/idmapd.conf" 114L, 3580C