利用keepalived可以實作對linux伺服器的高可用性,即主從備份,一台線上伺服器出故障另外一台伺服器頂替它,當出故障的伺服器恢複時又自動轉換成主伺服器,頂替它的伺服器再次轉換為備胎,當主伺服器當機、主伺服器網卡壞掉、甚至主伺服器服務挂掉(要用腳本監控實作轉換)都可以自動切換到從伺服器
yum install gcc gcc-c++ openssl-devel httpd kernel-devel -y
tar zxf keepalived-1.1.20.tar.gz
cd keepalived-1.1.20/
./configure --sysconf=/etc/ --with-kernel-dir=/usr/src/kernels/2.6.18-238.el5-i686/
make &&make install
cp /usr/local/sbin/keepalived /usr/sbin/
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER #設定為主伺服器
interface eth0 #監測網絡接口
virtual_router_id 51 #主、備必須一樣
priority 100 #(主、備機取不同的優先級,主機值較大,備份機值較小,值越大優先級越高)
advert_int 1 #VRRP Multicast廣播周期秒數
authentication {
auth_type PASS #VRRP認證方式,主備必須一緻
auth_pass 1111 #(密碼)
}
virtual_ipaddress {
192.168.65.65 #VRRP HA虛拟位址
}
}
####将keepalived.conf配置檔案修改成以上配置,即修改虛拟位址為192.168.65.65,這個虛拟位址是虛拟不存在的,使用者在通路網頁的時候通路的就是這個虛拟位址,本配置檔案要注意修改伺服器的主從狀态、優先級、組id以及虛拟ip,還可以修改Email位址通知自己等。
# service keepalived start
# service httpd start
# chkconfig --add httpd
# chkconfig --add keepalived
# chkconfig httpd on
# chkconfig keepalived on
從伺服器配置:
[root@web1_slave keepalived]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.65.65
}
}
#####從伺服器keepalived.conf的主配置檔案,優先級改為比主的優先級(100)小,這裡是90.
# echo "slave" > /var/www/html/index.html
######其他配置内容和主伺服器相同
當主伺服器當機或是網卡出問題,從伺服器都會取代它變成主伺服器

上圖是當主伺服器網卡重新開機時,在從伺服器日志記錄上顯示從伺服器先是切換成主伺服器,後又切換成從伺服器。
當web站點挂了時,還想讓從伺服器自動切換成主伺服器就需要腳本來執行。
下面建立一個腳本,名叫check_httpd.sh ,其位置都放在主從伺服器的/etc/keepalived/下面,(主從伺服器使用相同的腳本,腳本放的位置也都相同,在keepalived.conf修改的地方、内容也都相同)
下面是check_httpd.sh 腳本的内容
[root@web1_master keepalived]# cat check_httpd.sh
#!/bin/bash
CHECK_TIME=2
check()
{
curl http://127.0.0.1/ >/dev/null 2>&1
ret=$?
if [ $ret -ne 0 ];then
return $ret;
fi
}
while [ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -= 1"
check
HTTP_OK=$?
if [ $HTTP_OK -eq 0 ];then
exit 0
fi
if [ $HTTP_OK -ne 0 ] && [ $CHECK_TIME -eq 0 ]
then
exit 1
fi
done
修改主伺服器keepalived.conf配置檔案,結果如下:
[root@web1_master keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script check_http {
script "/etc/keepalived/check_httpd.sh"
weight -5
interval 1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
track_script {
check_http
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.65.65
}
}
修改從伺服器keepalived.conf配置檔案,結果如下:
[root@web1_slave keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script check_http {
script "/etc/keepalived/check_httpd.sh"
weight -5
interval 1
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
track_script {
check_http
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.65.65
}
}
分别重新開機兩台伺服器的keepalived服務
#service keepalivedrestart
到這裡,如果主伺服器的web程式出現故障,則從伺服器會在1秒鐘後自動切換成主伺服器,當主伺服器恢複時,從伺服器會自動切換成備胎,主伺服器依然是主伺服器。
腦裂#
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
firewall-cmd --direct --permanent --add-rule ipv4 filter OUTPUT 0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
firewall-cmd --reload
Keepalived使用vrrp多點傳播,預設位址是224.0.0.18,是以要配置防火牆放過。