haproxy keepalived實現高可用負載均衡(例項配置)

NO IMAGE

作者:split_two

環境四臺機器都是RedHat4.8(64位)版本:

IP地址             用處
192.168.5.55       MASTER
192.168.2.73       BACKUP
192.168.5.54       負載A
192.168.5.57       負載B
192.168.2.100      VIP

1、MASTER上安裝haproxy
複製程式碼 程式碼如下:
wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.20.tar.gz /root

解壓到當前目錄
tar zxvf haproxy-1.3.20.tar.gz
進到此目錄
cd haproxy-1.3.20
編譯及安裝,安裝到/usr/local目錄,但是安裝完此目錄下沒有haproxy資料夾,我也很奇怪!
make TARGET=linux26 prefix=/usr/local/haproxy install
但是出來的結果會提示如下資訊

install -d /usr/local/sbin
install haproxy /usr/local/sbin
install -d /usr/local/share/man/man1
install -m 644 doc/haproxy.1 /usr/local/share/man/man1
install -d /usr/local/doc/haproxy
for x in configuration architecture haproxy-en haproxy-fr; do \
        install -m 644 doc/$x.txt /usr/local/doc/haproxy ; \
done

提示你啟動haproxy在sbin目錄,其餘的在doc目錄
進入到haproxy目錄
cd /usr/local/doc/haproxy
新建一個haproxy主配置檔案
複製程式碼 程式碼如下:
vi haproxy.cfg
global
   log 127.0.0.1 local0
   maxconn 4096
   chroot /usr/local/doc/haproxy
   uid 501
   gid 501
   daemon
   nbproc 1
   pidfile /usr/local/doc/haproxy/haproxy.pid
defaults
   log    127.0.0.1  local3
   mode   http
   option httplog
   option httpclose
   option dontlognull
   option forwardfor
   option redispatch
   retries 2
   maxconn 2000
   balance roundrobin
   stats   uri    /haproxy-status    檢視狀態
   stats hide-version       隱藏haproxy版本
   stats realm Gemini\ Haproxy      檢視狀態需要口令
   stats auth admin:admin           使用者名稱和密碼
   contimeout  5000
   clitimeout  50000
   srvtimeout  50000
listen web_proxy 192.168.2.100:80
   server web1 192.168.5.54:8080 cookie app1inst1 check inter 2000 rise 2 fall 5
   server web2 192.168.5.57:8080 cookie app1inst2 check inter 2000 rise 2 fall 5

2、MASTER安裝keepalived
tar zxvf keepalived-1.1.15.tar.gz
vi /usr/src/kernels/2.6.9-89.EL-smp-x86_64/include/linux/types.h
將如下兩行註釋掉,否則編譯會出錯,跟我這個版本的系統有關係,你的也許不要!
複製程式碼 程式碼如下:
/*
typedef __u16 __bitwise __sum16;
typedef __u32 __bitwise __wsum;
*/
cd keepalived-1.1.15
./configure
make
make install

將keepalived作為系統服務啟動

cp /usr/local/etc/rc.d/init.d/keepalived /etc/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/
vi /etc/keepalived/keepalived.conf內容如下:
複製程式碼 程式碼如下:
! Configuration File for keepalived
global_defs {
   router_id LVA_DEVEL
}
vrrp_script chk_http_port {
   script “/root/check_haproxy.sh”
   interval 2
   weight  2
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
track_script {
    chk_http_port
}
    virtual_ipaddress {
        192.168.2.100
    }
}

上面呼叫了一個指令碼check_haproxy.sh,內容如下:
複製程式碼 程式碼如下:
#!/bin/bash
A=`ps -C haproxy –no-header | wc -l`
if [ $A -eq 0 ];then
/usr/local/sbin/haproxy -f /usr/local/doc/haproxy/haproxy.cfg
echo “haproxy start”
sleep 3
if [ `ps -C haproxy –no-header | wc -l` -eq 0 ];then
/etc/init.d/keepalived stop
echo “keepalived stop”
fi
fi

3、BACKUP上安裝haproxy,步驟就不詳細介紹,和MASTER上面一樣的。
haproxy.conf內容如下
複製程式碼 程式碼如下:
global
   log 127.0.0.1 local0
   maxconn 4096
   chroot /usr/local/doc/haproxy
   uid 501
   gid 501
   daemon
   nbproc 1
   pidfile /usr/local/doc/haproxy/haproxy.pid
defaults
   log    127.0.0.1  local3
   mode   http
   option httplog
   option httpclose
   option dontlognull
   option forwardfor
   option redispatch
   retries 2
   maxconn 2000
   balance roundrobin
   stats   uri    /haproxy-status
   stats hide-version
   stats realm Gemini\ Haproxy
   stats auth admin:admin
   contimeout  5000
   clitimeout  50000
   srvtimeout  50000
listen web_proxy 192.168.2.100:80
   server web1 192.168.5.54:8080 cookie app1inst1 check inter 2000 rise 2 fall 5
   server web2 192.168.5.57:8080 cookie app1inst2 check inter 2000 rise 2 fall 5

4、BACKUP上安裝keepalived,步驟也不多介紹,keepalived.conf檔案內容就兩處有變化,紅色字型標出
複製程式碼 程式碼如下:
! Configuration File for keepalived
global_defs {
   router_id LVA_DEVEL
}
vrrp_script chk_http_port {
   script “/root/check_haproxy.sh”
   interval 2
   weight  2
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
track_script {
    chk_http_port
}
    virtual_ipaddress {
        192.168.2.100
    }
}

呼叫指令碼check_haproxy.sh內容:
複製程式碼 程式碼如下:
#!/bin/bash
A=`ip a | grep 192.168.2.100 | wc -l`
B=`ps -ef | grep haproxy | grep -v grep | awk ‘{print $2}’`
if [ $A -gt 0 ];then
/usr/local/sbin/haproxy -f /usr/local/doc/haproxy/haproxy.cfg
else
kill -9 $B
fi

5、兩臺負載機器我就不多介紹了,用的是系統自帶的apache
登入192.168.5.54上操作:
echo ‘this is 192.168.5.54!’ > /var/www/html/index.html
修改/etc/httpd/conf/httpd.conf檔案的監聽埠為8080
sed -i ‘s/Listen 80/Listen 8080/g’ /etc/httpd/conf/httpd.conf
/etc/init.d/httpd start
登入192.168.5.57上操作:
echo ‘Hello,This is 192.168.5.57!’ > /var/www/html/index.html
修改/etc/httpd/conf/httpd.conf檔案的監聽埠為8080
sed -i ‘s/Listen 80/Listen 8080/g’ /etc/httpd/conf/httpd.conf
/etc/init.d/httpd start

6、測試步驟

啟動MASTER上的keepalived服務,再啟動BACKUP上的keepalived服務。
確定MASTER上是否有192.168.2.100地址,用ip a檢視即可!
然後手動殺掉MASTER上的haproxy程序,看看是否能馬上恢復程序?
停止MASTER上的keepalived服務,確認BACKUP是否接管VIP地址?
最後再啟動MASTER上的keepalived服務,再確認MASTER是否再接管VIP地址?
總結:我這測試都沒問題,如果有問題請給我留言!

原文:http://blog.chinaunix.net/uid-23916356-id-3387261.html

您可能感興趣的文章:

Keepalived HAProxy實現MySQL高可用負載均衡的配置LVS Keepalived構建高可用負載均衡(測試篇)LVS Keepalived構建高可用負載均衡配置方法(配置篇)haproxy keepalived實現高可用負載均衡(理論篇)keepalived實現nginx高可用