一步一步搭建Nginx高可用(二) :Nginx配置双向同步

一步一步搭建Nginx高可用(二): Nginx配置双向同步

一、背景描述

文章接:一步一步搭建Nginx高可用(一)
高可用是搭建好了,但是有一个问题Nginx配置文件的CRUD如何做到两台服务器一致呢?
服务器:10.20.11.80
服务器:10.20.11.81
同步目录:/usr/local/nginx/conf 该目录下所有文件
目标实现两台服务器,该目录双向实时同步。

二、Centos7下数据同步

①、为什么要用Rsync+sersync架构?

1.1、sersync是基于Inotify开发的,类似于Inotify-tools的工具

1.2、sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的这个文件或者这个目录。

②、Rsync+Inotify-tools与Rsync+sersync这两种架构有什么区别?

2.1、Rsync+Inotify-tools

(1):Inotify-tools只能记录下被监听的目录发生了变化(包括增加、删除、修改),并没有把具体是哪个文件或者哪个目录发生了变化记录下来;

(2):rsync在同步的时候,并不知道具体是哪个文件或者哪个目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此,效率很低。

2.2、Rsync+sersync

(1):sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字;

(2):rsync在同步的时候,只同步发生变化的这个文件或者这个目录(每次发生变化的数据相对整个同步目录数据来说是很小的,rsync在遍历查找比对文件时,速度很快),因此,效率很高。

小结:当同步的目录数据量不大时,建议使用Rsync+Inotify-tools;当数据量很大(几百G甚至1T以上)、文件很多时,建议使用Rsync+sersync。

三、使用rsync+inotify-tools实现数据实时同步

①、安装rsync、inotify-tools

注:先实现Master 作为源服务器,实时修改的数据通过rsync同步到Slave 81服务器!
具体步骤:80服务器安装rsyncd服务,81服务器安装inotify-tools,inotify-tools实时监测本地文件状态,如果有更新、删除、修改等实时推送到80服务器!
双向实时同步反之即可。

yum -y install rsync
yum install -y inotify-tools

已安装

[root@nginx-174-master-80 ~]# rpm -qa xinetd
xinetd-2.3.15-14.el7.x86_64
[root@nginx-174-master-80 ~]# rpm -qa rsync
rsync-3.1.2-10.el7.x86_64
[root@nginx-174-master-80 ~]# rpm -qa in*
inotify-tools-3.14-9.el7.x86_64
②、配置rsync.conf
[root@nginx-174-master-80 conf]# cat /etc/rsyncd.conf
##rsync config start
##created by root 2016-08-08 15:00
##syncd.conf config start 以上为注释部分
uid = nobody
gid = nobody
use chroot = no
max connetctions = 200
timeout = 100
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[nginx]
   path = /usr/local/nginx
   ignore errors
   read only = false
   list = false
   hosts allow = 10.20.11.0/24
   hosts deny = 0.0.0.0/32
   auth users = nginx_conf
   secrets file = /etc/rsync.password
##rsync config end
③、新增密码验证文件
[root@nginx-174-master-80 conf]# cat /etc/rsync.password
nginx_conf:gjgOIEgklie389dklkg #用户名:密码
[root@nginx-174-master-80 conf]# chmod 600 /etc/rsync.password
[root@nginx-174-master-80 conf]# ls -l /etc/ |grep rsync.password
-rw-------   1 root root       51 Sep  7 15:18 rsync.password

注:设置rsync 开机启动并启动rsyncd服务,systemctl enable rsyncd 、systemctl start rsyncd !

④、防火墙放行rsync 端口
[root@nginx-174-master-80 conf]# firewall-cmd --permanent --add-port=873/tcp
[root@nginx-174-master-80 conf]# firewall-cmd --reload
⑤、客户端验证是否可以通过rsync同步文件

注:先安装rsync 工具,然后创建密码文件。

[root@nginx-174-slave-81 rsync]#  yum -y install rsync 
#安装rsync 软件
[root@nginx-174-slave-81 rsync]# cat /etc/rsync.password
gjgOIEgklie389dklkg #用户名:密码

将rsync服务器文件同步到本地

[root@nginx-174-slave-81 rsync]# rsync -avzp --port 873 --progress --delete nginx_conf@10.20.11.80::nginx /usr/local/src/rsync/ --password-file=/etc/rsync.password

同步本地文件到服务器!

[root@nginx-174-slave-81 rsync]# /usr/bin/rsync -vzrtopg --progress --delete --password-file=/etc/rsync80.password /usr/local/nginx/conf nginx_conf@10.20.11.80::nginx
⑥、配置inotify-tools实时同步文件

6.1、安装inotify-tools 工具

[root@nginx-174-slave-81 ~]# yum isntall -y inotify-tools

6.2、创建目录/mnt/autoshell,并在此目录上编写脚本文件inotify.sh,(设置755权限)内容如下:

[root@nginx-174-slave-81 rsync]# touch /mnt/autoshell/inotify.sh
[root@nginx-174-slave-81 rsync]# chmod 755 /mnt/autoshell/inotify.sh
[root@nginx-174-slave-81 vhost]# cat /mnt/autoshell/inotify.sh
#!/bin/bash
SRC="/usr/local/nginx/conf"
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %f%e' -e modify,delete,create,attrib ${SRC} | while read files
do
  /usr/bin/rsync -vzrtopg --progress --delete --password-file=/etc/rsync80.password /usr/local/nginx/conf nginx_conf@10.20.11.80::nginx
  echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done

inotify.sh 实时监测本地/usr/local/nginx/conf 目录下文件变化状态,如果有modify,delete,create,attrib状态的文件实时推送到80服务器。

6.3、设置脚本开机自启

chmod +x /etc/rc.d/rc.local
echo 'nohup /bin/bash /mnt/autoshell/inotify.sh' >> /etc/rc.d/rc.local

6.4、双向实时同步
Slave(81)作为源服务器,实时修改的数据通过rsync同步到Master 80服务器!
81服务器安装rsyncd服务,并放行873端口。
80服务器安装inotify-tools,实时监测本地文件状态,有变更时推送给81.

四、重启服务器验证

①、rsyncd服务无法正常启动

[root@nginx-174-slave-81 vhost]# systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemon
   Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Wed 2021-09-08 10:26:14 CST; 4min 48s ago
  Process: 701 ExecStart=/usr/bin/rsync --daemon --no-detach $OPTIONS (code=exited, status=10)
 Main PID: 701 (code=exited, status=10)

Sep 08 10:26:14 nginx-174-slave-81 systemd[1]: Started fast remote file copy program daemon.
Sep 08 10:26:14 nginx-174-slave-81 rsyncd[701]: Unknown Parameter encountered: "max connetctions"
Sep 08 10:26:14 nginx-174-slave-81 systemd[1]: rsyncd.service: main process exited, code=exited, status=10/n/a
Sep 08 10:26:14 nginx-174-slave-81 systemd[1]: Unit rsyncd.service entered failed state.
Sep 08 10:26:14 nginx-174-slave-81 systemd[1]: rsyncd.service failed.

无奈将其加入/etc/rc.local

echo "rsync --daemon" >>/etc/rc.local
cat /etc/rc.local

参考:https://blog.csdn.net/xili2532/article/details/110918902?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nginx高可用配置可以通过使用keepalived和nginx结合来实现。在配置高可用性时,需要在多个服务器上安装nginx,并确保它们都使用相同的配置文件。以下是nginx高可用配置的一般步骤: 1. 首先,确保在所有服务器上安装了nginx。你可以通过下载nginx安装包,解压、配置和编译来完成安装。然后启动nginx进程。 2. 然后,你需要安装keepalived,它是一个用于实现高可用性的工具。使用xftp将脚本拖到服务器上,并在windows环境下编写脚本。脚本的作用是检测nginx进程是否在运行,如果没有运行,则启动nginx进程。如果nginx进程在启动后2秒内仍未运行,脚本会使用killall命令关闭keepalived。 3. 在每个服务器上创建nginx配置文件(通常是nginx.conf),并放置在相同的目录下(例如/usr/local/nginx/conf)。配置文件的基本内容框架包括全局区和http区块。在http区块中,可以设置虚拟主机和定位。 4. 在keepalived配置文件中,你需要指定虚拟IP地址和检测脚本的路径。配置文件通常位于/etc/keepalived/keepalived.conf,你可以根据自己的需求进行修改。确保所有服务器上的配置文件保持一致。 5. 启动keepalived服务,它将负责监控nginx进程,并在主服务器故障时切换到备份服务器。你可以使用命令`service keepalived start`启动keepalived服务。 通过这样的配置,当主服务器发生故障时,keepalived将自动切换到备份服务器,确保服务的高可用性。请注意,高可用配置需要根据具体的环境和需求进行调整,上述步骤仅为一般参考。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Nginx安装配置高可用](https://blog.csdn.net/lanyue1/article/details/89303090)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [nginx 学习 --->>> nginx 配置高可用集群](https://blog.csdn.net/m0_55668855/article/details/123611958)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值