负载均衡代理Haproxy安装与配置

最近项目要求对RabbitMQ做负载均衡,并且RabbitMQ是虚拟主机的,综合对比Nginx和Haproxy,选用Haproxy。
以下是Linux系统安装Haproxy步骤。

1. 下载Haproxy安装包,这里是haproxy-1.5.19.tar.gz。

较新haproxy1.8版本中,比如动态禁用后端服务器,日志管理等功能支持的并没有haproxy1.5系列要友好。

2. 上传安装包到指定目录

目录: /安装包路径/haproxy-1.5.19.tar.gz,示意图如下:

上传结果
3.解压安装包

解压到指定目录,我这里解压到当前目录
4. 安装编译依赖

[test@rabbitServerRAM1]$ su root
密码:
[root@rabbitServerRAM1 haproxy-1.5.19]# yum install gcc openssl-devel pcre-devel zlib-devel -y
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
base                                                                                                                                            | 4.1 kB  00:00:00     
软件包 gcc-4.8.5-4.el7.x86_64 已安装并且是最新版本
软件包 1:openssl-devel-1.0.1e-42.el7_1.9.x86_64 已安装并且是最新版本
软件包 pcre-devel-8.32-15.el7.x86_64 已安装并且是最新版本
软件包 zlib-devel-1.2.7-15.el7.x86_64 已安装并且是最新版本
无须任何处理

安装依赖要求root权限,已经安装好的可以直接编译安装。

5. 编译安装Haproxy

编译

[test@rabbitServerRAM1 soft_jar]$ cd haproxy-1.5.19
[test@rabbitServerRAM1 haproxy-1.5.19]$ make TARGET=linux2628 ARCH=x86_x64 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 PREFIX=/home/test/haproxy

如果是非root用户安装,需要切换到需要的用户在编译,我这里使用用户test,编译结果如下:
在这里插入图片描述
再次执行编译检查结果

[test@rabbitServerRAM1 haproxy-1.5.19]$ make TARGET=linux2628 ARCH=x86_x64 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 PREFIX=/home/test/haproxy
make: 对“all”无需做任何事。

提示表示已经编译成功无须再次编译,若想重新编译,只需执行make clean将编译好的删除,再次make重新编译即可。
注意:
执行make的时候参数介绍:TARGET,ARCH,PREFIX
   TARGET:内核版本控制
    - linux22 for Linux 2.2
    - linux24 for Linux 2.4 and above (default)
    - linux24e for Linux 2.4 with support for a working epoll (> 0.21)
    - linux26 for Linux 2.6 and above
    - linux2628 for Linux 2.6.28, 3.x, and above (enables splice and tproxy)

ARCH:cpu架构

i386,i486,i586,i686,x86_64
    
    PREFIX:编译路径

安装

[test@rabbitServerRAM1 haproxy-1.5.19]$ make install PREFIX=/home/test/haproxy

安装结果如下:
在这里插入图片描述

[test@rabbitServerRAM1 haproxy-1.5.19]# cd /home/test
[test@rabbitServerRAM1 test]# ls
 haproxy   soft_jar

6. 配置

   默认源码安装haproxy不提供配置文件,但在其解压目录examples下会提供相关haproxy的实例配置,但有的版本的源码haproxy实例配置不是很全面,最笨的

办法是使用yum安装rpm包格式的haproxy,然后复制其配置文件件作为参考,然后再将其卸载。此处提供一个简单框架的haproxy配置模板实例,详细的配置请参考官网

http://cbonte.github.o/haproxy-dconv/1.6/configuration.html

开始配置
  
复制解压目录下的examples目录到安装目录下,以后可能会用到。在安装目录下新建配置文件夹conf,复制examples下的haproxy.cfg到conf文件夹下。

[test@rabbitServerRAM1 test]$ cp -ra /home/test/soft_jar/haproxy-1.5.19/examples/ /home/test/haproxy
[test@rabbitServerRAM1 test]$ mkdir /home/test/haproxy/conf
[test@rabbitServerRAM1 test]$ cp /home/test/soft_jar/haproxy-1.5.19/examples/haproxy.cfg /home/test/haproxy/conf
[test@rabbitServerRAM1 test]$ vi /home/test/haproxy/conf/haproxy.cfg

haproxy配置文件及简单参数说明:

########################[全局配置]##########################
global
# 日志输出配置,所有日志都记录在本机,通过local3输出
log 127.0.0.1 local3
# 设定每个haproxy进程所接受的最大并发连接数,需要参考ulimit -n
maxconn 4096
# ulimit 文件描述符数量
#ulimit-n 819200
#chroot /usr/share/haproxy
# 运行haproxy用户
user haproxy
# 运行haproxy用户组
group haproxy
# 以后台形式运行haproxy
daemon
# 设置进程数量
nbproc 1
# 设置haproxy进程的pid文件路径
pidfile /usr/local/haproxy/var/haproxy.pid
# haproxy 调试级别,建议只在开启单进程的时候调试
#debug
#quiet
####################[默认配置]################################
defaults
# 日志使用全局配置
log global
# haproxy工作模式{tcp|http|health},tcp4层,http7层,health只返回OK
mode http
# 日志类别,采用httplog
option httplog
# 不记录请求报文包体数据为空的请求到日志,比如健康检查,减少写磁盘IO
option dontlognull
# 三次连接失败就认为服务器不可达,也可以通过后面设置
retries 3
# 允许在发往服务器的请求首部中插入“X-Forwarded-For”首部,用于后端主机记录真实请求的客户端ip
option forwardfor except 127.0.0.0/8
# 每次请求完毕后主动关闭http通道,haproxy不支持keep-alive,只能通过模拟实现
option httpclose
# 当server id对应的服务器挂掉后,强制定向到其他健康的后端服务器,以后将不支持
option redispatch
# 当服务器负载很高的时候,自动结束掉当前队列处理比较久的连接
option abortonclose
# 默认的最大连接数
maxconn 4096
# 连接超时
timeout connect 5000ms
# 客户端请求超时
timeout client 30000ms
# 服务端响应超时
timeout server 30000ms
# 心跳检测超时
timeout check 2000
# 持久连接超时时间
timeout http-keep-alive 10s
# http请求超时时间
timeout http-request 10s
# 默认队列超时时间
timeout queue 1m
# 设置默认负载均衡算法{roundrobin|source|leastconn…}
balance roundrobin
###########################[统计页面]############################
listen admin_stats
# 设置统计页面监听的套接字,listen虚拟节点为frontend和backend的专有通道
bind 0.0.0.0:10800
# 设置统计页面工作模式
mode http
# 启用状态页面功能
stats enable
# 采用http日志格式
option httplog
# 是否开启错误日志并记录
#log 127.0.0.1 local err
# 设置访问统计页面的最大连接数
maxconn 10
# 设置统计页面的自动刷新时间
stats refresh 30s
# 访问统计页面的url
stats uri /stats
# 登录统计页面时的提示信息
stats realm DongPing\ Haproxy
# 设置统计页面的用户认证,可以设置多个用户名
stats auth admin:admin
# 隐藏统计页面上Haproxy的版本信息
stats hide-version
# 设置haproxy管理页面可以执行一些特权动作,比如下线后端主机
stats admin if TRUE
##########################[设置haproxy 错误页面]##########################
errorfile 403 /usr/local/haproxy/errorfiles/403.http
errorfile 500 /usr/local/haproxy/errorfiles/500.http
errorfile 502 /usr/local/haproxy/errorfiles/502.http
errorfile 503 /usr/local/haproxy/errorfiles/503.http
errorfile 504 /usr/local/haproxy/errorfiles/504.http
#########################[Frontend 前端配置]##############################
frontend http-proxy
# 定义使用的套接字
bind *:80
# 抓取请求报文首部字段为Host,Referer的值到haproxy日志中
capture request header Host len 20
capture request header Referer len 60
capture request header User-agent len 60
capture request header X-Forward-For len 60
# 定义一个acl规则,匹配以/{static|images|javascript|stylesheets}开头的静态资源
acl url_static path_beg -i /static /images /javascript /stylesheets
# 定义一个acl规则,匹配以.{jpg|jpeg|gif|png|css|js}结尾的静态资源
acl url_static path_end -i .jpg .jpeg .gif .png .css .js
# 如果被url_static这条acl规则匹配由后端集群static_servers进行分发
use_backend static_servers if url_static
# 定义请求不匹配所有规则时默认转发到的后端主机
default_backend dynamic_servers
##################[backend 后端配置]##################
#定义一组动态资源主机
backend dynamic_servers
# 定义haproxy工作模式
mode http
# 定义负载均衡算法
balance roundrobin
# 健康检查
option httpchk /check.jsp HTTP/1.0
# 使用基于cookie粘性的负载均衡算法
cookie SERVERID insert indirect nocache
# 定义连接后端主机的一些策略
# server name ip:port /表示后端一台主机,name必须给出
# cookie name /设置插入的cookie信息
# check inter 2000 /使用check关键字表示对后端主机检测,inter 2000表示检测时间间隔为2s
# weight /表示权重
# rise /表示检测两次正确,则认为服务器可用(服务从故障到正常)
# fall /表示检测三次失败,则认为服务器不可用(服务器从正常到故障)
# backup /表示备用主机
server webapp_01 192.168.3.84:8080 cookie srv1 weight 2 check inter 2000 rise 2 fall 3
server webapp_02 192.168.3.84:8081 cookie srv2 weight 2 check inter 2000 rise 2 fall 3
# 定义一个sorryserver,当所有后端主机不可访问时,请求调度到此主机上
server webapp_3 192.168.3.83:8080 backup check inter 2000 rise 2 fall 3
# 定义一组静态资源主机
backend static_servers
mode http
option httpchk /check.html
balance roundrobin
server static_01 192.168.3.84:80 check inter 2000 fall 3
server static_02 192.168.3.84:81 check inter 2000 fall 3
#######################[tcp配置]#########################################
listen mysql
bind 0.0.0.0:3306
balance leastconn
mode tcp
log global
option tcplog
maxconn 4096
#log 127.0.0.1 local0 debug
server dbsrv_01 192.168.3.84:3306 weight 1 check port 3306 inter 2000 rise 1 fall 2 maxconn 300
server dbsrv_02 192.168.3.84:3307 weight 1 check port 3307 inter 2000 rise 1 fall 2 maxconn 300

根据实际环境需求,编辑配置文件haproxy.cfg,并保存。

7. 启动

语法检查无误后启动Haproxy:

[test@rabbitServerRAM1 conf]$ /home/test/haproxy/sbin/haproxy -f /home/test/haproxy/conf/haproxy.cfg -c
Configuration file is valid
[test@rabbitServerRAM1 conf]$ /home/test/haproxy/sbin/haproxy -f /home/test/haproxy/conf/haproxy.cfg -st `pidof haproxy`

注意:启动时可能遇到连接数问题,先查看本机句柄数ulimit -n,根据提示修改到符合条件即可(修改句柄数)。例如提示:

WARNING] 212/112729 (3600) : [/home/test/haproxy/sbin/haproxy.main()] Cannot raise FD limit to 8227, limit is 4096.
[WARNING] 212/112729 (3600) : [/home/test/haproxy/sbin/haproxy.main()] FD limit (4096) too low for maxconn=4096/maxsock=8227. Please raise 'ulimit-n' to 8227 or more to avoid any trouble.

修改后启动不报错,查看进程和文件夹内容是否启动成功。进程存在且生成stats和haproxy.pid代表启动成功。

[test@rabbitServerRAM1 conf]$ ps -ef|grep haproxy|grep -v grep
test      3625      1  0 11:29 ?        00:00:00 /home/test/haproxy/sbin/haproxy -f /home/test/haproxy/conf/haproxy.cfg -st 3601
[test@rabbitServerRAM1 conf]$ cd /home/ntsms/haproxy/
[test@rabbitServerRAM1 haproxy]$ ls
conf  doc  examples  sbin  share  stats
[test@rabbitServerRAM1 haproxy]$ cd conf
[test@rabbitServerRAM1 conf]$ ll
总用量 8
-rw-rw-r--. 1 test test 2051 7月  31 11:17 haproxy.cfg
-rw-r--r--. 1 test test    5 7月  31 11:29 haproxy.pid

8. 监控统计管理页面

haproxy的统计页可以实现强大的管理功能,最常用的就是软下线后端主机

访问 http://192.168.43.108:9188/stats,访问路径配置在haproxy.cfg文件中[统计页面]listen admin_stats下可配置。访问页面前需修改或关闭防火墙。在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值