1. 程式人生 > >搭建nfs共享存儲服務之一nfs服務端搭建

搭建nfs共享存儲服務之一nfs服務端搭建

linux

NFS相當於房源,RPC相當於中介。

nfs-utils: NFS服務的主程序,包括rpc.nfsd、rpc.mountd這兩個daemon和相關文件說明,以及執行命令文件等。

rpcbind: centos6.x下面RPC的主程序。NFS可以視為一個RPC程序,在啟動任何一個RPC程序之前,需要做好端口和功能的對應映射工作,這個映射工作就是由rpcbind服務來完成的。因此,在提供NFS服務之前必須先啟動rpcbind服務才行。

1.查看NFS軟件包 :

可使用如下命令查看默認情況下centos6裏NFS軟件的安裝情況

[[email protected] ~]# rpm -qa nfs-utils rpcbind #這個不用管道的命令更有效率

[[email protected] ~]#

vi /etc/yum.conf中把keepcache=0設置為keepcache=1意思是yum安裝後不刪除安裝包。

當不知道軟件名字時候,可以用rpm -aq |grep -E "nfs-|rpcbind"來過濾包含在引號內的字符串。grep -E在這裏相當於egrep。grep、egrep這兩個命令在運維工作中非常常用並且很好用。

先安裝基礎軟件

[[email protected] ~]# yum install lrzsz nmap tree dos2unix nc -y

[[email protected]

~]#yum update或者yum upgrade兩個至少要執行一個,相當於打補丁。

用yum安裝nfs和rpc兩個程序

[[email protected] ~]# yum install nfs-utils rpcbind -y

安裝完成後可以看到rpm軟件包沒有被刪除

[[email protected] ~]# tree /var/cache/yum/x86_64/6/base/packages/
/var/cache/yum/x86_64/6/base/packages/
|-- dos2unix-3.1-37.el6.x86_64.rpm
|-- keyutils-1.4-5.el6.x86_64.rpm

|-- libevent-1.4.13-4.el6.x86_64.rpm
|-- libgssglue-0.1-11.el6.x86_64.rpm
|-- lrzsz-0.12.20-27.1.el6.x86_64.rpm
|-- nc-1.84-24.el6.x86_64.rpm
|-- nfs-utils-1.2.3-75.el6.x86_64.rpm
|-- nfs-utils-lib-1.1.5-13.el6.x86_64.rpm
|-- nmap-5.51-6.el6.x86_64.rpm
`-- tree-1.5.3-3.el6.x86_64.rpm

用tar zcvf打包把/var/cache/yum/x86_64/6/base/packages/目錄打包成nfs-rpm.tar.gz文件

[[email protected] ~]# tar zcvf nfs-rpm.tar.gz /var/cache/yum/x86_64/6/base/packages/
tar: Removing leading `/‘ from member names
/var/cache/yum/x86_64/6/base/packages/
/var/cache/yum/x86_64/6/base/packages/nfs-utils-lib-1.1.5-13.el6.x86_64.rpm
/var/cache/yum/x86_64/6/base/packages/keyutils-1.4-5.el6.x86_64.rpm
/var/cache/yum/x86_64/6/base/packages/nfs-utils-1.2.3-75.el6.x86_64.rpm
/var/cache/yum/x86_64/6/base/packages/nc-1.84-24.el6.x86_64.rpm
/var/cache/yum/x86_64/6/base/packages/libevent-1.4.13-4.el6.x86_64.rpm
/var/cache/yum/x86_64/6/base/packages/libgssglue-0.1-11.el6.x86_64.rpm
/var/cache/yum/x86_64/6/base/packages/lrzsz-0.12.20-27.1.el6.x86_64.rpm
/var/cache/yum/x86_64/6/base/packages/tree-1.5.3-3.el6.x86_64.rpm
/var/cache/yum/x86_64/6/base/packages/dos2unix-3.1-37.el6.x86_64.rpm
/var/cache/yum/x86_64/6/base/packages/nmap-5.51-6.el6.x86_64.rpm

/>rz
zmodem ° Ctrl+C
100% 3425 KB 3425 KB/s 00:00:01 0 Errors


2.啟動NFS相關服務

啟動rpcbind服務。

因為NFS及其輔助程序都是屬於RPC(Remote Procedure CAll)協議的(使用的端口111),所以首先要確保系統運行了rpcbind服務。

啟動rpc服務器:

[[email protected] ~]# /etc/init.d/rpcbind start
Starting rpcbind: [ OK ]

查看rpc服務對應的端口是111

[[email protected] ~]# netstat -lntup |grep rpc
tcp 0 0 0.0.0.0:111 &a 0.0.0.0:* LISTEN 2020/rpcbind
tcp 0 0 :::111 :::* LISTEN 2020/rpcbind
udp 0 0 0.0.0.0:111 0.0.0.0:* 2020/rpcbind
udp 0 0 0.0.0.0:923 0.0.0.0:* 2020/rpcbind
udp 0 0 :::111 :::* 2020/rpcbind
udp 0 0 :::923 :::* 2020/rpcbind
[[email protected] ~]#

查看是否有房源

[[email protected] ~]# rpcinfo -p localhost
program vers proto port service
100000 4 tcp 111 portmapper
100000 3 tcp 111 portmapper
100000 2 tcp 111 portmapper
100000 4 udp 111 portmapper
100000 3 udp 111 portmapper
100000 2 udp 111 portmappe

啟動nfs,相當於向rpc註冊房源

[[email protected] ~]# /etc/init.d/nfs start
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS mountd: [ OK ]
Starting NFS daemon: [ OK ]
Starting RPC idmapd: [ OK ]

再看中介有沒有房

[[email protected] ~]# rpcinfo -p localhost
program vers proto port service
100000 4 tcp 111 portmapper
100000 3 tcp 111 portmapper
100000 2 tcp 111 portmapper
100000 4 udp 111 portmapper
100000 3 udp 111 portmapper
100000 2 udp 111 portmapper
100011 1 udp 875 rquotad
100011 2 udp 875 rquotad
100011 1 tcp 875 rquotad
100011 2 tcp 875 rquotad
100005 1 udp 34914 mountd
100005 1 tcp 43493 mountd
100005 2 udp 50054 mountd
100005 2 tcp 48624 mountd
100005 3 udp 37236 mountd
100005 3 tcp 39378 mountd
100003 2 tcp 2049 nfs
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100227 2 tcp 2049 nfs_acl
100227 3 tcp 2049 nfs_acl
100003 2 udp 2049 nfs
100003 3 udp 2049 nfs
100003 4 udp 2049 nfs
100227 2 udp 2049 nfs_acl
100227 3 udp 2049 nfs_acl
100021 1 udp 45751 nlockmgr
100021 3 udp 45751 nlockmgr
100021 4 udp 45751 nlockmgr
100021 1 tcp 44021 nlockmgr
100021 3 tcp 44021 nlockmgr
100021 4 tcp 44021 nlockmgr
nfs主端口2049是不變的,rpc主端口是111

所有的進程都會對應一個用戶,yum安裝後就會自動創建

[[email protected] ~]# id nfsnobody
uid=65534(nfsnobody) gid=65534(nfsnobody) groups=65534(nfsnobody)

假設把rpc服務停了,就找不到nfs了。

[[email protected] ~]# /etc/init.d/rpcbind stop
Stopping rpcbind: [ OK ]
[[email protected] ~]# rpcinfo -p localhost
rpcinfo: can‘t contact portmapper: RPC: Remote system error - Connection refused

這時啟動nfs就會報錯

[[email protected] ~]# /etc/init.d/nfs restart
Shutting down NFS daemon: [ OK ]
Shutting down NFS mountd: [ OK ]
Shutting down NFS quotas: [ OK ]
Shutting down RPC idmapd: [ OK ]
Starting NFS services: [ OK ]
Starting NFS quotas: Cannot register service: RPC: Unable to receive; errno = Connection refused
rpc.rquotad: unable to register (RQUOTAPROG, RQUOTAVERS, udp).
[FAILED]
Starting NFS mountd: [FAILED]
Starting NFS daemon: rpc.nfsd: writing fd to kernel failed: errno 111 (Connection refused)
rpc.nfsd: unable to set any sockets for nfsd
[FAILED]

剛剛只是測試而已,還是要啟動rpcbind的

執行命令啟用rcpbind

/etc/init.d/rpcbind srart

啟動nfs服務

/etc/init.d/nfs srart

在到rpcbind查看是否有nfs

rpcinfo -p localhost

2.2設置開機啟動

[[email protected] ~]# chkconfig nfs on
[[email protected] ~]# chkconfig rpcbind on

查看啟動先後順序:grep -E

[[email protected] ~]# ls /etc/rc.d/rc3.d/|grep -E "nfs|rpc"
K61nfs-rdma
K69rpcsvcgssd
S13rpcbind
S14nfslock
S19rpcgssd
S30nfs

[[email protected] ~]# head /etc/init.d/nfs
#!/bin/sh
#
# nfs This shell script takes care of starting and stopping
# the NFS services.
#
# chkconfig: - 30 60
# description: NFS is a popular protocol for file sharing across networks.
# This service provides NFS server functionality, which is \
# configured via the /etc/exports file.
# probe: true

運行NFS服務默認需要啟動的服務或者進程至少有:NFS quotas(rpc.rquotad)、NFS daemon(nfsd)、NFS mountd(rpc.mountd)。可以通過執行如下命令查看啟動NFS後,系統中運行的NFS相關進程。

[[email protected] ~]# ps -ef|egrep "nfs|rpc"
root 2080 2 0 12:35 ? 00:00:00 [rpciod/0]
root 2081 2 0 12:35 ? 00:00:00 [rpciod/1]
rpc 2499 1 0 12:47 ? 00:00:00 rpcbind
root 2567 1 0 12:49 ? 00:00:00 rpc.rquotad
root 2572 1 0 12:49 ? 00:00:00 rpc.mountd
root 2579 2 0 12:49 ? 00:00:00 [nfsd4]
root 2580 2 0 12:49 ? 00:00:00 [nfsd4_callbacks]
root 2581 2 0 12:49 ? 00:00:00 [nfsd]
root 2582 2 0 12:49 ? 00:00:00 [nfsd]
root 2583 2 0 12:49 ? 00:00:00 [nfsd]
root 2584 2 0 12:49 ? 00:00:00 [nfsd]
root 2585 2 0 12:49 ? 00:00:00 [nfsd]
root 2586 2 0 12:49 ? 00:00:00 [nfsd]
root 2587 2 0 12:49 ? 00:00:00 [nfsd]
root 2588 2 0 12:49 ? 00:00:00 [nfsd]
root 2619 1 0 12:49 ? 00:00:00 rpc.idmapd
root 2693 2677 0 13:01 pts/0 00:00:00 egrep nfs|rpc


NFS服務端總結:

1.打印系統版本

cat /etc/redhat-release

uname -r

uname -m

2.檢查並安裝NFS服務

rpm -qa nfs-utils rpcbind

LANG=en

yum install nfs-utils rpcbind

rpm -qa nfs-utils rpcbind

3.啟動RPC及服務並檢查

/etc/init.d/rpcbind start

ps -ef |grep rpc

/etc/init.d/rpcbind status

rpcinfo -p localhost


/etc/init.d/nfs start

rpcinfo -p localhost

4.設置開機自啟動並檢查

chkconfig nfs on

chkconfig rpcbind on


chkconfig --list nfs

chkconfig --list rpcbind

5.配置共享/data目錄

cat >>/etc/exports<<EOF

#shared data for bbs by jeremy at 20170806

/data 172.16.1.0/24(rw,sync)

EOF

cat /etc/exports

6.創建共享目錄

mkdir /data

ll /data

chown -R nfsnobody.nfsnobody /data


cat /var/lib/nfs/etab 參看配置參數


7.平滑加載NFS服務並檢查服務。

/etc/init.d/nfs raload (相當於exports -rv)

showmount -e localhost

showmount -e 172.16.1.31

mount -t nfs 172.16.1.31:/data /mnt

umount /mnt

本文出自 “sandshell” 博客,請務必保留此出處http://sandshell.blog.51cto.com/9055959/1953976

搭建nfs共享存儲服務之一nfs服務端搭建