Setup lsyncd in Centos7

Lsynvd is a great tool for file sync across multiple web servers,
instead of having a single NFS share, each web server will have their own copy of of files, solving single point of failure problem, and completely removing IOPS worry from web tier.

The following guide is to setup lsyncd on the primary web server (where code changes or file uploads will be done on),
in the configuration, 192.168.1.2 would be the web02:

[shell]
# Generate SSH key on primary web server
ssh-keygen -t rsa

# copy id_rsa.pub to web02 authorized_keys
ssh-copy-id [email protected]

# install compilers
yum -y install lua lua-devel pkgconfig gcc asciidoc

# download, compile, install lsyncd
cd /var/tmp
wget http://lsyncd.googlecode.com/files/lsyncd-2.1.5.tar.gz
tar xzvf lsyncd-2.1.5.tar.gz
cd lsyncd-2.1.5
./configure && make && make install

#put this to /etc/init.d/lsyncd
==============================================================
#!/bin/bash
#
# lsyncd: Starts the lsync Daemon
#
# chkconfig: 345 99 90
# description: Lsyncd uses rsync to synchronize local directories with a remote
# machine running rsyncd. Lsyncd watches multiple directories
# trees through inotify. The first step after adding the watches
# is to, rsync all directories with the remote host, and then sync
# single file buy collecting the inotify events.
# processname: lsyncd

. /etc/rc.d/init.d/functions

config=”/etc/lsyncd.lua”
lsyncd=”/usr/local/bin/lsyncd”
lockfile=”/var/lock/subsys/lsyncd”
pidfile=”/var/run/lsyncd.pid”
prog=”lsyncd”
RETVAL=0

start() {
if [ -f $lockfile ]; then
echo -n $”$prog is already running: ”
echo
else
echo -n $”Starting $prog: ”
daemon $lsyncd -pidfile $pidfile $config
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch $lockfile
return $RETVAL
fi
}
stop() {
echo -n $”Stopping $prog: ”
killproc $lsyncd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f $lockfile
return $RETVAL
}
case “$1” in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status $lsyncd
;;
*)
echo “Usage: lsyncd {start|stop|restart|status}”
exit 1
esac

exit $?
==============================================================

# chkconfig it lor
chkconfig lsyncd on
# create the log folder
mkdir /var/log/lsyncd
# you might want to set a cronjob to clear out old logs
# configuration file
# put this to /etc/lsyncd.lua: (replace target IP, source, exlcudes, perhaps ssh port)
====================================================
settings {
logfile = “/var/log/lsyncd/lsyncd.log”,
statusFile = “/var/log/lsyncd/lsyncd-status.log”,
statusInterval = 20
}
sync {
default.rsync,
source=”/source”,
target=”192.168.1.2:/destination”,
exclude=”uploads/”,
rsync = {
compress = true,
acls = true,
verbose = true,
rsh = “/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no” }
}
=====================================================
#Lastly, start the service
/etc/init.d/lsyncd start

[/shell]

Installation is now completed, any file change in /source of primary web server will be sync to web02 /destination

Related Posts
Leave a Reply

Your email address will not be published.Required fields are marked *