如何使用文件复制同步两个 CentOS 8 服务器

介绍

所有在线业务都需要考虑增长。 随着企业网站的访问者越来越多,底层基础设施需要扩展以提供与访问者习惯的相同水平的性能。 水平扩展,添加更多服务器而不是增加现有服务器的能力,是一种简单的方法来构建我们的 Web 服务器处理更大量的流量并保护我们免受硬件故障的能力。 确保其他 Web 服务器具有相同的文件和数据是一项潜在的耗时且具有挑战性的任务。 使用免费的开源软件(例如 lsyncd)自动执行该任务是确保我们拥有将文件从一台服务器复制到另一台服务器的安全、可靠且可重复的方法的一种方式。

先决条件

本文假设我们正在使用两个或更多核心管理的 CentOS 8 服务器运行 Apache 在默认配置中。 软件安装可能因操作系统和使用的默认软件配置而异。 我们还假设对 Vim 的功能有基本的了解。 任何文本编辑器都可以代替 Vim 工作。

使用的软件

本文探讨了一种在运行的两个 Web 服务器之间同步数据的方法 Apache 利用名为 Lsyncd 的开源软件。 引用 GitHub 页面上的描述:

“Lsyncd 监视本地目录树事件监视器接口(inotify 或 fsevents)。 它在几秒钟内聚合和组合事件,然后生成一个(或多个)进程以同步更改。 默认情况下,此过程使用 rsync。”

https://github.com/axkibe/lsyncd

这表示 lsyncd 在后台运行并跟踪我们对指定文件夹中的文件所做的更改。 它将在短时间内收集这些更改,然后通过使用 rsync 和 SSH 将文件更改“推送”到远程服务器来处理它们。 对我们来说主要的好处是:

  • Lsyncd 是免费的——这款免费软件可以免费下载和配置,无需任何软件或使用费用。
  • 设置很简单——我们只需要安装一个包,配置文件使用 Lua(虽然语法相当简单)。
  • 由可靠的技术组成 – rsync 和 SSH 是旧的、使用良好的实用程序,可在每台基于 Linux 的机器上轻松使用。

由于免费使用权和易于设置,Lsyncd 是一个完美的实用程序,可以在两台或多台主机之间同步数据。 我们可能希望跨主机同步数据的几个示例包括:

  • 负载平衡传入请求[1] – 当流量水平相对较低(或间歇性)或不经常访问新的和修改的内容时,此方法效果最佳。
  • 高可用性 – 请记住,高可用性有多个方面。 使用 lsyncd 将数据推送到另一个可以在发生硬件故障时接管的主机是一个很好的用例。
  • 实时/运行备份 – 保存已更改文件和文件夹的运行记录的好方法将确保我们将更改推送到第二台主机以进行备份。

[1] – 如果我们有一个高流量站点,我们最好使用我们的 Web 节点可以同时访问的共享文件系统。

不幸的是,在确定这是否是跨多个服务器同步数据的最佳方式时,我们需要考虑使用 Lsyncd 的一些缺点。

Lsyncd 是一种基于单向推送的实用程序。 这意味着我们有一个主服务器,我们可以在其中创建或编辑文件,然后主服务器将更改“推送”到附加的从节点。

在从节点上所做的任何更改都不会被主节点或其他节点拾取或共享。 此外,并非所有更改都被推出主服务器。 创建、删除或修改内容的文件被推送出去; 但是,所有权和权限更改不会传输到从节点。

  • Lsyncd 不是实时同步机制。 推送更改的默认时间范围是每 15 秒(尽管必要时可以在配置设置中进行修改)。
  • Lsyncd 是一种基于单向推送的实用程序。 这意味着我们有一个主服务器,我们可以在其中创建或编辑文件,然后主服务器将更改“推送”到附加的从节点。
  • 在从节点上所做的任何更改都不会被主节点或其他节点拾取或共享。 此外,不会将任何更改推出主服务器。 创建、删除或修改内容的文件被推送出去; 但是,所有权和权限更改不会传输到从节点。

基本配置

添加 EPEL 回购

要开始设置 Lsyncd,我们需要添加包含 Lsyncd 包的软件存储库。 这可以使用以下命令轻松执行:

root@alt ~]# yum -y install epel-release

安装需要一点时间,我们会看到相当数量的输出。 一旦它说“完成!” 我们可以继续前进。

[root@alt ~]# yum -y install epel-release
Loaded plugins: fastestmirror, langpacks, priorities
Loading mirror speeds from cached hostfile
1 packages excluded due to repository priority protections
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=====================================================================
 Package Arch Version Repository Size
=====================================================================
Installing:
 epel-release noarch 7-11 system-extras 15 k
Transaction Summary
=====================================================================
Install 1 Package
Total download size: 15 k
Installed size: 24 k
Downloading packages:
epel-release-7-11.noarch.rpm | 15 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : epel-release-7-11.noarch 1/1
  Verifying : epel-release-7-11.noarch 1/1
Installed:
  epel-release.noarch 0:7-11
Complete!

现在,我们需要确保我们刚刚设置的存储库已启用。 为此,我们要查看 repo 文件本身。

[root@alt ~]# vim /etc/yum.repos.d/epel.repo

然后我们需要确保将 repo 设置为 启用=1。

[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
#baseurl=https://download.fedoraproject.org/pub/epel/7/$basearch
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
***enabled=1***
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

安装 Lsyncd 软件

现在我们可以使用以下命令安装 Lsyncd 软件。

root@alt ~]# yum -y install lsyncd

此过程需要一些时间才能完成,然后我们可以在看到“完成!”后继续设置。

[root@alt ~]# yum -y install lsyncd
Loaded plugins: fastestmirror, langpacks, priorities
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 16 kB 00:00:00
 * epel: mirrors.BYNSS.com
epel | 5.4 kB 00:00:00
(1/3): epel/x86_64/group_gz | 90 kB 00:00:00
(2/3): epel/x86_64/updateinfo | 1.0 MB 00:00:00
(3/3): epel/x86_64/primary_db | 6.9 MB 00:00:00
166 packages excluded due to repository priority protections
Resolving Dependencies
--> Running transaction check
---> Package lsyncd.x86_64 0:2.2.2-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
======================================================================
 Package Arch Version Repository Size
======================================================================
Installing:
 lsyncd x86_64 2.2.2-1.el7 epel 83 k
Transaction Summary
======================================================================
Install 1 Package
Total download size: 83 k
Installed size: 227 k
Downloading packages:
warning: /var/cache/yum/x86_64/7/epel/packages/lsyncd-2.2.2-1.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY] 0.0 B/s | 0 B --:--:-- ETA
Public key for lsyncd-2.2.2-1.el7.x86_64.rpm is not installed
lsyncd-2.2.2-1.el7.x86_64.rpm | 83 kB 00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
 Userid : "Fedora EPEL (7) <[email protected]>"
 Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
 Package : epel-release-7-11.noarch (@system-extras)
 From : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : lsyncd-2.2.2-1.el7.x86_64 1/1
  Verifying : lsyncd-2.2.2-1.el7.x86_64 1/1
Installed:
  lsyncd.x86_64 0:2.2.2-1.el7
Complete!

在 Master 上配置 SSH

现在已经安装了 Lsyncd 包,我们需要确保主主机可以将文件推送到从主机,而无需用户干预。 我们将使用 SSH 密钥完成此操作。 出于本教程的目的,我们将假设当前没有安装其他 SSH 密钥。 要开始此过程,我们可以使用以下命令创建 SSH 密钥。

root@host # ssh-keygen -t rsa (or)
root@host # ssh-keygen -t rsa -b 4096 -C "$(whoami)@$(hostname)-$(date -u +%Y-%m-%d-%H:%M:%S%z)" 

注意:我们可以使用第二个命令来生成更强的密钥。

这个 SSH 密钥创建过程会问几个问题。 对于本教程,我们将使用默认值且不添加密码。

Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/username/.ssh/id_rsa.
Your public key has been saved in /home/username/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:rCwcRH+3vxiIsrkrwikDaE1UlTi8vr0g/wOfwSowCsw [email protected]:14:45+0000
The key's randomart image is:
+---[RSA 4096]----+
|. . . . |
| = o . . |
|o.= . . . . |
|oDo . . . . . |
|B. .. Y . |
|O+.. o . . |
|O++.o o . . . |
|=*. . ... . . o. |
|.o.=+.++. . . |
+----[SHA256]-----+
root@alt [~]#

生成 SSH 密钥后,我们将把公钥传输到我们的从属主机。 这个过程将允许我们在不需要输入密码的情况下验证和访问该主机。 我们可以使用以下命令转移密钥。

root@alt [~]# ssh-copy-id [email protected]

接下来,我们需要提供一次密码(因为 SSH 密钥还没有到位),然后我们就可以使用新的 SSH 密钥访问从站了。

[root@alt ~]# ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'opt.thisisnotadomain.com (209.59.144.32)' can't be established.
ECDSA key fingerprint is SHA256:R+KfXlPf2mvWLCYs89sobGJZ/1IUsHvO9fne4/4EvJ0.
ECDSA key fingerprint is MD5:9d:bd:7d:d2:66:6d:cd:8b:d2:ba:dc:d5:bc:6a:02:71.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
[root@alt ~]#

为了确保 lsyncd 可以使用我们创建的 SSH 密钥,我们需要修改主服务器上的 SSH 配置文件以添加一些信息片段。

root@alt [~]# vim ~/.ssh/config

使用 vim 编辑配置文件,并添加以下信息。

Host dest_host
 Hostname 172.16.144.32
 User root
 IdentityFile ~/.ssh/id_rsa

如您所见,我们创建了一个指定目标主机的条目,为其命名(在本例中为 dest_host)、与主机名关联的 IP (172.16.144.32)、我们将作为身份验证的用户(我们正在使用root),以及 SSH 私钥在主控主机上的位置 (~/.ssh/id_rsa)。

在 Master 上配置 Lsyncd

接下来,我们需要修改 Lsyncd 配置文件。 我们需要指定以下设置:

  • 常规日志文件位置
  • 状态日志文件位置
  • 写入状态文件的频率

我们还需要定义以下具体设置来同步数据:

  • 同步方法
  • 我们要同步的文件的源文件夹(我们使用 /var/www/html)
  • 目标主机 (/var/www/html)
  • 我们要同步的文件的目标文件夹

首先,使用 vim 命令编辑配置文件。

root@alt [~]# vim /etc/lsyncd.conf

从那里,我们修改配置以匹配上述参数。
要记住的事情:

  • 我们对大多数这些设置使用默认值。 我们还增加了 statusInterval 选项以更频繁地写入。 默认值为 10 秒,但我们选择了 1 秒。
  • 这 ”主机=选项” 应该在编辑我们的 SSH 配置文件时指定我们在上面给主机的名称。 在这种情况下,它是 dest_host.
  • 配置文件是用 Lua 编写的。 注意间距和“” 字符。这种格式是必需的并且是有目的的。
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd-status.log",
statusInterval = 10
}

-- Slave server configuration

sync {
default.rsync,
source="/var/www/",
target="IP:/var/www/",

rsync = {
compress = true,
acls = true,
verbose = true,
owner = true,
group = true,
perms = true,
rsh = "/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no"
}
}

现在我们已经安装、配置了 Lsyncd,并且我们的 SSH 密钥就位(以允许无用户身份验证),运行以下命令来启动和启用该服务。

[root@alt lsyncd]# systemctl start lsyncd
[root@alt lsyncd]# systemctl enable lsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/lsyncd.service to /usr/lib/systemd/system/lsyncd.service.
[root@alt lsyncd]#

现在 Lsyncd 正在运行,我们可以验证它是否正在监视文件夹以了解状态日志中的任何更改。

root@alt [~]# cd /var/log/lsyncd
[root@alt lsyncd]# tail lsyncd.status
Lsyncd status report at Thu Feb 6 10:07:37 2020

Sync1 source=/var/www/html/
There are 0 delays
Excluding:
  Nothing.

Inotify watching 1 directories
  1: /var/www/html/

我们将在日志中注意到文件正在从主服务器同步到从服务器。 在初始化 lsyncd 服务之前,这些是每台服务器上 /var/www/html 文件夹的内容:

[root@alt ~]# cd /var/www/html
[root@alt html]# ll
total 20
drwxr-xr-x 2 root root 4096 Feb 6 09:45 .
drwxr-xr-x 4 root root 4096 Feb 1 03:20 ..
-rw-r--r-- 1 root root 420 Feb 6 09:45 index.html
-rw-r--r-- 1 root root 7528 Feb 6 09:45 styles.css
[root@alt html]#

[root@opt ~]# cd /var/www/html
[root@opt html]# ll
total 0
[root@opt html]#

片刻之后,Lsyncd 服务将获取 alt 文件夹中的更改并将其与 opt 上的目标文件夹进行比较。 如果注意到更改,它将把这些文件修改推送到从节点。 我们可以查看主 Lsyncd 日志以验证传输是否发生,以及传输了哪些文件。

[root@alt ~]# cd /var/log/lsyncd
[root@alt lsyncd]# cat lsyncd.log

Tue Feb 11 08:07:28 2020 Normal: Rsyncing list
/
/index.html
/styles.css
Tue Feb 11 08:07:28 2020 Normal: Finished (list): 0
[root@alt lsyncd]#

在查看从节点上的文件夹时,我们可以看到传输的文件现在位于目标目录中。

[root@opt ~]# hostname
opt.thisisnotadomain.com
[root@opt ~]# ll /var/www/html
total 12
-rw-r--r-- 1 root root 420 Feb 6 09:45 index.html
-rw-r--r-- 1 root root 7528 Feb 6 09:45 styles.css
[root@opt ~]#

结论

现在我们已经运行了服务,Lsyncd 将在系统重新启动时启动,并将持续监视任何更改,然后将更改推送到从节点。 此方法是一个简单的系统,适用于特定用例并且易于配置。 最终,我们试图解决的问题是不能依赖即时复制的。 但是,Lsyncd 的好处在于,对于大多数不运行负载平衡设置或需要在另一台机器上备份文件的人来说,好处远远超过任何缺点。