linux下利用Haproxy和keepalived实现简单负载均衡

架构拓扑图如下:

haproxy
VIP:  192.168.1.150
Master:192.168.1.10
Slave: 192.168.1.11
多台realServer之间用keepalived实现HA.
webserver1:192.168.1.101
webserver2:192.168.1.102
webserver3:192.168.1.103
webserver4:192.168.1.104

1.下载:

#wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.14.4.tar.gz

2.解压安装

#tar zxvf haproxy-1.3.14.4.tar.gz
#mv  haproxy-1.3.14.4  haproxy
#cd haproxy
#make TARGET=linux26
默认安装路径为/usr/local/haproxy目录里.
注意:
2.4内核版本 make TARGET=linux24

freebsd  make  TARGET=freebsd
solaris系统  make TARGET=solaris CPU=ultrasparc REGEX=static-pcre)
然后增加一个haproxy用户

3.配置haproxy

vi /usr/local/haproxy/haproxy.cfg

global
        chroot /var/chroot/haproxy
        #让进程在后台运行,即作为守护进程运行,正式运行的时候开启,此处先禁止,等同于在命令行添加参数 -D
        daemon
        gid     501
        log 127.0.0.1 local3 # 使用系统的syslog记录日志(通过udp,默认端口号为514)
        nbproc  2
        pidfile /var/run/haproxy-private.pid
        ulimit-n        65535
        user    haproxy
        #限制单个进程的最大连接数
        maxconn         32000
        spread-checks           4
        tune.maxaccept          8
        tune.maxpollevents      100
defaults askwan
        log     global
        mode    http
        option  httplog
        # 不记录空连接
        option  dontlognull
        log 127.0.0.1 local3
        retries 3
        option redispatch
        maxconn 32000
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000
listen  askwan  0.0.0.0:80
        appsession JSESSIONID len 52 timeout 3h
        cookie SRV insert indirect nocache
        # http的7层模式
        mode http
        stats enable
        stats hide-version
        stats uri /haproxy-stats
        stats realm Haproxy\ statistics
        stats auth askwan:askwan
        stats refresh 3s
        monitor-uri /haproxy_test
        # 设置服务器分配算法
        balance roundrobin
        option httpclose
        option forwardfor
        option httpchk HEAD /index.html HTTP/1.0
        server server1 192.168.1.101:80 check inter 2000
        server server2 192.168.1.102:80 check inter 2000
        server server3 192.168.1.103:80 check inter 2000
        server server4 192.168.1.104:80 check inter 2000
# 后台服务器:
# weight  -- 调节服务器的负重
# check -- 允许对该服务器进行健康检查
# inter  -- 设置连续的两次健康检查之间的时间,单位为毫秒(ms),默认值 2000(ms)
# rise  -- 指定多少次连续成功的健康检查后,即可认定该服务器处于可操作状态,默认值 2
# fall  -- 指定多少次不成功的健康检查后,认为服务器为当掉状态,默认值 3
# maxconn  -- 指定可被发送到该服务器的最大并发连接数
slave机器上同样配置.
启动haproxy
#/usr/local/haproxy/haproxy -f haproxy.cfg

下面是监控页面

4.在两台haproxy机器上安装配置好keepalived

Keepalived配置
VIP 192.168.1.150
Realserver  192.168.1.10 (MASTER)
RealServer  192.168.1.11 (SLAVER)

安装keepalived

[root@master ~]# wget http://www.keepalived.org/software/keepalived-1.1.17.tar.gz
[root@master ~]# tar -zxvf keepalived-1.1.17.tar.gz
[root@master ~]# cd keepalived-1.1.17
[root@master ~]# ./configure
configure: error:
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files.
解决办法:
[root@master ~]# yum -y install openssl-devel
[root@master ~]# ./configure
[root@master ~]# make;make install

编译的时候出现这个提示,说明keepalived和内核结合了,如果不是这样的,需要加上这个参数./configure –with-kernel-dir=/kernel/path

如果编译的时候提示:

configure: error: Popt libraries is required

解决办法:

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

分别在两台机器上配置keepalived.conf文件,对于slave要将state 设置为 BACKUP

#vi /usr/local/etc/keepalived/keepalived.conf

global_defs {
   notification_email {
     askwan@yahoo.com
   }
   notification_email_from askwan@askwan.com
   smtp_server 192.168.1.3
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass askwan
    }
    virtual_ipaddress {
        192.168.1.150
#(如果有多个VIP,继续换行填写.)
    }
}
启用keepalived服务
#/usr/local/sbin/keepalived start
5调试

这个是Master机keepalived调试信息:

  • Apr 22 08:32:22 db1 Keepalived: Starting Keepalived v1.1.15 (04/22,2008)
  • Apr 22 08:32:22 db1 Keepalived: daemon is already running
  • Apr 22 08:32:24 db1 Keepalived: Starting Keepalived v1.1.15 (04/22,2008)
  • Apr 22 08:32:24 db1 Keepalived: daemon is already running
  • Apr 22 08:32:46 db1 Keepalived: Starting Keepalived v1.1.15 (04/22,2008)
  • Apr 22 08:32:46 db1 Keepalived: daemon is already running
  • Apr 22 08:37:42 db1 Keepalived: Starting Keepalived v1.1.15 (04/22,2008)
  • Apr 22 08:37:42 db1 Keepalived: daemon is already running
  • Apr 22 08:39:34 db1 Keepalived: Starting Keepalived v1.1.15 (04/22,2008)
  • Apr 22 08:39:34 db1 Keepalived: daemon is already running
  • Apr 22 08:41:20 db1 sshd(pam_unix)[6317]: session opened for user root by root(uid=0)
  • Apr 22 08:41:32 db1 Keepalived: Terminating on signal
  • Apr 22 08:41:32 db1 Keepalived_vrrp: Terminating VRRP child process on signal
  • Apr 22 08:41:32 db1 Keepalived: Stopping Keepalived v1.1.15 (04/22,2008)
  • Apr 22 08:42:03 db1 Keepalived: Starting Keepalived v1.1.15 (04/22,2008)
  • Apr 22 08:42:03 db1 Keepalived_vrrp: Using MII-BMSR NIC polling thread…
  • Apr 22 08:42:03 db1 Keepalived_vrrp: Registering Kernel netlink reflector
  • Apr 22 08:42:03 db1 Keepalived_vrrp: Registering Kernel netlink command channel
  • Apr 22 08:42:03 db1 Keepalived_vrrp: Registering gratutious ARP shared channel
  • Apr 22 08:42:03 db1 Keepalived: Starting VRRP child process, pid=6358
  • Apr 22 08:42:03 db1 Keepalived_vrrp: Opening file ‘/usr/local/keepalived/etc/keepalived/keepalived.conf’.
  • Apr 22 08:42:03 db1 Keepalived_vrrp: Configuration is using : 33290 Bytes
  • Apr 22 08:42:03 db1 Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)]
  • Apr 22 08:42:04 db1 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
  • Apr 22 08:42:05 db1 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
  • Apr 22 08:42:05 db1 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
  • Apr 22 08:42:05 db1 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.150
  • Apr 22 08:42:10 db1 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.150
可以看到此机此时处于MASTER状态

slave机调试信息

Apr 20 08:43:28 db2 Keepalived: Starting Keepalived v1.1.15 (04/22,2008)
  • Apr 22 08:43:28 db2 Keepalived_vrrp: Using MII-BMSR NIC polling thread…
  • Apr 22 08:43:28 db2 Keepalived_vrrp: Registering Kernel netlink reflector
  • Apr 22 08:43:28 db2 Keepalived_vrrp: Registering Kernel netlink command channel
  • Apr 22 08:43:28 db2 Keepalived_vrrp: Registering gratutious ARP shared channel
  • Apr 22 08:43:28 db2 Keepalived: Starting VRRP child process, pid=5857
  • Apr 22 08:43:28 db2 Keepalived_vrrp: Opening file ‘/usr/local/keepalived/etc/keepalived/keepalived.conf’.
  • Apr 22 08:43:28 db2 Keepalived_vrrp: Configuration is using : 36052 Bytes
  • Apr 22 08:43:28 db2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
  • Apr 22 08:43:28 db2 Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)]
可以看到此机此时处于BACKUP状态

5.测试

地址已经分配完成后,可以开始做测试 测试过程简单, 在浏览器里输入 http://192.168.1.150,这时可能看到默认的web页面.
在HA的测试过程说一下
192.168.1.10宕机后192.168.1.11接管
192.168.1.10恢复后 192.168.1.11移交服务权限10
本方案经过我多次测试基本达到了架构拓扑图中的需求。这样一个简单高可用集群方案搭建完成。

如果文章中有什么不对,你有更好的建议,请联系我 askwan (at)yahoo.cn

更过信息请参阅:
http://haproxy.1wt.eu/
http://www.keepalived.org /

作者:askwan@流年飘飘『AskWan』
地址:http://www.askwan.com/read.php?10
保留文章版权。转载时请勿必以链接形式注明作者和原始出处及本声明!

摘自:http://bbs.linuxtone.org/thread-844-1-1.html

相关教程:

CentOS5.5环境下布署(ipvsadm)LVS+keepalived:http://blog.haohtml.com/archives/6348

利用LVS+Keepalived 实现高性能高可用负载均衡服务器:http://blog.haohtml.com/archives/5365

Leave a Reply