NFS shared storage service (detailed graphic explanation)
Article Directory
I. Overview
(1) NFS (Network File System) network file system
NFS is a network file system protocol based on TCP/IP transmission. By using the NFS protocol, the client can access the shared resources in the remote server as if it were accessing a local directory.
For most load balancing clusters, it is a common practice to use the NFS protocol to share data storage. NFS is also a protocol that NAS storage devices must support. However, because NFS does not have a user authentication mechanism, and the data is transmitted in plain text on the network, the security is very poor, and it can generally only be used in a local area network.
The realization of the NFS service relies on the RPC (Remote Process Call) mechanism to complete the remote to local mapping process.
Therefore, it is necessary to install the nfs-utils and rpcbind software packages to provide NFS sharing services. The former is used for NFS sharing publishing and access, and the latter is used for RPC support.
The NFS configuration file is /etc/exports and the format is: shared directory location client address (permission option)
(2) Construction of NFS file sharing service
Server (virtual machine 1 ip: 192.168.126.10)
client (virtual machine 2 ip: 192.168.126.20)
The overall step process:
-
Install the corresponding software package on the server: yum -y install rpcbind nfs-utils
-
Turn off SElinux and firewall on the server side: setenforce 0; systemctl stop firewalld
-
Create a shared directory (you can also directly share the existing directory without creating it), and grant permissions: mkdir /gongxiang; chmod 777 /gonginag
-
Modify the shared configuration file /etc/exports
-
View the NFS shared directory published by this machine: exportfs -rv
-
Start rpcbind service and nfs service: systemctl start rpcbind; systemctl start nfs
-
Client close Selinux and firewall: setenforce 0; service iptables stop
-
The client installs rpcbind and starts: yum -y install rpcbind; service rpcbind start
-
Create a mount point, view, and mount: mkdir /mygongxiang; showmount -e 192.168.126.10; mount -t nfs 192.168…126.10:/gongxiang /mygongxiang
10. Verify test results
1. Specific steps
Operation on the server:
1、安装 nfs-utils、rpcbind 软件包
rpm -q rpcbind nfs-utils
yum -y install nfs-utils rpcbind
2. Turn off SElinux and firewall on the server side:
`systemctl stop firewalld
setenforce 0 ;
3. Set the shared directory
mkdir -p /opt/gongxiang
chmod 777 /opt/gongxiang #新建共享目录,名字自取,并且设置权限为777
`4, modify the shared configuration file /etc/exports, and then reload the exports file: exportfs -a
vim /etc/exports
#编辑nfs配置文件
/opt/gongxiang 192.168.126.0/24(rw,sync,no_root_squash)
The client address can be a host name, IP address, or network segment address, and wildcard characters "*" and "?" are allowed.
"Rw" means read and write allowed, and "ro" means read only.
sync: Means synchronous writing to the memory and hard disk.
no_root_squash: Indicates that the local root authority is granted when the client is accessed as root (the default is root_squash).
root_squash: means that when the client uses the root user to access the shared directory, the root user is mapped to an anonymous user.
Other commonly used options
all_squash: All access users are mapped to anonymous users or user groups.
async: save the data in the memory buffer first, and then write it to disk when necessary.
subtree_check (default): If the output directory is a subdirectory, the nfs server will check the permissions of its parent directory.
no_subtree_check: Even if the output directory is a subdirectory, the nfs server does not check the permissions of its parent directory, which can improve efficiency
5. Start rpcbind service and nfs service:
When manually loading the NFS sharing service, you should start rpcbind first, and then start nfs
systemctl start rpcbind ;
systemctl start nfs
systemctl enable rpcbind
#可以直接设置成开机自启
systemctl enable nfs
6. View the NFS shared directory published by this machine
exportfs -rv #发布共享
showmount -e
7. The client closes SElinux and firewall:
systemctl stop firewalldc
setenforce 0 ;
Operation on the client:
8. Access NFS shared resources in the client
Install nfs-utils, rpcbind packages
rpm -q rpcbind nfs-utils
yum -y install nfs-utils rpcbind
systemctl start rpcbind
systemctl enable rpcbind
View which directories are shared by the NFS server
showmount -e 192.168.126.10
9. Mount the nfs shared directory
Mount the NFS shared directory manually
mkdir /mygongxiang
mount 192.168.80.10:/opt/gongxiang /mygongxiang
mount #Confirm the mounting result, you can also use df -Th
Set up automatic mounting
vim /etc/fstab
192.168.80.10:/opt/gongxiang /mygongxiang nfs defaults,_netdev 0 0
_netdev: Indicates that the mounting device requires a network
10. Finally verify a shared result
Note: Forcibly uninstalling NFS.
If the server-side NFS service suddenly stops while the client is being mounted and used, the client will be stuck when executing the df -h command. At this time, you cannot uninstall directly by using the umount command directly. You need to add the -lf option to uninstall.
umount -lf /mygongxiang