Files
Linux/存储/NFS/Nfs高可用实现Rsync+Inotify.md
offends cee91802b3
Some checks failed
continuous-integration/drone Build is failing
synchronization
2025-08-25 15:57:40 +08:00

222 lines
5.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

> 本文作者:丁辉
# 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队列中可以排队的事件数量上限。如果你的应用程序需要监控大量的文件你可能需要增加这个值以确保不会丢失任何事件。但是要注意不要将其设置得过高以免消耗过多的内存资源。 |