> 本文作者:丁辉 # Nfs高可用实现Rsync+Inotify | 节点名称 | IP | 角色 | 同步目录 | | :----------: | :----------: | :--------: | :------: | | nfs-master-1 | 192.168.1.10 | NFS主节点1 | /data | | nfs-master-2 | 192.168.1.20 | NFS主节点2 | /data | > 适用架构: amd64 > > 适用架构: arm64 ## 开始部署 rsync [Rsync常用参数解释](https://gitee.com/offends/Linux/blob/main/%E5%AD%98%E5%82%A8/NFS/Rsync%E5%B8%B8%E7%94%A8%E5%8F%82%E6%95%B0%E8%A7%A3%E9%87%8A.md) > 所有节点执行 1. 安装 rsync - Centos ```bash yum install rsync -y ``` - Ubuntu ```bash apt install rsync -y ``` 2. 配置 rsync 配置文件 > 备份配置文件 > > ```bash > mv /etc/rsyncd.conf /etc/rsyncd.conf.bak > ``` 获取配置文件 [配置文件地址](https://gitee.com/offends/Linux/blob/main/File/Conf/rsyncd.conf) ```text curl -so /etc/rsyncd.conf https://gitee.com/offends/Linux/raw/main/File/Conf/rsyncd.conf ``` 替换内容如下(根据自己环境配置进行修改) - nfs-master-1 ```bash sed -i 's#comment = none#comment = backup to nfs-master-2#g' /etc/rsyncd.conf sed -i 's#hosts allow = none#hosts allow = 192.168.1.20/24#g' /etc/rsyncd.conf ``` - nfs-master-2 ```bash sed -i 's#comment = none#comment = backup to nfs-master-1#g' /etc/rsyncd.conf sed -i 's#hosts allow = none#hosts allow = 192.168.1.10/24#g' /etc/rsyncd.conf ``` 3. 相互配置认证文件 > 格式为: '用户:用户密码' (根据自己需求修改) - nfs-master-1 和 nfs-master-2 ```bash echo 'rsync:password' > /etc/rsync_salve.pass chmod 600 /etc/rsync_salve.pass echo "password" > /etc/rsync.paschmod 600 /etc/rsync_salve.passs chmod 600 /etc/rsync.pass ``` 4. 启动服务 - Centos ```bash systemctl enable rsyncd.service systemctl start rsyncd.service ``` - Ubuntu ```bash systemctl enable rsync.service systemctl start rsync.service ``` 5. 同步测试 - 192.168.1.10 传输文件到 192.168.1.20 ```bash rsync -arv --delete /data/ rsync@192.168.1.20::data --password-file=/etc/rsync.pass ``` - 192.168.1.20 传输文件到 192.168.1.10 ```bash rsync -arv --delete /data/ rsync@192.168.1.10::data --password-file=/etc/rsync.pass ``` > 开放防火墙 > > ```bash > iptables -A INPUT -p tcp --dport 873 -j ACCEPT > ``` ## 开始部署 inotify 1. 安装 inotify - Centos ```bash yum install epel-release -y yum install inotify-tools -y ``` - Ubuntu ```bash apt install inotify-tools ``` >Ubuntu配置文件位置:`/etc/default/rsync` 2. 获取配置文件 > 脚本经过优化, 可良好降低服务器消耗 [同步脚本地址](https://gitee.com/offends/Linux/blob/main/File/Shell/rsync_inotify.sh) ```bash curl -so /opt/rsync_inotify.sh https://gitee.com/offends/Linux/raw/main/File/Shell/rsync_inotify.sh && chmod 777 /opt/rsync_inotify.sh ``` 3. 配置 inotify 同步脚本(根据自己环境配置进行修改) - nfs-master-1 ```bash sed -i 's#HOST=none#HOST=192.168.1.20#g' /opt/rsync_inotify.sh ``` - nfs-master-2 ```bash sed -i 's#HOST=none#HOST=192.168.1.10#g' /opt/rsync_inotify.sh ``` **参数解释** - `modify`:文件内容被修改。 - `create`:文件或目录被创建。 - `delete`:文件或目录被删除。 - `attrib`:文件或目录的属性被修改。 - `close_write`:文件被关闭(通常在写入完成后触发)。 - `move`:文件或目录被移动。 4. 将 inotify 纳入 system 管理 > 我觉得网上的 `nohup sh /opt/rsync_inotify.sh &` 的启动方式肯定是没有 system 管理合适的 将 inotify 纳入 system 管理 [Service文件地址](https://gitee.com/offends/Linux/blob/main/File/Service/inotify.service) ```bash curl -so /etc/systemd/system/inotify.service https://gitee.com/offends/Linux/raw/main/File/Service/inotify.service ``` 5. 启动 inotify ```bash systemctl daemon-reload systemctl enable inotify systemctl start inotify ``` 6. 配置全量备份定时任务 ```bash crontab -e ``` - nfs-master-1 ```bash * */2 * * * rsync -avzP --password-file=/etc/rsync.pass /data/ rsync@192.168.1.20::data ``` - nfs-master-2 ```bash * */2 * * * rsync -avzP --password-file=/etc/rsync.pass /data/ rsync@192.168.1.10::data ``` ## 优化 inotify > 修改 `/proc/sys/fs/inotify` 目录下的文件,调大数值即可 **参数解释** | 参数 | 解释 | | :----------------: | :----------------------------------------------------------: | | max_user_watches | 这个参数定义了每个Inotify实例能够监控的文件或目录数量上限。如果你需要监控大量的文件或目录,你可能需要增加这个值。 | | max_user_instances | 这个参数定义了每个用户能够创建的Inotify实例的数量上限。如果你的应用程序需要同时监控多个目录,你可能需要增加这个值。同样,要注意不要将其设置得过高,以免占用过多系统资源。 | | max_queued_events | 这个参数定义了Inotify队列中可以排队的事件数量上限。如果你的应用程序需要监控大量的文件,你可能需要增加这个值,以确保不会丢失任何事件。但是,要注意不要将其设置得过高,以免消耗过多的内存资源。 |