通常web站点为了提升可用性都会使用多台服务器配置负载均衡, 但若是站点须要上传附件的话就会遇到一个问题, 上传到服务器内的附件如何可以让两台服务器都访问的到?html
通常状况咱们能够用这些方法:git
本文主要针对最后一种,直接存储在服务器本地磁盘的方式来讲明。github
lsyncd 是一个支持实时、双向、多机器的多模式文件同步工具。 使用 Lua 语言封装,采用 Linux 内核(2.6.13 及之后)的 inotify 触发机制,而后经过 rsync 作差别同步,达到实时的效果。web
为何咱们不直接使用rsync作文件同步,而要使用lsyncd呢? 这就要说如下lsyncd的主要特色:bash
lsync支持使用多种协议传输文件,本文只列举其中一种。服务器
安装其实很简单只须要使用yum网络
yum install -y epel-release
#安装
yum install -y rsync lsyncd
复制代码
#默认lsyncd配置文件路径
/etc/lsyncd.conf
#默认lsyncd日志路径,使用yum安装会自动配置日志截断,不须要额外干预
/var/log/lsyncd/lsyncd.log
#默认的rsync路径
/etc/rsyncd.conf
#默认的rsync日志路径
/var/log/messages
复制代码
在两台服务器上修改rsyncd配置, 在rsync配置内新增一个area,受权指定ip或者网段的设备进入指定路径读写 hosts allow配置两台服务器对方的ip 具体的配置项说明详见:download.samba.org/pub/rsync/r…负载均衡
#cat /etc/rsyncd.conf
uid = root
gid = root
use chroot = yes
max connections = 10
pid file = /var/run/rsyncd.pid
exclude = lost+found/
transfer logging = yes
timeout = 900
ignore nonreadable = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 *.xz
[ops]
path = /root/ops
ignore errors
read only = no
hosts allow = 192.168.0.1 192.168.0.2
hosts deny = *
复制代码
配置lsyncd.conf,一样分别在两台服务器上作配置,其中的target须要配置对方的ip 配置项说明详见:axkibe.github.io/lsyncd/manu…ssh
# cat /etc/lsyncd.conf
----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync, but executing moves through on the target.
--
-- For more examples, see /usr/share/doc/lsyncd*/examples/
--
settings {
logfile ="/var/log/lsyncd/lsyncd.log",
statusFile ="/var/log/lsyncd/lsyncd.status",
inotifyMode = "CloseWrite",
maxProcesses = 7,
nodaemon = false,
insist = true
}
sync {
default.rsync,
source = "/root/ops/",
target = "192.168.0.11::cs-conf-ops/",
delete= "running",
exclude = {
".*"
},
delay = 0,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = false,
verbose = true
}
}
复制代码
systemctl start rsyncd
systemctl start lsyncd
systemctl enable lsyncd
systemctl enable rsyncd
复制代码
能够在其中一台服务器建立或者修改一个文件,看看另一台有没有作出相应的变更工具