Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
1. Introduction to NFS storage service concepts
NFS is the abbreviation of Network File System in Chinese. It enables different host systems to share files or directories over a Network (usually a local area Network). The NFS server only supports data storage sharing between Linux servers. PS: Windows and Linux servers exist. Do not use NFS. Samba FTP is recommended
Basic functions of NFS service software
1) It can realize the unification of data information and 2) it can save the cost of disk hardware equipmentCopy the code
3. Principle of NFS Service data Storage
1. What is NFS? Shared storage network, Shared directory is through the network, let the others on the network server to be able to mount the access to a Shared directory of data (NFS is commonly used to store video, attachments, pictures, etc) of the static data, NFS is through the network to the server and the client data transmission, so as long as it is through the network will have a port, What are NFS ports? Unfortunately, NFS ports are random and change with each restart. So how does the client know the port of the server? In fact, the NFS server is implemented through remote procedure Call (RPC) protocol/service. That is, THE RPC service centrally manages NFS ports. Clients and servers use RPC to determine ports for data transmission. PS :(originally, the RPC managed NFS port allocation on the server. If the client wanted to transmit data, the RPC of the client would first ask the RPC of the server for the port of the server, and then establish a connection to the port before transmitting data). 2. What is RPC? Remote Procedure Call (RPC) is a protocol that requests services from Remote computer programs over the network without understanding the underlying network technology. The RPC protocol assumes the existence of some transport protocol, such as TCP or UDP, to carry information data between communication programs. In the OSI network communication model, RPC spans both the transport layer and the application layer. RPC makes it easier to develop applications including network distributed multiprograms. RPC uses client/server mode. The requester is a client and the service provider is a server. 3. Relationship between RPC and NFS Before the NFS server starts RPC, NFS can register the port information from the RPC. The CLIENT RPC can obtain the NFS port information from the RPC of the server. 4.RPC and NFS startup sequence 1 Start the RPC service first 2 Start the NFS service 3 Start the RPC service onlyCopy the code
4. Service NFS service deployment process
🔅1. Check whether the storage software is installed
Nfs-utils rpcbind(centos6) # The rpcbind service is a dependency of the NFs-utils software
[root@nfs01 ~]rpm -qa|grep -E 'nfs|rpc'
[root@nfs01 ~]yum install nfs-utils rpcbind
Install the NFS and RPC services
[root@nfs01 ~]rpm -ql nfs-utils Check whether the NFS service is installed
/etc/rc.d/init.d/nfs
/usr/sbin/showmount
[root@nfs01 ~]rpm -ql rpcbind Check whether the RPC service is installed/etc/rc.d/init.d/rpcbind /sbin/rpcbind /usr/sbin/rpcinfo -- Checks whether the NFS service has been registeredCopy the code
🔅2. Write a configuration file
[root@nfs01 ~] vim /etc/exports /data 172.16.1.0/24(RW,sync) ① ② ③ Define the function items of the NFS service directory information Define and configure some permissions or functions of a shared directory sync: synchronously transferring data Async: asynchronously transferring dataCopy the code
🔅3. Create a shared directory and authorize the directory
[root@nfs01 ~] mkdir /data -p (add p to create a directory repeatedly) [root@nfs01 ~] id nfsnobody -- Shared directory management user uid=65534(nfsnobody) gid=65534(nfsnobody) groups=65534(nfsnobody) [root@nfs01 ~] chown -R nfsnobody.nfsnobody /data/# change the user group of /data directory to nfsnobody
Copy the code
🔅4. Start NFS services
1. Start the RPC service [root@nfs01 ~] /etc/init.d/rpcbind start# Start RPC service
[root@nfs01 ~] ps -ef|grep rpcbind Check whether the RPC service is enabled for the process
rpc 2957 1 0 13:20 ? 00:00:00 rpcbind
root 2961 2915 0 13:22 pts/3 00:00:00 grep rpcbind
[root@nfs01 ~]# # netstat lntup | grep RPC view the RPC port informationTCP 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 2957/rpcbind TCP 0 0: ::111 ::* LISTEN 2957/rpcbind 2. Restart NFS [root@nfs01 ~] /etc/init.d/ NFS start# Start the NFS service process
[root@nfs01 ~] ps -ef|grep nfs Check whether the process is startedroot 3021 2 0 13:28 ? 00:00:00 [nfsd4] root 3022 2 0 13:28 ? 00:00:00 [nfsd4_callbacks] root 3023 2 0 13:28 ? 00:00:00 [nfsd] root 3024 2 0 13:28 ? 00:00:00 [nfsd] root 3025 2 0 13:28 ? 00:00:00 [nfsd] root 3026 2 0 13:28 ? 00:00:00 [nfsd] root 3027 2 0 13:28 ? 00:00:00 [nfsd] root 3028 2 0 13:28 ? 00:00:00 [nfsd] root 3029 2 0 13:28 ? 00:00:00 [nfsd] root 3030 2 0 13:28 ? 00:00:00 [nfsd] root 3065 2915 0 13:29 pts/3 00:00:00 grep nfs [root@nfs01 ~] rpcinfo -p localhost --- Check whether the local NFS service wants to register the RPC serviceCopy the code
5. Client NFS service deployment process
🔅1. Check the installation software
[root@backup ~]rpm -qa|grep -E 'nfs|rpc'[root@nfs01 ~] yum install nfs-utils rpcbindInstall the NFS and RPC services
[root@backup ~] rpm -ql nfs-utils Check whether the NFS service is installed
[root@backup ~] rpm -ql rpcbind Check whether the RPC service is installed
Copy the code
🔅2. Mount the shared directory
[root@backup ~] mount -t NFS 172.16.1.31:/data/MNT# test mount directory command
[root@backup ~] df -h # Check disk mount statusFilesystem Size Used Avail Use% Mounted on /dev/sda3 48G 1.8G 44G 4% / TMPFS 491M 0 491M 0% /dev/shm /dev/sda1 190M 35M 146M 19% /boot 172.16.1.31:/data 48G 1.8G 44G 4% / MNT You can see that the 172.16.1.31:/data directory is successfully mounted. If the mounting fails, run the following command on the server: [root@nfs01 ~]# showmount -e localhostCheck whether there is a directory for mounting. If there is no address, check whether the configuration file is modified as requiredCopy the code
🔅3. Perform a storage test
Server Clients add directories or files to see if they can view each otherCopy the code
Rpcinfo -p NFS server address information — Check whether the NFS service is registered in the RPC service showmount -e NFS server address information — Check whether the NFS server can use the shared directory
Note: Configure mappings based on your requirements. The preceding information is the most basic mapping
How to configure mapping information on the NFS server? 1) WWW user is expected to manage the storage shared directory. 2) Root user is expected to store data to the shared directory after mapping
Enterprise case: Change the default NFS mapping user. Change the default NFsnobody user to WWW user. Enterprise case: Change the default NFS mapping user.
1. Create a mapping user [root@nfs01 ~].# useradd -M -s /sbin/nologin wwwSet virtual user, can only be used for login [root@nfs01 ~]# id wwwuid=500(www) gid=500(www) groups=500(www) 2. Modifying an NFS Configuration File [root@nfs01 ~]# vim /etc/exports / data 172.16.1.0/24 (rw, sync, no_all_squash root_squash, anonuid = 500, anongid = 500) 3. Modifying directory permission information [root@nfs01 ~]# chown -R www.www /dataCheck whether the modification is complete [root@nfs01 ~]# ll /data -ddrwxr-xr-x 2 www www 4096 Sep 2 18:52 /data 4. Restarting the NFS service [root@nfs01 ~]# /etc/init.d/nfs restart
Copy the code
Client deployment process:
1. Create a mapping user [root@backup ~].# useradd www -u 500To ensure that the WWW user created is consistent with the server user UID, the -u parameter can be used to specify uid 2. Mount directory [root@backup ~]# mount -t NFS 172.16.1.31:/data/MNT3. Test [root@backup MNT]# touch root.txt
[www@backup mnt]$ touch www.txt
[fu@backup mnt]$ touch fu.txt
touch: cannot touch `fu.txtOnly root and WWW users can create data in/MNTCopy the code
Client supplement: If a conflict occurs when a client creates a user with a specified UID, see the following information
[root@backup ~]# useradd www -u 500
useradd: UID 500 is not unique A user has occupied the uid
[root@backup ~]# grep 500 /etc/passwd
# check which user is using the UIDDelete the WWW user and re-create it [root@nfs01 ~]# userdel -r WWW # delete
[root@nfs01 ~]# useradd WWW -u 666-m -s /sbin/nologinChange the uid in the configuration file root@nfs01 ~]# vim /etc/exports/data 172.16.1.0/24(rw,sync,no_all_squash,root_squash,anonuid=666,anongid=666) Modify the management user and management group of mount directory /data. The administrator of /data directory is the same as before, no update client created uid 666 [root@backup ~]# useradd www -u 666
[root@backup ~]# id www
uid=666(www) gid=666(www) groups=666(www)
Copy the code
After the server is restarted, the NFS service can be used normally.
Chkconfig NFS on chkconfig rpcbind On Check whether the NFS and RPC services are enabled upon startup [root@nfs01 ~]# chkconfig --list|grep -E 'rpc|nfs'
Copy the code
After the client is restarted, the NFS service can be used normally. Method :(after the client is restarted, the mount directory may be lost and you need to mount it again.)
Steps: / etc/rc. Localecho 'mount -t NFS 172.16.1.31:/data/ / MNT ' >>/etc/rc.local
Copy the code
PS: NFS service storage principle
The server does three things: 1) Start the RPC service 2) Start the NFS service 3) Register with the RPC service The client does three things: 1) Establish a network connection (RPC 111) 2) mount the client using the mount command 3) Implement data network storageCopy the code