haproxy+keepalived高可用負載均衡部署

一、安裝haproxy軟件

wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.22.tar.gz
tar zxvf haproxy-1.4.22.tar.gz
cd haproxy-1.4.22
make TARGET=linux26 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
cd /usr/local/haproxy
mkdir conf
cd conf

二、在/usr/local/haproxy/conf/目錄下創建配置文件(haproxy.cfg)

global
        maxconn 20480
        chroot /usr/local/haproxy
        uid 99
        gid 99
        daemon
        quiet
        nbproc 1
        stats socket /usr/local/haproxy/haproxy.stat mode 666
        log    127.0.0.1 local3
        pidfile  /usr/local/haproxy/haproxy.pid
        ulimit-n 65535
defaults
        log     global
        mode    http
        maxconn 20480
        #option  httplog clf
        option  httplog
        option  dontlognull
        option httpclose
        option abortonclose
        option redispatch
        retries 3
      monitor-uri /site_status 
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000
frontend WEB-BANMA-cluster 
        bind *:80  
        default_backend web_pool
                                                                                                                                                                                                                                                                                                                                                              
backend web_pool 
        balance source
#       balance leastconn    
        cookie  SERVERID
        option forwardfor
        option  httpchk GET /check.html
        server  app1_1 192.168.10.15:80   cookie app1inst1 check inter 2000 rise 2 fall 5 weight 3
        server  app1_2 192.168.10.16:80   cookie app1inst2 check inter 2000 rise 2 fall 5 weight 6
        stats refresh 5s
        stats uri /status
        stats realm Haproxy\ statistics
        stats auth admin:admin123
        stats admin if TRUE

三、如何給haproxy添加日誌記錄

①、如果是centos6.0以下版本配置

一、 vi /etc/syslog.conf
local3.*    /var/log/haproxy.log
二、vi /etc/sysconfig/syslog
SYSLOGD_OPTIONS="-m 0"
改成
SYSLOGD_OPTIONS="-r -m 0" #enables logging from remote machines
三、重啓動
/etc/init.d/syslog restart

②、Centos6.0或centos6.0以上版本

一、配置日誌
vi /etc/rsyslog.conf(去掉下面兩行前的註釋,一定要去掉,否則不記錄日誌)
$ModLoad imudp.so
$UDPServerRun 514
##添加日誌記錄
local3.*         /var/log/haproxy.log(添加haproxy日誌記錄文件)
二、重啓動服務
/etc/init.d/rsyslog restart

四、啓動haproxy,進行檢查,並查看日誌

/usr/local/haproxy/sbin/haporxy -f /usr/local/haproxy/conf/haproxy.cfg

113145161.jpg

#######################################################################################

一、keepalived安裝(主備機器安裝方法一樣)

wget http://rpm5.org/files/popt/popt-1.16.tar.gz
tar zxvf popt-1.16.tar.gz
cd popt-1.16
./configure
make && make install
wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
tar zxvf keepalived-1.2.7.tar.gz
cd keepalived-1.2.7
./configure --prefix=/usr/local/keepalived
make && make install

二、初始化腳本

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived 
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ 
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ 
mkdir -p /etc/keepalived/ 
ln -s  /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf  
chmod +x /etc/init.d/keepalived

三、主機器配置文件(vi /etc/keepalived/keepalived.conf ),備機器只要(修改有註釋地方即可)

! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/usr/local/keepalived/check_haproxy.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
        state MASTER   (備機,改MASTER爲BACKUP)
        interface eth0
        virtual_router_id 51
        priority 150  (備機,改150爲149)
        advert_int 1
        authentication {
        auth_type PASS
        auth_pass 1111
        }
        track_script {
                chk_http_port
        }
        virtual_ipaddress {
        10.1.1.60
        }
}

四、檢測haproxy狀態的文件

status=$(ps aux|grep haproxy | grep -v grep | grep -v bash | wc -l)
if [ "${status}" = "0" ]; then
   /etc/init.d/haproxy start
   status2=$(ps aux|grep haproxy | grep -v grep | grep -v bash |wc -l)
   if [ "${status2}" = "0"  ]; then
       /etc/init.d/keepalived stop
   fi
fi


五、啓動keepalived

/etc/init.d/keepalived start

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章