nfs双机热备

nfs双机热备

1.解决了nfs单点故障;
2.使用nfs+keepalived做了主备;
3.通过rsync+inotify实现主备间的共享目录进行同步。
4.nfs上的共享目录都是通过lvm挂载,使共享目录有扩展性。

  • 实验环境
主机名 IP地址
master-nfs 192.168.100.116
backup-nfs 192.168.100.169
server 192.168.100.155

\虚拟IP地址:192.168.100.100

配置主备

  1. 在双nfs主机上安装keepalived
\\主节点配置
[root@master-nfs ~]# yum -y install keepalived
[root@backup-nfs ~]# yum -y install keepalived
  1. 配置keepalived文件
[root@master-nfs ~]# vim /etc/keepalived/keepalived.conf 

! Configuration File for keepalived

global_defs {
  notification_email {
    acassen@firewall.loc
    failover@firewall.loc
    sysadmin@firewall.loc
  }
  notification_email_from Alexandre.Cassen@firewall.loc
  smtp_server 192.168.200.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
  vrrp_skip_check_adv_addr
#   vrrp_strict
  vrrp_garp_interval 0
  vrrp_gna_interval 0
}

vrrp_instance VI_1 {
   state MASTER
   interface ens32
   virtual_router_id 51
   priority 110
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass 1111
   }
   virtual_ipaddress {
       192.168.100.100/24
   }
}
....
\\备节点配置
[root@backup-nfs ~]# vim /etc/keepalived/keepalived.conf 

! Configuration File for keepalived

global_defs {
  notification_email {
    acassen@firewall.loc
    failover@firewall.loc
    sysadmin@firewall.loc
  }
  notification_email_from Alexandre.Cassen@firewall.loc
  smtp_server 192.168.200.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
  vrrp_skip_check_adv_addr
#   vrrp_strict
  vrrp_garp_interval 0
  vrrp_gna_interval 0
}

vrrp_instance VI_1 {
   state MASTER
   interface ens32
   virtual_router_id 51
   priority 100
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass 1111
   }
   virtual_ipaddress {
       192.168.100.100/24
   }
 ...

3.重启keepalived服务,查看ip

[root@master-nfs ~]# systemctl restart keepalived.service
[root@master-nfs ~]# systemctl enable keepalived.service
[root@backup-nfs ~]# systemctl restart keepalived.service 
[root@backup-nfs ~]# systemctl enable keepalived.service
[root@master-nfs ~]# ip a show ens32
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:05:94:b0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.116/24 brd 192.168.100.255 scope global ens32
       valid_lft forever preferred_lft forever
    inet 192.168.100.100/24 scope global secondary ens32
       valid_lft forever preferred_lft forever
    inet6 fe80::13e4:67f8:1586:dd18/64 scope link 
       valid_lft forever preferred_lft forever
[root@backup-nfs ~]# ip a show ens32
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:b9:a5:63 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.169/24 brd 192.168.100.255 scope global ens32
       valid_lft forever preferred_lft forever
    inet6 fe80::bb22:4f4e:8e9:25fd/64 scope link 
       valid_lft forever preferred_lft forever
\\关闭master上的keepalived服务查看地址是否偏移,漂移表示keepalived配置成功
[root@master-nfs ~]# systemctl stop keepalived.service
[root@backup-nfs ~]# ip a show ens32
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:b9:a5:63 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.169/24 brd 192.168.100.255 scope global ens32
       valid_lft forever preferred_lft forever
    inet 192.168.100.100/24 scope global secondary ens32
       valid_lft forever preferred_lft forever
    inet6 fe80::bb22:4f4e:8e9:25fd/64 scope link 
       valid_lft forever preferred_lft forever

创建共享目录

1.添加逻辑卷

[root@backup-nfs ~]# pvcreate /dev/sdb
[root@backup-nfs ~]# vgcreate dxk /dev/sdb
[root@backup-nfs ~]# lvcreate -L 500M -n dsb dxk
[root@backup-nfs ~]# mkfs.xfs /dev/dxk/dsb

2.创建目录

[root@backup-nfs ~]# mkdir /dsb
[root@backup-nfs ~]# mount /dev/dxk/dsb /dsb/

3.设置开机自启

[root@backup-nfs ~]# tail -1 /etc/fstab
UUID="86b85c23-effe-4a8a-ac0f-ba42d0a39b0d" /dsb    xfs         defaults 0 0
[root@backup-nfs ~]# mount -a

共享目录资源同步

1.源服务器

\\安装rsync
[root@backup-nfs ~]# yum -y install rsync
\\配置主配置文件
[root@backup-nfs ~]# vim /etc/rsyncd.conf
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
secrets file = /etc/rsync.pass
[ddd]
path = /
comment = This is client
uid = root
gid = root
port = 873
ignore errors
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = ddd
hosts allow = 192.168.100.0/24
hosts deny = 192.168.1.1
\\创建用户认证文件
[root@backup-nfs ~]# cat /etc/rsync.pass
ddd:123
[root@backup-nfs ~]# chmod 600 /etc/rsync.pass
\\启动
[root@backup-nfs ~]# systemctl restart rsyncd
[root@backup-nfs ~]# systemctl enable rsyncd

2.目标服务器

[root@master-nfs ~]# yum -y install rsync inotify-tools
[root@master-nfs ~]# cat /etc/rsync.pass
123
[root@master-nfs ~]# chmod 600 /etc/rsync.pass

3.手动数据同步

[root@master-nfs ~]# rsync -avzp --port=873 --progress --delete /dsb ddd@192.168.100.169::ddd --password-file=/etc/rsync.pass
[root@backup-nfs ~]# ls /dsb/
ddd  ddsb

4.自动同步

\\编写脚本
[root@master-nfs ~]# vim /scripts/inotifi.sh
host=192.168.100.169
src=/dsb
des=ddd
password=/etc/rsync.pass
user=ddd
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@$host::$des
 echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
[root@master-nfs ~]# nohup bash /scripts/inotifi.sh &   //启动脚本
[3] 2860
[root@master-nfs ~]# nohup: ignoring input and appending output to ‘nohup.out’
\\查看是否同步
[root@master-nfs ~]# echo dsb > /dsb/ddd
[root@backup-nfs ~]# cat /dsb/ddd
dsb

5.设置脚本的开机自启动

[root@master-nfs ~]# touch /etc/rc.d/rc.local
[root@master-nfs ~]# chmod +x /etc/rc.d/rc.local
[root@master-nfs ~]# echo "nohup /bin/bash /scripts/inotify.sh" >> /etc/rc.d/rc.local
[root@master-nfs ~]# tail -1 /etc/rc.d/rc.local
nohup /bin/bash /scripts/inotify.sh

7.重启后查看

[root@master-nfs ~]# ps -ef | grep inotifi.sh
root       1043   1033  0 17:10 ?        00:00:00 /bin/bash /scripts/inotifi.sh
root       1051   1043  0 17:10 ?        00:00:00 /bin/bash /scripts/inotifi.sh
root       1199   1147  0 17:10 pts/1    00:00:00 grep --color=auto inotifi.sh

安装nfs服务

1.主备操作一样
2.安装nfs服务

[root@master-nfs ~]# yum -y install nfs-utils

3.配置nfs

[root@master-nfs ~]# cat /etc/exports
/dsb  192.168.100.0/24(rw,sync,no_root_squash)
[root@master-nfs ~]# exportfs er
[root@master-nfs ~]# systemctl restart nfs-server.service
[root@master-nfs ~]# systemctl enable nfs-server.service

4.客户端检查(用虚拟IP)

[root@server ~]# showmount -e 192.168.100.100
Export list for 192.168.100.100:
/dsb 192.168.100.0/24

5.客户端挂载

[root@server ~]# mount -t nfs 192.168.100.100:/dsb /data/
[root@server ~]# df
Filesystem            1K-blocks    Used Available Use% Mounted on
/dev/mapper/rhel-root   8374272 1768892   6605380  22% /
devtmpfs                 488772       0    488772   0% /dev
tmpfs                    499848       0    499848   0% /dev/shm
tmpfs                    499848   13064    486784   3% /run
tmpfs                    499848       0    499848   0% /sys/fs/cgroup
/dev/sda1               1038336  127484    910852  13% /boot
tmpfs                     99972       0     99972   0% /run/user/0
192.168.100.100:/dsb     508672   25856    482816   6% /data

6.设置自动挂载

[root@server ~]# tail -1 /etc/fstab 
192.168.100.100:/dsb   /data     nfs   defaults,_netdev      0 0

7.编写当主nfs服务器down机后,keepalived自动停止,让虚拟ip漂移

[root@master-nfs ~]# cat /etc/keepalived/nfs_check.sh
#!/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

7.模拟故障

[root@master-nfs ~]# systemctl stop keepalived.service
\\这时需要卸载了重新读取配置文件
[root@server ~]# umount /data
[root@server ~]# mount -a
[root@server ~]# df
Filesystem            1K-blocks    Used Available Use% Mounted on
/dev/mapper/rhel-root   8374272 1768712   6605560  22% /
devtmpfs                 488772       0    488772   0% /dev
tmpfs                    499848   13068    486780   3% /run
tmpfs                    499848       0    499848   0% /sys/fs/cgroup
/dev/sda1               1038336  127484    910852  13% /boot
192.168.100.100:/dsb     508672   25856    482816   6% /data
展开阅读全文

没有更多推荐了,返回首页