Haproxy 安装配置详解

haproxy介绍

haproxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。根据官方数据,其最高极限支持10G的并发。
HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。

其支持从4层至7层的网络交换,即覆盖所有的TCP协议。就是说,Haproxy 甚至还支持 Mysql 的均衡负载。。

如果说在功能上,能以proxy反向代理方式实现 WEB均衡负载,这样的产品有很多。包括 Nginx,ApacheProxy,lighttpd,Cheroke 等。

但要明确一点的,Haproxy 并不是 Http 服务器。以上提到所有带反向代理均衡负载的产品,都清一色是 WEB 服务器。简单说,就是他们能自个儿提供静态(html,jpg,gif..)或动态(php,cgi..)文件的传输以及处理。而Haproxy 仅仅,而且专门是一款的用于均衡负载的应用代理。其自身并不能提供http服务。
haproxy配置主要有五部分:

global
参数是进程级的,通常和操作系统(OS)相关。这些参数一般只设置一次,如果配置无误,就不需要再次配置进行修改

defaults
配置默认参数的,这些参数可以被利用配置到frontend,backend,listen组件

frontend
接收请求的前端虚拟节点,Frontend可以根据规则直接指定具体使用后端的 backend(可动态选择)。

backend
后端服务集群的配置,是真实的服务器,一个Backend对应一个或者多个实体服务器。

listen
Frontend和Backend的组合体。

安装

个人安装脚本装,此步略

配置详解

##############全局配置段##############
##参数是进程级的,通常和操作系统(OS)相关##
global
log 127.0.0.1 local3 info #定义syslog服务器  日志级别info   #vim /etc/sysconfig/rsyslog     SYSLOGD_OPTIONS="-c 5 -r"    vim /etc/rsyslog.conf    添加一行     local3.*    /var/log/haproxy.log        /etc/init.d/rsyslog restart
maxconn 81920   #最大连接数
uid haproxy     #运行用户
gid haproxy        #运行用户组
daemon            #以守护进程的方式后台运行
nbproc 1        启动进程1
ulimit-n 65535                  #ulimit的数量限制 
tune.ssl.default-dh-param 2048    
###############默认的全局设置###############

##这些参数可以被利用配置到frontend,backend,listen组件##

defaults
log global        #全局日志
mode http        #运行模式协议
maxconn 81920    #每个进程可接受最大连接数
retries    3        #定义连接后端服务器的失败重连次数,连接失败次数超过此值后将会将对应后端服务器标记为不可用
option dontlognull    #不记录健康检查的日志信息 
option redispatch    #serverId对应的服务器挂掉后,强制定向到其他健康的服务器  
option abortonclose    #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
option httpclose    #每次请求完毕后主动关闭http通道 
option forwardfor    #启用X-Forwarded-For,在requests头部插入客户端IP发送给后端的server,使后端server获取到客户端的真实IP
balance roundrobin    #轮询算法 此算法是动态的,这表示其权重可以在运行时进行调整,不过,在设计上,每个后端服务器仅能最多接受4128个连接
#balance source #hash算法 类似nginx的ip_hash 
#balance leastconn #最小连接算法,但是为静态方法,在运行时调整其服务器权重不会生效;不过,其在后端服务器连接数上没有限制;
timeout connect 50000   #连接超时时间,默认单位是毫秒
timeout client 50000    #客户端超时 ,默认单位是毫秒
timeout server 50000    #服务器超时 ,默认单位是毫秒
timeout check 2000        #心跳检测超时 
#####监控页面的设置##############
stats refresh 30   #统计页面刷新间隔     
stats uri /haproxy    #设置统计页面的uri为/haproxy
stats auth  xxxx:xxxx #设置统计页面认证的用户和密码,如果要设置多个,另起一行写入即可
stats hide-version    #隐藏统计页面上的haproxy版本信息

errorfile 403 /export/servers/haproxy1.5/html/50x.html
errorfile 500 /export/servers/haproxy1.5/html/50x.html
errorfile 502 /export/servers/haproxy1.5/html/50x.html
errorfile 503 /export/servers/haproxy1.5/html/50x.html
errorfile 504 /export/servers/haproxy1.5/html/50x.html
#####后端服务器配置#####
frontend http
    bind *:80    #绑定监听哪个地址的哪个端口哪个网卡  可以指定端口范围
    mode http    #运行模式协议
    log global    #继承全局日志的设置
    option httplog    #启用http的log 
    option httpclose #每次请求完毕后主动关闭http通道,HA-Proxy不支持keep-alive模式   
    option forwardfor  except 127.0.0.1  #启用X-Forwarded-Fo,except来自于127.0.0.1不记录

    acl member hdr(Host) -i member.baidu.com
    acl agent hdr(Host) -i agent.baidu.com
    acl notice url_reg /article/detail.html?nid=notice&id=[^&]+
    redirect prefix https://member.baidu.com code 301 if member
    如果访问https://member.baidu.com code 则使用http 301 跳转到 member
    也就是 member.baidu.com
    redirect prefix https://agent.baidu.com code 301 if agent
    redirect location http://www.baidu.com/dynamic/newsdetail-NID_$1.html code 301 if notice

    acl WWW hdr_reg(Host) -i www.baidu.com
    如果请求主机满足www.baidu.com返回true -i(忽略大小写)  规则名字叫www
    use_backend HTTP_WEB if WWW
    名字叫HTTP_WEB的backend模块使用这个规则
    acl USER hdr_reg(Host) -i user.baidu.com
    use_backend HTTP_WEB if USER
    default_backend HTTP_WEB  #以上都不满足的时候使用默认HTTP_WEB的backend 

frontend https
    bind *:443 ssl crt /export/servers/haproxy1.5/conf/cert.pem
    mode http
    log global
    option httplog
    option httpclose
    option forwardfor
    default_backend HTTP_WEB

backend HTTP_WEB
    mode http   #http七层
    balance roundrobin    #简单轮训
    option httpchk GET /health.php   #心跳检测的文件 
    cookie SERVERID insert indirect nocache  #允许插入serverid到cookie中
    server web01 172.16.10.76:80 weight 1 cookie 1 check inter 2000 rise 3 fall 10
    server web02 172.16.10.77:80 weight 1 cookie 2 check inter 2000 rise 3 fall 10
    server web03 172.16.10.78:80 weight 1 cookie 3 check inter 2000 rise 3 fall 10
    server web04 172.16.10.79:80 weight 1 cookie 4 check inter 2000 rise 3 fall 10

#服务器定义,cookie 1表示serverid为web1,check inter 2000是检测心跳频率rise 3是3次正确认为服务器可用, fall 10是10次失败认为服务器不可用 weight代表权重数字越大权重越高    

######################################################################
mysql配置端简单注解 global default 使用上面的即可,无frontend和backend
######################################################################

listen  name 10.10.10.10:3307
        mode tcp
        maxconn 10000
        balance leastconn
        server  xxdb1_10.10.10.11:3306 10.10.10.11:3306  check port 3306 inter 5s rise 2 fall 3 weight 1
        server  xxdb2_10.10.10.12:3306 10.10.10.12:3306  check port 3306 inter 5s rise 2 fall 3 weight 2

listen  admin_status
        mode  http
        bind 10.10.10.10:13307
        option httplog
        log global
        stats enable
        stats refresh 30s
        stats hide-version
        stats realm Haproxy Statistics
        stats uri  /status
        stats auth  admin:xxxxxxx
        stats admin if TRUE
原文地址:https://www.cnblogs.com/iteemo/p/5549862.html