摘要:NFS


日志轮转:

https://blog.csdn.net/weixin_43265596/article/details/85469405

https://www.cnblogs.com/liangyuxing/p/11962863.html

https://blog.csdn.net/dhxaohei/article/details/8014764


一. NFS简介

**NFS(Network File System的简称,即网络文件系统)**可以通过网络,让不同的机器、不同的操作系统可以共享彼此的文件。NFS可以将远程的计算机磁盘挂载到本地,读写文件像访问本地磁盘一样操作

前提:关闭selinx
关闭iptables

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
1、安装
[root@cPen_A lianxi]# yum install nfs-utils
--------------------------------------------------------------------------------------------
2、修改配置文件 /etc/exports
[root@cPen_A lianxi]# vim /etc/exports #注:配置文件
/opt/www 192.168.255.0/24(rw,no_root_squash)
#注:192.168.255.0/24共享,rw权限 读写,no_root_squash 将远程根用户当成本地根用户,即不压制root
首先创建/opt/www目录,在这下面创建index.html文件,文件内容”this is test nfs”
再添加exports配置,nfs的配置:vim /etc/exports
添加如下行:
/opt/www 192.168.255.0/24(rw)
表示共享/opt/www目录,给192.168.255./24这个网段的主机都可以通过nfs来访问我本地的/opt/www目录
括号()里面的表示选项,rw表示读写

生效配置:
exportfs -a 或者重启服务

[root@cPen_A opt]# cd /opt
[root@cPen_A opt]# mkdir www
[root@cPen_A opt]# cd www
[root@cPen_A www]# vim index.html
this is test nfs
[root@cPen_A www]# pwd
/opt/www

#注:/opt 放额外的软件包的目录
--------------------------------------------------------------------------------------------
3、启动nfs服务
[root@cPen_A www]# service nfs start #注:启动服务
[root@cPen_A www]# showmount -e #注:检测是否可以连接
Export list for cPen_A:
/opt/www 192.168.255.0/24
#注:centos8下 启动 [root@cPen_B ~]# service nfs-server start
---------------------------------------------------------------
NFS的连接查看
可以在server端先自我测试一下是否可以连接。
命令:showmount [-ae] [hostname | IP]
-a:显示当前主机与客户端的NFS连接共享的状态
-e:显示某台主机的/etc/exports所共享的目录数据
---------------------------------------------------------------
4、在另一台机器上测试
出现返回 表示ok
[root@cPen_C ~]# showmount -e 192.168.255.28 #注:后面指定分析的机器的ip地址
Export list for 192.168.255.28: #注:出现这种情况,表示可以连接 连通性没有问题
/opt/www 192.168.255.0/24

5、挂载网络文件
在另外一台机器
[root@cPen_C ~]# mkdir /mnt2
[root@cPen_C ~]# mount -t nfs 192.168.255.28:/opt/www /mnt2 #注:挂载网络文件
[root@cPen_C ~]# df #注:使用df命令看磁盘挂载情况(这是网络磁盘挂载)
192.168.255.28:/opt/www 17811456 2165760 15645696 13% /mnt2

[root@cPen_C ~]# cd /mnt2
[root@cPen_C mnt2]# ls
index.html
[root@cPen_C mnt2]# cat index.html
this is test nfs
[root@cPen_C mnt2]# ls
index.html
[root@cPen_A ~]# cd /opt/www/ #注:服务端A主机 操作
[root@cPen_A www]# touch index2.html #注:服务端操作
[root@cPen_C mnt2]# ls
index2.html index.html
[root@cPen_C mnt2]# mkdir index2.html
mkdir: 无法创建目录"index2.html": 权限不够
#注:客户机C没有权限去操作服务器A文件
#没有权限的话 /etc/exports 设置 no_root_squash ,就可以操作了
no_root_squash 将远程根用户当成本地根用户,即不压制root
#注:让服务器将 远端的root用户 识别成 本地根用户
#注:修改配置文件后,需要使用exportfs -a 生效配置;再重新挂载(先取消挂载,再重新挂载)

#示例:取消挂载
[root@cPen_C mnt2]# cd .. #注:先退出mnt2目录,否则报繁忙
[root@cPen_C /]# umount /mnt2 #注:然后取消挂载

#修改配置文件后,需要使用exportfs -a 生效
#重新挂载:
先退出/mnt2目录,然后执行:
umount /mnt2
再执行挂载

二. npcbind和nfs

启动nfs之前,确保rpcbind服务已经启动
nfs启动的时候,会向rpcbind服务注册端口,会返回一个端口给nfs使用
可以使用
rpcinfo -p 命令 查看nfs端口是否已经注册成功
rpcbind就像房东,nfs是租客
#注:nfs自己不能起端口,rpcbind为nfs注册端口
#注:nfs起来之前 要确保rpcbind起来
[root@cPen_A www]# rpcinfo -p #注:rpcbind为nfs起2049端口
100003 3 tcp 2049 nfs


三. 实验:nfs和nginx 搭配使用 负载均衡和文件共享

A: 192.168.0.73 nfs 真实机
B: 192.168.0.79 代理机+真实机
C: 192.168.0.53 真实机

前提:
三台主机都安装好nginx,其中一台开启nfs服务
在A机器上开启了nfs服务,网站目录存放在/opt/www下,B、C可以挂载A的/opt/www目录到本地
A机器配置好nfs服务,开启nfs服务,然后在A、C两台上 执行:
[root@cPen_B ~]# mount -t nfs 192.168.0.73:/opt/www /opt/www
[root@cPen_C ~]# mount -t nfs 192.168.0.73:/opt/www /opt/www

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
B机器变成代理机
第1步:把8080端口起来
修改A、B、C三台主机nginx的配置,启动8080端口,绑定域名www.sc.com
#注:配置文件目录:/etc/nginx
#注:进入 /etc/nginx/conf.d目录下 创建 sc.conf
输入以下内容
[root@cPen_A ~]# cd /etc/nginx/
[root@cPen_A nginx]# cd conf.d/
[root@cPen_A conf.d]# vim sc.conf #注:后缀必须是 .conf,自动加载读取 该配置文件
server {
listen 8080; #注:监听8080端口
server_name www.sc.com; #注:指定域名

root /opt/www; #注:指定网站家目录,默认去家目录下找index.html

}
[root@cPen_A conf.d]# nginx -t #注:进行语法检测
[root@cPen_A conf.d]# nginx -s reload #注:生效配置
[root@cPen_A conf.d]# lsof -i:8080 #注:8080端口起来了
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 3193 root 6u IPv4 50354 0t0 TCP *:webcache (LISTEN)
nginx 3194 nginx 6u IPv4 50354 0t0 TCP *:webcache (LISTEN)
nginx 3195 nginx 6u IPv4 50354 0t0 TCP *:webcache (LISTEN)

使用lsof -i:8080 端口是否启动
使用curl -H "Host: www.sc.com" http://192.168.0.73:8080 查看返回的内容是否一致
访问192.168.0.68的8080端口,以www.sc.com域名去访问
[root@cPen_A ~]# curl -H "Host: www.sc.com" http://192.168.0.73:8080
this is test nfs
#注:三台机器访问返回的内容相同 都是 this is test nfs

--------------------------------------------------------------------------------------------
#注:在B机器上添加反向代理
对B机器进行负载均衡:把请求转发到其他机器(代理转发)(高可用)
[root@cPen_B conf.d]# vim sc.conf
server {
listen 8080;
server_name www.sc.com;

root /opt/www;

}

upstream sc { #注:代理转发,为upstream取名为sc
server 192.168.0.73:8080;
server 192.168.0.53:8080;
server 192.168.0.79:8080;
}
#注:虚拟主机,让不同的域名做不同的事情
server {
listen 80;
server_name www.sc.com;

root /opt/www;

location / { #注:访问网站的/目录,都会转发到sc
proxy_set_header Host $host; #注:头部信息,不加的话 可能变成B的主机地址
#注:让客户端发过来的头部host字段信息 和转发包的头部host字段保持一致
proxy_pass http://sc; #注:设置后端代理服务器
}
}

server {
listen 80;
server_name www.sc1.com;

root /usr/share/www;

location / {
proxy_set_header Host $host;
proxy_pass http://sc;
}
}

#注:输出www.sc.com时,请求时,会有数据包存放host头部信息:www.sc.com ,80端口收到这个包之后,知道是要转发到8080端口。B机器帮你请求A、B、C的8080端口发起请求
#注:客户机发起请求 www.sc.com,host头部信息www.sc.com(请求包里的,客户机携带的头部信息),B主机交给相应的server去处理,请求B主机的80端口去处理,转发到8080端口,B主机帮你去请求,B收到包的时候,交给server去处理。这时候A发起的这个包的头部信息 可能就是B的ip地址了(不是www.sc.com了),8080端口收到请求后,没有B的ip地址的server_name,会转发失败。所以 proxy_set_header Host $host 让它们的头部信息保持一致

--------------------------------------------------------------------------------------------
#生效配置
[root@cPen_B conf.d]# nginx -t
[root@cPen_B conf.d]# nginx -s reload

--------------------------------------------------------------------------------------------
#然后多次访问80端口,查看返回内容是否一致,以及查看每台机器的/var/log/nginx/access.log日志是否有新的请求过来 tail -f /var/log/nginx/access.log

[root@cPen_B conf.d]# curl -H "Host: www.sc.com" http://192.168.0.79
this is test nfs
#注:代理转发,访问80端口,转发到8080端口,分发到3台机器去执行了(负载均衡)

[root@cPen_C conf.d]# tail -f /var/log/nginx/access.log #注:C主机下 ,B主机帮请求
192.168.0.79 - - [01/Dec/2020:17:21:54 +0800] "GET / HTTP/1.0" 200 17 "-" "curl/7.61.1" "-"
#注:平均打到3台,默认做轮转

image-20221011191610524

#注:可以使用 killall nginx 全部杀死

#注:404 没找到,403 forbid 禁止

#注:A、B换成B、A
nginx代理机+真实机

A

nginx真实机

B nfs

nginx真实机

C

客户端访问www.sc.com 访问A nginx代理机的80端口(代理的意思是转发)
A机器 返回本机的8080去处理,或者B机器的8080去处理,或者C机器的8080去处理
负载均衡:2万人访问A机器80端口,80端口不会去处理请求,只是做代理转发,交给本机8080处理,或者B机器的8080处理,C机器的8080处理。A、B、C三台返回的页面网站 都要保持一致
把B机器/opt/www目录 挂载到A上和C上 (保存文件的一致性,改的都是B机器的nfs)

image-20221011191803355

#注:网络问题

桥接模式 VMnet0 同网段

NAT模式 VMnet8 同网段