ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

NFS共享存储

2022-05-19 01:33:19  阅读:143  来源: 互联网

标签:httpd 存储 00 html NFS www nfs 共享 root


NFS共享存储

目录

环境准备

主机名 WanIP LanIP 角色
web01 10.0.0.7 172.16.1.7 网站服务(nfs客户端)
web02 10.0.0.8 172.16.1.8 网站服务(nfs客户端)
nfs 10.0.0.31 172.16.1.31 共享存储(nfs服务端)

NFS概述

  • NFS(Network File System)通过网络来做文件存储
  • NFS用于企业集群架构中, 如果是大型网站, 会用到更复杂的分布式文件系统FastDFS,glusterfs,HDFS

为什么使用共享存储

  • 1.实现多台服务器之间数据共享
  • 2.实现多台服务器之间数据一致

NFS应用场景

  • 集群没有共享存储

    • A用户上传图片经过负载均衡,负载均衡将上传请求调度至WEB1服务器上。
    • B用户访问A用户上传的图片,此时B用户被负载均衡调度至WEB2上,因为WEB2上没有这张图片,所以B用户无法看到A用户传的图片。

  • 集群有共享存储

    • A用户上传图片无论被负载均衡调度至WEB1还是WEB2, 最终数据都被写入至共享存储
    • B用户访问A用户上传图片时,无论调度至WEB1还是WEB2,最终都会上共享存储访问对应的文件,这样就可以访问到资源了

NFS工作原理

1.用户进程访问NFS客户端,使用不同的函数对数据进行处理
2.NFS客户端通过TCP/IP的方式传递给NFS服务端。
3.NFS服务端接收到请求后,会先调用portmap进程进行端口映射。
4.nfsd进程用于判断NFS客户端是否拥有权限连接NFS服务端。
5.Rpc.mount进程判断客户端是否有对应的权限进行验证。
6.idmap进程实现用户映射和压缩
7.最后NFS服务端会将对应请求的函数转换为本地能识别的命令,传递至内核,由内核驱动硬件。

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

NFS 配置详解

  • 执行man exports命令,然后切换到文件结尾,可以快速查看如下样例格式:
nfs共享参数 参数作用
rw* 读写权限
ro 只读权限
root_squash 当NFS客户端以root管理员访问时,映射为NFS服务器的匿名用户(不常用)
no_root_squash 当NFS客户端以root管理员访问时,映射为NFS服务器的root管理员(不常用)
all_squash 无论NFS客户端使用什么账户访问,均映射为NFS服务器的匿名用户(常用)
no_all_squash 无论NFS客户端使用什么账户访问,都不进行压缩
sync* 同时将数据写入到内存与硬盘中,保证不丢失数据
async 优先将数据保存到内存,然后再写入硬盘;这样效率更高,但可能会丢失数据
anonuid* 配置all_squash使用,指定NFS的用户UID,必须存在系统
anongid* 配置all_squash使用,指定NFS的用户UID,必须存在系统

安装部署NFS服务端

## C5 C6安装nfs服务
[root@nfs ~]# yum install -y nfs rpcbind

## C7安装nfs服务
[root@nfs ~]# yum install -y nfs-utils

## 配置NFS服务端
NFS配置文件的位置
[root@nfs ~]# ll /etc/exports
-rw-r--r--. 1 root root 0 Jun 7 2013 /etc/exports

[root@nfs ~]# vim /etc/exports
# 共享目录 允许访问NFS服务端的网段 (可读可写,同步,任何用户都映射成nfs的匿名用户)
/data 172.16.1.0/24(rw,sync,all_squash)


## 创建共享目录
[root@nfs ~]# mkdir /data

## 修改共享目录的属主和属组为nfs的匿名用户
[root@nfs ~]# chown nfsnobody.nfsnobody /data

## 启动服务
[root@nfs ~]# systemctl start nfs

## 加入开机自启
[root@nfs ~]# systemctl enable nfs

## 检查进程
[root@nfs ~]# ps -ef|grep nfs


## 检测配置文件是否生效
[root@nfs ~]# cat /var/lib/nfs/etab
/data
172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check
,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,all_squash)

客户端操作

# 1.安装nfs
[root@web01 ~]# yum install -y nfs-utils
[root@web02 ~]# yum install -y nfs-utils

# 2.查看哪些目录可以挂载
[root@web01 ~]# showmount -e 172.16.1.31
[root@web02 ~]# showmount -e 172.16.1.31

[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24


## 3.挂载共享目录
[root@web01 ~]# mount -t nfs 172.16.1.31:/data /opt
[root@web02 ~]# mount -t nfs 172.16.1.31:/data /opt

NFS共享存储实战

环境配置

主机名 WanIP LanIP 角色 部署应用
web01 10.0.0.7 172.16.1.7 网站服务(nfs客户端) httpd、php、nfs
web02 10.0.0.8 172.16.1.8 网站服务(nfs客户端) httpd、php、nfs
nfs 10.0.0.31 172.16.1.31 共享存储(nfs服务端) nfs

部署交作业网站

# 1.安装apache和php
[root@web01 ~]# yum install -y httpd php
[root@web02 ~]# yum install -y httpd php

# 2.下载代码(到站点目录下)
[root@web01 html]# ll
-rw-r--r-- 1 root root 26927 May 18 09:33 kaoshi.zip
[root@web01 html]# pwd
/var/www/html

# 3.解压代码
[root@web01 html]# unzip kaoshi_modify.zip
[root@web02 html]# unzip kaoshi_modify.zip

# 4.启动apache服务
[root@web01 html]# systemctl start httpd
[root@web02 html]# systemctl start httpd

# 5.检查进程
[root@web01 html]# ps -ef|grep 'httpd'
[root@web02 html]# ps -ef|grep 'httpd'

[root@web02 html]# ps -ef|grep [h]ttpd
root       7633      1  0 11:58 ?        00:00:04 /usr/sbin/httpd -DFOREGROUND
apache     7634   7633  0 11:58 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     7635   7633  0 11:58 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     7636   7633  0 11:58 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     7637   7633  0 11:58 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     7638   7633  0 11:58 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     7665   7633  0 12:02 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     7666   7633  0 12:02 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     7667   7633  0 12:02 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND


# 6.端口检查
[root@web01 html]# netstat -lntup|grep httpd
tcp6       0      0 :::80                   :::*              LISTEN      7633/httpd    
[root@web02 html]# netstat -lntup|grep httpd
tcp6       0      0 :::80                   :::*              LISTEN      7633/httpd    

# 7.打开浏览器访问
http://10.0.0.7/
http://10.0.0.8/

# 8.授权站点目录
chown apache.apache /var/www/html


# 查看结果
[root@web01 html]# ll user_data
total 84
-rw-r--r-- 1 apache apache 83411 May 18 21:25 12_kobe.jpeg

[root@web02 html]# ll user_data
total 64
-rw-r--r-- 1 apache apache 63208 May 18 12:10 11_ok.jpg

给交作业网站部署共享存储(统一用户)

  • apache和nfs统一使用www用户uid是666 gid也是666

1.先部署nfs的服务端

# 创建用户和用户组 www用户uid是666 gid也是666
[root@nfs ~]# groupadd www -g 666
[root@nfs ~]# useradd www -u 666


# C5 C6安装nfs服务
[root@nfs ~]# yum install -y nfs rpcbind

# C7安装nfs服务
[root@nfs ~]# yum install -y nfs-utils


# 配置NFS服务端
NFS配置文件的位置
[root@nfs ~]# ll /etc/exports
-rw-r--r--. 1 root root 0 Jun 7 2013 /etc/exports

[root@nfs ~]# vim /etc/exports
# 共享目录 允许访问NFS服务端的网段 (可读可写,同步,指定用户映射成nfs的匿名用户)
/data 172.16.1.0/24(rw,sync,anonuid=666,anongid=666,all_squash)

# 创建共享目录
[root@nfs ~]# mkdir /data

# 修改共享目录的属主和属组为nfs的匿名用户
[root@nfs ~]# chown www.www /data

# 启动服务
[root@nfs ~]# systemctl start nfs

# 加入开机自启
[root@nfs ~]# systemctl enable nfs

# 检查进程
[root@nfs ~]# ps -ef|grep nfs

## 检测配置文件是否生效
[root@nfs ~]# cat /var/lib/nfs/etab
/data	172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=666,anongid=666,sec=sys,rw,secure,root_squash,all_squash)


2.部署客户端

# 1.创建用户和用户组 www用户uid是666 gid也是666
[root@web01 html]# groupadd www -g 666
[root@web01 html]# useradd www -u 666 -g 666

[root@web02 html]# groupadd www -g 666
[root@web02 html]# useradd www -u 666 -g 666


# 2.修改站点目录的属主和属组为nfs的匿名用户
[root@web01 html]# chown www.www /var/www/html/
[root@web01 html]# ll /var/www/html -d
drwxr-xr-x 3 www www 119 May 18 21:25 /var/www/html

[root@web02 html]# chown www.www /var/www/html/


# 3.修改apache服务的配置文件里的匿名用户
[root@web01 html]# vim /etc/httpd/conf/httpd.conf
User www
Group www


[root@web02 html]# vim /etc/httpd/conf/httpd.conf
User www
Group www

# 4.重启apache服务
[root@web01 html]# systemctl restart httpd
[root@web02 html]# systemctl restart httpd

# 5.检查进程
[root@web01 html]# ps -ef|grep 'httpd'
[root@web02 html]# ps -ef|grep 'httpd'

[root@web02 html]# ps -ef|grep [h]ttpd
root       7633      1  0 11:58 ?        00:00:04 /usr/sbin/httpd -DFOREGROUND
apache     7634   7633  0 11:58 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     7635   7633  0 11:58 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     7636   7633  0 11:58 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     7637   7633  0 11:58 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     7638   7633  0 11:58 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     7665   7633  0 12:02 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     7666   7633  0 12:02 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     7667   7633  0 12:02 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND


# 6.端口检查
[root@web01 html]# netstat -lntup|grep httpd
tcp6       0      0 :::80                   :::*              LISTEN      7633/httpd    
[root@web02 html]# netstat -lntup|grep httpd
tcp6       0      0 :::80                   :::*              LISTEN      7633/httpd


# 7.打开浏览器访问
http://10.0.0.7/
http://10.0.0.8/


# 8.创建用户数据上传目录
[root@web01 html]# mkdir /var/www/html/user_data/
[root@web02 html]# mkdir /var/www/html/user_data/

# 9.客户端挂载nfs服务端
[root@web01 html]# mount -t nfs 172.16.1.31:/data /var/www/html/user_data/
[root@web02 html]# mount -t nfs 172.16.1.31:/data /var/www/html/user_data/

# 浏览器访问,并上传查看是否同步共享
[root@web01 html]# ll user_data/
total 148
-rw-r--r-- 1 www www 63208 May 18 22:20 11_ok.jpg
-rw-r--r-- 1 www www 83411 May 18 22:09 12_kobe.jpeg

[root@web01 html]# ll user_data/
total 148
-rw-r--r-- 1 www www 63208 May 18 22:20 11_ok.jpg
-rw-r--r-- 1 www www 83411 May 18 22:09 12_kobe.jpeg

标签:httpd,存储,00,html,NFS,www,nfs,共享,root
来源: https://www.cnblogs.com/chenguobao/p/16287004.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有