ICode9

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

nfs 实时同步与单点故障解决

2020-08-14 19:00:54  阅读:240  来源: 互联网

标签:www data rsync 实时 nfs web01 root 单点故障


一、统一用户

1.httpd
2.NFS挂载目录

1.服务器创建统一用户

[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666

2.修改httpd用户

[root@web01 ~]# vim /etc/httpd/conf/httpd.conf
User www
Group www

#重启服务
[root@web01 ~]# systemctl restart httpd
[root@web01 ~]# ps -ef | grep httpd
root      31537      1  1 21:57 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
www       31538  31537  0 21:57 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
www       31539  31537  0 21:57 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
www       31540  31537  0 21:57 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
www       31541  31537  0 21:57 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
www       31542  31537  0 21:57 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
root      31544  18601  0 21:57 pts/1    00:00:00 grep --color=auto httpd

3.修改NFS服务端权限

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

#重启
[root@nfs /data]# systemctl restart nfs

1597388307766

4.NFS服务端重新授权目录

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

5.web端重新挂载

[root@web01 /var/www/html]# umount /var/www/html/upload
[root@web01 /var/www/html]# mount -t nfs 172.16.1.31:/data ./upload

二、NFS总结

1.NFS存储优点

1.简单易用,部署方便
2.数据可查,服务稳定

2.NFS服务缺点

1.存在单点故障, 如果构建高可用维护麻烦web->nfs()->backup
2.NFS数据明文, 并不对数据做任何校验。
3.客户端挂载NFS服务没有密码验证, 安全性一般(内网使用)

3.NFS应用建议

1.开机挂载
    如果希望NFS文件共享服务能一直有效,则需要将其写入到fstab文件中
    #编辑fstab文件
    [root@nfs-client ~]# vim /etc/fstab
    172.16.1.31:/data /nfsdir nfs defaults 0 0
    #验证fstab是否写正确
    [root@nfs-client ~]# mount -a
    
2.生产场景应将静态数据尽可能往前端推,减少后端存储压力
3.必须将存储里的静态资源通过CDN缓存,jpg\png\mp4\avi\css\js
4.如果没有缓存或架构本身历史遗留问题太大, 在多存储也无用

1597390707752

三、Rsync+NFS解决单点故障

1.准备环境

主机 角色 IP
backup rsync服务端,NFS服务端 172.16.1.41
nfs rsync客户端,NFS服务端 172.16.1.31
web01 rsync客户端,NFS客户端 172.16.1.7,10.0.0.7

1597392640005

2.web01搭建上传作业平台

1)安装httpd和php

[root@web01 ~]# yum install -y httpd php

2)上传代码

[root@web01 ~]# rz kaoshi.zip
[root@web01 ~]# yum unzip
[root@web01 ~]# unzip kaoshi.zip -d /var/www/html/ 

3)创建httpd用户

[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666

4)配置httpd

[root@web01 ~]# vim /etc/httpd/conf/httpd.conf
User www
Group www

#启服务
[root@web01 ~]# systemctl start httpd

5)修改权限

[root@web01 ~]# chown -R www.www /var/www/html/

3.nfs服务器搭建nfs服务端

1)安装NFS

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

2)配置NFS

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

3)查看配置生效

[root@backup ~]# 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)

4)创建用户

[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666

5)创建目录

[root@web01 ~]# mkdir /data
[root@web01 ~]# chown -R www.www /data

4.挂载web01数据目录

[root@web01 ~]# showmount -e 172.16.1.31

[root@web01 ~]# mount -t nfs 172.16.1.31:/data /var/www/html/upload

5.backup搭建rsync服务端

1)安装

2)配置

[root@backup ~]# cat /etc/rsyncd.conf 
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = true
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[backup]
comment = "文件备份目录"
path = /backup

[data]
comment = "数据备份目录"
path = /data

3)重启rsyncd

[root@backup ~]# systemctl restart rsyncd

6.NFS数据实时备份至backup服务器

0)安装inotify

[root@nfs ~]# yum -y install inotify-tools

1)编写脚本实时备份data目录

[root@nfs /data]# vim /scripts/backup_data.sh
#!/bin/bash
dir=/data
export RSYNC_PASSWORD=123456
/usr/bin/inotifywait  -mrq  --format '%w %f' -e create,delete,attrib,close_write  $dir | while read line;do
        cd  $dir  && rsync -az -R  --delete  .  rsync_backup@172.16.1.41::data >/dev/null 2>&1
done  &

2)启动脚本

[root@nfs /data]# sh /scripts/backup_data.sh

3)测试

1.访问交作业页面,上传图片
2.查看web服务器
[root@web01 ~]# ll /var/www/html/upload
3.查看nfs服务器data目录
[root@nfs ~]# ll /data/
4.查看backup服务器data目录
[root@backup ~]# ll /data/

7.backup服务器搭建NFS服务端

1)安装NFS

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

2)配置nfs

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

3)创建用户

[root@backup ~]# groupadd www -g 666
[root@backup ~]# useradd www -u 666 -g 666

4)启动服务

[root@backup ~]# systemctl start nfs

8.测试

1)nfs出现故障

[root@nfs ~]# systemctl stop nfs

2)切换挂载机器

[root@web01 ~]# umount -lf /var/www/html/upload
[root@web01 ~]# mount -t nfs 172.16.1.41:/data /var/www/html/upload

四、实时同步

1.什么实时同步

实时同步是一种只要当前目录发生变化则会触发一个事件,事件触发后会将变化的目录同步至远程服务器

2.为什么实时同步

保证数据的连续性, 减少人力维护成本,解决nfs单点故障

3.实时同步工具选择

sersync+RSYNC(√)、inotify+rsync

Inotify是一个通知接口,用来监控文件系统的各种变化,如果文件存取,删除,移动。可以非常方便地实现文件异动告警,增量备份,并针对目录或文件的变化及时作出响应。rsync+inotify可以实触发式实时同步增量备份

sersync是国人基于rsync+inotify-tools开发的工具,不仅保留了优点同时还强化了实时监控,文件过滤,简化配置等功能,帮助用户提高运行效率,节省时间和网络资源。

4.安装sersync(NFS服务器)

1)安装rsync和inotify

[root@nfs01 ~]# yum install rsync inotify-tools -y

2)下载sersync包

[root@nfs01 ~]# wget https://raw.githubusercontent.com/wsgzao/sersync/master/sersync2.5.4_64bit_binary_stable_final.tar.gz

3)解压安装包

[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz

4)移动并改名

[root@nfs ~]# mv GNU-Linux-x86 /usr/local/sersync

5)配置

[root@nfs ~]# cat /usr/local/sersync/confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
	#主机IP
    <host hostip="localhost" port="8008"></host>
    #调试模式
    <debug start="false"/>
    <fileSystem xfs="false"/>
    #文件过滤
    <filter start="false">
	<exclude expression="(.*)\.svn"></exclude>
	<exclude expression="(.*)\.gz"></exclude>
	<exclude expression="^info/*"></exclude>
	<exclude expression="^static/*"></exclude>
    </filter>
    #inotify监控配置
    <inotify>
    #inotify监控的行为
	<delete start="true"/>
	<createFolder start="true"/>
	<createFile start="true"/>
	<closeWrite start="true"/>
	<moveFrom start="true"/>
	<moveTo start="true"/>
	<attrib start="true"/>
	<modify start="true"/>
    </inotify>
	#推送部分
    <sersync>
    #本地监控的目录
	<localpath watch="/data">
		#远程IP及模块名字
	    <remote ip="172.16.1.41" name="data"/>
	</localpath>
	<rsync>
		#rsync同步时的参数
	    <commonParams params="-artuz"/>
	    #开启认证
	    <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
	    #如果rsync服务不是873端口,需要开启
	    <userDefinedPort start="false" port="874"/><!-- port=874 -->
	    #超时时间
	    <timeout start="false" time="100"/><!-- timeout=100 -->
	    <ssh start="false"/>
	</rsync>
	#指定错误日志
	<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
	#定时任务,默认600分钟进行一次全备
	<crontab start="false" schedule="600"><!--600mins-->
		#定时任务文件过滤
	    <crontabfilter start="false">
		<exclude expression="*.php"></exclude>
		<exclude expression="info/*"></exclude>
	    </crontabfilter>
	</crontab>
	<plugin start="false" name="command"/>
    </sersync>

    <plugin name="command">
	<param prefix="/bin/sh" suffix="" ignoreError="true"/>	<!--prefix /opt/tongbu/mmm.sh suffix-->
	<filter start="false">
	    <include expression="(.*)\.php"/>
	    <include expression="(.*)\.sh"/>
	</filter>
    </plugin>

    <plugin name="socket">
	<localpath watch="/opt/tongbu">
	    <deshost ip="192.168.138.20" port="8009"/>
	</localpath>
    </plugin>
    <plugin name="refreshCDN">
	<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
	    <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
	    <sendurl base="http://pic.xoyo.com/cms"/>
	    <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
	</localpath>
    </plugin>
</head>
[root@nfs ~]#

6)创建密码文件

[root@nfs ~]# echo 123456 > /etc/rsync.password
[root@nfs ~]# chmod 600 /etc/rsync.password

7)启动

#查看参数
[root@nfs ~]# /usr/local/sersync/sersync2 -h
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
_______________________________________________________
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
c参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序

[root@nfs ~]# /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml 
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d 	run as a daemon
option: -r 	rsync all the local files to the remote servers before the sersync work
option: -o 	config xml name:  /usr/local/sersync/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost	host port: 8008
will ignore the inotify createFile event 
daemon start,sersync run behind the console 
use rsync password-file :
user is	rsync_backup
passwordfile is 	/etc/rsync.password
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) 
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /data && rsync -artuz -R --delete ./ rsync_backup@172.16.1.41::data --password-file=/etc/rsync.password >/dev/null 2>&1 
run the sersync: 
watch path is: /data

作业:

1.恢复快照
2.搭建交作业系统
3.配置挂载数据目录
4.nfs的数据目录实时备份到backup服务器(要求使用sersync)

标签:www,data,rsync,实时,nfs,web01,root,单点故障
来源: https://www.cnblogs.com/xiaolang666/p/13503936.html

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

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

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

ICode9版权所有