nfs 研究雙機熱備記錄

實現說明:
實現兩臺服務器,nfs2同步nfs1,當nfs1掛掉後起來去同步nfs2,掛掉起來後不搶佔。
兩臺服務器分別爲nfs1,nfs2

nfs1 192.168.104.144

nfs2 192.168.104.145

vip 192.168.104.200

使用nfs+rsync+inotify+keepalived等工具

關閉防火牆和selinux,保證主機名能解析,設置好ssh互信

兩臺服務器:

# yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
# yum -y install nfs-utils keepalived rsync inotify-tools
設置nfs:

# systemctl enable --now nfs

# mkdir /home/data1
# chown -R 36:36 /home/data #此處賦予擁有用戶和組根據個人需求
# vi /etc/exports
/home/data *(rw,sync,no_subtree_check,all_squash,anonuid=36,anongid=36)
# systemctl restart nfs
設置rsync和inotify

注意:syncd服務不能手動啓動,否則會兩臺服務器衝突
# vi /etc/rsyncd.conf
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
secrets file = /etc/rsync.password
[etc_from_client]
path = /home/data1
comment = sync etc from client
uid = root
gid = root
port = 873
ignore errors
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = vdsm
# vi /etc/rsync.password
vdsm:123456
# vi /etc/rsync.auth
123456
# chmod 600 /etc/rsync.* #注意權限必須是600否則後面驗證密碼會失敗
創建腳本存放路徑

# mkdir /scripts/
設置自動同步數據

# vi /scripts/inotify.sh
#!/bin/bash
host=192.168.104.145 #nfs2的話設置192.168.104.144
src=/home/data1/ #注意後面要斜槓,否則會將目錄data1目錄同步
des=etc_from_client
auth=/etc/rsync.auth
user=vdsm
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=${auth} $src $user@$host::$des

echo "${files} was rsynced" >> /tmp/rsync.log 2>&1
done
# chmod -R 755 /scripts/
#設置開機自動執行腳本

# chmod +x /etc/rc.d/rc.local
# vi /etc/rc.d/rc.local
nohup sh /scripts/inotify.sh &
keepalived 的配置
主nfs1:
# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {br/>[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node1
vrrp_skip_check_adv_addr
}
vrrp_script monitor_nfs_status {
script "/scripts/monitor_nfs_status.sh"
interval 5
fall 3
rise 1
weight -15
}

vrrp_instance VI_1 {
state BACKUP
interface enp6s0f1
virtual_router_id 55
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.1.1.10/24
}
track_script {
monitor_nfs_status
}

track_interface {
enp6s0f1
}
notify_master "/scripts/keepalived_notify.sh master"
notify_backup "/scripts/keepalived_notify.sh backup"
notify_fault "/scripts/keepalived_notify.sh fault"
notify_stop "/scripts/keepalived_notify.sh stop"
}

備nfs2:
# vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
notification_email {br/>[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id nfs2
vrrp_skip_check_adv_addr
}
vrrp_script monitor_nfs_status {
script "/scripts/monitor_nfs_status.sh"
interval 5
fall 3
rise 1
weight -15
}

vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 55
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.104.200/24
}
track_script {
monitor_nfs_status
}

track_interface {
ens33
}
notify_master "/scripts/keepalived_notify.sh master"
notify_backup "/scripts/keepalived_notify.sh backup"
notify_fault "/scripts/keepalived_notify.sh fault"
notify_stop "/scripts/keepalived_notify.sh stop"
}
兩臺服務器:

設置keepalive檢測腳本
# vi /scripts/keepalived_notify.sh
#!/bin/bash
master() {
systemctl status nfs
if [ $? -ne 0 ];then
systemctl restart nfs
fi
systemctl status rsyncd
if [ $? -eq 0 ];then
systemctl stop rsyncd
fi
}

backup() {
systemctl status nfs
if [ $? -ne 0 ];then
systemctl restart nfs
fi
systemctl status rsyncd
if [ $? -ne 0 ];then
systemctl restart rsyncd
fi
}

fault() {
systemctl status nfs
if [ $? -ne 0 ];then
systemctl restart nfs
fi
if [ $? -eq 0 ];then
systemctl stop rsyncd
fi
}

case $1 in
master)
master
;;
backup)
backup
;;
fault)
fault
systemctl restart keepalived
;;
stop)
backup
systemctl restart keepalived
;;
*)
echo $"Usage: $0 {master|backup|fault|stop}"
esac
~

# vi /scripts/monitor_nfs_status.sh
!/bin/bash
systemctl status nfs &>/dev/null
if [ $? -eq 0 ];then

systemctl status rpcbind &>/dev/null
if [ $? -eq 0 ];then
exit 0
fi
exit 2
else
systemctl restart nfs &> /dev/null
if [ ps -C nfsd --no-header | wc -l -ne 0 ];then
pkill keepalived && exit 1
fi
fi
# chmod -R 755 /scripts/
若要設置防火牆,則要配置以下規則 //下面網卡要根據實際的網卡
//keepalived默認組播地址是224.0.0.18
# firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface ens33 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
success
# firewall-cmd --direct --permanent --add-rule ipv4 filter OUTPUT 0 --out-interface ens33 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
success
# firewall-cmd --reload
success

//rsyncd默認端口是873
# firewall-cmd --add-port=873/tcp --permanent
success
# firewall-cmd --add-port=873/udp --permanent
success
# firewall-cmd --reload
success
由於想nfs1作爲主,且keepalived設置了當存在master不搶佔

建議nfs1 先執行 systemctl enable --now keepalived

之後nfs2 再執行 systemctl enable --now keepalived

再兩臺服務器執行 source /etc/rc.d/rc.local

#由於是測試完再寫的,很可能哪裏寫錯或者漏寫,可以查看tail -f /var/log/rsyncd.log 和message日誌

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章