一、为了配合作rabbitmq的负载均衡,本次的安装包是安装的低版本1.5.8版本,下载包的地址:https://src.fedoraproject.org/repo/pkgs/haproxy/
二、按照上两篇博客,先把rabbitmq集群装好:
单机版Rabbitmq部署:http://www.javashuo.com/article/p-tfcqshgq-bv.html
Rabbitmq集群部署:http://www.javashuo.com/article/p-xchveufg-gk.htmlhtml
一、将haproxy-1.5.8.tar.gz上传到服务器/root下面并解压linux
[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
二、编译安装:web
1)先查看本机系统版本redis
[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
三、添加配置文件
该版本解压后文件内就没有haproxy.cfg文件,因此须要本身写。这个配置文件是按照搭建rabbitmq的负载均衡来配置的:服务器
[root@localhost haproxy]# mkdir /etc/haproxy #建立配置文件目录 [root@localhost haproxy]# cd /etc/haproxy [root@localhost haproxy]# cat haproxy.cfg (配置文件解析能够参考连接:https://blog.51cto.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]#
四、编辑haproxy的启动文件:cookie
[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]#
修改启动文件的权限:session
[root@localhost haproxy]# chmod 777 /etc/init.d/haproxy
五、根据haproxy.cfg的配置,建立相应的配置文件并修改权限app
[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
六、启动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]#
七、haproxy管理页面访问:
http://131.10.11.116:1080/haproxy/stats #访问的时候要注意防火墙,开始防火墙没关闭,访问不到。 后面的/haproxy/stats也是haproxy.cfg里面配置的,能够改
根据/etc/haproxy/haproxy.cfg的配置,页面的用户名和密码是admin /admin
登陆进去看到是这样的(测试的haproxy忘了截图,找了个另外环境的haproxy截图):
一、建立日志文件/var/log/haproxy/haproxy.log
cd /var/log sudo mkdir haproxy cd haproxy sudo touch haproxy.log sudo chmod a+w haproxy.log
二、开启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"
三、配置haproxy
修改/etc/haproxy/haproxy.cfg文件,在global区段添加
log 127.0.0.1 local0 info
四、重启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