NFS sersync rsync 服务 持续更新

架构图

在这里插入图片描述

服务器规划
192.168.233.129nfs客户端
192.168.233.130nfs服务端 rsync客户端 、sersync、rsyncd服务端
192.168.30.47nfs备用服务端 rsync客户端、sersync 、rsyncd服务端
NFS服务搭建

192.168.233.130

#安装nfs服务器会自动生成nfsnobody用户
yum -y install nfs-utils  (下载nfs-utils 会安装rpcbind服务)
#启动rpcbind服务
systemctl restart rpcbind
#设置开机自启
systemctl enable rpcbind
#启动nfs服务
systemctl restart nfs
#设置开机自启
systemctl enable nfs
#修改nfs配置文件
vim /etc/exports
/data/upload 192.168.233.0/24(rw,sync)
[root@localhost 202203]# exportfs -v
/data/upload  	192.168.233.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)
#创建共享目录
mkdir /data/upload
#授权nfsnobody用户对该目录有读写权限
chown -R nfsnobody.nfsnobody /data/upload
#查看系统默认配置参数
cat /var/lib/nfs/etab
/data/upload	192.168.233.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,no_all_squash)
#重新加载nfs配置文件 (也可restart)
exportfs -rv
#192.168.233.129测试能否挂载
showmount -e 192.168.233.130
Export list for 192.168.233.130:
/data/upload 192.168.233.0/24
nfs客户端挂载

临时挂载

mount -t nfs 192.168.233.130:/data/upload /usr/local/tomcat/webapps/ZunShangERP2-2.0/upload

在这里插入图片描述
永久挂载

echo "mount -t nfs 192.168.233.130:/data/upload /usr/local/tomcat/webapps/ZunShangERP2-2.0/upload" >>/etc/rc.local
[root@localhost ~]# tail -2 /etc/rc.local
echo never > /sys/kernel/mm/transparent_hugepage/enabled
mount -t nfs 192.168.233.130:/data/upload /usr/local/tomcat/webapps/ZunShangERP2-2.0/upload
chmod a+x /etc/rc.d/rc.local 
#/etc/fstab这种方式 在本机启动前确认nfs服务端能够提供nfs服务,否则该服务器重启会失败
192.168.233.130:/data/upload   /usr/local/tomcat/webapps/ZunShangERP2-2.0/upload   nfs  defaults    0 0

测试
129服务器初始数据
129服务器数据
130服务器初始数据
130服务器数据
#在192.168.233.129nfs客户端上修改 添加 删除文件等操作
#新建数据

cp 123.png 456.png

130服务器数据
在这里插入图片描述
129服务器删除数据
在这里插入图片描述
130服务器数据
在这里插入图片描述

rsync+sersync服务搭建

192.168.30.47服务器部署rsync服务端

  1. 安装rsyncd服务
yum -y install rsync
  1. 修改rsyncd配置文件
[root@localhost ~]# cat /etc/rsyncd.conf
uid = root
gid = root
fake super = yes #为模块设置“fake super = yes”会导致守护进程的行为就像--fake-super指定了命令行选项一样。这允许存储文件的完整属性,而不必让守护程序实际以 root 身份运行
use chroot = no #默认为true,修改为no,增加对目录文件软连接的备份
max connections = 200  #最大连接数 
timeout = 600  #设置超时时间
ignore errors #忽略I/O错误
read only = false #设置rsync服务端文件为读写权限
list = false #不显示rsync服务端资源列表
log file = /var/log/rsyncd.log  #日志文件位置,启动rsync后自动产生这个文件,无需提前创建

[upload]   #自定义模块名称
comment = upload #模块注释
auth users = rsync  #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开,也可不设置
secrets file = /etc/rsync.passwd #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件
path = /data/upload/ #rsync服务端数据目录路径
port=873 #默认端口
hosts allow = 服务器IP #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开,也可不设置
hosts deny = 服务器IP #禁止行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开,也可不设置
  1. 创建用户认证文件
vim /etc/rsync.passwd 
rsync:123456

#设置文件权限
chmod 600 /etc/rsync.passwd
useradd rsync -s /sbin/nologin -M #rsyncd 以rsync用户启动
setfacl -m u:rsync:rwx /etc/rsync.passwd  #

#启动rsync服务
systemctl restart rsyncd

192.168.233.130服务器rsync客户连接测试
在这里插入图片描述

rsync -avH --port=873 --progress /data/upload/* rsync@192.168.30.47::upload --password-file=/etc/rsync.passwd

192.168.233.130服务器部署sersync应用

  1. 下载sersync包
    github下载地址
curl -O http://8.210.62.122/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz
  1. 解压
tar xzf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
cd /usr/local/
mv GNU-Linux-x86 sersync
  1. 修改sersync配置文件
cp confxml.xml confxml.xml.bak
#需要修改的部分
[root@localhost sersync]# diff confxml.xml confxml.xml.bak 
15c15
< 	<createFile start="true"/>
---
> 	<createFile start="false"/>
19,20c19,20
< 	<attrib start="true"/>
< 	<modify start="true"/>
---
> 	<attrib start="false"/>
> 	<modify start="false"/>
24,25c24,25
< 	<localpath watch="/data/upload/">
< 	    <remote ip="192.168.30.47" name="upload"/>
---
> 	<localpath watch="/opt/tongbu">
> 	    <remote ip="127.0.0.1" name="tongbu1"/>
31,33c31,33
< 	    <auth start="true" users="rsync" passwordfile="/etc/rsync.passwd"/>
< 	    <userDefinedPort start="true" port="873"/><!-- port=874 -->
< 	    <timeout start="true" time="100"/><!-- timeout=100 -->
---
> 	    <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
> 	    <userDefinedPort start="false" port="874"/><!-- port=874 -->
> 	    <timeout start="false" time="100"/><!-- timeout=100 -->
36c36
< 	<failLog path="/usr/local/sersync/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
---
> 	<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->

#完整配置文件

[root@localhost sersync]# cat confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" port="8008"></host> <!-- 设置本地的ip与端口 -->
    <debug start="false"/> <!-- 开启debug模式 -->
    <fileSystem xfs="false"/> <!-- 开启xfs文件系统 -->
    <filter start="false"> <!--同步忽略推送的文件(正则表达式),默认关闭 -->
	<exclude expression="(.*)\.svn"></exclude>
	<exclude expression="(.*)\.gz"></exclude>
	<exclude expression="^info/*"></exclude>
	<exclude expression="^static/*"></exclude>
    </filter>
    <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/upload/"> <!-- 本地同步的目录路径 -->
	    <remote ip="192.168.30.47" name="upload"/> <!-- 远端rsyncd服务端IP,rsync模块名称 -->
	    <!--<remote ip="192.168.8.39" name="tongbu"/>-->
	    <!--<remote ip="192.168.8.40" name="tongbu"/>-->
	</localpath>
	<rsync>
	    <commonParams params="-artuz"/> <!-- rsync指令参数 -->
	    <auth start="true" users="rsync" passwordfile="/etc/rsync.passwd"/> <!-- rsync同步认证需要的用户与本地的存储的密码文件 -->
	    <userDefinedPort start="true" port="873"/><!-- port=874 --> <!-- 设置rsync服务端端口,与配置文件的模块端口对应,默认为关闭 -->
	    <timeout start="true" time="100"/><!-- timeout=100 --> <!-- 设置超时时间 -->
	    <ssh start="false"/> <!-- 开始ssh加密传输,开启需设置SSH加密证书 -->
	</rsync> 
	<failLog path="/usr/local/sersync/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> <!-- sersync传输失败日志脚本路径,每隔60会重新执行该脚本,执行完毕会自动清空 -->
	<crontab start="false" schedule="600"><!--600mins--> <!-- 设置rsync+crontab定时传输,默认关闭,当服务器或者客户端压力大时,可以开启。 -->
	    <crontabfilter start="false">
		<exclude expression="*.php"></exclude>
		<exclude expression="info/*"></exclude>
	    </crontabfilter>
	</crontab>
	<plugin start="false" name="command"/> <!-- 设置sersync传输后调用name指定的插件脚本,默认关闭 -->
    </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>

测试sersync同步命令

cd /data/upload/Pics/202203
cp 456.png 789.png
[root@localhost 202203]# /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
passwordfile is 	/etc/rsync.passwd
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/upload && rsync -artuz -R --delete ./  --port=873  --timeout=100 rsync@192.168.30.47::upload --password-file=/etc/rsync.passwd >/dev/null 2>&1 
run the sersync: 
watch path is: /data/upload

#查看进程
ps -ef|grep -v grep|grep sersync2
root     114774      1  0 01:48 ?        00:00:00 /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
  1. 基于inotify内核参数优化
vim /etc/sysctl.conf
fs.inotify.max_queued_events = 9999999
fs.inotify.max_user_watches = 9999999
fs.inotify.max_user_instances = 65535
[root@localhost scripts]# sysctl -p
fs.inotify.max_queued_events = 9999999  #事件队列可以容纳的事件
fs.inotify.max_user_watches = 9999999 #设置每个用户可以运行的进程数量
fs.inotify.max_user_instances = 65535 #监控最多的文件
  1. 设置开机自启
echo "/usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml" >> /etc/rc.local
chmod a+x /etc/rc.local
  1. 定时检测sersync2进程存活
    脚本地址
[root@vpn-wiki scripts]# cat check_rsync2.sh 
#!/usr/bin/bash

#Tate:2022.3.28
#Author:Yingjian
#function: 定时检测sersync2进程存活
#env
Date2=`date +%F`
Date=`date +%Y%m%d_%H_%M`
pidlist=`ps -ef| grep -v grep |grep sersync2 |awk '{print $2}'`
if [ "$pidlist" != "" ];then
echo "$Date Sersync2进程存活 进程ID为$pidlist" >>/var/log/${Date2}check_rsync.log
else
echo "$Date Sersync2进程不存在,正在启动" >>/var/log/${Date2}check_rsync.log
killall sersync2 || /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
fi

定时清理日志脚本

[root@localhost scripts]# cat clean_rsync2.sh 
#!/usr/bin/bash

#Tate:2022.3.28
#Author:Yingjian
#function: 定时清理check_rsync2日志文件
find /var/log/*check_rsync.log -mtime 7 -exec rm -rf {} \;

计划任务

crontab -l
* * * * * /usr/bin/bash /data/scripts/check_rsync2.sh
5 0 * * * /usr/bin/bash /data/scripts/clean_rsync2.sh

服务器192.168.30.47rsync客户端同步192.168.233.130rsyncd服务端 按照如上再配置一次

模拟NFS单节点故障
  1. 192.168.30.47服务器开启nfs服务
    查看本文192.168.233.130NFS服务搭建
    web服务器192.168.233.129查看192.168.30.47NFS服务能否连接
[root@localhost upload]# showmount -e 192.168.30.47
Export list for 192.168.30.47:
/data/upload 192.168.233.0/24
  1. 关闭192.168.233.130NFS服务
systemctl stop nfs
  1. 192.168.233.129NFS客户端关闭对130NFS服务连接
umount -lf /usr/local/tomcat/webapps/ZunShangERP2-2.0/upload
#开启对30.47NFS服务器/data/upload目录挂载
[root@localhost ~]# mount -t nfs 192.168.30.47:/data/upload /usr/local/tomcat/webapps/ZunShangERP2-2.0/upload
mount.nfs: access denied by server while mounting 192.168.30.47:/data/upload

访问被拒绝
查看30.47nfs挂载日志
在这里插入图片描述
挂载被拒绝 因为在/etc/exports上没有开放192.168.30.46 这个ip是我的宿主机IP,192.168.233.129这台web服务器是我的vaware机,并且配置的为NAT网卡,而192.168.30.47服务配置的是桥接网卡,所以在访问这个30.47的服务时,会由30.46宿主机为网管转发请求。
重新修改30.46NFS配置文件

[root@localhost test]# cat /etc/exports
/data/upload 192.168.30.46(rw,sync)
[root@localhost data]# exportfs -rv
exporting 192.168.30.46:/data/upload

192.168.233.129tomcat服务器重新mount挂载

showmount -e 192.168.30.47
Export list for 192.168.30.47:
/data/upload 192.168.30.46
[root@localhost ~]# mount -t nfs 192.168.30.47:/data/upload /usr/local/tomcat/webapps/ZunShangERP2-2.0/upload
[root@localhost ~]# df -Th
文件系统                   类型      容量  已用  可用 已用% 挂载点
devtmpfs                   devtmpfs  979M     0  979M    0% /dev
tmpfs                      tmpfs     991M     0  991M    0% /dev/shm
tmpfs                      tmpfs     991M  9.7M  981M    1% /run
tmpfs                      tmpfs     991M     0  991M    0% /sys/fs/cgroup
/dev/mapper/centos-root    xfs        17G  6.7G   11G   40% /
/dev/sda1                  xfs      1014M  137M  878M   14% /boot
tmpfs                      tmpfs     199M     0  199M    0% /run/user/0
192.168.30.47:/data/upload nfs4       17G  2.6G   15G   15% /usr/local/tomcat/webapps/ZunShangERP2-2.0/upload

在129web服务器上进行创建、删除等操作
在这里插入图片描述
在30.47nfs back服务器上查看数据
在这里插入图片描述
129上拷贝生成新的数据
在这里插入图片描述
30.47上同步生成新的数据
在这里插入图片描述
4. 129web服务器定时检测130nfs服务器存活脚本
脚本地址

[root@localhost scripts]# cat check_130nfs.sh 
#!/usr/bin/bash

#Tate:2022.3.28
#Author:Yingjian
#function: 定时检测130nfs服务器存活
#env
Date2=`date +%F`
Date=`date +%Y%m%d_%H_%M`
IP=130

ping -c 1 192.168.233.$IP  &>/dev/null
if [ $? -eq 0 ];then
echo "$Date 130nfs服务存活 " >>/var/log/${Date2}check_130nfs.log
else
echo "$Date 130nfs服务宕机 正在切换到nfs 备份服务器" >>/var/log/${Date2}check_130nfs.log
umount -lf /usr/local/tomcat/webapps/ZunShangERP2-2.0/upload && mount -t nfs 192.168.30.47:/data/upload /usr/local/tomcat/webapps/ZunShangERP2-2.0/upload
fi

定时清理日志脚本

[root@localhost scripts]# cat clean_130nfs.sh 
#!/usr/bin/bash

#Tate:2022.3.28
#Author:Yingjian
#function: 定时清理check_rsync2日志文件
find /var/log/*check_130nfs.log -mtime 7 -exec rm -rf {} \;
总结

配置的时候注意路径,出现问题先看日志。
本文参考:https://www.csdn.net/tags/MtTaEg3sMDkwNjkxLWJsb2cO0O0O.html
本文参考:https://blog.csdn.net/yosigo_/article/details/116034030
本文参考:https://www.cnblogs.com/zhuxiangru/p/10716904.html
本文参考:https://www.bilibili.com/read/cv4093007
本文参考:https://blog.csdn.net/weixin_46837396/article/details/114034551
本文参考:https://blog.csdn.net/mr_rsq/article/details/79273002
本文参考:https://blog.csdn.net/vic_qxz/article/details/80537264

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值