一、简介

    

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

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

                                                         ------百科资料

二、Haproxy安装配置

①安装

# wget http://www.haproxy.org/download/1.5/src/haproxy-1.5.14.tar.gz
# tar -zxvf haproxy-1.5.14.tar.gz
# cd haproxy-1.5.14
# make TARGET=linux2628 ARCH=x86_64   ##TARGET指定内核版本,ARCH指定CPU架构
# make install 
install -d "/usr/local/sbin"
install haproxy "/usr/local/sbin"
install haproxy-systemd-wrapper "/usr/local/sbin"
install -d "/usr/local/share/man"/man1
install -m 644 doc/haproxy.1 "/usr/local/share/man"/man1
install -d "/usr/local/doc/haproxy"
for x in configuration architecture haproxy-en haproxy-fr; do \
install -m 644 doc/$x.txt "/usr/local/doc/haproxy" ; \
done


②创建配置文件和启动文件

# mkdir /etc/haproxy
# cp haproxy-1.5.14/examples/haproxy.cfg  /etc/haproxy/
# cp haproxy-1.5.14/examples/haproxy.init  /etc/init.d/haproxy
# chmod u+x /etc/init.d/haproxy
# ln -s /usr/local/sbin/haproxy /usr/sbin/
# mkdir /usr/share/haproxy


三、编辑配置文件

# vi /etc/haproxy/haproxy.cfg
global                        ------全局配置------
        log 127.0.0.1   local0   ##日志输出配置,所有日志都记录在本机,通过local0输出
        log 127.0.0.1   local1 notice
        #log loghost    local0 info
        maxconn 4096                     ##最大连接数
        chroot /usr/share/haproxy          ##chroot运行路径
        uid 99                         ##所属用户UID
        gid 99                             ##所属运行的GID
        daemon                         ##以后台形式运行haproxy
        #debug                         ##调试模式,输出启动信息到标准输出
        #quiet                         ##安静模式,启动时无输出
defaults                     ------默认配置-----
        log     global
        mode    http    ##默认模式{tcp|http|health},tcp是4层,http是7层,health只会返回OK
        option  httplog          ##日志类别:http日志格式
        option  dontlognull           ##不记录健康检查的日志信息
        retries 3                       ##3次连接失败就认为服务不可用
        option  redispatch          ##ServerID对应的服务器挂掉后,强制定向到其他健康服务器
        maxconn 4000                         ##默认最大连接数
        timeout connect      5000                    ##连接超时
        timeout client      50000                   ##客户端超时
        timeout server      50000                   ##服务端超时
        
listen  web-proxy 192.168.10.128:8000
        mode http
        option  httpchk /index.html
        balance roundrobin                          ##算法
        server  web1 192.168.10.129:80 cookie server01 check inter 2000 rise 2 fall 3
        server  web2 192.168.10.130:80 cookie server02 check inter 2000 rise 3 fall 3
        ##服务器定义(check指健康状况检查,inter 2000指检测频率;rise 2指从离线状态转换至正常状态需要成功检查的次数;fall 3指失败3次即认为服务器不可用)

listen status                                ##监控页面设置
        mode http                             ##http的7层模式
        bind 0.0.0.0:1080                    ##监听端口
        stats enable                         
        stats hide-version                     ##隐藏统计页面上的HAproxy版本信息
        stats uri     /haproxyadmin           ##监控页面URL
        stats auth    admin:admin                  ##监控页面用户名和密码
        stats admin if TRUE               ##手工启用、禁用后端服务器


错误:

[WARNING] 286/001645 (41708) : parsing [/etc/haproxy/haproxy.cfg:21]: keyword 'redispatch' is deprecated in favor of 'option redispatch', and will not be supported by future versions.
[WARNING] 286/001645 (41708) : parsing [/etc/haproxy/haproxy.cfg:23] : the 'contimeout' directive is now deprecated in favor of 'timeout connect', and will not be supported in future versions.
[WARNING] 286/001645 (41708) : parsing [/etc/haproxy/haproxy.cfg:24] : the 'clitimeout' directive is now deprecated in favor of 'timeout client', and will not be supported in future versions.
[WARNING] 286/001645 (41708) : parsing [/etc/haproxy/haproxy.cfg:25] : the 'srvtimeout' directive is now deprecated in favor of 'timeout server', and will not be supported in future versions.

解决:根据提示更改相应选项的名称即可


四、配置日志文件

# vi /etc/rsyslog.conf
local0.*                                                /var/log/haproxy.log
# systemctl restart rsyslog


五、启用并查看HAproxy

①启用和关闭HAproxy

a.

# haproxy -f /etc/haproxy/haproxy.cfg           ##开启haproxy
# yum install psmisc               ##注:最小化安装centos7无killall命令
# killall haproxy                   ##关闭haproxy


b.

# systemctl start haproxy             ##开启haproxy
# systemctl stop haproxy               ##关闭haproxy

②登录页面查看

浏览器输入http://localhost:1080/haproxyadmin,输入账号密码进行查看

wKioL1Yd1-qC4UWpAAY1AJAS0cI598.jpg


注:由于并未启用web1和web2,所以都是down的。