说明

NFS高可用目的

部署NFS集群热备高可用环境,用作jumpserver集群的远程存储,实现jumpserver数据持久化。

NFS高可用思路

NFS + Keepalived 实现高可用,防止单点故障。

Rsync+Inotify 实现主备间共享数据进行同步。

环境准备

IP地址 角色 软件
10.0.80.31 NFS-Master NFS、keepalived、Rsync、Inotify_tools
10.0.80.32 NFS-Slave1 NFS、keepalived、Rsync、Inotify_tools
10.0.80.33 NFS-Slave2 NFS、keepalived、Rsync、Inotify_tools
10.0.80.220 VIP

技术要求

  • 三个NFS节点机器的配置要一致
  • keepalived监控nfs进程,master的nfs主进程宕掉无法启动时由slave的nfs接管继续工作。
  • jumpserver的数据备份到slave,同时master和slave数据用rsync+inotify实时同步,保证数据完整性。
  • 生产环境下,最好给NFS共享目录单独挂载一块硬盘或单独的磁盘分区。

安装部署NFS服务

(Master和两台Slave机器同样操作)

安装nfs

1
yum -y install nfs-utils

创建nfs共享目录

1
mkdir /data/

设置共性属性

编辑export文件,jumpserver节点挂载nfs共享目录,这里可以使用jumpserver节点的ip网段进行挂载配置也可以直接使用jumpserver节点的具体ip(一个ip配置一行)进行挂载配置。

vim /etc/exports

1
/data/k8s_storage 10.0.80.0/24(rw,sync,no_root_squash)

使配置生效

1
exportfs -r

查看生效

1
2
exportfs
/data 10.0.80.0/24

启动rpcbind、nfs服务

1
2
systemctl restart rpcbind && systemctl enable rpcbind
systemctl restart nfs && systemctl enable nfs

查看 RPC 服务的注册状况

1
rpcinfo -p localhost

showmount测试

Master节点测试

1
2
3
showmount -e 10.0.80.31
Export list for 10.0.80.31:
/data 10.0.80.0/24

Slave节点测试

1
2
3
showmount -e 10.0.80.32
Export list for 10.0.80.32:
/data 10.0.80.0/24

或者在三台机器任意一个节点上手动尝试挂载NFS,看是否挂载成功(我在10.0.80.32上测试的):

1
2
3
4
5
6
7
8
9
10
11
12
mkdir /haha     #<===== 创建挂载目录
mount -t nfs 10.0.80.31:/data/ /haha/ #<=====挂载nfs
df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda5 192G 2.2G 190G 2% /
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 8.9M 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda2 797M 143M 654M 18% /boot
tmpfs 783M 0 783M 0% /run/user/0
10.0.80.31:/data 192G 2.2G 190G 2% /haha #<===== nfs已经挂载成功

安装部署keepalived

(Master和两台Slave机器同样操作)

安装keepalived

1
yum -y install keepalived

Master节点的keepalived.conf配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[root@sz19f-mysql-redis-nfs-10-0-80-31-vm.belle.lan:/root]
#cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bak #备份配置文件

[root@sz19f-mysql-redis-nfs-10-0-80-31-vm.belle.lan:/root]
#>/etc/keepalived/keepalived.conf

[root@sz19f-mysql-redis-nfs-10-0-80-31-vm.belle.lan:/root]
#vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id master #id可以随便设
}
vrrp_script chk_nfs {
script "/etc/keepalived/nfs_check.sh" #监控脚本
interval 2
weight -30 #keepalived部署了两台,所以设为30,如果两台就设为20
}
vrrp_instance VI_1 {
state BACKUP #两台主机都设为backup非抢占模式
interface ens192 #网卡名写自己的,不要照抄
virtual_router_id 61
priority 100 #master设为100,backup设为80和60,反正要比100小
advert_int 1
nopreempt #设置为非抢占模式必须要该参数
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nfs
}
virtual_ipaddress {
10.0.80.220 #虚拟ip
}
}

这里特别需要注意:
上面配置中一定要设置keepalived为非抢占模式,如果设置成抢占模式会在不断的切换主备时容易造成NFS数据丢失。

Slave1 节点的keepalived.conf配置

只需将priority参数项修改为80,其他配置的和master节点一样,脚本也一样。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[root@sz19f-mysql-redis-nfs-10-0-80-32-vm.belle.lan:/root]
# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bak

[root@sz19f-mysql-redis-nfs-10-0-80-32-vm.belle.lan:/root]
# >/etc/keepalived/keepalived.conf

[root@sz19f-mysql-redis-nfs-10-0-80-32-vm.belle.lan:/root]
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id master
}
vrrp_script chk_nfs {
script "/etc/keepalived/nfs_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 61
priority 80
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nfs
}
virtual_ipaddress {
10.0.80.220
}
}

Slave2 节点的keepalived.conf配置

只需将priority参数项修改为60,其他配置的和master节点一样,脚本也一样。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[root@sz19f-mysql-redis-nfs-10-0-80-33-vm.belle.lan:/root]
# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bak

[root@sz19f-mysql-redis-nfs-10-0-80-33-vm.belle.lan:/root]
# >/etc/keepalived/keepalived.conf

[root@sz19f-mysql-redis-nfs-10-0-80-33-vm.belle.lan:/root]
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id master
}
vrrp_script chk_nfs {
script "/etc/keepalived/nfs_check.sh"
interval 2
weight -30
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 61
priority 60
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nfs
}
virtual_ipaddress {
10.0.80.220
}
}

编辑nfs_check.sh监控脚本

1
# vim /etc/keepalived/nfs_check.sh
1
2
3
4
5
6
7
8
9
#!/bin/bash
A=`ps -C nfsd --no-header | wc -l`
if [ $A -eq 0 ];then
systemctl restart nfs-server.service
sleep 2
if [ `ps -C nfsd --no-header| wc -l` -eq 0 ];then
pkill keepalived
fi
fi

设置脚本执行权限

1
# chmod 755 /etc/keepalived/nfs_check.sh

启动keepalived服务

1
# systemctl start keepalived && systemctl enable keepalived

查看服务进程是否启动

1
2
3
4
5
# ps -ef|grep keepalived
root 17979 1 0 14:39 ? 00:00:00 /usr/sbin/keepalived -D
root 17980 17979 0 14:39 ? 00:00:00 /usr/sbin/keepalived -D
root 17981 17979 0 14:39 ? 00:00:00 /usr/sbin/keepalived -D
root 18149 16786 0 14:42 pts/0 00:00:00 grep --color=auto keepalived

查看VIP是否存在

在三台节点机器上执行"ip addr"命令查看vip,其中会在一台机器上产生vip地址。

1
2
3
[root@sz19f-mysql-redis-nfs-10-0-80-31-vm.belle.lan:/etc/keepalived]
# ip a |grep 10.0.80.220
inet 10.0.80.220/32 scope global ens192

测试vip地址可以ping通就没问题

1
2
3
4
5
6
7
[root@sz19f-mysql-redis-nfs-10-0-80-32-vm.belle.lan:/root]
# ping 10.0.80.220
PING 10.0.80.220 (10.0.80.220) 56(84) bytes of data.
64 bytes from 10.0.80.220: icmp_seq=1 ttl=64 time=0.525 ms
64 bytes from 10.0.80.220: icmp_seq=2 ttl=64 time=0.247 ms
64 bytes from 10.0.80.220: icmp_seq=3 ttl=64 time=0.261 ms
64 bytes from 10.0.80.220: icmp_seq=4 ttl=64 time=0.231 ms

keepalived故障测试

掉vip所在的Master节点机器上的keepalived服务后,发现vip会自动飘移到另两台Backup机器的其中一台上才算测试成功。当该Master节点的keepalived服务重新启动后,vip不会重新飘移回来。因为keepalived采用了非抢占模式。

​ 如果keepalived设置为抢占模式,vip会在Master节点的keepalived重启恢复后自动飘回去,但是这样一直来回切换可能会造成NFS数据不完整,因此这里必须设置成非抢占模式。

​ 由于配置了nfs的nfs_check.sh监控脚本,所以当其中一台节点机器上的NFS服务宕停后会自动重启NFS。如果NFS服务重启失败,则会自动关闭该节点机器上的keepalived服务,如果该节点有vip则会自动飘移到另外一台节点上。

安装部署Rsync+Inofity

(Master和两台Slave机器同样操作)

安装rsync和inotify

1
yum -y install rsync inotify-tools

Master节点机器配置rsyncd.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@sz19f-mysql-redis-nfs-10-0-80-31-vm.belle.lan:/etc/keepalived]
# cp /etc/rsyncd.conf /etc/rsyncd.conf_bak
# >/etc/rsyncd.conf
# vim /etc/rsyncd.conf
uid = root
gid = root
use chroot = 0
port = 873
hosts allow = 10.0.80.0/24 #允许ip访问设置,可以指定ip或ip段
max connections = 0
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
log format = %t %a %m %f %b
transfer logging = yes
syslog facility = local3

[master_web]
path = /data/
comment = master_web
ignore errors
read only = no #是否允许客户端上传文件
list = no
auth users = rsync #指定由空格或逗号分隔的用户名列表,只有这些用户才允许连接该模块
secrets file = /etc/rsyncd.passwd #保存密码和用户名文件,需要自己生成

编辑密码和用户文件(格式为"用户名:密码")

1
2
3
[root@sz19f-mysql-redis-nfs-10-0-80-31-vm.belle.lan:/etc/keepalived]
# vim /etc/rsyncd.passwd
rsync:123456

编辑同步密码

(注意这个文件和上面的密码和用户文件路径不一样)
该文件内容只需要填写从服务器的密码,例如这里从服务器配的用户名密码都是rsync:123456,则主服务器则写123456一个就可以了

1
2
3
[root@sz19f-mysql-redis-nfs-10-0-80-31-vm.belle.lan:/etc/keepalived]
# vim /opt/rsyncd.passwd
123456

设置文件执行权限

1
2
3
[root@sz19f-mysql-redis-nfs-10-0-80-31-vm.belle.lan:/etc/keepalived]
# chmod +x /etc/rsyncd.passwd
# chmod +x /opt/rsyncd.passwd

启动服务

1
2
[root@sz19f-mysql-redis-nfs-10-0-80-31-vm.belle.lan:/etc/keepalived]
# systemctl enable rsyncd && systemctl restart rsyncd

检查rsync服务进程是否启动

1
2
3
4
[root@sz19f-mysql-redis-nfs-10-0-80-31-vm.belle.lan:/etc/keepalived]
# ps -ef|grep rsync
root 18474 1 0 16:21 ? 00:00:00 /usr/bin/rsync --daemon --no-detach
root 18669 5954 0 16:22 pts/0 00:00:00 grep --color=auto rsync

Slave1节点机器配置rsyncd.conf

就把master主机/etc/rsyncd.conf配置文件里的[master_web]改成[slave_web]
其他都一样,密码文件也设为一样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@sz19f-mysql-redis-nfs-10-0-80-32-vm.belle.lan:/root]
# cp /etc/rsyncd.conf /etc/rsyncd.conf_bak
# >/etc/rsyncd.conf
# vim /etc/rsyncd.conf
uid = root
gid = root
use chroot = 0
port = 873
hosts allow = 10.0.80.0/24
max connections = 0
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
log format = %t %a %m %f %b
transfer logging = yes
syslog facility = local3

[slave_web]
path = /data/
comment = master_web
ignore errors
read only = no
list = no
auth users = rsync
secrets file = /etc/rsyncd.passwd

编辑密码和用户文件(格式为"用户名:密码")

1
2
3
[root@sz19f-mysql-redis-nfs-10-0-80-32-vm.belle.lan:/root]
# vim /etc/rsyncd.passwd
rsync:123456

编辑同步密码

1
2
3
[root@sz19f-mysql-redis-nfs-10-0-80-32-vm.belle.lan:/root]
# vim /opt/rsyncd.passwd
123456

设置文件执行权限

1
2
3
[root@sz19f-mysql-redis-nfs-10-0-80-32-vm.belle.lan:/root]
# chmod +x /etc/rsyncd.passwd
# chmod +x /opt/rsyncd.passwd

启动服务

1
2
[root@sz19f-mysql-redis-nfs-10-0-80-32-vm.belle.lan:/root]
# systemctl enable rsyncd && systemctl restart rsyncd

检查rsync服务进程是否启动

1
2
3
4
[root@sz19f-mysql-redis-nfs-10-0-80-32-vm.belle.lan:/root]
# ps -ef |grep rsync
root 20568 1 0 16:36 ? 00:00:00 /usr/bin/rsync --daemon --no-detach
root 20789 5995 0 16:37 pts/0 00:00:00 grep --color=auto rsync

Slave2节点机器配置rsyncd.conf

就把master主机/etc/rsyncd.conf配置文件里的[master_web]改成[slave_web]
其他都一样,密码文件也设为一样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@sz19f-mysql-redis-nfs-10-0-80-33-vm.belle.lan:/root]
# cp /etc/rsyncd.conf /etc/rsyncd.conf_bak
# >/etc/rsyncd.conf
# vim /etc/rsyncd.conf
uid = root
gid = root
use chroot = 0
port = 873
hosts allow = 10.0.80.0/24
max connections = 0
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
log format = %t %a %m %f %b
transfer logging = yes
syslog facility = local3

[slave_web]
path = /data/
comment = master_web
ignore errors
read only = no
list = no
auth users = rsync
secrets file = /etc/rsyncd.passwd

编辑密码和用户文件(格式为"用户名:密码")

1
2
3
[root@sz19f-mysql-redis-nfs-10-0-80-33-vm.belle.lan:/root]
# vim /etc/rsyncd.passwd
rsync:123456

编辑同步密码

1
2
3
[root@sz19f-mysql-redis-nfs-10-0-80-33-vm.belle.lan:/root]
# vim /opt/rsyncd.passwd
123456

设置文件执行权限

1
2
3
4

[root@sz19f-mysql-redis-nfs-10-0-80-33-vm.belle.lan:/root]
# chmod +x /etc/rsyncd.passwd
# chmod +x /opt/rsyncd.passwd

启动服务

1
2
[root@sz19f-mysql-redis-nfs-10-0-80-33-vm.belle.lan:/root]
# systemctl enable rsyncd && systemctl restart rsyncd

检查rsync服务进程是否启动

1
2
3
4
[root@sz19f-mysql-redis-nfs-10-0-80-33-vm.belle.lan:/root]
# ps -ef |grep rsync
root 21644 1 0 16:47 ? 00:00:00 /usr/bin/rsync --daemon --no-detach
root 21764 5998 0 16:48 pts/0 00:00:00 grep --color=auto rsync

验证数据同步

这里我们先手动验证下Master节点NFS数据同步到Slave节点

在Master节点的NFS共享目录下创建测试数据

1
2
3
4
5
6
7
8
9
[root@sz19f-mysql-redis-nfs-10-0-80-31-vm.belle.lan:/data]
# cd /data
# mkdir test
# touch {a,b}
# ll
total 0
-rw-r--r-- 1 root root 0 Apr 17 16:52 a
-rw-r--r-- 1 root root 0 Apr 17 16:52 b
drwxr-xr-x 2 root root 6 Apr 17 16:52 test

手动同步Master节点的NFS共享目录数据到Slave节点的NFS共享目录下

1
2
3
[root@sz19f-mysql-redis-nfs-10-0-80-31-vm.belle.lan:/data]
# rsync -avzp --delete /data/ rsync@10.0.80.32::slave_web --password-file=/opt/rsyncd.passwd
# rsync -avzp --delete /data/ rsync@10.0.80.33::slave_web --password-file=/opt/rsyncd.passwd

到Slave节点查看

1
2
3
4
5
6
[root@sz19f-mysql-redis-nfs-10-0-80-32-vm.belle.lan:/data]
# ls /date
a b test
[root@sz19f-mysql-redis-nfs-10-0-80-33-vm.belle.lan:/data]
# ls /data/
a b test

两个slave节点已经同步过来了文件。

上面rsync同步命令说明:

  • /data/ 是同步的NFS共享目录
  • rsync@10.0.80.32::slave_web
  • rsync 是Slave节点服务器的/etc/rsyncd.passwd文件中配置的用户名
  • 10.0.80.32为Slave节点服务ip
  • slave_web 为Slave服务器的rsyncd.conf中配置的同步模块名
  • –password-file=/opt/rsyncd.passwd 是Master节点同步到Slave节点使用的密码文件,文件中配置的是Slave节点服务器的/etc/rsyncd.passwd文件中配置的密码

设置Rsync+Inotify自动同步

这里需要注意:不能设置Master和Slave节点同时执行rsync自动同步,即不能同时设置双向同步。因为Master节点将数据同步到Slave节点,如果Slave节点再将数据同步回到Master节点,这个就矛盾了。所以需要确保只有一方在执行自动同步到另一方的操作。方式就是判断当前节点服务器是否存在VIP,如存在VIP则自动同步数据到另一台节点上。如不存在VIP则不执行自动同步操作。

*+++++++ Master节点服务器操作 *+++++++

编写自动同步脚本

vim /opt/rsync_inotify.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
slave1_host=10.0.80.32
slave2_host=10.0.80.33
src=/data/
des=slave_web
password=/opt/rsyncd.passwd
user=rsync
inotifywait=/usr/bin/inotifywait

$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files ;do
rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$slave1_host::$des
rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$slave2_host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done

编写VIP监控脚本

vim /opt/vip_monitor.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash
VIP_NUM=`ip addr|grep 220|wc -l`
RSYNC_INOTIRY_NUM=`ps -ef|grep /usr/bin/inotifywait|grep -v grep|wc -l`
if [ ${VIP_NUM} -ne 0 ];then
echo "VIP在当前NFS节点服务器上" >/dev/null 2>&1
if [ ${RSYNC_INOTIRY_NUM} -ne 0 ];then
echo "rsync_inotify.sh脚本已经在后台执行中" >/dev/null 2>&1
else
echo "需要在后台执行rsync_inotify.sh脚本" >/dev/null 2>&1
nohup sh /opt/rsync_inotify.sh &
fi
else
echo "VIP不在当前NFS节点服务器上" >/dev/null 2>&1
if [ ${RSYNC_INOTIRY_NUM} -ne 0 ];then
echo "需要关闭后台执行的rsync_inotify.sh脚本" >/dev/null 2>&1
ps -ef|grep rsync_inotify.sh|grep -v grep|awk '{print $2}'|xargs kill -9
ps -ef|grep inotifywait|grep -v grep|awk '{print $2}'|xargs kill -9
else
echo "rsync_inotify.sh脚本当前未执行" >/dev/null 2>&1
fi
fi

编写持续执行脚本

vim /opt/rsync_monit.sh

1
2
3
4
5
#!/bin/bash
while [ "1" = "1" ]
do
/bin/bash -x /opt/vip_monitor.sh >/dev/null 2>&1
done

后台运行脚本

1
2
3
4
5
6
# chmod 755 /opt/rsync_inotify.sh
# chmod 755 /opt/vip_monitor.sh
# chmod 755 /opt/rsync_monit.sh

# nohup sh /opt/rsync_inotify.sh &
# nohup sh /opt/rsync_monit.sh &

设置同步脚本开机启动

1
2
# chmod +x /etc/rc.d/rc.local
# echo "nohup sh /opt/rsync_monit.sh & " >> /etc/rc.d/rc.local

*+++++++* Slave1节点服务器操作 *+++++++***

编写自动同步脚本

vim /opt/rsync_inotify.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
slave1_host=10.0.80.31
slave2_host=10.0.80.33
src=/data/
des1=master_web
des2=slave_web
password=/opt/rsyncd.passwd
user=rsync
inotifywait=/usr/bin/inotifywait

$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files ;do
rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$slave2_host::$des2
rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$slave1_host::$des1
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done

编写VIP监控脚本

vim /opt/vip_monitor.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash
VIP_NUM=`ip addr|grep 220|wc -l`
RSYNC_INOTIRY_NUM=`ps -ef|grep /usr/bin/inotifywait|grep -v grep|wc -l`
if [ ${VIP_NUM} -ne 0 ];then
echo "VIP在当前NFS节点服务器上" >/dev/null 2>&1
if [ ${RSYNC_INOTIRY_NUM} -ne 0 ];then
echo "rsync_inotify.sh脚本已经在后台执行中" >/dev/null 2>&1
else
echo "需要在后台执行rsync_inotify.sh脚本" >/dev/null 2>&1
nohup sh /opt/rsync_inotify.sh &
fi
else
echo "VIP不在当前NFS节点服务器上" >/dev/null 2>&1
if [ ${RSYNC_INOTIRY_NUM} -ne 0 ];then
echo "需要关闭后台执行的rsync_inotify.sh脚本" >/dev/null 2>&1
ps -ef|grep rsync_inotify.sh|grep -v grep|awk '{print $2}'|xargs kill -9
ps -ef|grep inotifywait|grep -v grep|awk '{print $2}'|xargs kill -9
else
echo "rsync_inotify.sh脚本当前未执行" >/dev/null 2>&1
fi
fi

编写持续执行脚本

vim /opt/rsync_monit.sh

1
2
3
4
5
#!/bin/bash
while [ "1" = "1" ]
do
/bin/bash -x /opt/vip_monitor.sh >/dev/null 2>&1
done

后台运行脚本

(只执行rsync_monit.sh)

1
2
3
4
5
# chmod 755 /opt/rsync_inotify.sh
# chmod 755 /opt/vip_monitor.sh
# chmod 755 /opt/rsync_monit.sh

# nohup sh /opt/rsync_monit.sh &

设置脚本的开机启动

1
2
# chmod +x /etc/rc.d/rc.local
# echo "nohup sh /opt/rsync_monit.sh & " >> /etc/rc.d/rc.local

*+++++++* Slave2节点服务器操作 *+++++++***

编写自动同步脚本

vim /opt/rsync_inotify.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
slave1_host=10.0.80.31
slave2_host=10.0.80.32
src=/data/
des1=master_web
des2=slave_web
password=/opt/rsyncd.passwd
user=rsync
inotifywait=/usr/bin/inotifywait

$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files ;do
rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$slave1_host::$des1
rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$slave2_host::$des2
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done

编写VIP监控脚本

vim /opt/vip_monitor.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash
VIP_NUM=`ip addr|grep 220|wc -l`
RSYNC_INOTIRY_NUM=`ps -ef|grep /usr/bin/inotifywait|grep -v grep|wc -l`
if [ ${VIP_NUM} -ne 0 ];then
echo "VIP在当前NFS节点服务器上" >/dev/null 2>&1
if [ ${RSYNC_INOTIRY_NUM} -ne 0 ];then
echo "rsync_inotify.sh脚本已经在后台执行中" >/dev/null 2>&1
else
echo "需要在后台执行rsync_inotify.sh脚本" >/dev/null 2>&1
nohup sh /opt/rsync_inotify.sh &
fi
else
echo "VIP不在当前NFS节点服务器上" >/dev/null 2>&1
if [ ${RSYNC_INOTIRY_NUM} -ne 0 ];then
echo "需要关闭后台执行的rsync_inotify.sh脚本" >/dev/null 2>&1
ps -ef|grep rsync_inotify.sh|grep -v grep|awk '{print $2}'|xargs kill -9
ps -ef|grep inotifywait|grep -v grep|awk '{print $2}'|xargs kill -9
else
echo "rsync_inotify.sh脚本当前未执行" >/dev/null 2>&1
fi
fi

编写持续执行脚本

vim /opt/rsync_monit.sh

1
2
3
4
5
#!/bin/bash
while [ "1" = "1" ]
do
/bin/bash -x /opt/vip_monitor.sh >/dev/null 2>&1
done

后台运行脚本

(只执行rsync_monit.sh)

1
2
3
4
5
# chmod 755 /opt/rsync_inotify.sh
# chmod 755 /opt/vip_monitor.sh
# chmod 755 /opt/rsync_monit.sh

# nohup sh /opt/rsync_monit.sh &

设置脚本的开机启动

1
2
# chmod +x /etc/rc.d/rc.local
# echo "nohup sh /opt/rsync_monit.sh & " >> /etc/rc.d/rc.local

最后验证下自动同步

比如当前VIP在Master节点,在Master节点创建测试数据,观察是否自动同步到Slave节点

1
2
3
4
5
6
7
[root@sz19f-mysql-redis-nfs-10-0-80-31-vm.belle.lan:/data]
# ip a |grep 10.0.80.220
inet 10.0.80.220/32 scope global ens192
# echo "1234" > test
# ll
total 4
-rw-r--r-- 1 root root 5 Apr 17 18:02 test

到Slave节点上查看,已自动同步过来

1
2
3
4
5
6
[root@sz19f-mysql-redis-nfs-10-0-80-32-vm.belle.lan:/data]
# cat test
1234
[root@sz19f-mysql-redis-nfs-10-0-80-33-vm.belle.lan:/data]
# cat test
1234 #<==== 两个slave节点都已经通不过来数据,说明自动同步没问题。

接着重启一下Master节点的keeplived,将VIP飘移到Slave节点

1
2
3
4
5
[root@sz19f-mysql-redis-nfs-10-0-80-31-vm.belle.lan:/data]
# systemctl restart keepalived
[root@sz19f-mysql-redis-nfs-10-0-80-32-vm.belle.lan:/data]
# ip a |grep 10.0.80.220
inet 10.0.80.220/32 scope global ens192 #<=====在slave节点上开vip已经飘过来

在Slave节点创建测试数据,观察是否自动同步到Master节点

1
2
3
# rm -rf /data/*
# mkdir /data/a
# echo "heihei" > /data/b

到Master节点和另一个slave节点查看,发现数据已经同步过来了

1
2
3
4
5
6
7
8
[root@sz19f-mysql-redis-nfs-10-0-80-31-vm.belle.lan:/data]
# ll
total 4
drwxr-xr-x 2 root root 6 Apr 17 21:54 a
-rw-r--r-- 1 root root 7 Apr 17 21:54 b
-rw------- 1 root root 0 Apr 17 21:31 nohup.out
# cat b
heihei

模拟Master节点和Slave节点关机,观察开机后:

/opt/rsync_monit.sh脚本会实现开机自启动。

按照上面Master和Slave节点的自动同步验证OK。