Configuration of Lsyncd Service in MultiServer Architecture

Configuration of Lsyncd Service in MultiServer Architecture

Overview:

When you have multi-server architecture then you will need to make sure that both the servers are in synchronization. You will find lots of tools which help you to keep your servers in sync. For this kind of work, Lsyncd is the best tool to keep servers in sync.

What is Lsyncd?

Lsyncd is a tool which helps you to maintain the code synchronization over multiserver architecture. It means you have two servers and code resides under the "/data/docroot" directory. When some changes are done on any of the server lsyncd will help to immediately copy or synced the changes on another server.

This article will help you in the configuration of lsyncd which will maintain the synchronization task between the two servers.

In this guide, we will configure the lsyncd on Debian server but it will work a similar way for other a Linux distros as well.

Before using lsyncd on servers make sure both the servers are accessible or able to ssh each other with their internal IP addresses. Also, add ssh key based authentication on both the server so each server will access each other without password.
If you do not know how to configure ssh key based login then follow my previous guide on SSH.

Once done with the above mentioned recommended settings follow below steps to configure lsyncd service.

Step 1: Install Lsyncd

Use below commands to install lsyncd on server or system. First, run apt-get update to update the repository on the server. For lsyncd, you do not need to add any extra repository because lsyncd come will default repository.

$ apt-get update
$ apt-get install lsyncd

Note : You must have root or sudo access on the system to run above commands.

Step 2: Location of Default Configuration file

If you have no idea how to configure lsyncd which works for your systems then refer the lsyncd examples guide. This examples files resides under the "/usr/share/doc/lsyncd/examples/" directory. Default configuration or syntax to setup lsyncd check "lrsyncssh.lua" file.

$ cat /usr/share/doc/lsyncd/examples/lrsyncssh.lua
----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync, but executing moves through on the target.
--
sync{
default.rsyncssh,
source="src",
host="localhost",
targetdir="dst/"
}

Above is the basic and default configuration settings of lsyncd.

Step 3: Create Lsyncd Directory

After installing lsyncd service it will not create any configuration file or directory on the system. You need to manually create directory and configuration file under the "/etc" directory.

Run below command to create directory and file on the system.

$ mkdir -p /etc/lsyncd
$ touch /etc/lsyncd/lsyncd.conf.lua

If you directly want to create a configuration file in /etc then you can do it that way also. There is no compulsion to create the lsyncd directory.

Step 4: Create Lsyncd log Directory

As you know lsyncd will not create any configuration directory. Same way to store the logs of lsyncd you need to create log directory and log file as well on the system.

$ mkdir -p /var/log/lsyncd
$ touch /var/log/lsyncd/lsyncd.log
$ touch /var/log/lsyncd/lsyncd-status.log

Step 5: Configuration To Sync Remote Hosts

Now we will see the configuration part of lsyncd where we will add lsyncd configuration on both the server.

Server1 : Here we will add configuration to sync code from server1 to server2.

vim /etc/lsyncd/lsyncd.conf.lua

----
-- User configuration file for lsyncd.
--
-- a Simple example for default rsync.
--
settings = {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd-status.log",
statusInterval = 60
}

-- Slave server configuration

sync {
default.rsync,
delete=false,
source="/data/docroot/codebase/",
target="10.0.0.58:/data/docroot/codebase/",
--exclude={"var", "media"},
--rsyncOpts = {"-pEog"}
}

We have use target IP is internal IP of the second server.

In settings part, We are defining the path of the log file where all the logs will store.

statusInterval : Code will sync every 60 seconds. you can set it as per your requirement.

In Sync part we will define source and target to sync code.

default.rsync : Uses default rsync to sync code.
delete=false : Doesn't delete files on the remote host even though they're deleted at the source.
Source          : Use the path of the codebase to sync.
target            : Use the IP and codebase path of destination end.
--exclude     : This option helps to exclude the directory which you don't want to sync. Optional.
rsyncOpts    : Uses for optional rsync parameters.


Server 2: Here we will add configuration to sync code from server2 to server1.

vim /etc/lsyncd/lsyncd.conf.lua

----
-- User configuration file for lsyncd.
--
-- a Simple example for default rsync.
--
settings = {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd-status.log",
statusInterval = 60
}

-- Slave server configuration

sync {
default.rsync,
delete=false,
source="/data/docroot/codebase/",
target="10.0.0.85:/data/docroot/codebase/",
--exclude={"var", "media"},
--rsyncOpts = {"-pEog"}
}

Just change the target ip with first server internal IP.

Step 6: Start Lsyncd Service

After all the configuration settings, start the lsyncd service with below command,

$ /etc/init.d/lsyncd start
$ /etc/init.d/lsyncd stop
$ /etc/init.d/lsyncd restart
$ /etc/init.d/lsyncd status

To confirm lsyncd is working properly then use below tail command to check the log file. It will show running sync files in between two servers.

$ tail -f /var/log/lsyncd/lsyncd.log

This article will help to sync directories between multiple servers and also good for load balanced environments. I hope you like the article if you find any difficulties to configure lsyncd then please do comment your queries or problem via the comment section, till then stay tuned to techthings.org for more such valuable articles.

Leave a Reply

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