HA proxy

HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在时下的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。

HAProxy实现了一种事件驱动、单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户端(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。

实验环境3台服务器2台apache后端,1个haproxy前端

192.168.1.107充当外网IP,10.0.0.3是连接内网的网卡  apache1和apache2的网关指向10.0.0.3

安装HAproxy 我的系统是centos6.5 64 位,默认自带有haproxy软件,当然你也可以源码编译,我这块为方便就yum啦

yum install haproxy 就OK了

装完之后可以查看装了那些文件

Installed:
  haproxy.x86_64 0:1.4.24-2.el6                                                                                                              

Complete!
[root@tomcat2 ~]# rpm -q haproxy
haproxy-1.4.24-2.el6.x86_64
[root@tomcat2 ~]# rpm -ql haproxy
/etc/haproxy
/etc/haproxy/haproxy.cfg
/etc/logrotate.d/haproxy
/etc/rc.d/init.d/haproxy
/usr/bin/halog
/usr/sbin/haproxy
/usr/share/doc/haproxy-1.4.24
/usr/share/doc/haproxy-1.4.24/CHANGELOG
/usr/share/doc/haproxy-1.4.24/LICENSE
/usr/share/doc/haproxy-1.4.24/README
/usr/share/doc/haproxy-1.4.24/acl-content-sw.cfg
......

配置之前备份配置文件

[root@tomcat2 ~]# cd /etc/haproxy/
[root@tomcat2 haproxy]# cp haproxy.cfg haproxy.cfg.bak
[root@tomcat2 haproxy]# ls
haproxy.cfg  haproxy.cfg.bak

修改

[root@tomcat2 haproxy]# vim /etc/sysconfig/rsyslog 
根据配置文件提示加上-r
SYSLOGD_OPTIONS="-c 2 -r"

在/etc/rsyslog.cnf

vim /etc/rsyslog.conf 
local2.*  
root@tomcat2 haproxy]# /etc/init.d/rsyslog restart
关闭系统日志记录器:                                       [确定]
启动系统日志记录器:                                       [确定]                                              /var/log/hapeoxy.log

修改配置文件如下

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000
listen stats
    mode http
    bind 0.0.0.0:1080
    stats enable
    stats hide-version
    stats uri     /haproxyadmin?stats
    stats realm   Haproxy\ Statistics
    stats auth    admin:admin
    stats admin if TRUE
frontend web
    bind *:80
    mode http
    log global
    option httpclose
    option logasap
    option dontlognull
    capture request  header Host len 20
    capture request  header Referer len 60
    default_backend servers
backend servers
    balance roundrobin
    server apache1 10.0.0.1:80 check maxconn 2000
    server apache2 10.0.0.2:80 check maxconn 2000

重启服务

[root@tomcat2 haproxy]# /etc/init.d/haproxy restart
停止 haproxy:                                             [确定]
正在启动 haproxy:                                         [确定]
[root@tomcat2 haproxy]# netstat -antlp | grep  haproxy
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      2029/haproxy        
tcp        0      0 0.0.0.0:1080                0.0.0.0:*                   LISTEN      2029/haproxy

测试

http://192.168.1.107

在刷新

表示ok负载均衡!

访问haproxy后台管理

http://192.168.1.107:1080/haproxyadmin?stats

用户名密码admin

 感觉和lvs nat模型一样

haproxy也可以代理其他tcp端口提供的服务代理mysql如下配置

负载均衡MySQL服务的配置示例

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

defaults
    mode                    tcp
    log                     global
    option                  httplog
    option                  dontlognull
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 600

listen stats
    mode http
    bind 0.0.0.0:1080
    stats enable
    stats hide-version
    stats uri     /haproxyadmin?stats
    stats realm   Haproxy\ Statistics
    stats auth    admin:admin
    stats admin if TRUE


frontend mysql
    bind *:3306
    mode tcp
    log global
    default_backend DBservers

backend DBservers
    balance leastconn
    server db1 10.0.0.1:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300
    server db2 10.0.0.2:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300




转载于:https://my.oschina.net/kcw/blog/317629

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值