CentOS 5.9 下使用Haproxy+Keepalived构建高可用linux集群

好久都没有认真的写过篇博客了,这两篇博客都是蜻蜓点水似的一带而过,没什么营养。苦苦等来的负载均衡系列,本以为会很精彩,谁知就像很多很烂的电影一样,正准备高潮了,突然就没了,很突兀。话不多说,新的一年,希望一切安好。

一:安装

a:下载
wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz

b:安装
#安装文档请参考 haproxy-1.4.24/README
#uname -a           //查看linux内核版本
tar zxvf haproxy-1.4.24.tar.gz
cd haproxy-1.4.24
make TARGET=linux26 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy

二:配置

KEEPALIVED_M:
#全局定义
global_defs {
   #运行keepalived的机器的一个标示
   router_id HAPROXY
}

vrrp_script chk_haproxy {
    script "/usr/local/bin/chk_haproxy.sh"
    interval 2
    weight 2
}

#vrrp实例配置
vrrp_instance VI_WEB {
    state MASTER
    #state BACKUP
    #nopreempt
    interface eth0
    virtual_router_id 58  
    priority 180 
    #检查间隔,默认1s
    advert_int 1 
    #在切换到MASTER状态后,延迟进行gratuitous ARP 请求
    garp_master_delay 1
    authentication {
        auth_type PASS
        auth_pass KJj23576hYgu23IP
    }   
      #设置额外的监控,里面的任意一个网卡出现问题,都会进入FAULT状态
      track_interface {
       eth0
    }
      track_script {
        chk_haproxy
    }
    virtual_ipaddress {
    192.168.245.133
    }
}

KEEPALIVED_B:
#全局定义
global_defs {
   #运行keepalived的机器的一个标示
   router_id HAPROXY
}

vrrp_script chk_haproxy {
    script "/usr/local/bin/chk_haproxy.sh"
    interval 2
    weight 2
}

#vrrp实例配置
vrrp_instance VI_WEB {
    state BACKUP
    #state BACKUP
    #nopreempt
    interface eth0
    virtual_router_id 58  
    priority 100 
    #检查间隔,默认1s
    advert_int 1 
    #在切换到MASTER状态后,延迟进行gratuitous ARP 请求
    garp_master_delay 1
    authentication {
        auth_type PASS
        auth_pass KJj23576hYgu23IP
    }   
      #设置额外的监控,里面的任意一个网卡出现问题,都会进入FAULT状态
      track_interface {
       eth0
    }
      track_script {
        chk_haproxy
    }
    virtual_ipaddress {
    192.168.245.133
    }
}

HAPROXY:
global
        log 127.0.0.1   local3
        maxconn 65535
        chroot          /usr/local/haproxy
        uid nobody
        gid nobody
        daemon
        nbproc 1
        pidfile /usr/local/haproxy/haproxy.pid

defaults
         log     127.0.0.1       local3
         mode   http
         option httplog
         option httpclose
         option dontlognull
         option forwardfor
         #option forwardfor header Client-IP
         option redispatch
         retries 2
         maxconn 2000
         contimeout      5000
         clitimeout      50000
         srvtimeout      50000

listen  web_ha
        bind 0.0.0.0:9999
        mode http
        transparent
        stats refresh 30s
        stats   uri     /haproxy-stats
        stats hide-version
        stats realm Haproxy\statistics
        stats auth admin:haproxy

listen  main_webs
        bind  *:80
        mode http
        option httplog
        cookie SESSION_COOKIE insert indirect nocache
        log global
        #option httpchk HEAD /index.php HTTP/1.0
        option httpchk GET /do_not_delete/do_not_delete.php HTTP/1.0
        server s129 192.168.245.129:80 weight 16 cookie s129 check inter 2000 rise 2 fall 3
        server s130 192.168.245.130:80 weight 20 cookie s130 check inter 2000 rise 2 fall 3


chk_haproxy.sh:
#!/bin/bash
STATUS=`ps -C haproxy --no-header |wc -l`

if [ "$STATUS" -eq "0" ]; then
  /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg
  STATUS2=`ps -C haproxy --no-header|wc -l`
        if [ "$STATUS2" -eq "0"  ]; then
        kill -9 $(ps -ef | grep keepalived | grep -v grep | awk '{print $2}')
        fi
fi

三:是时候结束了

自言自语:

Haproxy是个很强的负载均衡器,可玩性很多,七层可以根据http报文来过滤,值得投入时间来学习,配合keepalived来做高可用,线上跑着就很稳定了。

参考资料:

haproxy 官网: http://haproxy.1wt.eu/

keepalived 官网:http://www.keepalived.org/