利用空餘時間,做個nfs共享實驗,豈不美滋滋!!!
系統測試環境:
主機ip 系統 主機名(服務)
192.168.241.130 centos6.5 hadoop1(nfs-srever)
192.168.241.133 centos6.5 hadoop2(nfs-client1)
192.168.241.134 centos6.5 hadoop2(nfs-client2)
爲了測試,三臺服務器全部關了軟件防火牆,在生產環境中需要做防火牆規則,我會在在後面對這塊做詳細說明。
服務器端(hadoop1上):
[root@hadoop1 ~]# yum -y install nfs-utils rpcbind
nfs的相關說明:
NFS的常用目錄如下
/etc/exports NFS服務的主要配置文件
/usr/sbin/exportfs NFS服務的管理命令
/usr/sbin/showmount 客戶端的查看命令
/var/lib/nfs/etab 記錄NFS分享出來的目錄的完整權限設定值
/var/lib/nfs/xtab 記錄曾經登錄過的客戶端信息
編輯nfs配置文件
NFS服務的配置文件爲 /etc/exports,這個文件是NFS的主要配置文件,不過系統並沒有默認值,所以這個文件不一定會存在,可能要使用vim手動建立,然後在文件裏面寫入配置內容。
vim /etc/exports
/test *(rw,sync,no_root_squash,insecure)
注意:我開始寫的是 /test 192.168.241.130(rw,sync,no_root_squash,insecure),但是這樣客戶端(hadoop2和hadoop3上)掛載事就會出現這種情況
[root@hadoop2 /]# mount -t nfs 192.168.241.130:/test /nfsclient
mount.nfs: access denied by server while mounting 192.168.241.130:/test
後來上網找了解決方式就是按照我上面的方式才行,真是無語。
解釋說明
/nfs/server 192.168.0.0/255.255.255.0(rw,no_root_squash)
1、每行分兩部分,由空格分開。
2、第一部份(空格之前的那段)是你要共享出去的目錄。
3、第二部份(空格之後的那段)又可以分三部分:
(1)、“192.168.0.0”表示有權共享本目錄的IP網段。
(2)、“255.255.255.0”是那個有權共享的網段所用的掩碼
(3)、括號裏面又有兩部分,用逗號隔開:
(i)、“rw”表示來訪者對所共享出去的目錄享有讀和寫的權力,如果只給讀不給寫就將之換成“ro” (意思是 Read Only)
(ii)、"no_root_squash"表示如果來訪者是該機的 root 則在本機也給予 root 待遇,如果沒有"no_root_squash"(以及前面的逗號),那來訪者在本機只相當於本機的 nobody 用戶。
(iii)、insecure:允許客戶端從大於1024的tcp/ip端口連接服務器
啓動服務端的服務
[root@hadoop1 test]# /etc/init.d/rpcbind start
[root@hadoop1 test]# /etc/init.d/nfslock start
[root@hadoop1 test]# /etc/init.d/nfs start
注意啓動順序,一定是上面的順序,否則會出錯
設置開機自啓動
[root@hadoop1 test]# chkconfig rpcbind on
[root@hadoop1 test]# chkconfig nfslock on
[root@hadoop1 test]# chkconfig nfs on
檢查服務是否開啓
[root@hadoop1 test]# ps -ef |grep nfs
root 4987 2 0 17:57 ? 00:00:00 [nfsd4]
root 4988 2 0 17:57 ? 00:00:00 [nfsd4_callbacks]
root 4989 2 0 17:57 ? 00:00:00 [nfsd]
root 4990 2 0 17:57 ? 00:00:00 [nfsd]
root 4991 2 0 17:57 ? 00:00:00 [nfsd]
root 4992 2 0 17:57 ? 00:00:00 [nfsd]
root 4993 2 0 17:57 ? 00:00:00 [nfsd]
root 4994 2 0 17:57 ? 00:00:00 [nfsd]
root 4995 2 0 17:57 ? 00:00:00 [nfsd]
root 4996 2 0 17:57 ? 00:00:00 [nfsd]
root 5126 4011 0 18:04 pts/0 00:00:00 grep nfs
[root@hadoop1 test]# ps -ef |grep rpcbind
rpc 3674 1 0 17:16 ? 00:00:00 rpcbind
root 5128 4011 0 18:04 pts/0 00:00:00 grep rpcbind
[root@hadoop1 test]# netstat -tanpl |grep rpc
tcp 0 0 0.0.0.0:875 0.0.0.0:* LISTEN 4977/rpc.rquotad
tcp 0 0 0.0.0.0:48590 0.0.0.0:* LISTEN 4981/rpc.mountd
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 3674/rpcbind
tcp 0 0 0.0.0.0:50960 0.0.0.0:* LISTEN 4981/rpc.mountd
tcp 0 0 0.0.0.0:59572 0.0.0.0:* LISTEN 3692/rpc.statd
tcp 0 0 0.0.0.0:50461 0.0.0.0:* LISTEN 4981/rpc.mountd
tcp 0 0 :::36045 :::* LISTEN 4981/rpc.mountd
tcp 0 0 :::111 :::* LISTEN 3674/rpcbind
tcp 0 0 :::49659 :::* LISTEN 4981/rpc.mountd
tcp 0 0 :::60635 :::* LISTEN 3692/rpc.statd
tcp 0 0 :::45215 :::* LISTEN 4981/rpc.mountd
hadoop2和hadoop3客戶端操作,以hadoop2爲例說明
[root@hadoop2 ~]# yum -y install nfs-utils
在本機使用命令可以看到共享目錄,在客戶端安裝了nfs-utils軟件後,使用同樣的命令也可以看到。
[root@hadoop1 test]# showmount -e 192.168.241.130
Export list for 192.168.241.130:
/test *
在hadoop2上創建掛載目錄,並將本地目錄(nfsclient)掛載到nfs共享目錄(test)上去
mkdir /nfsclient
[root@hadoop2 /]# mount -t nfs 192.168.241.130:/test /nfsclient
此時在各自的掛載目錄上做一系列測試證明nfs共享是否成功
在nfs-server上,也就是hadoop1上做以下操作
[root@hadoop1 test]# mkdir data lala haha
[root@hadoop1 test]# vim page.test
this is test page !!!
那麼在nfsclient1的/nfsclient目錄下看看有沒有這些內容
[root@hadoop2 nfsclient]# cat page.test
this is test page !!!
[root@hadoop2 nfsclient]# pwd
/nfsclient
顯然,這是成功的,但是這樣還不全面的,我們從客戶端再做一些測試
我們將這下面的東西全部刪掉
[root@hadoop2 nfsclient]# ls
data haha lala page.test
[root@hadoop2 nfsclient]# rm -rf ./*
[root@hadoop2 nfsclient]# ls
[root@hadoop2 nfsclient]# pwd
/nfsclient
看下nfsserver(hadoop1)的/test目錄下還有沒有,看下服務端hadoop1,顯然沒有了。
[root@hadoop1 test]# ls
[root@hadoop1 test]# pwd
/test
那麼再測試下客戶端寫入文件,在服務端能否看到和刪除。
[root@hadoop2 nfsclient]# pwd
/nfsclient
[root@hadoop2 nfsclient]# mkdir zhangsan lisi wangwu
[root@hadoop2 nfsclient]# vim nongyao.txt
laalalalalalalalala
看下服務端
[root@hadoop1 test]# pwd
/test
[root@hadoop1 test]# cat nongyao.txt
laalalalalalalalala
顯然服務端是有的,這就形成了數據共享,不用測試,hadoop3也是一樣的,這樣一個簡單的nfs共享服務器就做成了。