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 21 13:59 max_queued_events
-rw-r--r-- 1 root root 0 21 13:59 max_user_instances
-rw-r--r-- 1 root root 0 21 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]

说明:原数据目录里没有数据存在

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值