设为首页 收藏本站
查看: 915|回复: 0

[经验分享] Rabbitmq集群部署Haproxy

[复制链接]

尚未签到

发表于 2019-1-1 10:05:41 | 显示全部楼层 |阅读模式
安装前的说明:
  1、为了配合做rabbitmq的负载均衡,本次的安装包是安装的低版本1.5.8版本,下载包的地址:https://src.fedoraproject.org/repo/pkgs/haproxy/
2、按照上两篇博客,先把rabbitmq集群装好:
单机版Rabbitmq部署:http://blog.运维网.com/10950710/2135676
Rabbitmq集群部署:http://blog.运维网.com/10950710/2135717

安装步骤:
  1、将haproxy-1.5.8.tar.gz上传到服务器/root下面并解压

[root@localhost haproxy-1.5.8]# pwd
/root/haproxy-1.5.8
[root@localhost haproxy-1.5.8]# ll
总用量 368
-rw-rw-r--.  1 root root 278375 10月 31 2014 CHANGELOG
drwxrwxr-x. 10 root root    127 10月 31 2014 contrib
drwxrwxr-x.  4 root root   4096 10月 31 2014 doc
drwxrwxr-x.  2 root root   4096 10月 31 2014 ebtree
drwxrwxr-x.  3 root root   4096 10月 31 2014 examples
drwxrwxr-x.  6 root root     60 10月 31 2014 include
-rw-rw-r--.  1 root root   2029 10月 31 2014 LICENSE
-rw-rw-r--.  1 root root  27752 10月 31 2014 Makefile
-rw-rw-r--.  1 root root  26322 10月 31 2014 README
-rw-rw-r--.  1 root root   4040 10月 31 2014 ROADMAP
drwxrwxr-x.  2 root root   4096 10月 31 2014 src
-rw-rw-r--.  1 root root     14 10月 31 2014 SUBVERS
drwxrwxr-x.  2 root root   4096 10月 31 2014 tests
-rw-rw-r--.  1 root root     24 10月 31 2014 VERDATE
-rw-rw-r--.  1 root root      6 10月 31 2014 VERSION
  2、编译安装:
  1)先查看本机系统版本

[root@localhost haproxy-1.5.8]# uname -r
3.10.0-693.el7.x86_64
[root@localhost haproxy-1.5.8]#
  参数说明:
· TARGET=linux310,内核版本,使用uname -r查看内核,如:3.10.0-514.el7,此时该参数就为linux310;kernel 大于2.6.28的可以用:TARGET=linux2628;
· ARCH=x86_64,系统位数;
· PREFIX=/usr/local/haprpxy #/usr/local/haprpxy,为haprpxy安装路径。
  2)编译安装

[root@localhost haproxy-1.5.8]# make TARGET=linux310 ARCH=x86_64 PREFIX=/usr/local/haproxy   #执行之前,确认已经安装了gcc,否则这一步骤会执行失败
[root@localhost haproxy-1.5.8]# make install PREFIX=/usr/local/haproxy
  3、添加配置文件
该版本解压后文件内就没有haproxy.cfg文件,所以需要自己写。这个配置文件是按照搭建rabbitmq的负载均衡来配置的:

[root@localhost haproxy]# mkdir /etc/haproxy   #创建配置文件目录
[root@localhost haproxy]# cd /etc/haproxy
[root@localhost haproxy]# cat haproxy.cfg   (配置文件解析可以参考链接:http://blog.运维网.com/blief/1750952,写得很详细)
###########全局配置#########
global
log 127.0.0.1   local3 info
#log 127.0.0.1   local0
log /dev/log    local0
log /dev/log    local1 notice
chroot /var/lib/haproxy     # 改变当前工作目录
stats socket /run/haproxy/admin.sock mode 660 level admin   # 创建监控所用的套接字目录
pidfile  /var/run/haproxy.pid   # haproxy的pid存放路径,启动进程的用户必须有权限访问此文件
maxconn  100000                   # 最大连接数,默认4000
user   haproxy                  # 默认用户
group   haproxy                 # 默认用户组
daemon                          # 创建1个进程进入deamon模式运行。此参数要求将运行模式设置为"daemon
# Default SSL material locations
#ca-base /etc/ssl/certs
#crt-base /etc/ssl/private
# Default ciphers to use on SSL-enabled listening sockets.
# For more information, see ciphers(1SSL). This list is from:
#  https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
#ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS
#ssl-default-bind-options no-sslv3
###########默认配置#########
defaults
log global
mode    http                                # 默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
option  httplog                             # 采用http日志格式
option  dontlognull                         # 启用该项,日志中将不会记录空连接。所谓空连接就是在上游的负载均衡器
# 或者监控系统为了探测该 服务是否存活可用时,需要定期的连接或者获取某
# 一固定的组件或页面,或者探测扫描端口是否在监听或开放等动作被称为空连接;
# 官方文档中标注,如果该服务上游没有其他的负载均衡器的话,建议不要使用
# 该参数,因为互联网上的恶意扫描或其他动作就不会被记录下来
timeout connect 5000                    # 连接超时时间
timeout client  50000                   # 客户端连接超时时间
timeout server  50000                   # 服务器端连接超时时间
#option  httpclose       # 每次请求完毕后主动关闭http通道
option  httplog         # 日志类别http日志格式
#option  forwardfor      # 如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip  
option  redispatch      # serverId对应的服务器挂掉后,强制定向到其他健康的服务器
timeout connect 10000   # default 10 second timeout if a backend is not found
maxconn     100000       # 最大连接数
retries     3           # 3次连接失败就认为服务不可用,也可以通过后面设置
#errorfile 400 /etc/haproxy/errors/400.http
#errorfile 403 /etc/haproxy/errors/403.http
#errorfile 408 /etc/haproxy/errors/408.http
#errorfile 500 /etc/haproxy/errors/500.http
#errorfile 502 /etc/haproxy/errors/502.http
#errorfile 503 /etc/haproxy/errors/503.http
#errorfile 504 /etc/haproxy/errors/504.http
####################################################################
listen http_front
bind 0.0.0.0:1080           #监听端口  
stats refresh 30s           #统计页面自动刷新时间  
stats uri /haproxy/stats            #统计页面url  
stats realm Haproxy Manager #统计页面密码框上提示文本  
stats auth admin:admin      #统计页面用户名和密码设置*****  
#stats hide-version         #隐藏统计页面上HAProxy的版本信息
#####################映射rabbitmq的15674端口###############################
listen rabbitmq_admin 0.0.0.0:15672
bind 0.0.0.0:15672
server rabbitmq101 10.0.0.101:15672
server rabbitmq102 10.0.0.102:15672
server rabbitmq103 10.0.0.103:15672
#####################映射rabbitmq的15674端口###############################
listen rabbitmq_admin 0.0.0.0:15674
bind 0.0.0.0:15674
server rabbitmq101 10.0.0.101:15674
server rabbitmq102 10.0.0.102:15674
server rabbitmq103 10.0.0.103:15674
####################映射rabbitmq的5672端口###############################
listen rabbitmq_cluster 0.0.0.0:5672  #这里配置的是服务端口,代码里面连接服务就直接用haproxy的IP:5672连接服务了,如10.0.0.101:5672
bind 0.0.0.0:5672
option tcplog
mode tcp
timeout client  3h
timeout server  3h
option          clitcpka
balance leastconn      #负载均衡算法(#banlance roundrobin 轮询,balance source 保存session值,支持static-rr,leastconn,first,uri等参数)
#balance url_param userid
#balance url_param session_id check_post 64
#balance hdr(User-Agent)
#balance hdr(host)
#balance hdr(Host) use_domain_only
#balance rdp-cookie
#balance leastconn
#balance source //ip
server   rabbitmq101 10.0.0.101:5672 check inter 5s rise 2 fall 3   #check inter 2000 是检测心跳频率,rise 2是2次正确认为服务器可用,fall 3是3次失败认为服务器不可用
server   rabbitmq102 10.0.0.102:5672 check inter 5s rise 2 fall 3
server   rabbitmq103 10.0.0.103:5672 check inter 5s rise 2 fall 3
[root@localhost haproxy]#
  4、编辑haproxy的启动文件:

[root@localhost haproxy]# cat /etc/init.d/haproxy
#!/bin/sh
#
# haproxy
#
# chkconfig:   - 85 15
# description:  HAProxy is a free, very fast and reliable solution \
#               offering high availability, load balancing, and \
#               proxying for TCP and  HTTP-based applications
# processname: haproxy
# config:      /etc/haproxy/haproxy.cfg
# pidfile:     /var/run/haproxy.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
exec="/usr/local/haproxy/sbin/haproxy"
prog=$(basename $exec)
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
cfgfile=/etc/haproxy/haproxy.cfg
pidfile=/var/run/haproxy.pid
lockfile=/var/lock/subsys/haproxy
check() {
$exec -c -V -f $cfgfile $OPTIONS
}
start() {
$exec -c -q -f $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
echo -n $"Starting $prog: "
# start it up here, usually something like "daemon $exec"
daemon $exec -D -f $cfgfile -p $pidfile $OPTIONS
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
# stop it here, often "killproc $prog"
killproc $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
$exec -c -q -f $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
stop
start
}
reload() {
$exec -c -q -f $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
echo -n $"Reloading $prog: "
$exec -D -f $cfgfile -p $pidfile $OPTIONS -sf $(cat $pidfile)
retval=$?
echo
return $retval
}
force_reload() {
restart
}
fdr_status() {
status $prog
}
case "$1" in
start|stop|restart|reload)
$1
;;
force-reload)
force_reload
;;
check)
check
;;
status)
fdr_status
;;
condrestart|try-restart)
[ ! -f $lockfile ] || restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"
exit 2
esac
[root@localhost haproxy]#
  修改启动文件的权限:

[root@localhost haproxy]# chmod 777  /etc/init.d/haproxy
  5、根据haproxy.cfg的配置,创建相应的配置文件并修改权限

[root@localhost  haproxy]# useradd haproxy
[root@localhost  haproxy]# mkdir  /var/lib/haproxy
[root@localhost  haproxy]# mkdir /run/haproxy/
[root@localhost  haproxy]# chown haproxy:haproxy -R /usr/local/haproxy/
[root@localhost  haproxy]# chown haproxy:haproxy -R /var/lib/haproxy
[root@localhost  haproxy]# chown haproxy:haproxy -R /run/haproxy/
[root@localhost  haproxy]# chown haproxy:haproxy -R /etc/haproxy/
[root@localhost haproxy]#chown haproxy:haproxy -R /etc/init.d/haproxy
  6、启动haproxy服务:

[root@localhost haproxy]# service haproxy start
Starting haproxy (via systemctl):                          [  确定  ]
  查看进程:

[root@localhost haproxy]# netstat -tlunp|grep 1080
tcp        0      0 0.0.0.0:1080            0.0.0.0:*               LISTEN      8900/haproxy        
[root@localhost haproxy]#
[root@localhost haproxy]# ps -ef|grep haproxy
haproxy    8900      1  0 17:43 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -D -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
root       8986   5662  0 18:17 pts/1    00:00:00 grep --color=auto haproxy
[root@localhost haproxy]#
  7、haproxy管理页面访问:
http://131.10.11.116:1080/haproxy/stats   #访问的时候要注意防火墙,开始防火墙没关闭,访问不到。 后面的/haproxy/stats也是haproxy.cfg里面配置的,可以改
根据/etc/haproxy/haproxy.cfg的配置,页面的用户名和密码是admin /admin

  登录进去看到是这样的(测试的haproxy忘了截图,找了个另外环境的haproxy截图):


=======================Haproxy开启日志记录==========================
  1、创建日志文件/var/log/haproxy/haproxy.log

    cd /var/log  
sudo mkdir haproxy  
cd haproxy  
sudo touch haproxy.log  
sudo chmod a+w haproxy.log 
  2、开启rsyslog的haproxy日志记录功能
2.1 编辑/etc/rsyslog.conf文件,将下面两行去掉注释:

    $ModLoad imudp  
$UDPServerRun 514  
  并且在

    # Save boot messages also to boot.log  
local7.*                                                /var/log/boot.log 
  这两行后面添加三行新的内容:

    # Save haproxy log 
local3.*                       /var/log/haproxy/haproxy.log 
local0.*                       /var/log/haproxy/haproxy.log 
  2.2 修改/etc/sysconfig/rsyslog 文件

将SYSLOGD_OPTIONS=""  改成 SYSLOGD_OPTIONS="-r -m 0 -c 2" 
  3、配置haproxy
修改/etc/haproxy/haproxy.cfg文件,在global区段添加

log         127.0.0.1 local0  info
  4、重启rsyslog和haproxy服务,haproxy就能记录日志了。

    systemctl restart rsyslog
service haproxy restart
  参考文档:
https://blog.csdn.net/u012758088/article/details/78642063
  https://blog.csdn.net/secsky/article/details/60576455
  https://www.cnblogs.com/saneri/p/5234101.html
  https://www.cnblogs.com/yangxiaoyi/p/6919163.html




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-658166-1-1.html 上篇帖子: Haproxy的使用和配置文件详解 下篇帖子: [HAProxy] 负载均衡haproxy安装配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表