1 HAProxy介绍

HAProxy同样是一个知名的开源负载均衡器,号称提供高可用性,负载均衡和TCP、HTTP代理,使用也非常广泛。

从2001年的1.0版本到2020年的2.2版本,HAProxy可以说是经过了许多高流量场景的考验(可从官方http://www.haproxy.org/they-use-it.html 看到),性能比较稳定。

HAProxy和Nginx都属于四层、七层负载均衡,所以从产品实现的功能来讲,两者差别不大,只是配置的方式方法不同。但是Nginx也可以单独作为web服务器,这点是HAProxy不具备的。可以说HAProxy偏向属于技术栈的负载均衡器或反向代理类别,而nginx则偏向属于Web服务器类别。

负载均衡器、高性能和速度快是考虑使用HAProxy的主要因素;而高性能http服务器、性能和易于配置是考虑使用nginx的主要原因。

2 安装使用

HAProxy是没有windows版本的,这点不同于Nginx。

HAProxy官网从国内可能无法直接访问,我们可以用国内的软件源(比如华为源https://mirrors.huaweicloud.com/haproxy/)来下载。

下面以CentOS7操作系统,HAProxy最新版本2.2.2为例,介绍安装过程。

首先解压缩并接入目录:

tar -zxvf haproxy-2.2.2.tar.gz; cd haproxy-2.2.2

在make编译操作之前,应该先安装pcre、openssl、zlib等相关软件,否则会报错。

使用yum安装: yum install openssl openssl-devel pcre pcre-devel gcc

之后我们可以看到当前目录下有INSTALL安装指导文件,依据该文件,我们直接执行编译操作: make TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1

TARGET参数指定了系统平台;

USE_PCRE=1表示启用PCRE,用于匹配正则表达式;

USE_OPENSSL=1表示启用OPENSSL,用于HTTPS证书;

USE_ZLIB=1表示开启zlib压缩功能。

然后我们使用make install命令完成安装。

另外在当前目录下的examples目录下,有自带的启动脚本haproxy.init,修改其中的haproxy命令路径后就可以使用它启动、停止服务了。

3 配置文件

HAProxy的配置文件一般命名为haproxy.cfg,大致分为全局配置(global)、默认配置(defaults)、前端配置(frontend)、后端配置(backend),还有一个(统计页面)listen stats。

下面是一个简单的配置案例,用来说明一个HTTP请求的处理流程:

global
        log 127.0.0.1   local0 info
        pidfile     /var/run/haproxy.pid
        user        haproxy
        group       haproxy
        daemon
        maxconn     1024

defaults
        log     global
        mode    http
        option httplog
        option forwardfor  except 127.0.0.1
        option dontlognull
        timeout client      15s
        timeout server      15s 

frontend front_webs
        bind *:80        
        bind *:443 ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH no-sslv3  ssl crt /etc/ssl/certs/servername.pem 
        capture response header Content-Length len 10

        acl www_example_com    hdr(host) -i www.example.com    
        redirect scheme https if { hdr_beg(host) -i \ www.example.com } ! { ssl_fc } 
        use_backend www_example_com_pool if www_example_com

backend www_example_com_pool
        balance roundrobin
        timeout connect 5s
        timeout server  5s
        option httpchk GET /status.html
        http-check expect status 200
        server web_1 192.168.60.133:80 check inter 10000 maxconn 800
        server web_2 192.168.60.156:80 check inter 10000 maxconn 800

listen stats
       bind *:10443
       mode http
       stats uri /haproxy_status
       stats realm Haproxy\ statistic
       stats auth admin:123456

下面详细解释下各个指令。

global部分

log local0 info为日志级别,这样就可以使用系统的rsyslog服务获取日志了。

pidfile指定了服务的pid文件。

user、group指定了以什么用户启动haproxy进程。

Deamon意味着haproxy进程运行在后台,它相当于命令行"-D “参数。

maxconn指定了最大并发数。

defaults部分

HAProxy是在反向代理模式下工作的,所以服务器会把HAProxy的IP地址看作是客户端的地址。这和Nginx作为反向代理是一样的,都有这个问题,即默认情况下后端服务器获取不到真实客户端的IP地址。为了解决这个问题,HAProxy和Nginx可以在发送给服务器的请求中添加一个HTTP头 “X-Forwarded-For”。这个"X-Forwarded-For"带有客户端的真实IP地址。HAProxy实现的指令是option forwardfor,代表开启"X-Forwarded-For”。

log format、mode http、option httplog用来设置HTTP日志。

frontend部分

front_webs是为frontend命名,frontend部分可以有多个。

bind绑定端口,如果是https协议的话可在后边使用 ssl crt来指定域名证书。

capture response header 捕获请求返回时的头部信息。

acl www_example_com hdr(host) -i www.example.com

acl,即访问控制列表,用来定义一组规则或者条件,此处指定义www_example_com规则,只要是header中的host字段匹配到www.example.com,都会使用www_example_com规则。

redirect scheme https 一行表示http的请求会强制跳转为https。

use_backend www_example_com_pool if www_example_com

使用if判断,如果规则匹配www_example_com,则使用后端服务器www_example_com_pool所定义的部分。

backend部分

www_example_com_pool 为backend的命名

balance 指定负载均衡使用的算法,这里为轮询。

timeout指定连接超时时间。

option httpchk GET /status.html 对后端server的健康监测,此处是监测/status.html文件。

http-check expect status 200 如果/status.html状态不为200,则将相应server标记为down。

server定义服务器ip和端口

check inter 1000 maxconn 800表示每1秒(1000毫秒)监测一次,且此server最大连接数为800。

listen stats部分

定义了haproxy的统计接口和账号密码。

访问http://haproxy_ip:10443,可以看到如下图的统计信息。

img