rsync实现负载均衡集群文件同步,搭建线上测试部署环境

闲来无事,搭建一个负载均衡集群,至于负载均衡集群搭建过程,找时间写下。这次主要写集群之间的文件同步,以及线上测试环境的搭建。

笔者看过很多公司都没有线上测试环境,真是崩溃了,不造怎么确保线上线下环境一致的。

 

笔者此次使用三台服务器:

192.168.138.3   web服务器

192.168.138.4   web服务器

192.168.138.10  web服务器+线上测试环境+源站

 

其中3 4 服务器作为集群中的web服务器,对外开放,是负载均衡集群的部分。

其中10 服务器不对外开放,代码发布到该服务器,在该服务器上进行测试,完成后程序由该服务器同步到其他集群服务器上,同时网站后台以及自动脚本位于该服务器上。(如果为了安全期间,大家可以对该服务器进行配置,只允许你们公司内部网络访问,在家时通过VPN连接内部网络,这样就可以确保后台安全)

 

本文主要是大家rsync服务,至于IP配置,web服务器搭建,大家可以看我之前的文章。

 

这里我们为了开发方便,在10服务器上制定一个规则,即只要rsync.txt存在我们就开始同步,这样只要开发上传该文件同步就开始,同步完成后自动删除该文件。

 

第一步:笔者这里安装的是centos6.4 已经默认安装了rsync 所以笔者就不再进行安装,未安装的可以自行安装,过程相对简单

第二步:安装inotify inotify-tools

笔者这里centos6.4已经默认安装了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 --prefix=/usr/local/inotify-tools
make && make install

 

第三步:对客户端 服务端进行配置

讲之前一定要搞清楚 10是客户端  3 4 是否服务端  这个不要搞倒了   是10的文件同步到3 4 上面

首先对 3 4 进行配置,这里笔者贴出自己的配置文件   /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.138.10
hosts deny = *
list = false
uid = root
gid = root
auth users = laiwojia
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.138.10
hosts deny = *
list = false
uid = root
gid = root
auth users = laiwojia
secrets file = /usr/local/rsync/conf/server.pass

我们这里配置了两个模块 'laiwojia-data'和'laiwojia' ,其中'laiwojia-dada'只要发生变化就要同步的(想想大家把后台放在10上面,那么这个data里面的文件就是后台生成的持久化文件,后台配置,然后整个集群通用),而'laiwojia'了这个模块是发布系统所用的模块,需要在站点下有'rsync.txt'文件时才同步。(由此我们可以看出,'data'中的文件不能通过发布系统发布)

 

看了上面的配置,大家就明白,'/usr/website/html/www.laiwojia.la/data/'这个目录必须存在, '/usr/local/rsync/conf/server.pass'这个秘密文件也必须存在

秘密文件中的内容为

laiwojia:123abc+-

记住3 4 作为服务端 密码文件要有前缀 有的淫写成 '123abc+-' 后面就会不通过

 

然后我们对10进行配置,这里笔者贴出配置文件

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

然后还要建立一个秘密文件'/usr/local/rsync/conf/server.pass' 由于10是客户端,因此秘密没有前缀

123abc+-

 

第四步:编写shell脚本

首先是改变就同步的shell脚本,我们命名为 'inotify-ha-rsync.sh' 内容为

#!/bin/bash
host3=192.168.138.3
host4=192.168.138.4

src=/usr/website/html/www.laiwojia.la/data/
dst3=laiwojia-data
dst4=laiwojia-data
user=laiwojia

/usr/local/inotify-tools/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@$host3::$dst3
    /usr/bin/rsync  -vzrtopg --delete --progress --password-file=/usr/local/rsync/conf/server.pass  $src $user@$host4::$dst4
    echo "${files} was rsyncd" >>/tmp/rsync.log 2>&1
done

OK,我们来测试下

在'/usr/website/html/www.laiwojia.la/data/' 目录下建立test.php 并改写其内容,我们看看结果(事先要运行这个shell)

vim test.php

#以下是内容
<?php
this is a test
hello tom!

看看运行结果

然后看看3 和 4 机器上是否有test.php  发现两个中都有test.php 文件,好了我们看看内容

cat test.php

<?php
this is a test
hello tom

OK,这个实时同步的算是完成了

 

 

剩下我们要看看考虑到线上测试部署的,首先我们还是要建立一个shell脚本,命名为ha-rsync.sh,内如如下

!/bin/bash
host3=192.168.138.4
host4=192.168.138.3

src=/usr/website/html/www.laiwojia.la/
excludedir=$src"data/" $src"rsync.txt"
dst3=laiwojia
dst4=laiwojia
user=laiwojia
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@$host3::$dst3
        /usr/bin/rsync  -vzrtopg --delete --progress --exclude=$excludedir  --password-file=/usr/local/rsync/conf/server.pass  $src $user@$host4::$dst4
fi
rm -rf $src"rsync.txt"

好我们来测试下

touch rsync.txt
touch test.html

然后运行脚本,到各个服务器上看看,是不是都有了。这样的话,如果你把代码发到10上,没有rsync.txt就不会同步,那么这是测试可以进行线上测试,等测试好了,开发就上传一个rsync.txt 然后就开始同步了

 

当然了,这里还可以搞得更复杂,比如安全起见,一些配置文件不要让它同步以免某天你发错了把本地的给发上去了,还可以指定那些目录不同步,等等,这些都可以实现,只不过shell写的复杂一点而已

 

posted @ 2016-02-01 18:12  八面碰壁居士  阅读(5614)  评论(4编辑  收藏  举报