随着业务的迭代,系统载荷也在增大,曾经一台机器打天下再也满足不了现在环境的需求。我们不得不考虑如何降低系统的负荷,以保证业务的正常的进行。以文件服务器为例,我们使用nfs+rsync实现服务器间的文件同步,多台机器分摊压力,降低文件读写对服务器的压力。
文件同步架构图:
文件同步架构图.png
从架构图看,其实并没有主从机器的概念,因为每台服务器的文件发生变更,都意味着会同步一份到其他机器。
一、实现NFS挂载(该步骤全程需要root权限)
- 安装NFS:
yum -y install nfs-utils rpcbind
- 配置NFS共享目录:
vi /etc/exports
配置可挂载的ip,参考如下(“/data/sharedata”是文件服务器的共享目录,后面的IP是可挂载该文件服务器的机器IP):
/data/sharedata 10.1.132.24(rw,sync,no_wdelay,no_root_squash,anonuid=1001,anongid=1001)
/data/sharedata 10.1.132.6(rw,sync,no_wdelay,no_root_squash,anonuid=1001,anongid=1001)
- 配置NFS服务开机自启:
systemctl start rpcbind && systemctl start nfs
systemctl enable rpcbind && systemctl enable nfs
- 测试挂载:
mount -t nfs 10.1.132.24:/file_serve /sharedata
df -Th
umount /file_serve
二、配置主服务器
- 安装rsync:
yum -y install rsync.x86_64
- 修改rsyncd.conf文件内容:
- 执行如下指令:
vi /etc/rsyncd.conf
- 文件内容参考:
uid = juque
gid = dev94
port = 873
use chroot = no
max connections = 100
log file = /var/log/rsyncd.log
exclude = lost+found/
transfer logging = yes
timeout = 900
read only = false
list = false
fake super = yes
ignore nonreadable = yes
ignore errors
[data]
path = /sharedata
auth users = juque
secrets file = /etc/rsync_master.pass
hosts allow = 192.168.1.10
# dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
# [ftp]
# path = /home/ftp
# comment = ftp export area
文件注解如下:
uid、gid必须调整为普通用户、普通组,否则会导致使用root权限同步文件;
auth users:同步文件使用的普通用户;
log file:同步文件的日志,可检查同步是否异常;
secrets file: 放置密码,格式参考如下:
juque:pass123
rsync_master.pass 需要赋权,否则提示文件无权限:
chmod 600 /etc/rsync_master.pass
hosts allow:允许同步的ip,多个ip逗号分隔;
- 启动服务:
rsync --daemon --config=/etc/rsyncd.conf
- 配置自动同步:
- 上传 sersync2.5.4_64bit_binary_stable_final.tar.gz
- 解压:
tar xvf sersync2.5.4_64bit_binary_stable_final.tar.gz
- 复制目标文件夹到目录:/usr/local/
mv GNU-Linux-x86/ sersync
-
修改配置文件:
文件参考:
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="10.129.0.14" 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>
<delete start="false"/>
<createFolder start="true"/>
<createFile start="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>
<sersync>
<localpath watch="/sharedata">
<remote ip="10.129.0.75" name="data"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-az"/>
<auth start="true" users="admin" passwordfile="/etc/rsync.pass"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="true" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/var/log/rsync_fail.log" timeToExecute="60"/><!--default every 60mins execute once-->
<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>
</head>
文件讲解:
13行:改为false,不同步删除操作;
24行:watch对应本机的共享目录;
25行:ip对应远程服务器的ip,name对应远程服务器配置文件的[data];
30行:param改为:-az;
31行:调整为true,passwordfile对应的文件不是:rsync_master.pass,其格式只有密码没有用户,参考:
36行:修改日志文件的后缀;
- 启动自动同步服务:
/usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
三、配置备份服务器(多台服务器配置相同)
- 安装rsync及同步服务,参考主服务器的配置;
- 测试文件同步:
- 停用自动同步进程;
- 在目录/sharedata目录添加任意文件;
- 执行同步指令:
rsync -arv /sharedata/ gientech@主服务器IP::data --password-file=/etc/rsync.pass
解释:--password-file 对应 configxml.xml文件的 /etc/rsync.pass;
- 期望其他服务器同步生成文件;
- 启动自动同步进程:
/usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
- 测试自动同步:
- 在同步目录创建任意文件;
- 检查其他文件服务器是否存在该文件;
四、实现高可用
- 使用虚拟IP映射到集群,实现对集群的轮询访问;
- 使用域名,把集群的ip挂载到域名;
- nginx反向代理nfs;