天天看點

HA之keepalived

用keepalived作HA(結合haproxy)真是爽呆了,漂移速度非常快,下面是編譯安裝配置過程

#wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz 

#cd keepalived-1.2.2

#./configure

# make && make install && make clean

#cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ 

#cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ 

#mkdir /etc/keepalived 

#cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ 

#cp /usr/local/sbin/keepalived /usr/sbin/ 

修改keepalived.conf

以下是master上的

! Configuration File for keepalived 

#全局定義

global_defs { 

   notification_email { # 郵件通知

        [email protected] #郵件位址

   } 

   notification_email_from [email protected]  #發件人位址

   smtp_server smtp.163.com #smtp server

   smtp_connect_timeout 5 #smtp 逾時時間

   router_id LVS_DEVEL 

vrrp_script chk_haproxy { 

     script "killall -0 haproxy" 

     interval 2 

     weight   2 

vrrp_instance VI_1 { #實列名-可以自定義

    state MASTER #角色

    interface eth0 #HA檢測的裝置

    virtual_router_id 50 #虛拟路由ID一組叢集必須設定為同一個ID,tcpdump vrrp可獲得

    priority 200 #權重,backup不能高于master

    advert_init 1 #檢測時間間隔

    smtp_alert #開啟smtp通知

    garp_master_delay 1 #主從切換時間,機關為秒。

    mcast_src_ip xxx.xxx.xxx.xxx #master主機位址

    authentication { #認證設定

        auth_type PASS #認證類型

        auth_pass $RFV7ygv  #認證密碼,設定為一緻

    } 

    track_interface { 

        eth0 

        eth1 

    virtual_ipaddress { #虛拟IP定義,可以多個

        xxx.xxx.xxx.xxx #vip定義

track_script {

chk_haproxy #HA檢測的定義

}

backup上的keepalived

   notification_email { 

        [email protected] 

   notification_email_from [email protected] 

   smtp_server smtp.163.com 

   smtp_connect_timeout 5 

vrrp_instance VI_1 { 

    state BACKUP #角色

    interface eth0 

    virtual_router_id 50 #與master保持一緻

    priority 100 #權重低于master

    advert_init 1 

    smtp_alert 

    garp_master_delay 1 

    mcast_src_ip xxx.xxx.xxx.xxx #backup主機位址

    authentication { 

        auth_type PASS 

        auth_pass $RFV7ygv 

    virtual_ipaddress { 

        xxx.xxx.xxx.xxx #vip address

chk_haproxy

PS:

配置完keepalived檔案後 ,簡單測試一下哈 ^_^

把master主機網卡停止, 可以瞬間切換到backup主機

啟動master主機的網卡  master又搶占回vip

在業務應用十分頻繁的系統上,如果master發生故障,切換回backup,master主機online後,又切換回到master,如此頻繁的切換是無法容忍的,可以設定不搶占的方法

具體方法是将master主機的state也設定為BACKUP,在state BACKUP下面加入nopreempt

backup主機不需要加入,兩個主機根據priority的高低進行搶占,高的為MASTER主機

本文轉自dongfang_09859 51CTO部落格,原文連結:http://blog.51cto.com/hellosa/530619,如需轉載請自行聯系原作者

繼續閱讀