nginx读写分离+rsync+notify

先配置一个简单的基于nginx的读写分离,为了方便都用过二进制安装

实验环境:

nginx:192.168.122.94

web1(写):192.168.122.64

web2(读):192.168.122.64

写服务器web1:写的这边服务器要确保开启DAV功能,并Apache配置文件加载了DAV相关模块

1.   看是否有dav这个模块

[root@web1 ~]# yum -y install httpd

[root@web1 ~]# echo this is web1 > /var/www/html/index.html         
[root@web1 ~]# cat /etc/httpd/conf.modules.d/00-dav.conf             
LoadModule dav_module modules/mod_dav.so
LoadModule dav_fs_module modules/mod_dav_fs.so
LoadModule dav_lock_module modules/mod_dav_lock.so

2.在/var/www/html 默认的根目录下添加dav on 开启dav功能,若根目录更改,则应该,修改

[root@web1 html]# vim /etc/httpd/conf/httpd.conf 
<Directory "/var/www/html">           

Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
    dav on
</Directory>

3、确保运行httpd进程的apache用户对网页根目录拥有写入权限
[root@web02 ~]# setfacl -m u:apache:rwx /var/www/html/

4.用客户端测试是否能够上传文件

[root@physical ~]# curl -T huahua http://192.168.122.64        //curl -T 后面跟要上传的路径
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>201 Created</title>
</head><body>
<h1>Created</h1>
<p>Resource /huahua has been created.</p>
</body></html>

[root@web1 ~]# ls /var/www/html/
111  file3  huahua  index.html  kailey  kkk  uuu  //可以对web1上传文件

 

读客户端web2:

[root@web2 ~]# yum -y install httpd

[root@web2 ~]# systemctl enable httpd

echo hello this is web2 > /var/www/html/index.html        //实际生产中这两个服务器首页应该一样,一个负责写,一个负责读

 

nginx端:

[root@nginx ~]# cat /etc/yum.repos.d/nginx.repo       //配置nginx的repo源,能安装比较新的二进制nginx
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
[root@nginx ~]# yum  -y install nginx

[root@nginx ~]# vim /etc/nginx/conf.d/default.conf 
    location / {
        proxy_pass http://192.168.122.246;   //读代理到web2
        if ($request_method = PUT){
            proxy_pass http://192.168.122.64;  //写代理到web1
        }
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }


客户端测试结果:

[root@physical ~]# curl http://192.168.122.94     //访问只能出现一个页面,就是读的那个服务器
hello this is web2
[root@physical ~]# curl -T kkk http://192.168.122.94
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>201 Created</title>
</head><body>
<h1>Created</h1>
<p>Resource /kkk has been created.</p>
</body></html>

[root@web1 html]# ls
111  file3  huahua  index.html  inotify.sh  kailey  kkk        //可以看到web1写这端的服务器以应有数据

但是有问题客户端一个文件上传到写服务器上,但是访问的却是读的哪个服务器,那么就
就无法看到,所以需要让写的网站根目录上的文件随时的同步到读的网站根目录的服务器上。


rsync+notify解决服务器无法同步问题

读客户端web2:

1.修改/etc/rsyncd.conf 配置文件

[root@web2 html]# cat /etc/rsyncd.conf 
uid=root
gid=root
user chroot=no
port=873
max connections = 2000
timeout = 200
log file = /var/run/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log format = %t %a %m %f %b
[rsync]
path = /var/www/html
commemt = rsync
list = yes
read only = no
write only = no
auth users = test
secrets file = /etc/rsyncd.secret
ignore errors = yes
hosts allow = *

2.设置同步时的帐号密码

[root@web2 ~]# echo "test:123" > /etc/rsyncd.secret     //读服务器端设置一个帐号密码存入 /etc/rsyncd.secret文件中
[root@web2 ~]# chmod 600 /etc/rsyncd.secret       //对文件设置权限
[root@web2 ~]# ll /etc/rsync.secret
-rw------- 1 root root 14 4月 18 09:25 /etc/rsync.secret
[root@web2 ~]# cat /etc/rsyncd.secret
test:123

3.启动rsync

[root@web2 ~]# rsync --daemon
[root@web2 html]# ss -antp|grep rsync
LISTEN     0      5            *:873                      *:*                   users:(("rsync",pid=1329,fd=3))
LISTEN     0      5           :::873                     :::*                   users:(("rsync",pid=1329,fd=5))

4.设置开机自启动,写入到/etc/rc.local里面
[root@web2 ~]# vim /etc/rc.local
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf


写服务器端web1配置:

1. 将读服务器的 密码写入/etc/rsyncd.secret中,使其可用同步
[root@web1 ~]# echo "123" > /etc/rsyncd.secret           

[root@web11 ~]# chmod 600 /etc/rsyncd.secret     //对文件设置权限
[root@web1 ~]# ll /etc/rsync.password
-rw------- 1 root root 14 4月 18 09:25 /etc/rsync.password
[root@web1 ~]# cat /etc/rsyncd.secret
123

2.   安装inotify的工具
[root@web01 ~]# rpm -ivh epel-release-latest-7.noarch.rpm
yum install inotify-tools -y                                                         
[root@web2 html]# cat inotify.sh                              //编写一个同步脚本,脚本位置随意
#!/bin/bash
host=192.168.122.246
src=/var/www/html
dst=rsync
user=test
rsync_passfile=/etc/rsyncd.secret
inotify_home=/usr/
if [ ! -e "$src" ] \
|| [ ! -e "${rsync_passfile}" ] \
|| [ ! -e "${inotify_home}/bin/inotifywait" ] \
|| [ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Directory"
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file
do
# rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host::$dst >/dev/null 2>&1
cd $src && rsync -aruz -R --delete ./ --timeout=100 $user@$host::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
done
exit 0

3.执行脚本放到后台

 [root@web1 ~]# sh inotify.sh &
[2] 2453

客户端测试效果:

[root@physical ~]# touch xin
[root@physical ~]# curl -T xin http://192.168.122.94
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>201 Created</title>
</head><body>
<h1>Created</h1>
<p>Resource /xin has been created.</p>
</body></html>

[root@web1 ~]# ls /var/www/html/
111  file3  huahua  index.html  kailey  kkk  uuu  xin
[root@web2 html]# ls
111  file3  huahua  index.html  kailey  kkk  uuu  xin   //读服务器也同时同步了数据

  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值