keepalived+haproxy構建高可用負載均衡集羣

Keepalived+haproxy構建web高可用負載均衡系統linux

       本章將使用keepalived作爲高可用集羣調度用戶請求,而且檢測haproxy負載均衡器的狀態,實時地完成節點的動態調整。
web

keepalived原理圖:redis


Layer3:Keepalived使用Layer3的方式工做式時,Keepalived會按期向服務器羣中的服務器算法

發送一個ICMP的數據包(既咱們平時用的Ping程序),若是發現某臺服務的IP地址沒有激活,Keepalived便報告這臺服務器失效,並將 它從服務器羣中剔除,這種狀況的典型例子是某臺服務器被非法關機。Layer3的方式是以服務器的IP地址是否有效做爲服務器工做正常與否的標準。在本文 中將採用這種方式。vim

Layer4:若是您理解了Layer3的方式,Layer4就容易了。Layer4主要以TCP端口的狀態來決定服務器工做正常與否。如web server的服務端口通常是80,若是Keepalived檢測到80端口沒有啓動,則Keepalived將把這臺服務器從服務器羣中剔除。後端

Layer5:Layer5就是工做在具體的應用層了,比Layer3,Layer4要複雜一點,在網絡上佔用的帶寬也要大一些。 Keepalived將根據用戶的設定檢查服務器程序的運行是否正常,若是與用戶的設定不相符,則Keepalived將把服務器從服務器羣中剔除。bash

haproxy工做機制:服務器

HAProxy相比LVS的使用要簡單不少,功能方面也很豐富。當 前,HAProxy支持兩種主要的代理模式:"tcp"也即4層(大多用於郵件服務器、內部協議通訊服務器等),和7層(HTTP)。在4層模式 下,HAProxy僅在客戶端和服務器之間轉發雙向流量。7層模式下,HAProxy會分析協議,而且能經過容許、拒絕、交換、增長、修改或者刪除請求 (request)或者回應(response)裏指定內容來控制協議,這種操做要基於特定規則。cookie

系統環境:CentOS6.4
網絡

                iptables 關閉      

               selinux is    disabled

1、keepalived的安裝與配置

#tar zxf keepalived-1.2.7.tar.gz

#cd keepalived-1.2.7

#./configure --prefix=/usr/local/keepalived                              指定路徑編譯

其中可能安裝一下依賴包:

openssl-devel   popt-devel        libnl-devel

# make && make install                                                            執行make安裝

# cd /usr/local/keepalived/                                                      默認主目錄,存在sbin/和etc/目錄

# ln -s /usr/local/keepalived/sbin/keepalived /sbin/                 便於管理keepalived,將文件連接

# ln -s /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

#ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

# ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

# vim /etc/keepalived/keepalived.conf
 root@localhost
}
   notification_email_from keepalived@server3.example.com                       通知收件人地址
virtual_router_id 88                                                                                    定義虛擬路由id
    priority 100                                                                                            定義主節點(權值決定)
   virtual_ipaddress {                                                                                    定義VIP
        192.168.0.88
    }
}

使用lvs+keepalived時使用一下配置
virtual_server 192.168.0.88 80{                                                                  定義虛擬服務
    delay_loop 6
    lb_algo rr                                                                                                使用輪詢方式調度算法
    lb_kind DR                                                                                            使用DR直連模式
  #  persistence_timeout 50       這個是nat的必須作的,若是加上這個的話,就會持續連接這個                     
    protocol TCP
    real_server 192.168.0.198 80 {                                                             定義真實服務
        weight 1
         TCP_CHECK   {  
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.0.199 80 {
        weight 1
         TCP_CHECK   {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }

#/etc/init.d/keepalived start

由於要構建keepalived高可用集羣,所以須要在keepalived備用節點上安裝配置一樣的服務。


二 、 haproxy的安裝與配置

#tar zxf haproxy-1.4.23.tar.gz

#cd haproxy-1.4.23

#find -name *.spec                                                                    發現spec文件,能夠將源碼包打壓成rpm

#yum install rpm-build -y                                                            安裝rpm包構建軟件

#rpmbuild -tb haproxy-1.4.23.tar.gz                                            使用源碼包構建rpm包

#cd /root/rpmbuild/RPMS/x86_64                                                生成的rpm包

#rpm -ivh *.rpm                                                                            安裝keepalived的rpm包

# vim /etc/haproxy/haproxy.cfg
defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        option   redispatch
        maxconn 2000
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000
listen status_auth 192.168.0.103:8080                           給監控界面作一個端口綁定,指定用戶和密碼
         stats enable
         stats uri /admin
         stats auth  user:passwd                                         定義監控頁面的用戶和密碼
         stats refresh   6

slisten   ha_proxy 192.168.0.103:80                在103主機上定義真實主機(Real Server)
        balance roundrobin
        server  inst1 192.168.0.198:80
        server  inst2 192.168.0.199:80

# vim /etc/rsyslog.conf
開啓tcp或者udp
$ModLoad imudp
$UDPServerRun 514
local0.*                                               /var/log/haproxy.log
#/etc/init.d/rsyslog restart

#/etc/init.d/haproxy start

web負載均衡方案haproxy構建完畢!

訪問http://192.168.0.103 能夠訪問到198和199主機的內容

訪問http://192.168.0.103:8080 使用用戶名和密碼登錄能夠查看連接狀態


3、整合keepalived和haproxy

# vim check_haproxy.sh                                 編寫一個檢測haproxy狀態腳本
#!/bin/bash
/etc/init.d/haproxy status &> /dev/null || /etc/init.d/haproxy restart &> /dev/null
if [ $? -ne 0 ] ;then
/etc/init.d/keepalived stop &> /dev/null
fi

# chmod +x check_haproxy.sh
#./check_haproxy.sh                                  在haproxy關閉時自動啓動haproxy

修改keepalived文件

# vim keepalived.conf
vrrp_script check_haproxy {
      script "/opt/check_haproxy.sh"
      interval 2
     weight  2
}

    virtual_ipaddress {
        192.168.0.88                                     新的VIP
    }
    track_script {
         check_haproxy
}

# vim /etc/haproxy/haproxy.cfg
listen  www.domain.com  *:80                        因此IP均可以訪問(給本機VIP作解析)
        cookie  SERVERID rewrite
        balance roundrobin
        server  app1_1 192.168.0.198:80 cookie app1inst1 check inter 2000 rise 2 fall 3
        server  app1_2 192.168.0.199:80 cookie app1inst2 check inter 2000 rise 2 fall 3

listen status_auth *:8080                                使用任意客戶端檢測
         stats enable
         stats uri /admin
         stats auth  user:passwd                                         定義監控頁面的用戶和密碼
         stats refresh   6

# /etc/init.d/keepalived restart

# ip addr show                                   查看主keepalived已經加載了VIP資源
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:9c:c9:22 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.103/24 brd 192.168.0.255 scope global eth0         103爲主節點IP
    inet 192.168.0.88/32 scope global eth0                                  88爲VIP

# /etc/init.d/haproxy status                  查看狀態爲running

注意:構建keepalived+haproxy高可用負載均衡集羣必須在兩個節點分別配置兩個服務,而且保證相同的配置和服務,在備用節點上注意將權值修改小些。

測試高可用和負載均衡:

任何一個主機的keepalived出現故障,另一個主機會接管相應的資源。

任何一個haproxy出現故障,客戶仍然能夠訪問後端Real Server提供的服務。

相關文章
相關標籤/搜索