haproxy安装配置
# tar -zxvf haproxy-1.7.1.tar.gz
# cd haproxy-1.7.1
# make TARGET=linux26 PREFIX=/usr/local/haproxy #将haproxy安装到/usr/local/haproxy
# make install PREFIX=/usr/local/haproxy
# cd /usr/local/haproxy
# vi haproxy.cfg
haproxy/sbin/haproxy -v 查看版本
vi /etc/rsyslog.conf
去掉下面两行前面的#号
$ModLoad imudp
$UDPServerRun 514
添加 localhaproxy.* /usr/local/haproxy/log/haproxy.log
重启rsyslog: systemctl restart rsyslog (systemctl 需安装工具包)
启动服务:
# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg
重启服务:
# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg -st `cat /usr/local/haproxy/logs/haproxy.pid` (没有换行)
停止服务:
# killall haproxy
在浏览器中输入haproxy监控地址:http://10.112.178.108:1080/haproxy-admin,如图所示
haproxy.cfg配置(/haproxy/haproxy-1.7.1/examples下有各种配置样例)
global
log 127.0.0.1 local3
#log 127.0.0.1 localhaproxy0 notice
maxconn 4096 #最大连接数
defaults
log global
mode http
option httplog #日志类别,采用httplog
option dontlognull #不记录健康检查日志信息
#retries 2 #2次连接失败不可用
option forwardfor #后端服务获得真实ip
option httpclose #请求完毕后主动关闭http通道
option abortonclose #服务器负载很高,自动结束比较久的链接
timeout connect 5s
timeout client 5s
timeout server 5s
#timeout check 10s #心跳检测超时
balance roundrobin #负载均衡方式,轮询
########统计页面配置########
listen stats
bind 0.0.0.0:1080
mode http
option httplog
log 127.0.0.1 local3 err
#stats refresh 30s
#maxconn 10 #最大连接数
stats uri /admin #状态页面 http//ip:1080/admin访问
stats realm Haproxy\ Statistics
stats auth admin:admin #用户和密码:admin
stats hide-version #隐藏版本信息
stats admin if TRUE #设置手工启动/禁用
frontend myfrontend
# primary cert is /etc/cert/server.pem
# /etc/cert/certdir/ contains additional certificates for SNI clients
#bind :443 ssl crt /etc/cert/server.pem crt /etc/cert/certdir/
bind :1081
default_backend mybackend
backend mybackend
# a http backend
#server s3 10.0.0.3:80
server web1 10.112.178.108:11103 #cookie 1 weight 5 check inter 2000 rise 2 fall 3
server web2 10.112.178.108:50001
# a https backend
#server s4 10.0.0.3:443 ssl verify none
http://10.112.178.108:1080/admin
http://10.112.178.108:1081/cashier-task/ 访问此链接,可发现两个端口的服务切换显示
**************************************** mq 负载均衡
defaults
mode http
timeout connect 5s
timeout client 5s
timeout server 5s
listen stats
bind 0.0.0.0:1080
mode http
option httplog
log 127.0.0.1 local3 err
#stats refresh 30s
#maxconn 10
stats uri /admin
stats realm Haproxy\ Statistics
stats auth admin:admin
stats hide-version
stats admin if TRUE
frontend myfrontend
# primary cert is /etc/cert/server.pem
# /etc/cert/certdir/ contains additional certificates for SNI clients
#bind :443 ssl crt /etc/cert/server.pem crt /etc/cert/certdir/
bind :5670
mode tcp #tcp模式
option clitcpka
default_backend mybackend
backend mybackend
# a http backend
mode tcp
option srvtcpka
option redispatch
balance roundrobin
option tcp-check
server m3 192.168.89.131:5672
server m4 192.168.89.131:5673
# a https backend
#server s4 10.0.0.3:443 ssl verify none

测试结果:5670端口连接mq,连接4次会平均分配到5672和5673两台服务上

浙公网安备 33010602011771号