基于负载均衡集群的架构基本要求是web文件的同步,使客户无论负载均衡至那一台机器上都可以得到相同的页面,所以就有了需要同步web镜像的要求,inotify+rsync实现服务器文件实时同步可以实现这个功能,但是这个基于shell script来实现,个人感觉不是很正式,就在今天看见了sersync,这是由金山一位工程师开发的一款开源的软件,代码托管在google code,需要翻墙,感谢前人造的轮子,我们需要做的就是拆分业务,即如何使用好这款开源软件。
业务需求
服务器A(主服务器):10.211.55.39
服务器B(从服务器): 10.211.55.43
业务需求服务器A指定目录下的操作(增删改),实时同步到服务器B,保证服务器A与服务器B上的文件要时刻保持一致。
从服务器
直接通过yum安装即可:
1 | ~]# yum install -y rsync |
服务器B
1 | 创建密码文件 |
2 | ~]# echo “jusene:123456” > /etc/rsync.pass |
3 | ~]# chmod 600 /etc/rsync.pass |
4 | 配置文件 |
5 | ~]# vim /etc/rsyncd.conf |
6 | uid=root |
7 | pid=root |
8 | max connections=20 |
9 | use chroot=yes |
10 | log file=/var/log/rsyncd.log |
11 | pid file=/var/run/rsync.pid |
12 | lock file=/var/run/rsync.lock |
13 | |
14 | [www] |
15 | path=/www |
16 | read only=no |
17 | hosts allow=10.211.55.39 |
18 | auth users=jusene |
19 | secrets file=/etc/rsync.pass |
启动rsync的服务端
1 | ~]# systemctl start rsyncd.socket |
主服务器
直接通过yum安装即可:
1 | ~]# yum install -y rsync |
安装sersync:
sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤,所以在结合rsync同步的时候,节省了运行时耗和网络资源,因此更快。
1 | ~]# echo '123456' > /etc/rsync.pass |
2 | ~]# chmod 600 /etc/rsync.pass |
3 | ~]# wget --no-check-certificate https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz |
4 | ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz |
5 | ~]# cd GNU-Linux-x86 |
6 | ~]# mkdir -p /usr/local/sersync/{bin,conf,log} |
7 | ~]# cp sersync2 /usr/local/sersync/bin/ |
8 | ~]# cp confxml.xml /usr/local/sersync/conf/ |
9 | ~]# cd /usr/local/sersync/conf/ |
10 | ~]# cat confxml.xml |
11 | <?xml version="1.0" encoding="ISO-8859-1"?> |
12 | |
13 | <head version="2.5"> |
14 | |
15 | # 设置本地IP和端口 |
16 | |
17 | <host hostip="localhost" port="8008"></host> |
18 | |
19 | # 开启DUBUG模式 |
20 | |
21 | <debug start="false"/> |
22 | |
23 | # 开启xfs文件系统 |
24 | |
25 | <fileSystem xfs="false"/> |
26 | |
27 | # 同步时忽略推送的文件(正则表达式),默认关闭 |
28 | |
29 | <filter start="false"> |
30 | |
31 | <exclude expression="(.*)\.svn"></exclude> |
32 | |
33 | <exclude expression="(.*)\.gz"></exclude> |
34 | |
35 | <exclude expression="^info/*"></exclude> |
36 | |
37 | <exclude expression="^static/*"></exclude> |
38 | |
39 | </filter> |
40 | |
41 | <inotify> |
42 | |
43 | # 设置要监控的事件 |
44 | |
45 | <delete start="true"/> |
46 | |
47 | <createFolder start="true"/> |
48 | |
49 | <createFile start="true"/> |
50 | |
51 | <closeWrite start="true"/> |
52 | |
53 | <moveFrom start="true"/> |
54 | |
55 | <moveTo start="true"/> |
56 | |
57 | <attrib start="true"/> |
58 | |
59 | <modify start="true"/> |
60 | |
61 | </inotify> |
62 | |
63 | <sersync> |
64 | |
65 | # 本地同步的目录路径 |
66 | |
67 | <localpath watch="/www"> |
68 | |
69 | # 远程IP和rsync模块名 |
70 | |
71 | <remote ip="10.211.55.43" name="www"/> |
72 | |
73 | <!--<remote ip="192.168.8.39" name="tongbu"/>--> |
74 | |
75 | <!--<remote ip="192.168.8.40" name="tongbu"/>--> |
76 | |
77 | </localpath> |
78 | |
79 | <rsync> |
80 | |
81 | # rsync指令参数 |
82 | |
83 | <commonParams params="-auvzP"/> |
84 | |
85 | # rsync同步认证 |
86 | |
87 | <auth start="true" users="jusene" passwordfile="/etc/rsync.pass"/> |
88 | |
89 | # 设置rsync远程服务端口,远程非默认端口则需打开自定义 |
90 | |
91 | <userDefinedPort start="false" port="874"/><!-- port=874 --> |
92 | |
93 | # 设置超时时间 |
94 | |
95 | <timeout start="true" time="100"/><!-- timeout=100 --> |
96 | |
97 | # 设置rsync+ssh加密传输模式,默认关闭,开启需设置SSH加密证书 |
98 | |
99 | <ssh start="false"/> |
100 | |
101 | </rsync> |
102 | |
103 | # sersync传输失败日志脚本路径,每隔60会重新执行该脚本,执行完毕会自动清空。 |
104 | |
105 | <failLog path="/usr/local/sersync/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> |
106 | |
107 | # 设置rsync+crontab定时传输,默认关闭 |
108 | |
109 | <crontab start="false" schedule="600"><!--600mins--> |
110 | |
111 | <crontabfilter start="false"> |
112 | |
113 | <exclude expression="*.php"></exclude> |
114 | |
115 | <exclude expression="info/*"></exclude> |
116 | |
117 | </crontabfilter> |
118 | |
119 | </crontab> |
120 | |
121 | # 设置sersync传输后调用name指定的插件脚本,默认关闭 |
122 | |
123 | <plugin start="false" name="command"/> |
124 | |
125 | </sersync> |
126 | |
127 | # 插件脚本范例 |
128 | |
129 | <plugin name="command"> |
130 | |
131 | <param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix--> |
132 | |
133 | <filter start="false"> |
134 | |
135 | <include expression="(.*)\.php"/> |
136 | |
137 | <include expression="(.*)\.sh"/> |
138 | |
139 | </filter> |
140 | |
141 | </plugin> |
142 | |
143 | # 插件脚本范例 |
144 | |
145 | <plugin name="socket"> |
146 | |
147 | <localpath watch="/opt/tongbu"> |
148 | |
149 | <deshost ip="192.168.138.20" port="8009"/> |
150 | |
151 | </localpath> |
152 | |
153 | </plugin> |
154 | |
155 | <plugin name="refreshCDN"> |
156 | |
157 | <localpath watch="/data0/htdocs/cms.xoyo.com/site/"> |
158 | |
159 | <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/> |
160 | |
161 | <sendurl base="http://pic.xoyo.com/cms"/> |
162 | |
163 | <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/> |
164 | |
165 | </localpath> |
166 | |
167 | </plugin> |
168 | |
169 | </head> |
启动sersync:
1 | ~]# ./sersync2 -d -r -o ../conf/confxml.xml |
2 | set the system param |
3 | execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches |
4 | execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events |
5 | parse the command param |
6 | option: -d run as a daemon |
7 | option: -r rsync all the local files to the remote servers before the sersync work |
8 | option: -o config xml name: ../conf/confxml.xml |
9 | daemon thread num: 10 |
10 | parse xml config file |
11 | host ip : localhost host port: 8008 |
12 | will ignore the inotify createFile event |
13 | daemon start,sersync run behind the console |
14 | use rsync password-file : |
15 | user is jusene |
16 | passwordfile is /etc/rsyncd.pass |
17 | config xml parse success |
18 | please set /etc/rsyncd.conf max connections=0 Manually |
19 | sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) |
20 | Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads) |
21 | please according your cpu ,use -n param to adjust the cpu rate |
22 | ------------------------------------------ |
23 | rsync the directory recursivly to the remote servers once |
24 | working please wait... |
25 | execute command: cd /www && rsync -auvzP -R --delete ./ --timeout=100 jusene@10.211.55.43::www --password-file=/etc/rsyncd.pass >/dev/null 2>&1 |
26 | run the sersync: |
27 | watch path is: /www |