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]

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/570876.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python 全栈开发,Day36(作业讲解(大文件下载以及进度条展示),socket的更多方法介绍,验证客户端链接的合法性hmac,socketserver)...

先来回顾一下昨天的内容 黏包现象粘包现象的成因 &#xff1a;   tcp协议的特点 面向流的 为了保证可靠传输 所以有很多优化的机制   无边界 所有在连接建立的基础上传递的数据之间没有界限   收发消息很有可能不完全相等   缓存机制&#xff0c;导致没发过去的消息会在…

Python中xPath技术和BeautifulSoup的使用

xpath基本知识 XPath语法&#xff1a;使用路径表达式来选取XML或HTML文档中的节点或节点集 路径表达式 nodename:表示选取此节点的所有子节点 / &#xff1a; 表示从根节点选取 // &#xff1a;选择任意位置的某个节点。 . &#xff1a;选取当前节点 .. &#xff1a;选…

Redis集群搭建使用

一:关于redis cluster 1:redis cluster的现状 目前redis支持的cluster特性 1):节点自动发现 2):slave->master 选举,集群容错 3):Hot resharding:在线分片 4):进群管理:cluster xxx 5):基于配置(nodes-port.conf)的集群管理 6):ASK 转向/MOVED 转向机制. 2:redis cluster 架…

为Spring Cloud Config Server配置远程git仓库

简介 虽然在开发过程&#xff0c;在本地创建git仓库操作起来非常方便&#xff0c;但是在实际项目应用中&#xff0c;多个项目组需要通过一个中心服务器来共享配置&#xff0c;所以Spring Cloud配置中心支持远程git仓库&#xff0c;以使分散的项目组更方便的进行协作。 基础环境…

Linux一个命令创建多个目录:seq命令

mkdir $(seq -f %g 1 10) 其他seq用法如下&#xff1a; 它還有三個選項-f, --formatFORMAT use printf style floating-point FORMAT (default: %g)-s, --separatorSTRING use STRING to separate numbers (default: \n)-w, --equal-width equalize width by pad…

div水平垂直居中的六种方法

在平时&#xff0c;我们经常会碰到让一个div框针对某个模块上下左右都居中&#xff08;水平垂直居中&#xff09;&#xff0c;其实针对这种情况&#xff0c;我们有多种方法实现。 方法一: 绝对定位方法&#xff1a;不确定当前div的宽度和高度&#xff0c;采用 transform: trans…

Redis集群监控及Redis桌面客户端

之前在生产环境部署了Redis集群&#xff0c;一直苦于没有工具监控&#xff0c;最近找了下网上推荐redmon和Redislive的比较多&#xff0c;查看了两个项目的github,都几年没有更新&#xff0c;这两个项目应该没有人在维护了&#xff0c;如果哪位有更好的替代方案麻烦告知&#x…

phpcms 添加稿件到栏目 add_content

private $db; private $content_model; public function __construct() {parent::__construct ();$this->content_model pc_base::load_model(content_model);$this->siteid 1; }public function add(){$info[title]标题;$info[description]摘要;$info[content]内容;$i…

Python基础学习数值运算之内建函数

1、abs(num) 返回num的绝对值 2、coerce(num1,num2) 将num1和num2转换为同一类型&#xff0c;然后以一个无级的形式返回 3、divmod(num1,num2) 除法取余运算的结合。返回一个元组(num1/num2,num1%num2)。对浮点型和复数的商进行下舍入&#xff08;复数仅取实数部分的商&am…

GIT安装部署

git git简介 Git不仅是一款开源的分布式版本控制系统&#xff0c;而且有其独特的功能特性&#xff0c;例如大多数的分布式版本控制系统只会记录每次文件的变化&#xff0c;说白了就是只会关心文件的内容变化差异&#xff0c;而Git则是关注于文件数据整体的变化&#xff0c;直接…

mac远程桌面登陆windows

找了苹果官方网站&#xff0c;有付费的&#xff0c;找了appstore 还是没有免费使用的&#xff0c;最后找了微软官方非appstore下载的 Microsoft_Remote_Desktop_Beta 在此下载 http://mac.softpedia.com/get/Utilities/Microsoft-Remote-Desktop-Connection.shtml 实在下载不…

牛客网Wannafly挑战赛15 B车辆安排(模拟)AND C 出队(规律)

传送门 &#xff1a;B题&#xff1a;点我 C题&#xff1a; 点我 题目描述 有n个队伍&#xff0c;每个队伍的人数小于等于5&#xff0c;每辆车最多坐5个人&#xff0c;要求一个队伍的人都在一辆车上&#xff0c;求最少的车数 输入描述: 第一行n第二行n个数&#xff0c;表示每个队…

分布式大数据多维分析(OLAP)引擎Apache Kylin安装配置及使用示例

原文地址&#xff1a;http://lxw1234.com/archives/2016/04/643.htm Apache Kylin是一个开源的分布式分析引擎&#xff0c;提供Hadoop之上的SQL查询接口及多维分析&#xff08;OLAP&#xff09;能力以支持超大规模数据&#xff0c;最初由eBay 开发并贡献至开源社区。它能在亚秒…

Apache Kylin原理学习之Cube的创建与Build

原文地址&#xff1a;http://lxw1234.com/archives/2016/05/655.htm Cube是一种典型的多维数据分析技术&#xff0c;一个Cube可以有多个事实表&#xff0c;多个维表构成。如果您还不了解这些概念&#xff0c;建议您搜索下数据仓库、OLAP、Cube、星型模型、事实表、维度表等等。…

5-12

1,每个递归函数都有两部分&#xff0c;基线条件和递归条件 base case and recursive case 2,调用一个函数的时候&#xff0c;其他的函数调用会暂停并处于未完成的状态 3.调用栈会消耗大量的内存&#xff0c;栈很高的时候意味着计算机要存储大量的函数调用信息&#xff0c;此时的…

kylin与superset集成实现数据可视化

原文地址&#xff1a;http://minirick.duapp.com/kylinyu-supersetji-cheng-shi-xian-shu-ju-ke-shi-hua/?utm_sourcetuicool&utm_mediumreferral apache kylin是一个开源分布式引擎&#xff0c;提供Hadoop之上的SQL查询接口及多维分析&#xff08;OLAP&#xff09;能力以…

Ambari离线部署Hadoop集群踩到的坑

1、远程拷贝HDP组件不全导致安装client时缺少rpm包&#xff0c;手动拷贝解决 2、安装HAWQ&#xff0c;启动时报错 passwordlell ssh hawq hosts &#xff0c;hawq master 和其他主机机拷贝文件输入密码受限&#xff0c;两方面原因&#xff1a; 一 root 用户 ssh 无密登录时 权…

2016012026 管凌波 散列函数的应用及其安全性

2016012026 管凌波 散列函数的应用及其安全性 一、散列函数是什么。 散列函数即为Hash函数&#xff0c;就是把任意长度的输入&#xff08;预映射&#xff09;&#xff0c;通过散列算法&#xff0c;变换成固定长度的输出&#xff0c;该输出就是散列值。这种转换是一种压缩映射&a…

Team Foundation Server (TFS) 2015 安装指导

原文地址&#xff1a;http://www.cnblogs.com/danzhang/p/4718035.html http://www.cnblogs.com/danzhang 张洪君 微软ALM MVP 1. 概述 微软于8月6日发布了大家期待已久的TFS 2015正式版&#xff0c; https://www.visualstudio.com/en-us/news/tfs2015-vs.aspx &#xff0c;…

VM虚拟机链接克隆及linux eth0网卡的快速设置方法

对于后台开发者来说,在学习过程中必然接触众多中间件,在自己的虚拟机进行操作甚至搭建cluster是很常见的事情.我在初学者时摸索出一套快速的克隆虚拟机方法.分享给大家.产品VMware Workstation版本10.0.2 build-1744117这是我的虚拟机命名,我觉得这样的命名比较合适,clone机/服…