项目实践:Keepalived状态检测实现NFS主从同步复制部署

  • A+

一.处理上节已配置的问题

1.NFS客户端配置恢复

1)卸载NFS挂载点

  1. [root@mynfs02 /]# umount /mnt/mydata  
  2. [root@mynfs02 /]# umount /mnt/mylogs  
  3. [root@mynfs02 /]# umount /mnt/mytmp  
  4. [root@mynfs02 /]  
  5. [root@mynfs02 /]# df -Th  
  6. 文件系统                类型      容量  已用  可用 已用% 挂载点  
  7. devtmpfs                devtmpfs  1.9G     0  1.9G    0% /dev  
  8. tmpfs                   tmpfs     1.9G     0  1.9G    0% /dev/shm  
  9. tmpfs                   tmpfs     1.9G  9.3M  1.9G    1% /run  
  10. tmpfs                   tmpfs     1.9G     0  1.9G    0% /sys/fs/cgroup  
  11. /dev/mapper/centos-root xfs        50G  4.1G   46G    9% /  
  12. /dev/vda1               xfs      1014M  185M  830M   19% /boot  
  13. /dev/mapper/centos-home xfs        42G   37M   42G    1% /home  
  14. tmpfs                   tmpfs     379M   12K  379M    1% /run/user/42  
  15. tmpfs                   tmpfs     379M     0  379M    0% /run/user/0  
  16. [root@mynfs02 /]  

2)卸载NFS客户端工具

  1. [root@mynfs02 /]# yum erase nfs-utils  
  2. 已加载插件:fastestmirror, langpacks  
  3. 正在解决依赖关系  
  4. --> 正在检查事务  
  5. ---> 软件包 nfs-utils.x86_64.1.1.3.0-0.68.el7.2 将被删除  

3)删除本地挂载目录

  1. [root@mynfs02 /]# cd /mnt  
  2. [root@mynfs02 mnt]# ls  
  3. mydata  mylogs  mytmp  
  4. [root@mynfs02 mnt]# rm -rf *  
  5. [root@mynfs02 mnt]# ll  
  6. 总用量 0  

2.NFS服务端配置恢复

1)删除共享目录

  1. [root@mynfs01 /]# rm -rf mydata  
  2. [root@mynfs01 /]# rm -rf mylogs  
  3. [root@mynfs01 /]# rm -rf mytmp  
  4. [root@mynfs01 /]# ll  

2)卸载NFS服务端工具

  1. [root@mynfs01 ~]#  yum erase nfs-utils  
  2. 已加载插件:fastestmirror, langpacks  
  3. 正在解决依赖关系  
  4. --> 正在检查事务  
  5. ---> 软件包 nfs-utils.x86_64.1.1.3.0-0.68.el7.2 将被 删除  
  6. --> 正在处理依赖关系 nfs-utils,它被软件包 libvirt-daemon-driver-storage-core-4.5.0-33.el7.x86_64 需要  
  7. --> 正在检查事务  

3)删除原配置信息


二.基础环境规划与准备

1.服务器基础信息:

NFS服务器IP地址规划

  1. Master IP:192.168.250.193  
  2. Slave IP:192.168.250.194  
  3. VIP:192.168.250.195  

NFS服务器系统信息

  1. [root@mynfs01 /]# cat /etc/redhat-release  
  2. CentOS Linux release 7.8.2003 (Core)  
  3. [root@mynfs01 /]  
  4.   
  5. [root@mynfs02 /]# cat /etc/redhat-release  
  6. CentOS Linux release 7.8.2003 (Core)  
  7. [root@mynfs02 /]  

2.分别在MASTER和SLAVE上创建共享目录

  1. [root@mynfs01 /]# mkdir -p /mynfsdata  
  2. [root@mynfs02 /]# mkdir -p /mynfsdata  
  3. [root@mynfs02 /]  
3.关闭防火墙和selinux
  1. [root@mynfs01 /]# systemctl stop firewalld  
  2. [root@mynfs01 /]# systemctl disable firewalld  
  3. Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.  
  4. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.  
  5. [root@mynfs01 /]  
  6.   
  7. [root@mynfs01 /]# setenforce 0  
  8. [root@mynfs01 /]# vim /etc/selinux/config  
  9. [root@mynfs01 /]  
  10. SELINUX=disabled  

三.安装并配置NFS服务器

1.安装NFS和RPC服务

这里分别在master上和slave服务器上分别安装,slave的安装过程略。

  1. [root@mynfs01 /]# yum -y install nfs-utils rpcbind  
  2. 已加载插件:fastestmirror, langpacks  
  3. Loading mirror speeds from cached hostfile  
  4.  * base: mirrors.aliyun.com  
  5.  * extras: mirrors.aliyun.com  
  6.  * updates: mirrors.aliyun.com  
  7. 软件包 rpcbind-0.2.0-49.el7.x86_64 已安装并且是最新版本  
  8. 正在解决依赖关系  
  9. --> 正在检查事务  
  10. ---> 软件包 nfs-utils.x86_64.1.1.3.0-0.68.el7.2 将被 安装  
  11. --> 解决依赖关系完成  
  12.   
  13. 依赖关系解决  
  14.   
  15. ==================================================================================================  
  16.  Package                                          架构                                          版本                                                        源                                              大小  
  17. ==================================================================================================  
  18. 正在安装:  
  19.  nfs-utils                                        x86_64                                        1:1.3.0-0.68.el7.2                                          updates                                        413 k  
  20.   
  21. 事务概要  
  22. ==================================================================================================  
  23. 安装  1 软件包  
  24.   
  25. 总下载量:413 k  
  26. 安装大小:1.1 M  
  27. Downloading packages:  
  28. nfs-utils-1.3.0-0.68.el7.2.x86_64.rpm                                                                                                                                                     | 413 kB  00:00:00       
  29. Running transaction check  
  30. Running transaction test  
  31. Transaction test succeeded  
  32. Running transaction  
  33.   正在安装    : 1:nfs-utils-1.3.0-0.68.el7.2.x86_64                                                                                                                                                          1/1   
  34.   验证中      : 1:nfs-utils-1.3.0-0.68.el7.2.x86_64                                                                                                                                                          1/1   
  35.   
  36. 已安装:  
  37.   nfs-utils.x86_64 1:1.3.0-0.68.el7.2                                                                                                                                                                              
  38.   
  39. 完毕!  
  40. [root@mynfs01 /]  

2.配置NFS共享目录

分别在master和slave上配置共享目录

  1. [root@mynfs01 /]# echo '/mynfsdata *(rw,sync,all_squash)' >> /etc/exports  
  2. [root@mynfs01 /]  
  3.   
  4. [root@mynfs02 /]# echo '/mynfsdata *(rw,sync,all_squash)' >> /etc/exports  
  5. [root@mynfs02 /]  
  6. 注:All_squash:客户机上的任何用户访问该共享目录时都映射成匿名用户。  

3.设置开机自启动功能

  1. [root@mynfs01 /]# systemctl start nfs && systemctl start rpcbind  
  2. [root@mynfs01 /]# systemctl enable nfs && systemctl enable rpcbind  
  3. Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.  
  4. [root@mynfs01 /]# systemctl status nfs && systemctl status rpcbind  
  5. ● nfs-server.service - NFS server and services  
  6.    Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)  
  7.   Drop-In: /run/systemd/generator/nfs-server.service.d  
  8.            └─order-with-mounts.conf  
  9.    Active: active (exited) since 六 2022-03-05 08:10:07 CST; 2 days ago  
  10.  Main PID: 17660 (code=exited, status=0/SUCCESS)  
  11.    CGroup: /system.slice/nfs-server.service  
  12.   
  13. 3月 05 08:10:06 mynfs01 systemd[1]: Starting NFS server and services...  
  14. 3月 05 08:10:07 mynfs01 systemd[1]: Started NFS server and services.  
  15. ● rpcbind.service - RPC bind service  
  16.    Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled)  
  17.    Active: active (running) since 五 2022-03-04 08:28:47 CST; 3 days ago  
  18.  Main PID: 738 (rpcbind)  
  19.    CGroup: /system.slice/rpcbind.service  
  20.            └─738 /sbin/rpcbind -w  
  21.   
  22. 3月 04 08:28:47 mynfs01 systemd[1]: Starting RPC bind service...  
  23. 3月 04 08:28:47 mynfs01 systemd[1]: Started RPC bind service.  
  24. [root@mynfs01 /]  

四.配置文件同步功能

1.在salve上配置rsync服务

这里进行master数据同步

1)安装rsync
  1. [root@mynfs02 /]# yum -y install rsync.x86_64  
  2. 已加载插件:fastestmirror, langpacks  
  3. Loading mirror speeds from cached hostfile  
  4.  * base: mirrors.aliyun.com  
  5.  * extras: mirrors.aliyun.com  
  6.  * updates: mirrors.aliyun.com  
  7. 软件包 rsync-3.1.2-10.el7.x86_64 已安装并且是最新版本  
  8. 无须任何处理  
  9. [root@mynfs02 /]  
2)修改/etc/rsyncd.conf文件
  1. vim /etc/rsyncd.conf  
  2.   
  3. uid = root  
  4. gid = root  
  5. port = 873  
  6. pid file = /var/run/rsyncd.pid  
  7. log file = /var/log/rsyncd.log  
  8. lock file = /var/run/rsyncd.lock  
  9. use chroot = no  
  10. max connections = 200  
  11. read only = false  
  12. list = false  
  13. fake super = yes  
  14. ignore errors  
  15. [data]  
  16. path = /mynfsdata  
  17. auth users = rsyncuser  
  18. secrets file = /etc/rsync_salve.pass  
  19. hosts allow = 192.168.250.193  #填写master服务器的IP  
3)生成认证文件
  1. [root@mynfs02 /]# echo 'rsyncuser:rsyncuser12#' > /etc/rsync_salve.pass  
  2. [root@mynfs02 /]# chmod 600 /etc/rsync_salve.pass  
  3. [root@mynfs02 /]  
4)修改文件夹权限
  1. [root@mynfs02 /]# chown -R root:root /mynfsdata  
5)启动服务
  1. [root@mynfs02 /]# rsync --daemon --config=/etc/rsyncd.conf  
  2. [root@mynfs02 /]  

2.在master上进行测试

1)安装同步工具
  1. [root@mynfs01 /]# yum -y install rsync.x86_64  
  2. 已加载插件:fastestmirror, langpacks  
  3. Loading mirror speeds from cached hostfile  
  4.  * base: mirrors.aliyun.com  
  5.  * extras: mirrors.aliyun.com  
  6.  * updates: mirrors.aliyun.com  
  7. 软件包 rsync-3.1.2-10.el7.x86_64 已安装并且是最新版本  
  8. 无须任何处理  
  9. [root@mynfs01 /]  
  10.   
  11. [root@mynfs01 /]# chown -R root:root /mynfsdata  
  12. [root@mynfs01 /]# echo "rsyncuser12#" > /etc/rsync.pass  
  13. [root@mynfs01 /]# chmod 600 /etc/rsync.pass  
  14. [root@mynfs01 /]  
2)创建测试文件,测试推送(在master上进行)
  1. [root@mynfs01 /]# rsync -arv /mynfsdata/ rsyncuser@192.168.250.194::data --password-file=/etc/rsync.pass  
  2. sending incremental file list  
  3. ./  
  4. aaa.txt  
  5.   
  6. sent 119 bytes  received 38 bytes  314.00 bytes/sec  
  7. total size is 4  speedup is 0.03  
  8. [root@mynfs01 /]  
3)在slave上查看
  1. [root@mynfs02 /]# ls /mynfsdata  
  2. aaa.txt  
  3. [root@mynfs02 /]  
  4. 说明同步过来了。  

3.在master配置自同步

从网上下载sersync2.5.4_64bit_binary_stable_final.tar.gz,上传后,移到下面目录然后安装。

  1. [root@mynfs01 ~]# mv sersync2.5.4_64bit_binary_stable_final.tar.gz /usr/local/  
  2.   
  3. [root@mynfs01 local]# tar xvf sersync2.5.4_64bit_binary_stable_final.tar.gz   
  4. GNU-Linux-x86/  
  5. GNU-Linux-x86/sersync2  
  6. GNU-Linux-x86/confxml.xml  
  7. [root@mynfs01 local]# ls  
  8. bin  etc  games  GNU-Linux-x86  include  lib  lib64  libexec  sbin  sersync2.5.4_64bit_binary_stable_final.tar.gz  share  src  
  9.   
  10. [root@mynfs01 local]# mv GNU-Linux-x86/ sersync  
  11. [root@mynfs01 local]# cd sersync/  
  12. [root@mynfs01 sersync]  

修改配置文件confxml.xml

  1. [root@mynfs01 sersync]# sed -ri 's#<delete start="true"/>#<delete start="false"/>#g' confxml.xml  
  2. [root@mynfs01 sersync]# sed -ri '24s#<localpath watch="/opt/tongbu">#<localpath watch="/mynfsdata">#g' confxml.xml  
  3. [root@mynfs01 sersync]# sed -ri '25s#<remote ip="127.0.0.1" name="tongbu1"/>#<remote ip="192.168.250.194" name="data"/>#g' confxml.xml  
  4. [root@mynfs01 sersync]# sed -ri '30s#<commonParams params="-artuz"/>#<commonParams params="-az"/>#g' confxml.xml  
  5. [root@mynfs01 sersync]# sed -ri '31s#<auth start="false" users="root" passwordfile="/etc/rsync.pas"/>#<auth start="true" users="rsyncuser" passwordfile="/etc/rsync.pass"/>#g' confxml.xml  
  6. [root@mynfs01 sersync]# sed -ri '33s#<timeout start="false" time="100"/><!-- timeout=100 -->#<timeout start="true" time="100"/><!-- timeout=100 -->#g' confxml.xml  
  7. [root@mynfs01 sersync]# pwd  
  8. /usr/local/sersync  
  9. [root@mynfs01 sersync]  

启动Sersync服务

  1. [root@mynfs01 ~]# /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml  
  2. set the system param  
  3. execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches  
  4. execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events  
  5. parse the command param  
  6. option: -d  run as a daemon  
  7. option: -r  rsync all the local files to the remote servers before the sersync work  
  8. option: -o  config xml name:  /usr/local/sersync/confxml.xml  
  9. daemon thread num: 10  
  10. parse xml config file  
  11. host ip : localhost host port: 8008  
  12. will ignore the inotify delete event  
  13. daemon start,sersync run behind the console   
  14. use rsync password-file :  
  15. user is rsyncuser  
  16. passwordfile is     /etc/rsync.pass  
  17. config xml parse success  
  18. please set /etc/rsyncd.conf max connections=0 Manually  
  19. sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)   
  20. Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)  
  21. please according your cpu ,use -n param to adjust the cpu rate  
  22. ------------------------------------------  
  23. rsync the directory recursivly to the remote servers once  
  24. working please wait...  
  25. execute command: cd /mynfsdata && rsync -az -R  ./  --timeout=100 rsyncuser@192.168.250.194::data --password-file=/etc/rsync.pass >/dev/null 2>&1   
  26. run the sersync:   
  27. watch path is: /mynfsdata  
  28. [root@mynfs01 ~]  

4.测试自同步

  1. # 在 master 中的/mynfsdata  目录创建文件  
  2. [root@mynfs01 mynfsdata]# touch mytest2.txt  
  3. 然后在slave中的/mynfsdata   是否有该文件  
  4. [root@mynfs02 mynfsdata]# ls  
  5. aaa.txt  mytest2.txt  
  6. [root@mynfs02 mynfsdata]# cat mytest2.txt  
  7. 111  

以上就做完了 salve 同步 master 的文件,但是当 master 宕机后恢复,master 无法同步 salve 文件,所以要配置 master 同步 salve 文件。

5.在master配置rsync服务,进行同步slave数据

1)修改/etc/rsyncd.conf

其中 hosts allow 填写slave ip=192.168.250.194

  1. [root@mynfs01 ~]# vim /etc/rsyncd.conf  
  2. [root@mynfs01 ~]  
  3. uid = root  
  4. gid = root  
  5. port = 873  
  6. pid file = /var/run/rsyncd.pid  
  7. log file = /var/log/rsyncd.log  
  8. lock file = /var/run/rsyncd.lock  
  9. use chroot = no  
  10. max connections = 200  
  11. read only = false  
  12. list = false  
  13. fake super = yes  
  14. ignore errors  
  15. [data]  
  16. path = /mynfsdata  
  17. auth users = rsyncuser  
  18. secrets file = /etc/rsync_master.pass  
  19. hosts allow = 192.168.250.194  
2)生成认证文件
  1. [root@mynfs01 ~]# echo 'rsyncuser:rsyncuser12#' > /etc/rsync_master.pass  
  2. [root@mynfs01 ~]# chmod 600 /etc/rsync_master.pass  
  3. [root@mynfs01 ~]  
3)修改文件夹权限
  1. [root@mynfs01 ~]# chown -R root:root  /mynfsdata  
  2. [root@mynfs01 ~]  
4)启动服务
  1. [root@mynfs01 ~]# rsync --daemon --config=/etc/rsyncd.conf  
  2. [root@mynfs01 ~]  

6.在slave上测试

  1. [root@mynfs02 ~]# echo "rsyncuser12#" > /etc/rsync.pass  
  2. [root@mynfs02 ~]# chmod 600 /etc/rsync.pass  
  3. [root@mynfs02 ~]  
1)创建测试文件,测试推送
  1. [root@mynfs02 ~]# cd /mynfsdata  
  2. [root@mynfs02 mynfsdata]# echo "hello world" > myfile.txt  
  3. [root@mynfs02 mynfsdata]  
  4. [root@mynfs02 mynfsdata]# rsync -arv  /mynfsdata rsyncuser@192.168.250.193::data --password-file=/etc/rsync.pass  
  5. sending incremental file list  
  6. mynfsdata/  
  7. mynfsdata/aaa.txt  
  8. mynfsdata/myfile.txt  
  9. mynfsdata/mytest2.txt  
  10.   
  11. sent 289 bytes  received 77 bytes  732.00 bytes/sec  
  12. total size is 20  speedup is 0.05  
  13. [root@mynfs02 mynfsdata]  
2)在master上查看
  1. [root@mynfs01 mynfsdata]# cd mynfsdata/  
  2. [root@mynfs01 mynfsdata]# ls  
  3. aaa.txt  myfile.txt  mytest2.txt  
  4. [root@mynfs01 mynfsdata]# pwd  
  5. /mynfsdata/mynfsdata  
  6. [root@mynfs01 mynfsdata]# cat myfile.txt  
  7. hello world  
  8. [root@mynfs01 mynfsdata]  
  9. 问题:这是目录重复问题,由于上面写错了。  

7.在slave上配置自动同步

这里先本地上传sersync文件,然后进行安装,或从百度网盘下载,网盘地址:

  1. 链接: https://pan.baidu.com/s/1t25V9jab2s2-r5P31JQJDw  
  2. 提取码: kqcx  

本地上传并安装

  1. [root@mynfs02 /]# cd /usr/local  
  2. [root@mynfs02 local]# rz  
  3.   
  4.   
  5. [root@mynfs02 local]# ls  
  6. bin  etc  games  include  lib  lib64  libexec  sbin  sersync2.5.4_64bit_binary_stable_final.tar.gz  share  src  
  7. [root@mynfs02 local]  
  8.   
  9. [root@mynfs02 local]# tar xvf sersync2.5.4_64bit_binary_stable_final.tar.gz  
  10. GNU-Linux-x86/  
  11. GNU-Linux-x86/sersync2  
  12. GNU-Linux-x86/confxml.xml  
  13. [root@mynfs02 local]# ls  
  14. bin  etc  games  GNU-Linux-x86  include  lib  lib64  libexec  sbin  sersync2.5.4_64bit_binary_stable_final.tar.gz  share  src  
  15. [root@mynfs02 local]  
  16. [root@mynfs02 local]# mv GNU-Linux-x86/ sersync  
  17. [root@mynfs02 local]# cd sersync/  
  18. [root@mynfs02 sersync]  

修改配置文件

  1. [root@mynfs02 sersync]# sed -ri 's#<delete start="true"/>#<delete start="false"/>#g' confxml.xml  
  2. [root@mynfs02 sersync]# sed -ri '24s#<localpath watch="/opt/tongbu">#<localpath watch="/mynfsdata">#g' confxml.xml  
  3. [root@mynfs02 sersync]# sed -ri '25s#<remote ip="127.0.0.1" name="tongbu1"/>#<remote ip="192.168.250.193" name="data"/>#g' confxml.xml  
  4. [root@mynfs02 sersync]# sed -ri '30s#<commonParams params="-artuz"/>#<commonParams params="-az"/>#g' confxml.xml  
  5. [root@mynfs02 sersync]# sed -ri '31s#<auth start="false" users="root" passwordfile="/etc/rsync.pas"/>#<auth start="true" users="rsyncuser" passwordfile="/etc/rsync.pass"/>#g' confxml.xml  
  6. [root@mynfs02 sersync]# sed -ri '33s#<timeout start="false" time="100"/><!-- timeout=100 -->#<timeout start="true" time="100"/><!-- timeout=100 -->#g' confxml.xml  
  7. [root@mynfs02 sersync]  

启动Sersync

  1. [root@mynfs02 sersync]# /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml  
  2. set the system param  
  3. execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches  
  4. execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events  
  5. parse the command param  
  6. option: -d  run as a daemon  
  7. option: -r  rsync all the local files to the remote servers before the sersync work  
  8. option: -o  config xml name:  /usr/local/sersync/confxml.xml  
  9. daemon thread num: 10  
  10. parse xml config file  
  11. host ip : localhost host port: 8008  
  12. will ignore the inotify delete event  
  13. daemon start,sersync run behind the console   
  14. use rsync password-file :  
  15. user is rsyncuser  
  16. passwordfile is     /etc/rsync.pass  
  17. config xml parse success  
  18. please set /etc/rsyncd.conf max connections=0 Manually  
  19. sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)   
  20. Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)  
  21. please according your cpu ,use -n param to adjust the cpu rate  
  22. ------------------------------------------  
  23. rsync the directory recursivly to the remote servers once  
  24. working please wait...  
  25. execute command: cd /mynfsdata && rsync -az -R  ./  --timeout=100 rsyncuser@192.168.250.193::data --password-file=/etc/rsync.pass >/dev/null 2>&1   
  26. run the sersync:   
  27. watch path is: /mynfsdata  
  28. [root@mynfs02 sersync]  

以上,就已经做好了主从相互同步的操作。

五.安装keepalive并配置

1.master服务器上操作

1)安装keepalive
  1. [root@mynfs01 /]# yum -y install keepalived  
  2. 已加载插件:fastestmirror, langpacks  
  3. Loading mirror speeds from cached hostfile  
  4.  * base: mirrors.aliyun.com  
  5.  * extras: mirrors.aliyun.com  
  6.  * updates: mirrors.aliyun.com  
2)配置keepalived.conf文件
  1. [root@mynfs01 /]# vim /etc/keepalived/keepalived.conf  
  2. [root@mynfs01 /]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.ori.bak.20220307  
  3. [root@mynfs01 /]# vim /etc/keepalived/keepalived.conf  
  4. [root@mynfs01 /]# cat /etc/keepalived/keepalived.conf  
  5. ! Configuration File for keepalived  
  6.   
  7. global_defs {  
  8.    router_id NFS-Master  
  9. }  
  10.   
  11. vrrp_instance VI_1 {  
  12.     state MASTER  
  13.     interface eth0  
  14.     virtual_router_id 51  
  15.     priority 150  
  16.     advert_int 1  
  17.     authentication {  
  18.         auth_type PASS  
  19.         auth_pass keepalived12#  
  20.     }  
  21.     virtual_ipaddress {  
  22.         192.168.250.195  
  23.     }  
  24. }  
  25.   
  26. [root@mynfs01 /]  
3)启动服务
  1. [root@mynfs01 /]# systemctl start keepalived.service && systemctl enable keepalived.service  
  2. Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.  
  3. [root@mynfs01 /]  

2.在slave服务器上操作

1)安装keepalive
  1. [root@mynfs02 /]# yum -y install keepalived  
  2. 已加载插件:fastestmirror, langpacks  
  3. Loading mirror speeds from cached hostfile  
  4.  * base: mirrors.aliyun.com  
  5.  * extras: mirrors.aliyun.com  
  6.  * updates: mirrors.aliyun.com  
2)配置keepalived.conf文件
  1. [root@mynfs02 /]# vim /etc/keepalived/keepalived.conf  
  2. [root@mynfs02 /]# cat /etc/keepalived/keepalived.conf  
  3. ! Configuration File for keepalived  
  4.   
  5. global_defs {  
  6.     router_id NFS-Slave     
  7. }  
  8.   
  9. vrrp_instance VI_1 {  
  10.     state MASTER  
  11.     interface eth0  
  12.     virtual_router_id 51  
  13.     priority 100  
  14.     advert_int 1  
  15.     authentication {  
  16.         auth_type PASS  
  17.         auth_pass keepalived12#  
  18.     }  
  19.     virtual_ipaddress {  
  20.         192.168.250.195  
  21.     }  
  22. }  
  23. [root@mynfs02 /]  
3)启动服务
  1. [root@mynfs02 /]# systemctl start keepalived.service && systemctl enable keepalived.service  
  2. Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.  
  3. [root@mynfs02 /]  

3.检查虚拟ip

在master上执行,进行检查虚拟IP

  1. [root@mynfs01 /]# ip a | grep 192.168.250.195  
  2.     inet 192.168.250.195/32 scope global eth0  

4.测试

在nfs客户机上挂载,这里找一台上节的mysql集群主机中的一台作为nfs客户端进行挂载测试。

  1. [root@mysql01 ~]# mount -t nfs 192.168.250.195:/mynfsdata  /mnt  
  2. mount.nfs: access denied by server while mounting 192.168.250.195:/mynfsdata  
  3. [root@mysql01 ~]  
  4. 解决:  
  5. [root@mynfs02 /]# vim /etc/exports  
  6. /mynfsdata 192.168.250.0/24(rw,sync,all_squash)  
  7.   
  8. [root@mynfs02 /]# exportfs -rv  
  9. exporting 192.168.250.0/24:/mynfsdata  
  10. [root@mynfs02 /]  

上面处理后,挂载成功。

  1. [root@mysql01 ~]# mount -t nfs 192.168.250.195:/mynfsdata  /mnt  
  2. [root@mysql01 ~]  

这时模拟主NFS宕机,然后测试VIP是否漂移。

  1. [root@mynfs01 /]# systemctl stop keepalived.service   
  2.   
  3. [root@mynfs01 /]# ip a | grep 192.168.250.195  
  4. [root@mynfs01 /]  

没有VIP输出,然后到从机上测试

  1. [root@mynfs02 /]# ip a | grep 192.168.250.195  
  2.     inet 192.168.250.195/32 scope global eth0  
  3. [root@mynfs02 /]  

说明IP地址已漂移。

六.设置keepalive脚本来检测nfs存活

这里需要写一个脚本检测nfs存活,根据结果来进行漂移。

  1. [root@mynfs01 /]# vim /root/check_nfs.sh  
  2. [root@mynfs01 /]# cat /root/check_nfs.sh  
  3. #!/bin/sh  
  4. /usr/bin/systemctl status keepalived &>/dev/null  
  5. if [ $? -ne 0 ]  
  6. then  
  7.     echo "keepalived未启动,无需监测."  
  8.     exit 0  
  9. else  
  10.     /usr/bin/systemctl status nfs &>/dev/null  
  11.     if [ $? -ne 0 ]  
  12.     then  
  13.         /usr/bin/systemctl restart nfs  
  14.         /usr/bin/systemctl status nfs &>/dev/null  
  15.         if [ $? -ne 0 ]  
  16.         then  
  17.             /usr/bin/systemctl stop keepalived  
  18.         fi  
  19.     fi  
  20. fi  
  21. [root@mynfs01 /]  

加入定时任务:

  1. [root@mynfs01 /]# crontab -l  
  2. */1 * * * *  /bin/sh /root/check_nfs.sh &> /root/check_nfs.log  
  3. [root@mynfs01 /]  

同样在从机上操作。

moonrong
  • 版权声明:本站原创文章,于2022年4月27日10:33:55,由 发表,共 19712 字。
  • 版权声明: 本文由于2022年4月27日10:33:55 发表在 好派笔记,共 19712 字。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: