rsync实现负载均衡集群文件同步,搭建线上测试部署环境
此次测试使用三台服务器,CentOS7(1511)版本:
192.168.254.1 web服务器+线上测试环境+源站
192.168.254.2 web服务器
192.168.254.3 web服务器
其中2 3 服务器作为集群中的web服务器,对外开放,是负载均衡集群的部分。
其中1 服务器不对外开放,代码发布到该服务器,在该服务器上进行测试,完成后程序由该服务器同步到其他集群服务器上,同时网站后台以及自动脚本位于该服务器上。(如果为了安全期间,大家可以对该服务器进行配置,只允许你们公司内部网络访问,在家时通过VPN连接内部网络,这样就可以确保后台安全)
这里讲两个场景供参考
1. 通过shell脚本,实现文件夹实时同步
2. 在1服务器上制定一个规则,即只要rsync.txt存在我们就开始同步,这样只要开发上传该文件同步就开始,同步完成后自动删除该文件。
第一步:安装rsync
centos 7(1511)版本,已经默认安装了rsync,所以就不再进行安装,未安装的可以自行安装,过程相对简单。
第二步:安装inotify和inotify-tools
Centos 7已经默认安装了inotify,如果要查看是否安装可以使用如下命令
ll /proc/sys/fs/inotify
如果列出如下三项,则证明已经安装
-rw-r--r-- 1 root root 0 2月 1 13:59 max_queued_events
-rw-r--r-- 1 root root 0 2月 1 13:59 max_user_instances
-rw-r--r-- 1 root root 0 2月 1 13:59 max_user_watches
没有安装的可以自行安装。
然后需要安装inotify-tools工具
tar -zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make && make install
第三步:对客户端和服务端进行配置
记住1是客户端,2 3 是需要同步的服务端。是将1的文件同步到2 3上面
首先对 2 3 进行配置,相应配置文件 /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict mode = no
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /usr/data/rsync/rsyncd.log
[laiwojia-data]
path = /usr/website/html/www.laiwojia.la/data/
comment = web4 files
ignore errors
read only = no
write noly = no
hosts allow = 192.168.254.1
hosts deny = *
list = false
uid = root
gid = root
auth users = root
secrets file = /usr/local/rsync/conf/server.pass
[laiwojia]
path = /usr/website/html/www.laiwojia.la/
comment = web4 files
ignore errors
read only = no
write noly = no
hosts allow = 192.168.254.1
hosts deny = *
list = false
uid = root
gid = root
auth users = root
secrets file = /usr/local/rsync/conf/server.pass
这里配置了两个模块 'laiwojia-data'和'laiwojia' ,其中'laiwojia-dada'只要发生变化就要同步的(想想大家把后台放在1上面,那么这个data里面的文件就是后台生成的持久化文件,后台配置,然后整个集群通用),而'laiwojia'了这个模块是发布系统所用的模块,需要在站点下有'rsync.txt'文件时才同步。
从上面的配置可以看出,'/usr/website/html/www.laiwojia.la/data/'这个目录必须存在, '/usr/local/rsync/conf/server.pass'这个密码文件也必须存在。
# mkdir -p /usr/website/html/www.laiwojia.la/data/
# mkdir -p /usr/local/rsync/conf/
密码文件/usr/local/rsync/conf/server.pass中的内容为
root:123abc+-
记住2 3 作为服务端 密码文件要有前缀,如果写成 '123abc+-' 后面就会不通过
给密码文件设置访问权限:
# chmod 600 server.pass
然后我们对1进行配置,这里贴出配置文件
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict mode = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /usr/data/rsync/rsyncd.log
建立目录:
# mkdir -p /usr/website/html/www.laiwojia.la/data/
# mkdir -p /usr/local/rsync/conf/
然后还要建立一个密码文件'/usr/local/rsync/conf/server.pass' 由于1是客户端,因此密码没有前缀
123abc+-
设置访问权限:
# chmod 600 server.pass
第四步:编写shell脚本
首先是改变就同步的shell脚本,命名 'datarsync.sh' 内容为
#!/bin/bash
host2=192.168.254.2
host3=192.168.254.3
src=/usr/website/html/www.laiwojia.la/data/
dst2=laiwojia-data
dst3=laiwojia-data
user=root
/usr/local/bin/inotifywait\
-mrq --timefmt '%d/%m/%y'\
--format '%T %w%f%e'\
-e modify,delete,create,attrib $src\
|while read files
do
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/usr/local/rsync/conf/server.pass $src $user@$host2::$dst2
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/usr/local/rsync/conf/server.pass $src $user@$host3::$dst3
echo "${files} was rsyncd" >>/tmp/rsync.log 2>&1
done
测试:
运行shell脚本
# sh datarsync.sh
在'/usr/website/html/www.laiwojia.la/data/' 目录下建立test.php 并改写其内容。
vim test.php
#以下是内容
<?php
this is a test
hello tom!
看看运行结果
然后查看2 和 3 机器上是否有test.php 发现服务端都有test.php 文件,查看内容
cat test.php
<?php
this is a test
hello tom
实时同步成功!
第二个场景
制定一个规则实现线上测试部署后同步,首先我们还是要建立一个shell脚本,命名为harsync.sh,内如如下
!/bin/bash
host2=192.168.254.2
host3=192.168.254.3
src=/usr/website/html/www.laiwojia.la/
excludedir=$src"data/" $src"rsync.txt"
dst3=laiwojia
dst4=laiwojia
user=root
rsync_file=${src}"rsync.txt"
if [ -f "$rsync_file" ]
then
/usr/bin/rsync -vzrtopg --delete --progress --exclude=$excludedir --password-file=/usr/local/rsync/conf/server.pass $src $user@$host2::$dst2
/usr/bin/rsync -vzrtopg --delete --progress --exclude=$excludedir --password-file=/usr/local/rsync/conf/server.pass $src $user@$host3::$dst3
fi
rm -rf $src"rsync.txt"
测试:
touch rsync.txt
touch test.html
运行脚本:
# sh harsync.sh
各个服务器上查看是否同步。这样,如果你把代码发到1上,没有rsync.txt就不会同步,可以进行线上测试,等测试好了,可以通过上传一个rsync.txt 然后就可以实现文件同步。
rsync 常见错误与解决方法整理
问题一:
@ERROR: chroot failed
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服务器端的目录不存在或无权限,创建目录并修正权限可解决问题。
问题二:
@ERROR: auth failed on module tee
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服务器端该模块(tee)需要验证用户名密码,但客户端没有提供正确的用户名密码,认证失败。
提供正确的用户名密码解决此问题。
问题三:
@ERROR: Unknown module ‘tee_nonexists'
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
原因:
服务器不存在指定模块。提供正确的模块名或在服务器端修改成你要的模块以解决问题。
问题1:
在client上遇到问题:
rsync -auzv --progress --password-file=/etc/rsync.pas root@192.168.133.128::backup /home/
rsync: could not open password file "/etc/rsync.pas": No such file or directory (2)
Password:
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]
遇到这个问题:client端没有设置/etc/rsync.pas这个文件,而在使用rsync命令的时候,加了这个参数--
password-file=/etc/rsync.pas
问题2:
rsync -auzv --progress --password-file=/etc/rsync.pas root@192.168.133.128::backup /home/
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]
遇到这个问题:client端已经设置/etc/rsync.pas这个文件,里面也设置了密码111111,和服务器一致,但是
服务器段设置有错误,服务器端应该设置/etc/rsync.pas ,里面内容root:111111 ,这里登陆名不可缺少
问题3:
rsync -auzv --progress --password-file=/etc/rsync.pas root@192.168.133.128::backup /home/
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]
遇到这个问题,是因为服务器端的/home/backup 其中backup这个目录并没有设置,所以提示:chdir failed
问题4:
rsync: write failed on "/home/backup2010/wensong": No space left on device (28)
rsync error: error in file IO (code 11) at receiver.c(302) [receiver=3.0.7]
rsync: connection unexpectedly closed (2721 bytes received so far) [generator]
rsync error: error in rsync protocol data stream (code 12) at io.c(601) [generator=3.0.7]
磁盘空间不够,所以无法操作。
可以通过df /home/backup2010 来查看可用空间和已用空间
问题5:网络收集问题
1、权限问题
类似如下的提示:rsync: opendir "/kexue" (in dtsChannel) failed: Permission denied (13)注意查看同步的目录权限是否为755
2、time out
rsync: failed to connect to 203.100.192.66: Connection timed out (110)
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5]
检查服务器的端口netstat –tunlp,远程telnet测试。
可能因为客户端或者服务端的防火墙开启 导致无法通信,可以设置规则放行 rsync(873端口) 或者直接关闭防火墙。
还有一种在同步过程中可能会提示没有权限 (将同步目录加上SvcwRsync全部权限即可,更简单的方法就是将SvcwRsync设为管理员即可)
3、服务未启动
rsync: failed to connect to 10.10.10.170: Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5]
启动服务:rsync --daemon --config=/etc/rsyncd.conf
4、磁盘空间满
rsync: recv_generator: mkdir "/teacherclubBackup/rsync……" failed: No space left on device (28)
*** Skipping any contents from this failed directory ***
5、Ctrl+C或者大量文件
rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(544) [receiver=3.0.5]
rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(544) [generator=3.0.5]
说明:导致此问题多半是服务端服务没有被正常启动,到服务器上去查查服务是否有启动,然后查看下 /var/run/rsync.pid 文件是否存在,最干脆的方法是杀死已经启动了服务,然后再次启动服务或者让脚本加入系统启动服务级别然后shutdown -r now服务器
6、xnetid启动
rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(759) [receiver=3.0.5]
查看rsync日志
rsync: unable to open configuration file "/etc/rsyncd.conf": No such file or directory
xnetid查找的配置文件位置默认是/etc下,根据具体情况创建软链接。例如:
ln -s /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf
或者更改指定默认的配置文件路径,在/etc/xinetd.d/rsync配置文件中。
Rsync configure:
配置一:
ignore errors
说明:这个选项最好加上,否则再很多crontab的时候往往发生错误你也未可知,因为你不可能天天去看每时每刻去看log,不加上这个出现错误的几率相对会很高,因为任何大点的项目和系统,磁盘IO都是一个瓶颈
Rsync error:
错误一:
@ERROR: auth failed on module xxxxx
rsync: connection unexpectedly closed (90 bytes read so far)
rsync error: error in rsync protocol data stream (code 12) at io.c(150)
说明:这是因为密码设置错了,无法登入成功,检查一下rsync.pwd,看客服是否匹配。还有服务器端没启动rsync 服务也会出现这种情况。
错误二:
password file must not be other-accessible
continuing without password file
Password:
说明:这是因为rsyncd.pwd rsyncd.sec的权限不对,应该设置为600。如:chmod 600 rsyncd.pwd
错误三:
@ERROR: chroot failed
rsync: connection unexpectedly closed (75 bytes read so far)
rsync error: error in rsync protocol data stream (code 12) at io.c(150)
说明:这是因为你在 rsync.conf 中设置的 path 路径不存在,要新建目录才能开启同步
错误四:
rsync: failed to connect to 218.107.243.2: No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(104) [receiver=2.6.9]
说明:防火墙问题导致,这个最好先彻底关闭防火墙,排错的基本法就是这样,无论是S还是C,还有ignore errors选项问题也会导致
错误五:
@ERROR: access denied to www from unknown (192.168.1.123)
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: error in rsync protocol data stream (code 12) at io.c(359)
说明:此问题很明显,是配置选项host allow的问题,初学者喜欢一个允许段做成一个配置,然后模块又是同一个,致使导致
错误六:
rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(244) [generator=2.6.9]
rsync error: received SIGUSR1 (code 19) at main.c(1182) [receiver=2.6.9]
说明:导致此问题多半是服务端服务没有被正常启动,到服务器上去查查服务是否有启动,然后查看下 /var/run/rsync.pid 文件是否存在,最干脆的方法是杀死已经启动了服务,然后再次启动服务或者让脚本加入系统启动服务级别然后shutdown -r now服务器
错误七:
rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(604) [sender=2.6.9]
说明:原数据目录里没有数据存在
rsync实现负载均衡集群文件同步,搭建线上测试部署环境相关推荐
- 双机热备+负载均衡(Heartbeat+DRBD+NFS+Keepalived+Lnmp)线上方案
双机热备+负载均衡 线上方案 (Heartbeat+DRBD+NFS+Keepalived+Lnmp) gotop 对于网站服务器来说,可靠性之重要不用我多说,但要想做到可靠性一般需要昂贵的设备,这里 ...
- 高性能Linux服务器 第11章 构建高可用的LVS负载均衡集群
高性能Linux服务器 第11章 构建高可用的LVS负载均衡集群 libnet软件包<-依赖-heartbeat(包含ldirectord插件(需要perl-MailTools的rpm包)) l ...
- LVS负载均衡集群服务搭建详解(一)
LVS概述 1.LVS:Linux Virtual Server 四层交换(路由):根据请求报文的目标IP和目标PORT将其转发至后端主机集群中的某台服务器(根据调度算法): 不能够实现应用层的负载均 ...
- LVS负载均衡集群服务搭建详解
一.LVS概述 1.LVS:Linux Virtual Server 四层交换(路由):根据请求报文的目标IP和目标PORT将其转发至后端主机集群中的某台服务器(根据调度算法): 不能够实现应用层的 ...
- 汇总-13台虚拟机搭建一个高可用负载均衡集群架构
要求 用13台虚拟机搭建一个高可用负载均衡集群架构出来,并运行三个站点,具体需求如下. 设计你认为合理的架构,用visio把架构图画出来 搭建lnmp.tomcat+jdk环境 三个站点分别为:dis ...
- Nginx负载均衡集群介绍
第1章 集群介绍 1.1 集群简介 1.1.1 什么是集群 简单说,集群就是一组(若干个)相互独立的计算机,利用高速通信网络组成的一个较大的计算机服务系统,每个集群节点(即集群中的每台计算机)都是运行 ...
- 运维工程师必备之负载 均衡集群及LVS详解
原文地址:运维工程师必备之负载 均衡集群及LVS详解作者:蚁巡运维平台 来源: chrinux 的BLOG 时间: 2013-07-01 14:00 此博文主要介绍集群和负载均衡的基本理论和类别,内容 ...
- 使用lvs搭建负载均衡集群
有时候,单台服务器的性能可能无法应付大规模的服务请求,且其一旦出现故障,就会造成用户在一段时间内无法访问.通过集群技术,可以在付出较低成本的情况下获得在性能.可靠性.灵活性方面的相对较高的收益. 集群 ...
- RHCS套件实现高可用负载均衡集群(三)——共享存储
注:此博文在博文"RHCS套件实现高可用负载均衡集群(二)"的基础上完成 实验开始前设置实验环境 target端配置--server3 [root@server3 ~]# yum ...
最新文章
- oracle11g insufficient,ORACLE11GORA-01031:insufficientprivileges
- Redis 很屌,不懂使用规范就糟蹋了
- 009_JavaScript语句、表达式和代码块
- oracle表空间可以迁移,Oracle 表空间迁移
- 如何把一个软件嵌入另一个软件_新增一个软件一个游戏
- Caffe自己修改训练方法
- qt中实现左右分割线_Qt项目中,实现屏幕截图并生成gif的详细示例(值得细读)...
- 串口通讯编程一日通2(Overlapped IO模型)
- 7-9 根据后序和中序遍历输出先序遍历 (10 分)
- Java之品优购课程讲义_day16(2)
- VCSA 6.X(VMware vCenter Server Appliance)空间不足问题处理
- Android开发笔记(九十六)集合动画与属性动画
- 攻防世界-Misc-gif
- java无法输出_这个java程序为什么不能输出结果?
- TM2013自定义消息记录保存目录
- breakall lisp文件_breakall.lsp下载
- 用PHP查看微信撤回的消息,vbot微信聊天机器人微信聊天消息详解(9):撤回消息和防撤回消息...
- Unity 自定义扩展Hierachy右键菜单
- windows10双系统删除linux,双系统删除教程详解:Windows(linux)双系统,教你如何删除其中一个!...
- (随笔)无人机集群通信组网系统—无人机自组网