環境四台機器都是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
解壓到目前目錄
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
3、BACKUP上安裝haproxy,步驟就不詳細介紹,和MASTER上面一樣的。
haproxy.conf内容如下
stats uri /haproxy-status
stats hide-version
stats realm Gemini\ Haproxy
stats auth admin:admin
4、BACKUP上安裝keepalived,步驟也不多介紹,keepalived.conf檔案内容就兩處有變化,紅色字型标出
state BACKUP
priority 99
調用腳本check_haproxy.sh内容:
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
else
kill -9 $B
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
6、測試步驟
啟動MASTER上的keepalived服務,再啟動BACKUP上的keepalived服務。
确定MASTER上是否有192.168.2.100位址,用ip a檢視即可!
然後手動殺掉MASTER上的haproxy程序,看看是否能馬上恢複程序?
停止MASTER上的keepalived服務,确認BACKUP是否接管VIP位址?
最後再啟動MASTER上的keepalived服務,再确認MASTER是否再接管VIP位址?
總結:我這測試都沒問題,如果有問題請給我留言!