HAproxy+Keepalived高可用负载均衡部署

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

HAproxy+Keepalived高可用负载均衡部署

#######################################################################################
一、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

HAproxy 的详细介绍:请点这里
HAproxy 的下载地址:请点这里

推荐阅读:

相关推荐