

接下来开始部署代理/负载均衡服务器集群(图中虽然花了3个节点,实际我只部署了2个节点)。
这里使用代理/负载均衡作用主要有以下几点:
1、监控redis集群是否正常
2、负载均衡rabitmq控制台服务集群
3、监控rabbitmq消息队列集群是否正常、stomp消息代理集群是否正常
4、负载均衡zipkin集群
5、负载均衡xxl-job集群
6、负载均衡auth(认证/授权)集群
7、根据不同的域名进行转发
1、LB1+LB2搭建高可用主备集群
具体搭建详情直接看腾讯云提供的文档,他们写的很清楚!
VIP+Keepalived 搭建高可用主备集群,参考文档(步骤1-步骤4,注意一定要申请havip):https://cloud.tencent.com/document/product/215/20186
不过这里还是提供两份keepalived的配置文件仅供参考:
https://github.com/xlvchao/spartacus/blob/main/docs/haproxy.cfg/LB1-keepalived.conf
https://github.com/xlvchao/spartacus/blob/main/docs/haproxy.cfg/LB2-keepalived.conf
2、在LB1、LB2上分别搭建haproxy服务器
HAProxy是啥
HAProxy 是一个免费的负载均衡软件,可以运行于大部分主流的 Linux 操作系统上。HAProxy 提供了L4(TCP)和L7(HTTP)两种负载均衡能力,具备丰富的功能。HAProxy 的社区非常活跃,版本更新快速(最新稳定版1.7.2于2017/01/13推出)。最关键的,HAProxy具备媲美商用负载均衡器的性能和稳定性。因为 HAProxy 的上述优点,它当前不仅仅是免费负载均衡软件的首选,更几乎成为了唯一选择。
HAProxy 的核心功能
负载均衡:L4和L7两种模式,支持RR/静态RR/LC/IP Hash/URI Hash/URL_PARAM Hash/HTTP_HEADER Hash 等丰富的负载均衡算法健康检查:支持TCP和HTTP两种健康检查模式会话保持:对于未实现会话共享的应用集群,可通过 Insert Cookie/Rewrite Cookie/Prefix Cookie,以及上述的多种Hash方式实现会话保持SSL:HAProxy 可以解析 HTTPS 协议,并能够将请求解密为 HTTP 后向后端传输HTTP 请求重写与重定向监控与统计:HAProxy 提供了基于 Web 的统计信息页面,展现健康状态和流量数据。基于此功能,使用者可以开发监控程序来监控 HAProxy 的状态
HAProxy的关键特性
1、性能采用单线程、事件驱动、非阻塞模型,减少上下文切换的消耗,能在1ms内处理数百个请求。并且每个会话只占用数KB的内存。大量精细的性能优化,如O(1)复杂度的事件检查器、延迟更新技术、Single-buffereing、Zero-copy forwarding等等,这些技术使得HAProxy在中等负载下只占用极低的CPU资源。HAProxy大量利用操作系统本身的功能特性,使得其在处理请求时能发挥极高的性能,通常情况下,HAProxy自身只占用15%的处理时间,剩余的85%都是在系统内核层完成的。HAProxy作者在8年前(2009)年使用1.4版本进行了一次测试,单个HAProxy进程的处理能力突破了10万请求/秒,并轻松占满了10Gbps的网络带宽。2、稳定性作为建议以单进程模式运行的程序,HAProxy对稳定性的要求是十分严苛的,按照作者的说法,HAProxy在13年间从未出现过一个会导致其崩溃的BUG,HAProxy一旦成功启动,除非操作系统或硬件故障,否则就不会崩溃(这里多少还是有点夸大的成分,哈哈)。在上文中提到过,HAProxy的大部分工作都是在操作系统内核完成的,所以HAProxy的稳定性主要依赖于操作系统,作者建议使用2.6或3.x的Linux内核,对sysctls参数进行精细的优化,并且确保主机有足够的内存,这样HAProxy就能够持续满负载稳定运行数年之久。
安装haproxy
#创建目录mkdir /apps#下载2.3.9版本源码压缩包:http://www.haproxy.org/download/2.3/src/,拷贝至/apps中后,进行解压cd /appstar -zxvf haproxy-2.3.9.tar.gz#进入haproxy-2.3.9目录进行编译(编译完成后会生成haproxy可执行文件)cd /apps/haproxy-2.3.9make TARGET=generic#加入环境变量vim /etc/profileexport PATH=$PATH:/apps/haproxy-2.3.9#使配置生效source /etc/profile#进入haproxy-2.3.9目录,创建配置文件,编辑配置vim /apps/haproxy-2.3.9/haproxy.cfg#配置内容global#工作目录#chroot /usr/local/etc/haproxy#日志文件,使用rsyslog服务中local5日志设备(/var/log/local5),等级infolog 127.0.0.1 local5 info#守护进程运行daemondefaultslog globalmode http#日志格式option tcplog#日志中不记录负载均衡的心跳检测记录option dontlognull#连接超时(毫秒)timeout connect 5000#客户端超时(毫秒)timeout client 50000#服务器超时(毫秒)timeout server 50000#每个进程可用的最大连接数maxconn 2000#使用keepalive检测死链option tcpka#监控界面listen haproxy-monitor#监控界面的访问的IP和端口bind 0.0.0.0:8888#访问协议mode http#URI相对地址stats uri /haproxy#统计报告格式stats realm Global\ statistics#登陆帐户信息stats auth root:Pwd@123option httplog#redis负载均衡-应用不使用该代理,主要用于监控集群listen proxy-redis-clusterbind 0.0.0.0:6379mode tcpbalance roundrobinoption tcplogserver redis1 10.0.0.5:6371 weight 1 check inter 5000 rise 2 fall 3server redis2 10.0.0.5:6372 weight 1 check inter 5000 rise 2 fall 3server redis3 10.0.0.5:6373 weight 1 check inter 5000 rise 2 fall 3server redis4 10.0.0.5:6374 weight 1 check inter 5000 rise 2 fall 3server redis5 10.0.0.5:6375 weight 1 check inter 5000 rise 2 fall 3server redis6 10.0.0.5:6376 weight 1 check inter 5000 rise 2 fall 3#rabbitmq负载均衡1-应用不使用该代理,主要用于监控集群listen proxy-rabbit-clusterbind 0.0.0.0:5672mode tcpbalance roundrobinserver rabbit1 10.0.0.5:5672 weight 1 check inter 5000 rise 2 fall 3server rabbit2 10.0.0.5:6672 weight 1 check inter 5000 rise 2 fall 3server rabbit3 10.0.0.5:7672 weight 1 check inter 5000 rise 2 fall 3#rabbitmq负载均衡2-应用不使用该代理,主要用于监控集群listen proxy-rabbit-stompbind 0.0.0.0:61613mode tcpbalance roundrobinserver stomp1 10.0.0.5:61613 weight 1 check inter 5000 rise 2 fall 3server stomp2 10.0.0.5:61614 weight 1 check inter 5000 rise 2 fall 3server stomp3 10.0.0.5:61615 weight 1 check inter 5000 rise 2 fall 3#rabbitmq负载均衡3-用于外网登陆控制台listen proxy-rabbit-portalbind 0.0.0.0:15672mode httpbalance roundrobinserver portal1 10.0.0.5:15672 weight 1 check inter 5000 rise 2 fall 3server portal2 10.0.0.5:25672 weight 1 check inter 5000 rise 2 fall 3server portal3 10.0.0.5:35672 weight 1 check inter 5000 rise 2 fall 3option httplog#zipkin负载均衡-应用统一通过该代理接入zipkin集群listen proxy-zipkin-clusterbind 0.0.0.0:9411mode httpbalance leastconnserver zipkin1 10.0.0.11:9411 weight 1 check inter 5000 rise 2 fall 3server zipkin2 10.0.0.11:9412 weight 1 check inter 5000 rise 2 fall 3server zipkin3 10.0.0.11:9413 weight 1 check inter 5000 rise 2 fall 3option httplog#xxljobadmin负载均衡-一方面用于外网登陆控制台,另一方面应用统一通过该代理接入xxljob集群listen proxy-xxljobadmin-clusterbind 0.0.0.0:1111mode httpbalance leastconnserver xxl1 10.0.0.11:1111 weight 1 check inter 5000 rise 2 fall 3server xxl2 10.0.0.11:1112 weight 1 check inter 5000 rise 2 fall 3server xxl3 10.0.0.11:1113 weight 1 check inter 5000 rise 2 fall 3option httplog#SPARTACUS-AUTU负载均衡-用于网关通过该代理接入认证/授权服务器的验签接口listen proxy-auth-clusterbind 0.0.0.0:3001mode httpbalance leastconnserver auth1 10.0.0.10:3001 weight 1 check inter 5000 rise 2 fall 3server auth2 10.0.0.4:3002 weight 1 check inter 5000 rise 2 fall 3server auth3 10.0.0.14:3003 weight 1 check inter 5000 rise 2 fall 3option httplog#根据域名做负载均衡#xauth.spartacus.run 认证/授权集群的代理,用于博客端的社交登陆(QQ/微信)#tiyan.spartacus.run 网关集群的代理,博客端网址、管理端网址的访问前缀frontend wwwbind *:80mode httpacl is_xauth hdr_beg(host) -i xauth.spartacus.runacl is_tiyan hdr_beg(host) -i tiyan.spartacus.runuse_backend auth_server if is_xauthuse_backend gateway_server if is_tiyanbackend gateway_servermode httpbalance leastconn#下面两个配置可保证请求头(x-forwarded-for)中会传递用户的真实IPoption http-server-closeoption forwardforserver gateway1 10.0.0.10:2001 weight 1 check inter 5000 rise 2 fall 3server gateway2 10.0.0.4:2002 weight 1 check inter 5000 rise 2 fall 3server gateway3 10.0.0.14:2003 weight 1 check inter 5000 rise 2 fall 3backend auth_servermode httpbalance leastconn#下面两个配置可保证请求头(x-forwarded-for)中会传递用户的真实IPoption http-server-closeoption forwardforserver auth1 10.0.0.10:3001 weight 1 check inter 5000 rise 2 fall 3server auth2 10.0.0.4:3002 weight 1 check inter 5000 rise 2 fall 3server auth3 10.0.0.14:3003 weight 1 check inter 5000 rise 2 fall 3#启动haproxyhaproxy -f /apps/haproxy-2.3.9/haproxy.cfg#查看haproxy是否启动成功lsof -i:8888#如何重启:查看haproxy进程pid,杀掉重启ps -ef | grep haproxykill -9 pidhaproxy -f /apps/haproxy-2.3.9/haproxy.cfg
也可直接从这里下载haproxy的配置文件:https://github.com/xlvchao/spartacus/blob/main/docs/haproxy.cfg/haproxy.cfg
3、云服务器安全组
注意,LB1、LB2所在云主机要开放以下几个端口,否则对应的服务的控制台无法访问:
以上都设置好后,我们就可以愉快的访问:
1、haproxy控制台(账号密码即使haproxy配置文件中的登陆帐户信息):
http://**公网IP**:8888/haproxy
2、xxl-job控制台(默认账号密码之前的文档中已经说明):
http://**公网IP**:1111/xxl-job-admin/
3、rabbitmq控制台(默认账号密码之前的文档中已经说明):
http://**公网IP**:15672
注意:这里的公网IP(它不是任何一台云主机的公网IP)需要自行申请,然后与 步骤1 中VIP进行绑定即可(如果详细操作了步骤1,这里应该知道我在说什么)!
如果搞不定,请加群讨论,扫码关注,发送“加群”
