場景:centos
環境中有兩臺httpd服務器,一臺作主,一臺作備用;平時只用主向外提供http服務;當主宕機後,keepalived把vip綁定到備機上去,這樣就由備機提供http服務了。bash
**keepalvied 這個軟件仍是更新的比較快的,主要問題再於它的文檔並沒能跟上來,不一樣版本之間的差異比較大,我這裏用的是1.2.13版服務器
【0】環境說明oop
主機ip 系統版本 keepavlied版本
192.168.80.131 centos6.6 keepalived-1.2.13
192.168.80.132 centos6.6 keepalived-1.2.13
【1】安裝keepalived 所須要的依賴spa
yum -y install gcc ipvsadm openssl-devel popt-devel kernel-devel
**因爲keepalived 一般來講是和lvs配合用的,雖然這裏還用不着lvs,可是我仍是把ipvsadm安裝上了code
【2】編譯安裝keepalivedrouter
tar -xzvf keepalived-1.2.13.tar.gz cd keepalived-1.2.13 ./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-504.el6.x86_64 make make install
【3】修改192.168.80.131主機上的/usr/local/keepalived/etc/keepalived.confserver
! Configuration File for keepalived global_defs { router_id LVS_001B #router_id 這要個惟一 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 #這個virtual_router_id 在兩臺機器上要相同 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.80.80/24 } } virtual_server 192.168.80.80 80 { delay_loop 2 lb_algo rr lb_kind DR persistence_timeout 60 protocol TCP real_server 192.168.80.131 80 { weight 1 notify_down /tmp/stop_keepalive.sh #當檢測到real_server 宕機的時候就會執行這個腳本 TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 2 delay_before_retry 1 } } }
【4】修改192.168.80.132主機上的/usr/local/keepalived/etc/keepalived.confblog
! Configuration File for keepalived global_defs { router_id LVS_001B } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.80.80/24 } } virtual_server 192.168.80.80 80 { delay_loop 2 lb_algo rr lb_kind DR persistence_timeout 60 protocol TCP real_server 192.168.80.132 80 { weight 1 notify_down /tmp/stop_keepalive.sh TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 2 delay_before_retry 1 } } }
【5】腳本/tmp/stop_keepalived.sh的內容以下:ip
#!/bin/bash
pkill keepalived
【6】啓動keepalived程序
nohup /usr/local/keepalived/sbin/keepalived -D -f /usr/local/keepalived/etc/keepalived/keepalived.conf &