[关闭]
@yanglt7 2018-10-21T15:57:09.000000Z 字数 7257 阅读 716

【Web 集群实战】18_NFS 网络文件共享服务

Web集群实战


1. NFS 介绍

1.1 什么是 NFS

1.2 NFS 系统原理

1.3 什么是 RPC

2. NFS 服务器端配置

2.1 操作系统及内核版本信息

  1. [root@ylt001 ~]# cat /etc/redhat-release
  2. CentOS Linux release 7.4.1708 (Core)
  3. [root@ylt001 ~]# uname -r
  4. 3.10.0-693.el7.x86_64
  5. [root@ylt001 ~]# uname -m
  6. x86_64

2.2 查看NFS 软件包

  1. [root@ylt001 ~]# rpm -qa nfs-utils rpcbind
  2. nfs-utils-1.3.0-0.48.el7.x86_64
  3. rpcbind-0.2.0-42.el7.x86_64

2.3 启动 NFS 相关服务

  1. [root@ylt001 ~]# systemctl status rpcbind.service
  2. * rpcbind.service - RPC bind service
  3. Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; indirect; vendor preset: enabled)
  4. Active: inactive (dead)
  5. [root@ylt001 ~]# systemctl start rpcbind.service
  6. [root@ylt001 ~]# systemctl status rpcbind.service
  7. * rpcbind.service - RPC bind service
  8. Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; indirect; vendor preset: enabled)
  9. Active: active (running) since Sat 2018-10-06 23:14:19 CST; 26s ago
  10. Oct 06 23:14:19 ylt001 systemd[1]: Starting RPC bind service...
  11. Oct 06 23:14:19 ylt001 systemd[1]: Started RPC bind service.
  12. [root@ylt001 ~]# lsof -i :111
  13. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  14. systemd 1 root 46u IPv6 14504 0t0 TCP *:sunrpc (LISTEN)
  15. systemd 1 root 47u IPv4 14505 0t0 TCP *:sunrpc (LISTEN)
  16. rpcbind 1538 rpc 4u IPv6 14504 0t0 TCP *:sunrpc (LISTEN)
  17. rpcbind 1538 rpc 5u IPv4 14505 0t0 TCP *:sunrpc (LISTEN)
  18. rpcbind 1538 rpc 8u IPv4 23285 0t0 UDP *:sunrpc
  19. rpcbind 1538 rpc 10u IPv6 23287 0t0 UDP *:sunrpc
  20. [root@ylt001 ~]# netstat -lntup|grep rpcbind
  21. udp 0 0 0.0.0.0:865 0.0.0.0:* 1538/rpcbind
  22. udp 0 0 0.0.0.0:111 0.0.0.0:* 1538/rpcbind
  23. udp6 0 0 :::865 :::* 1538/rpcbind
  24. udp6 0 0 :::111 :::* 1538/rpcbind
  25. [root@ylt001 ~]# rpcinfo -p localhost
  26. program vers proto port service
  27. 100000 4 tcp 111 portmapper
  28. 100000 3 tcp 111 portmapper
  29. 100000 2 tcp 111 portmapper
  30. 100000 4 udp 111 portmapper
  31. 100000 3 udp 111 portmapper
  32. 100000 2 udp 111 portmapper
  1. [root@ylt001 ~]# LANG=en
  2. [root@ylt001 ~]# systemctl status nfs
  3. * nfs-server.service - NFS server and services
  4. Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
  5. Active: inactive (dead)
  6. [root@ylt001 ~]# systemctl start nfs
  7. [root@ylt001 ~]# systemctl status nfs
  8. * nfs-server.service - NFS server and services
  9. Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
  10. Active: active (exited) since Sat 2018-10-06 23:21:19 CST; 3s ago
  11. Oct 06 23:21:19 ylt001 systemd[1]: Starting NFS server and services...
  12. Oct 06 23:21:19 ylt001 systemd[1]: Started NFS server and services.
  13. [root@ylt001 ~]# rpcinfo -p localhost
  14. program vers proto port service
  15. 100000 4 tcp 111 portmapper
  16. 100000 3 tcp 111 portmapper
  17. 100000 2 tcp 111 portmapper
  18. 100000 4 udp 111 portmapper
  19. 100000 3 udp 111 portmapper
  20. 100000 2 udp 111 portmapper
  21. 100024 1 udp 47859 status
  22. 100024 1 tcp 34218 status
  23. 100005 1 udp 20048 mountd
  24. 100005 1 tcp 20048 mountd
  25. 100005 2 udp 20048 mountd
  26. 100005 2 tcp 20048 mountd
  27. 100005 3 udp 20048 mountd
  28. 100005 3 tcp 20048 mountd
  29. 100003 3 tcp 2049 nfs
  30. 100003 4 tcp 2049 nfs
  31. 100227 3 tcp 2049 nfs_acl
  32. 100003 3 udp 2049 nfs
  33. 100003 4 udp 2049 nfs
  34. 100227 3 udp 2049 nfs_acl
  35. 100021 1 udp 38028 nlockmgr
  36. 100021 3 udp 38028 nlockmgr
  37. 100021 4 udp 38028 nlockmgr
  38. 100021 1 tcp 39989 nlockmgr
  39. 100021 3 tcp 39989 nlockmgr
  40. 100021 4 tcp 39989 nlockmgr
  1. [root@ylt001 ~]# systemctl enable rpcbind
  2. [root@ylt001 ~]# systemctl enable nfs
  3. Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
  4. [root@ylt001 ~]# systemctl list-unit-files |grep enabled| egrep "rpcbind|nfs"
  5. nfs-server.service enabled
  6. nfs.service enabled
  7. rpcbind.socket enabled
  8. nfs-client.target enabled

2.4 配置案例

  1. [root@ylt001 ~]# mkdir -p /nfsdata
  2. # 创建哟啊共享的 NFS 目录,也可以使用已存在的目录
  3. [root@ylt001 ~]# cd /nfsdata/
  4. [root@ylt001 nfsdata]# touch test.txt
  5. # 创建测试文件
  6. [root@ylt001 nfsdata]# chown -R nfsnobody.nfsnobody /nfsdata
  7. # 在服务器端把要共享的 NFS 目录赋予 NFS 默认用户 nfsnobody 用户和用户组权限
  8. [root@ylt001 nfsdata]# ls -ld /nfsdata
  9. drwxr-xr-x 2 nfsnobody nfsnobody 4096 Oct 7 00:04 /nfsdata
  10. [root@ylt001 nfsdata]# grep nfsnobody /etc/passwd
  11. nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
  1. [root@ylt001 nfsdata]# cat /etc/exports
  2. /nfsdata 192.168.2.0/24(rw,sync)
  3. [root@ylt001 nfsdata]# exportfs -rv
  4. exporting 192.168.2.0/24:/nfsdata
  5. [root@ylt001 nfsdata]# cat /etc/exports
  6. /nfsdata 192.168.2.0/24(rw,sync)
  7. [root@ylt001 nfsdata]# showmount -e localhost
  8. Export list for localhost:
  9. /nfsdata 192.168.2.0/24
  10. [root@ylt001 nfsdata]# cat /var/lib/nfs/etab
  11. /nfsdata 192.168.2.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,secure,root_squash,no_all_squash)
  1. [root@ylt001 nfsdata]# mount -t nfs 192.168.2.141:/nfsdata /mnt
  2. [root@ylt001 nfsdata]# df -h
  3. Filesystem Size Used Avail Use% Mounted on
  4. /dev/mapper/centos-root 9.8G 6.2G 3.1G 68% /
  5. devtmpfs 897M 0 897M 0% /dev
  6. tmpfs 912M 0 912M 0% /dev/shm
  7. tmpfs 912M 9.0M 903M 1% /run
  8. tmpfs 912M 0 912M 0% /sys/fs/cgroup
  9. /dev/sda2 190M 147M 30M 84% /boot
  10. tmpfs 183M 0 183M 0% /run/user/0
  11. tmpfs 183M 0 183M 0% /run/user/1000
  12. 192.168.2.141:/nfsdata 9.8G 6.2G 3.1G 68% /mnt

3. NFS 客户端配置

3.1 操作系统及内核版本信息

  1. [root@ylt001 ~]# cat /etc/redhat-release
  2. CentOS Linux release 7.4.1708 (Core)
  3. [root@ylt001 ~]# uname -r
  4. 3.10.0-693.el7.x86_64
  5. [root@ylt001 ~]# uname -m
  6. x86_64

3.2 查看NFS 软件包

  1. [root@ylt001 ~]# rpm -qa nfs-utils rpcbind
  2. nfs-utils-1.3.0-0.48.el7.x86_64
  3. rpcbind-0.2.0-42.el7.x86_64

3.3 启动 RPC 服务

  1. [root@ylt001 ~]# systemctl stop firewalld
  1. [root@ylt001 ~]# LANG=en
  2. [root@ylt001 ~]# systemctl start rpcbind
  3. [root@ylt001 ~]# systemctl status rpcbind
  4. * rpcbind.service - RPC bind service
  5. Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; indirect; vendor preset: enabled)
  6. Active: active (running) since Sun 2018-10-07 01:23:06 CST; 7h left
  7. Oct 07 01:23:06 ylt001 systemd[1]: Starting RPC bind service...
  8. Oct 07 01:23:06 ylt001 systemd[1]: Started RPC bind service.

3.4 挂载 NFS 共享目录 /nfsdata

  1. [root@ylt001 ~]# showmount -e 192.168.2.141
  2. Export list for 192.168.2.141:
  3. /nfsdata 192.168.2.0/24
  4. [root@ylt001 ~]# mount -t nfs 192.168.2.141:/nfsdata /mnt
  5. [root@ylt001 ~]# df -h
  6. Filesystem Size Used Avail Use% Mounted on
  7. /dev/mapper/centos-root 9.8G 6.2G 3.1G 68% /
  8. devtmpfs 897M 0 897M 0% /dev
  9. tmpfs 912M 0 912M 0% /dev/shm
  10. tmpfs 912M 9.0M 903M 1% /run
  11. tmpfs 912M 0 912M 0% /sys/fs/cgroup
  12. /dev/sda2 190M 147M 30M 84% /boot
  13. tmpfs 183M 0 183M 0% /run/user/0
  14. tmpfs 183M 0 183M 0% /run/user/1000
  15. 192.168.2.141:/nfsdata 9.8G 6.2G 3.1G 68% /mnt
  1. [root@ylt001 ~]# ls /mnt/
  2. test.txt
  3. [root@ylt001 ~]# mkdir /mnt/test
  4. [root@ylt001 ~]# ll /mnt/
  5. total 4
  6. drwxr-xr-x 2 nfsnobody nfsnobody 4096 Oct 6 17:37 test
  7. -rw-r--r-- 1 nfsnobody nfsnobody 0 Oct 7 2018 test.txt
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注