Deployment and introduction of nfs server

Posted by Dizzee15 on Sat, 18 Dec 2021 06:17:44 +0100

1, Introduction to nfs server

1. What is nfs?

nfs is the Network File System. In English, Network File System is a UNIX presentation layer protocol developed by SUN company, which enables users to access files elsewhere on the network as if they were using their own computer.

That is to share the files in your own machine on the network so that other machines can use it.

2. Why do you need an nfs server?

nfs servers can realize data homology and ensure the consistency of website data. No matter which back-end server the load balancer allocates the request to, the content seen by the client is the same.

nfs server is not the best solution to ensure data consistency. It is a relatively cheap solution. Its performance is not particularly good. It is suitable for students in the learning stage. Companies generally use special storage servers.

SAN Storage Area Network (SAN) adopts Fibre Channel (FC) technology, connects storage array and server host through FC switch, and establishes an area network dedicated to data storage.

3. Advantages and disadvantages of NFS server?

Advantages: any linux server can be built, the cost is very low, and the construction is very easy
Disadvantages: the reading speed is limited, which is related to network quality, disk IO, CPU, memory and other factors. It is transmitted on the traditional tcp/ip network

4. Implementation principle of NFS service

NFS supports many functions, and different functions will be started with different programs. Each function will enable some ports to transmit data. Therefore, the port corresponding to the NFS function is not fixed. The client needs to know the relevant ports on the NFS server to establish a connection for data transmission, and RPC is a service used to uniformly manage NFS ports, In addition, the unified external port is 111, and RPC will record the information of NFS port, so that we can communicate the port information between the server and the client through RPC. The main function of PRC is to specify the port number corresponding to each NFS function and notify the client that the client can connect to the normal port.

Before starting NFS SERVER, Start the RPC service first (i.e. portmap or rpcbind service, the same below) otherwise, NFS SERVER cannot register with the RPC service area. In addition, if the RPC service is restarted, all the previously registered NFS port data will be lost. Therefore, at this time, the NFS program managed by the RPC service should also be restarted to re register with RPC. Generally, after modifying the NFS configuration document, it is not necessary to restart NFS, and you can directly execute the command Line / etc / init D / NFS reload or exportfs – rv to make the modified / etc/exports effective.

Generally speaking, nfs does not listen to a port number, but outsources it to rpc services. rpc helps nfs listen to the port, and then tells the client which process corresponds to the port.

2, Deployment of nfs server

1. Preparation

Prepare a centos system and modify the ip and host name

IP:192.168.10.233 host name: NFS server

It is recommended to close the firewall and selinux. The specific operation is shown in the previous article

2. Install and start nfs service

#Install nfs related software
[root@nfs-server ~]# yum install nfs-utils -y

#Start nfs service
[root@nfs-server ~]# service nfs-server start
Redirecting to /bin/systemctl start nfs-server.service

#Set nfs service startup
[root@nfs-server ~]# systemctl enable nfs-server
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /usr/lib/systemd/system/nfs-server.service.

#Check whether the nfs service is started
[root@nfs-server ~]# ps aux|grep nfs
root       73072  0.0  0.1  50112  2904 ?        Ss   12:07   0:00 /usr/sbin/nfsdcld
root       75737  0.0  0.0      0     0 ?        S    12:13   0:00 [nfsd]
root       75738  0.0  0.0      0     0 ?        S    12:13   0:00 [nfsd]
root       75739  0.0  0.0      0     0 ?        S    12:13   0:00 [nfsd]
root       75740  0.0  0.0      0     0 ?        S    12:13   0:00 [nfsd]
root       75741  0.0  0.0      0     0 ?        S    12:13   0:00 [nfsd]
root       75742  0.0  0.0      0     0 ?        S    12:13   0:00 [nfsd]
root       75743  0.0  0.0      0     0 ?        S    12:13   0:00 [nfsd]
root       75744  0.0  0.0      0     0 ?        S    12:13   0:00 [nfsd]
root       76441  0.0  0.0  12324  1108 pts/1    S+   12:15   0:00 grep --color=auto nfs

3. Edit the / etc/exports file

#Write specific shared directories and permissions
[root@nfs-server ~]# vim /etc/exports
[root@nfs-server ~]# cat /etc/exports
/web 192.168.10.0/24(ro,all_squash,async)

#/web is the path of the shared folder. If you use the absolute path, you need to create a new one
#192.168. 10.0/24 indicates the ip address network segment of the client allowed to access
#(ro,all_squash,async) indicates permission restriction
#ro means read-only
#rw means readable and writable
#all_squash any user on the client who comes to visit it will be considered as an ordinary user
#root_ Square when the nfs client is accessed as root administrator, it is mapped to the anonymous user of the nfs server
#no_ root_ Square when an NFS client is accessed as a root administrator, it is mapped to the root administrator of the NFS server
#sync synchronizes and writes data to memory and hard disk to ensure no data loss
#async is asynchronous. It gives priority to saving data to memory and then writing to disk. It is efficient, but data may be lost

#Create a new / web directory and write shared files
[root@nfs-server ~]# mkdir /web
[root@nfs-server ~]# cd /web
[root@nfs-server web]# vim index.html
[root@nfs-server web]# cat index.html 
welcome to hunan changsha!!!

#Refresh the list of output files
[root@nfs-server web]# exportfs -rv
exporting 192.168.10.0/24:/web

4. Install NFS utils software on the client

#Install NFS utils software on the client

#This is Ip 192.168 10.230 client
[root@manager ~]# yum install nfs-utils -y
#View folders shared by nfs server
[root@manager ~]# showmount -e 192.168.10.233
Export list for 192.168.10.233:
/web 192.168.10.0/24

#This is Ip 192.168 10.231 client
[root@worker2 ~]# yum install nfs-utils -y
[root@worker2 ~]# showmount -e 192.168.10.233
Export list for 192.168.10.233:
/web 192.168.10.0/24

#This is Ip 192.168 10.232 client
[root@worker1 ~]# yum install nfs-utils -y
[root@worker1 ~]# showmount -e 192.168.10.233
Export list for 192.168.10.233:
/web 192.168.10.0/24

5. Deploy the service and mount the nfs server

#Deploying services on the manager machine
[root@manager ~]# docker service create  --name sc-nginx2  --mount 'type=volume,source=nfsvolume,target=/usr/share/nginx/html,volume-driver=local,volume-opt=type=nfs,volume-opt=device=:/web,"volume-opt=o=addr=192.168.10.233,ro,nfsvers=4,async"'  --replicas 6   -p 8889:80    nginx:latest

pu05ln49zba3dt9bebd7uo00k
overall progress: 6 out of 6 tasks 
1/6: running   [==================================================>] 
2/6: running   [==================================================>] 
3/6: running   [==================================================>] 
4/6: running   [==================================================>] 
5/6: running   [==================================================>] 
6/6: running   [==================================================>] 
verify: Service converged 


#The name of the sc-nginx2 service
#source=nfsvolume docker is the name of the volume on the host computer. It does not need to be created in advance and will be created automatically
#target=/usr/share/nginx/html the directory where the web page is stored
#Volume driver = local to access the local directory
#Volume opt = type = nfs volume options supported for nfs
#Volume opt = device =: / Web represents the directory shared by the nfs server
#volume-opt=o=addr=192.168.10.233,ro,nfsvers=4,async mounts the IP address and options of the specific nfs server
#--The number of replicas, that is, 6 containers
#-P 8889: the mapping of port 80. The 8889 port of this machine is mapped to port 80 in the container
#nginx:latest image version



#View created services
[root@manager ~]# docker service ls
ID        NAME          MODE       REPLICAS      IMAGE          PORTS
pu05ln49zba3  sc-nginx2  replicated   6/6      nginx:latest   *:8889->80/tcp

#View the details of the created volume
#Options indicates the specific information of the mount
[root@manager ~]# docker volume inspect nfsvolume
[
    {
        "CreatedAt": "2021-08-29T14:58:55+08:00",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/nfsvolume/_data",
        "Name": "nfsvolume",
        "Options": {
            "device": ":/web",
            "o": "addr=192.168.10.233,ro,nfsvers=4,async",
            "type": "nfs"
        },
        "Scope": "local"
    }
]

6. Test deployed services

Topics: Linux Docker nfs swarm