keepalived+haproxy實現web服務的高可用和負載均衡

簡介:

keepalived是一個相似於layer3, 4 & 5交換機制的軟件,也就是咱們平時說的第3層、第4層和第5層交換.Keepalived的做用是檢測web服務器的狀態,若是有一臺web服務器死機,或工做出現故障,Keepalived將檢測到,並將有故障的web服務器從系統中剔除,web服務器工做正常後Keepalived自動將web服務器加入到服務器羣中,這些工做所有自動完成,不須要人工干涉,須要人工作的只是修復故障的web服務器.
 
Haproxy 反向代理服務器,支持雙機熱備支持虛擬主機,但其配置簡單,擁有很是不錯的服務器健康檢查功能,當其代理的後端服務器出現故障, HAProxy會自動將該服務器摘除,故障恢復後再自動將該服務器加入.新的1.3引入了frontend,backend,frontend根據任意HTTP請求頭內容作規則匹配,而後把請求定向到相關的backend.
 
實驗環境以及服務器信息:
 
OS:   RedHat  AS 5.1
 
軟件列表:
      keepalived-1.2.2.tar.gz
      haproxy-1.4.13.tar.gz
 
服務器信息:
Master server  10.10.0.99   (調度主服務器)
Slave server    10.10.0.98   (從調度服務器)
       VIP: 10.10.0.97                   (調度服務器的虛擬IP)
 
Real server:
10.10.0.96
10.10.0.95

安裝keepalived

#  tar zxvf  keepalived-1.2.2.tar.gz
#  cd keepalived-1.2.2
#  ./configure –-prefix=/usr/local/keepalived
#  make&&make install
#  cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
#  cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
#  mkdir /etc/keepalived
#  cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
#  cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
#  chkconfig –add keepalived
#  chkconfig  keepalived on
#  vi /etc/keepalived/keepalived.conf
 
global_defs {
notification_email {
xxxxxx@139.com
}
notification_email_from xxxxxx@139.com
smtp_server mail.139.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 51
priority 100
advert_int
authentication {
auth_type PASS
auth_pass 1111
    }
virtual_ipaddress {
10.10.0.97
}
}
virtual_server 10.10.0.97 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP
    real_server 10.10.0.95 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 10.10.0.96 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
關於keepalived配置文件的選項解釋能夠去網上搜下 因爲選項參數居多這裏就不說明了
#  service keepalived start
 
 
 

安裝haproxy

#   tar zxvf  haproxy-1.4.13.tar.gz
#   mv  haproxy-1.4.13 haproxy
#   cd haproxy
#   make TARGET=linux26  
#   vi /usr/local/haproxy/conf/haproxy.conf
#   mkdir –p /var/chroot/haproxy
 
global
        chroot /var/chroot/haproxy
        daemon
        gid     0
        log 127.0.0.1 local3
        nbproc  2
        pidfile /var/run/haproxy-private.pid

        ulimit   -n        65535
        user    root
        maxconn         32000
        spread-checks           4
        tune.maxaccept          8
        tune.maxpollevents      100
defaults sxit
        log     global
        mode    http
        option  httplog
        option  dontlognull
        log 127.0.0.1 local3
        retries 3
        option redispatch
        maxconn     32000
        contimeout      5000
        clitimeout        50000
        srvtimeout       50000
listen  sxit 0.0.0.0:80
        appsession JSESSIONID len 52 timeout 3h
        cookie SRV insert indirect nocache
        mode http
        stats enable
        stats hide-version
        stats uri  /haproxy-stats
        stats realm Haproxy\ statistics
        stats auth sxit:sxit
        stats refresh 3s
        monitor-uri /haproxy_test
        balance roundrobin
        option httpclose
        option forwardfor
        option httpchk HEAD /index.html HTTP/1.0
        server s1 10.10.0.95:80 check inter 2000  
weight 3

        server s3 10.10.0.96:80 check inter 2000  
weight 3
        
# /usr/local/haproxy/sbin/haproxy –f  /usr/local/haproxy/config/haproxy.config
 
若是啓動沒有報什麼錯誤的話,就在瀏覽器上輸入以下地址
http://10.10.0.97/haproxy-stats   (查看服務器狀態信息的頁面,登陸的時候輸入上面設置的帳號和密碼sxit),頁面狀態以下:
 

測試負載均衡效果以及高可用性

負載均衡測試:

   啓動真實服務器的web服務,2個真實服務器上建立2個首頁文件,內容分別爲test1test2,若是在瀏覽器上訪問web服務每次刷新既顯示test1又顯示tetst2就說明負載均衡已經生效了.

高可用性測試:

拔掉主調度服務器的網線或者關閉主調度服務器,看下VIP是否順利的切換到從調度器,假如切換正常,那就說明keepalived已經成功生效了.
相關文章
相關標籤/搜索