背景

源服务器向多个目标服务器实时同步目录

示例

源服务器:

IP:192.168.12.10

目录:/lsync_10

目标服务器:

IP:192.168.12.46

IP:192.168.12.48

目录:/lsync_test

安装

yum -y install epel-release

yum -y install lsyncd

systemctl restart lsyncd

systemctl enable lsyncd

systemctl statuslsyncd

配置

在源服务器操作

# 添加如下配置

# vim /etc/lsyncd.conf

settings {

–pid文件

logfile = “/var/log/lsyncd.log”,

–状态文件

statusFile = “/var/log/lsyncd.status”,

–同步模式,意思就是有更新就同步

inotifyMode = “CloseWrite or Modify”,

–最大8个进程

maxProcesses = 8,

insist = true

}

–需要同步服务器的IP地址池

servers = {

“192.168.12.46”,

“192.168.12.48”,

}

–使用for循环遍历IP池

for _, server in ipairs(servers) do

sync {

–本地目录间同步,使用rsync

default.rsync,

–同步的源目录,使用绝对路径

source = “/lsync_10”,

–定义目的地址,注意:冒号后面是绝对路径

target = server..”:/lsync_test”,

–累计事件,等待rsync同步延时时间。这里设置0秒,表示实时同步。

delay = 0,

rsync ={

–rsync命令的绝对路径

binary =”/usr/bin/rsync”,

archive =true,

compress =true,

verbose = true,

–使用ssh协议连接到目标服务器,如果端口不是22,请修改一下。

rsh = “/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no”

},

}

end

//重启服务

systemctl restart lsyncd

# 新建源目录

mkdir /lsync_10

[root@host10 lsync_10]# pwd

/lsync_10

[root@host10 lsync_10]# mkdir test10;echo “host10 add test” >>test10/host10

在目标服务器上操作

# 创建目录

mkdir /lsync_test

测试

配置没什么语法错误,重启服务后就会立即将源服务器的对应目录同步到指定的多个目标服务器对应目录下

# 在源服务器查看lsync同步日志

# tail -f /var/log/lsyncd* -n0

tail: /var/log/lsyncd.status: file truncated

==> /var/log/lsyncd.log <==

Sat Dec 12 12:00:19 2020 Normal: Calling rsync with filter-list of new/modified files/dirs

/test10/

/

Sat Dec 12 12:00:19 2020 Normal: Calling rsync with filter-list of new/modified files/dirs

/test10/

/

Sat Dec 12 12:00:19 2020 Normal: Finished a list after exitcode: 0

Sat Dec 12 12:00:19 2020 Normal: Calling rsync with filter-list of new/modified files/dirs

/test10/host10

/test10/

/

Sat Dec 12 12:00:19 2020 Normal: Finished a list after exitcode: 0

Sat Dec 12 12:00:19 2020 Normal: Calling rsync with filter-list of new/modified files/dirs

/test10/host10

/test10/

/

Sat Dec 12 12:00:19 2020 Normal: Finished a list after exitcode: 0

Sat Dec 12 12:00:19 2020 Normal: Finished a list after exitcode: 0

#确认目标服务器对应路径下是否获取到源服务器同步过来的最新数据

[root@host46 lsync_test]# ll

total 0

drwxr-xr-x 2 root root 19 Dec 12 2020 test10

[root@host46 lsync_test]# cat test10/host10

host10 add test

[root@host46 lsync_test]#

[root@host48 lsync_test]# ll

total 0

drwxr-xr-x 2 root root 19 Dec 12 2020 test10

[root@host48 lsync_test]# cat test10/host10

host10 add test

[root@host48 lsync_test]#

测试结果

完成了一对多的数据实时同步

思考

单向数据同步情况下,删除了目标节点上的源服务器同步过来的数据

1、不会触发实时同步(源–>目标)

2、不会影响源服务器上的数据(单向同步)

3、单向数据同步下,删除源服务器上的数据也不会触发删除已同步给目标服务器上的数据(未指定–delete参数)

实现数据双向实时同步

1、目标节点上也要安装lsync软件包及相应的配置文件

2、双向节点上的数据发生变化都会触发实时数据同步操作