本來想做DRBD+HEARTBEAT,但是領導說再加硬盤浪費資源,沒有必要,而且在已有硬盤上做風險較大,所以就只能用rsync來實現數據同步了,實驗中發現很多的坑,都用腳本和計划任務給填上了,打算把這套直接用在生產環境中,到時候如果還遇到什么問題,再進行修正和補全,下面是項目細節:
主機配置:
web:192.168.6.10 Centos 6.4
nfs1:192.168.6.1 Centos 6.4
nfs2:192.168.6.2 Centos 6.4
keepalived 1.2.13 VIP:192.168.6.105
1.安裝NFS和keepalived
這部分比較簡單,直接用yum -y install nfs keepalived
2.配置NFS(兩台同樣配置)
mkdir /home/shares
vim /etc/exports
/home/shares 192.168.6.0/24(rw,sync,no_root_squash)
cd /home/shares
echo aaa > /home/shares/a.txt #用作后面腳本判斷
touch files{1..10}
service nfs start
3.配置keepalived
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER #輔改成BACKUP
interface eth0
virtual_router_id 51
priority 100 #輔改成50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.6.105
}
}
service keepalived start
web:mount -t nfs 192.168.6.1:/home/shares /shares
4.ssh免密登錄
ssh-keygen
ssh-copy-id
5.shell腳本
cd /etc/keepalived
vim notify_master.sh #判斷NFS端口是否存在,不存在就停止keepalived服務
#!/bin/bash
port=`netstat -anp|grep 2049`
if [ "$port" == "" ];then
/sbin/service keepalived stop
fi
vim change.sh(兩台NFS都需要)
#!/bin/bash
ip=`/sbin/ip a|grep "eth0"|grep "105"`
web="192.168.6.10"
if [ "$ip" != "" ];then #判斷VIP是否存在,如果存在就表示現在這台是master
/usr/bin/ssh $web "cat /shares/a.txt" #在web上運行命令,查看/shares目錄是否可用,執行cat a.txt消耗內存cpu較小
if [ "$?" != 0 ];then #如果不可用$?返回值不為0,則卸載原來的/shares目錄,再重新掛載這個目錄,這樣做的原因是,VIP切換的時候,必須重新掛載/shares,否則不能使用這個目錄
/usr/bin/ssh $web "umount /shares&&mount -t nfs 192.168.6.105:/home/shares /shares"
fi
fi
chmod +x change.sh
chmod +x notify_master.sh
6.計划任務crontab
crontab -e
* * * * * /etc/keepalived/notify_master.sh #每分鍾檢查一次NFS服務是否正常
* * * * * /etc/keepalived/change.sh #每分鍾檢查一次keepalived狀態
0 00 * * * rsync /home/shares 192.168.6.2:/home/shares #每天夜間同步數據