CentOs7安装以及使用NFS服务

avatar 2019年9月30日17:51:51 评论 1,006 次浏览

NFS服务是一个网络数据共享服务,我们可以使用客户端通过IP或者域名访问到NFS服务端,在客户端对远程的服务进行操作,减少本地空间压力。前面已经在虚拟机上添加了一个硬盘,也格式化并且挂在到虚拟机上了,下面我们学习一下安装NFS服务:

[root@wulaoer ~]# yum -y install nfs-utils

因为nfs-utils包包含了rpcbind,所以安装到时候会自动安装rpcbind,上面安装完成之后我们可以看一下passwd文件是否创建了新用户:

[root@wulaoer ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin     #这就是新创建的用户,uid和gid都是65534

配置NFS端口

NFS除了固定端口也会有一些随机的端口使用,如果开启了防火墙,需要设置端口NFS端口,我的已经关闭了,直接设置即可。

[root@wulaoer ~]# cat /etc/sysconfig/nfs 
#
# Note: For new values to take effect the nfs-config service
# has to be restarted with the following command:
#    systemctl restart nfs-config
#
# Optional arguments passed to in-kernel lockd
#LOCKDARG=
# TCP port rpc.lockd should listen on.
#LOCKD_TCPPORT=32803
# UDP port rpc.lockd should listen on.
#LOCKD_UDPPORT=32769
#
# Optional arguments passed to rpc.nfsd. See rpc.nfsd(8)
RPCNFSDARGS=""
# Number of nfs server processes to be started.
# The default is 8. 
#RPCNFSDCOUNT=16
#
# Set V4 grace period in seconds
#NFSD_V4_GRACE=90
#
# Set V4 lease period in seconds
#NFSD_V4_LEASE=90
#
# Optional arguments passed to rpc.mountd. See rpc.mountd(8)
RPCMOUNTDOPTS=""
# Port rpc.mountd should listen on.
#MOUNTD_PORT=892
#
# Optional arguments passed to rpc.statd. See rpc.statd(8)
STATDARG=""
# Port rpc.statd should listen on.
#STATD_PORT=662
# Outgoing port statd should used. The default is port
# is random
#STATD_OUTGOING_PORT=2020
# Specify callout program
#STATD_HA_CALLOUT="/usr/local/bin/foo"
#
#
# Optional arguments passed to sm-notify. See sm-notify(8)
SMNOTIFYARGS=""
#
# Optional arguments passed to rpc.idmapd. See rpc.idmapd(8)
RPCIDMAPDARGS=""
#
# Optional arguments passed to rpc.gssd. See rpc.gssd(8)
# Note: The rpc-gssd service will not start unless the 
#       file /etc/krb5.keytab exists. If an alternate
#       keytab is needed, that separate keytab file
#       location may be defined in the rpc-gssd.service's
#       systemd unit file under the ConditionPathExists
#       parameter
RPCGSSDARGS=""
#
# Enable usage of gssproxy. See gssproxy-mech(8).
GSS_USE_PROXY="yes"
#
# Optional arguments passed to blkmapd. See blkmapd(8)
BLKMAPDARGS=""
##########################分割线###############################
MOUNTD_PORT=4001      #在文件的最后增加端口 
STATD_PORT=4002
LOCKD_TCPPORT=4003
LOCKD_UDPPORT=4003
RQUOTAD_PORT=4004

nfs端口是2049,rpcbind的端口是111固定的,如果有防火墙也需要开启。

# iptables -I INPUT 5 -p tcp -m tcp --dport 111 -j ACCEPT
# iptables -I INPUT 5 -p udp -m udp --dport 111 -j ACCEPT
# iptables -I INPUT 5 -p tcp -m tcp --dport 2049 -j ACCEPT
# iptables -I INPUT 5 -p udp -m udp --dport 2049 -j ACCEPT
# iptables -I INPUT 5 -p tcp -m tcp --dport 4001:4004 -j ACCEPT
# iptables -I INPUT 5 -p udp -m udp --dport 4001:4004 -j ACCEPT
# iptables-save >/etc/sysconfig/iptables

权限配置

选项说明
ro:共享目录只读
rw:共享目录可读可写
all_squash:所有访问用户都映射为匿名用户或用户组
no_all_squash(默认):访问用户先与本机用户匹配,匹配失败后再映射为匿名用户或用户组
root_squash(默认):将来访的root用户映射为匿名用户或用户组
no_root_squash:来访的root用户保持root帐号权限
anonuid=<UID>:指定匿名访问用户的本地用户UID,默认为nfsnobody(65534)
anongid=<GID>:指定匿名访问用户的本地用户组GID,默认为nfsnobody(65534)
secure(默认):限制客户端只能从小于1024的tcp/ip端口连接服务器
insecure:允许客户端从大于1024的tcp/ip端口连接服务器
sync:将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性
async:将数据先保存在内存缓冲区中,必要时才写入磁盘
wdelay(默认):检查是否有相关的写操作,如果有则将这些写操作一起执行,这样可以提高效率
no_wdelay:若有写操作则立即执行,应与sync配合使用
subtree_check(默认) :若输出目录是一个子目录,则nfs服务器将检查其父目录的权限
no_subtree_check :即使输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率

配置客户端权限:

[root@wulaoer ~]# cat /etc/exports
/mnt/db *(rw,sync,no_root_squash)        #所有用户均可读写,如果指定用户可以把‘*’更换成ip段

exportnfs参数说明

-a 全部挂载或卸载 /etc/exports中的内容
-r 重新读取/etc/exports 中的信息 ,并同步更新/etc/exports、/var/lib/nfs/xtab
-u 卸载单一目录(和-a一起使用为卸载所有/etc/exports文件中的目录)
-v 输出详细的共享参数

启动服务

[root@wulaoer ~]# systemctl start rpcbind.service
[root@wulaoer ~]# systemctl enable rpcbind.service
[root@wulaoer ~]# systemctl start nfs.service
[root@wulaoer ~]# systemctl enable nfs.service

使用客户端挂载nfs

客户端安装服务

[root@client ~]# yum -y install nfs-utils

启动服务

[root@client ~]# systemctl start rpcbind.service
[root@client ~]# systemctl enable rpcbind.service
[root@client ~]# systemctl start nfs.service
[root@client ~]# systemctl enable nfs.service

使用showmount查看一下挂载的nfs服务

[root@client ~]# showmount -e 10.211.55.40
Export list for 10.211.55.40:
/mnt/db *

注:如果这里连接不上一定是防火墙的问题,查看一下自己设置的端口或者关闭防火墙。

挂载nfs

[root@client ~]# mount -t nfs 10.211.55.40:/mnt/db /mnt
[root@client ~]# ls /mnt/
example-db-example-storage-pvc-2a7885cb-cced-495e-adfa-cb44ae2ae94f
example-monitor-grafana-pvc-pvc-6b5a4f53-3523-4804-ae42-449913336309
example-monitor-pinpoint-hbase-monitor-pinpoint-hbase-0-pvc-86a68dfa-d6c7-4774-ad80-c3249b6b131e
kube-system-kubernetes-grafana-pvc-pvc-b6bbadc5-c097-4fe9-9fad-2d4bbd93dddd
kube-system-kubernetes-prometheus-pvc-pvc-87f194b7-530e-4079-950c-a20de51c6a85
lost+found
[root@client ~]#

这样只是临时挂载,也可以写到fstab文件中,不过挂载时可以指定权限,只是类型必须是nfs

[root@client ~]# vim /etc/fstab 
10.211.55.40:/mnt/db /mnt       nfs     defaults        0 0

也可以使用autofs挂载

# yum -y install autofs
# vi /etc/auto.master
#添加一行
/-    /etc/auto.mount
# vi /etc/auto.mount
#添加一行
/mnt -fstype=nfs,rw  10.211.55.40:/mnt/db

#启动服务
# systemctl start autofs 
# systemctl enable autofs

如果想让nfs高可用,只需要使用多个服务,把挂载的盘进行数据同步即可,这里就不多说了。

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: