nfs server storage

Introduction of basic 1.nfs

1.1: What is nfs?

Its main function is to allow the network can share files and directories between different machines each other systems. NFS NFS server can allow the client to the NFS server shared directory distal end mounted to a local NFS client. In local NFS client machine seems, NFS server shared directory if your disk partitions and directories

1.2: nfs can you do?

nfs able to achieve sharing files between different host systems
so that the directory can be shared between different hosts
https://blog.csdn.net/wykkunkun/article/details/79638288

1.3: nfs can solve any problem?

1, to solve the consistency problem of multiple machines static resources
2, Sauter machine resources to solve shared
3, to solve the problem of wasted disk space

1.4: Note the use of nfs?

1, days plus shared storage access latency and consumes only bring sites, and will not increase the speed of access to the site.
2, CDN: 1. Buy vendors CDN ---> user requests img ---> CDN ---> Load Balancing -> Web -> storage -> CDN caches the picture
2. All of the web are made shared storage, picture this time unanimously, simply picture regularly pushed to CDN

1.5: Principles nfs achieve resolution?

Local file-based operations
1. When a user performs mkdir command, BashShell not complete the command operation, it will translate to the kernel.
2.Kernel kernel parses drives the corresponding disk device is completed, the completion of creation directory.
NFS implementation principle
1.NFS client performs add, delete and other operations, the client uses a different function of the packaging operation. (Windows Linux mac)
2.NFS client is passed to the NFS server via TCP / IP approach. (Reliable)
3.NFS service. After receiving the request, it will first call the process portmap port mapping.
4.nfsd process for determining whether the NFS client has permission to connect NFS server.
5.Rpc.mount process to determine whether the client has a corresponding authority for verification. Read
6.idmap process of implementing user mapping and compression.
7. Finally NFS client server function will convert the local command can be executed, and then passes the command to the kernel, the kernel driver hardware.

    注意: rpc是一个远程过程调用,那么使用nfs必须有rpcbind服务

2, installation, configuration, nfs service

2.1: Installation

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

2.2: Configuration

1.共享什么目录?
2.共享给谁使用?
3.共享后目录,客户端拥有什么权限?
[root@nfs ~]# cat /etc/exports
/data 172.16.1.0/24(rw)

2.3: initialize the environment according to the configuration

[root@nfs ~]# mkdir /data
[root@nfs ~]# chown -R nfsnobody.nfsnobody /data/

2.4: Start

[root@nfs ~]# systemctl enable nfs
[root@nfs ~]# systemctl start nfs   

2.5: Client Test

[root@backup ~]# yum install nfs-utils -y
[root@backup ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24

挂载远程172.16.1.31的/data至本地的/mnt目录
[root@backup ~]# mount -t nfs 172.16.1.31:/data /mnt

2.6: Error Model

    #访问被拒绝 (没有允许该网段访问)
[root@backup ~]# mount -t nfs 10.0.0.31:/data /media/
mount.nfs: access denied by server while mounting 10.0.0.31:/data


    #能够连接,但是权限被拒绝
[root@backup mnt]# touch file
touch: cannot touch ‘file’: Permission denied

2.7: Multiple clients share a storage server (NFS)

yum install nfs-utils -y
showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24

挂载远程172.16.1.31的/data至本地的/mnt目录
mount -t nfs 172.16.1.31:/data /mnt

2.8: Implement boot automatically mount (because the server does not restart)

[root@web01 ~]# cat /etc/fstab
172.16.1.31:/data             /media          nfs     defaults        0 0

PS:  如果nfs服务端出现问题:  客户端重启则会无法启动成功
        1.等待1分38s  自动进入系统
        2.进入单用户模式,注释开机自启动nfs
        3.将nfs恢复正常

2.9: nfs-related configuration parameters

nfs共享参数     参数作用
rw*             读写权限 (最多)
ro              只读权限 (只希望看,不希望写)

root_squash     当NFS客户端以root管理员访问时,映射为NFS服务器的匿名用户nfsnobody(不常用)
no_root_squash  当NFS客户端以root管理员访问时,映射为NFS服务器的root管理员(不常用)
no_all_squash   无论NFS客户端使用什么账户访问,都不进行用户压缩  ( 后面讲云计算课程会用上 )
all_squash      无论NFS客户端使用什么账户访问,均映射为NFS服务器的匿名用户(常用)
sync*           同时将数据写入到内存与硬盘中,保证不丢失数据
async           优先将数据保存到内存,然后再写入硬盘;这样效率更高,但可能会丢失数据
anonuid*        配置all_squash使用,指定NFS的用户UID,必须存在系统
anongid*        配置all_squash使用,指定NFS的用户UID,必须存在系统

2.9.1: rw sum ro

[root@nfs ~]# cat /etc/exports
/data 172.16.1.0/24(ro)
[root@nfs ~]# systemctl restart nfs

#提示,该目录是一个只读文件
[root@web01 media]# touch file
touch: cannot touch ‘file’: Read-only file system

2.9.2: Verify all_squash anonuid anongid

[root@nfs ~]# cat /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)

#1.创建系统真实用户,指定uid和gid为666
[root@nfs ~]# groupadd -g 666 www
[root@nfs ~]# useradd -u666 -g666 www
[root@nfs ~]# id www
uid=666(www) gid=666(www) groups=666(www)

#2.变更属主和属组
[root@nfs ~]# chown -R www.www /data/

#3.重启nfs
[root@nfs ~]# systemctl restart nfs

#4.客户端使用(一定要与服务端使用的匿名用户一致)
[root@web01 ~]# groupadd -g 666 www
[root@web01 ~]# useradd -u666 -g666 www
[root@web01 ~]# mount -t nfs 172.16.1.31:/data/ /media/

2.10: nfs how to share multiple directories?

[root@nfs ~]# cat /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
/data_2 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)

1.NFS存储优点
1.NFS简单易用、方便部署、数据可靠、服务稳定、满足中小企业需求。
2.NFS的数据都在文件系统之上,所有数据都是能看得见。 

            除了NFS:          ( Glusterfs分布式  赠送 )  MooseFS   FastDFS  

2.NFS存储局限
1.存在单点故障, 本身NFS不支持高可用,也不支持集群.
2.NFS数据都是明文,并不对数据做任何校验,也没有密码验证(强烈建议内网使用)。

3.NFS应用建议
1.生产场景应将静态数据(jpg\png\mp4\avi\css\js)尽可能放置CDN场景进行环境, 以此来减少后端存储压力
2.如果没有缓存或架构、代码等,本身历史遗留问题太大,在多存储也没意义


    NFS就是用来共享  其他什么都没有.     所有的静态都是CDN提供访问的

nfs practice

Preparing the Environment

服务器系统   角色  IP
CentOS 7.6  NfsServer(A)    172.16.1.31
CentOS 7.6  NfsClient(B)    172.16.1.41
CentOS 7.6  NfsClient(C)    172.16.1.7

准备3台虚拟机服务器,并且请按照要求搭建配置NFS服务。
NFS服务端(A)

NFS客户端(B)
NFS客户端(C)
1.在NFS服务端(A)上共享/data/w(可写)   及/data/r(只读)
2.在NFS客户端(B/C)上进行挂载

A server configuration

[root@nfs ~]# yum install nfs-utils -y
[root@nfs ~]# cat /etc/exports
/data/r 172.16.1.0/24(ro,sync,all_squash,anonuid=666,anongid=666)
/data/w 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)


根据配置进行初始化环境
[root@nfs ~]# groupadd -g 666 www
[root@nfs ~]# useradd -u666 -g666 www
[root@nfs ~]# mkdir /data/{r,w} -p
[root@nfs ~]# chown -R www.www /data/

[root@nfs ~]# systemctl restart nfs

Configure the client B

卸载之前遗留的挂载信息
[root@web01 ~]# umount -lf 172.16.1.31:/data
[root@web01 ~]# umount -lf 172.16.1.31:/data_2

挂载
[root@web01 ~]# mount -t nfs 172.16.1.31:/data/r /mnt
[root@web01 ~]# mount -t nfs 172.16.1.31:/data/w /media/

Guess you like

Origin www.cnblogs.com/yang-dan/p/12074965.html