接下来开始部署代理/负载均衡服务器集群(图中虽然花了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 /apps
tar -zxvf haproxy-2.3.9.tar.gz
#进入haproxy-2.3.9目录进行编译(编译完成后会生成haproxy可执行文件)
cd /apps/haproxy-2.3.9
make TARGET=generic
#加入环境变量
vim /etc/profile
export 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),等级info
log 127.0.0.1 local5 info
#守护进程运行
daemon
defaults
log global
mode 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@123
option httplog
#redis负载均衡-应用不使用该代理,主要用于监控集群
listen proxy-redis-cluster
bind 0.0.0.0:6379
mode tcp
balance roundrobin
option tcplog
server redis1 10.0.0.5:6371 weight 1 check inter 5000 rise 2 fall 3
server redis2 10.0.0.5:6372 weight 1 check inter 5000 rise 2 fall 3
server redis3 10.0.0.5:6373 weight 1 check inter 5000 rise 2 fall 3
server redis4 10.0.0.5:6374 weight 1 check inter 5000 rise 2 fall 3
server redis5 10.0.0.5:6375 weight 1 check inter 5000 rise 2 fall 3
server redis6 10.0.0.5:6376 weight 1 check inter 5000 rise 2 fall 3
#rabbitmq负载均衡1-应用不使用该代理,主要用于监控集群
listen proxy-rabbit-cluster
bind 0.0.0.0:5672
mode tcp
balance roundrobin
server rabbit1 10.0.0.5:5672 weight 1 check inter 5000 rise 2 fall 3
server rabbit2 10.0.0.5:6672 weight 1 check inter 5000 rise 2 fall 3
server rabbit3 10.0.0.5:7672 weight 1 check inter 5000 rise 2 fall 3
#rabbitmq负载均衡2-应用不使用该代理,主要用于监控集群
listen proxy-rabbit-stomp
bind 0.0.0.0:61613
mode tcp
balance roundrobin
server stomp1 10.0.0.5:61613 weight 1 check inter 5000 rise 2 fall 3
server stomp2 10.0.0.5:61614 weight 1 check inter 5000 rise 2 fall 3
server stomp3 10.0.0.5:61615 weight 1 check inter 5000 rise 2 fall 3
#rabbitmq负载均衡3-用于外网登陆控制台
listen proxy-rabbit-portal
bind 0.0.0.0:15672
mode http
balance roundrobin
server portal1 10.0.0.5:15672 weight 1 check inter 5000 rise 2 fall 3
server portal2 10.0.0.5:25672 weight 1 check inter 5000 rise 2 fall 3
server portal3 10.0.0.5:35672 weight 1 check inter 5000 rise 2 fall 3
option httplog
#zipkin负载均衡-应用统一通过该代理接入zipkin集群
listen proxy-zipkin-cluster
bind 0.0.0.0:9411
mode http
balance leastconn
server zipkin1 10.0.0.11:9411 weight 1 check inter 5000 rise 2 fall 3
server zipkin2 10.0.0.11:9412 weight 1 check inter 5000 rise 2 fall 3
server zipkin3 10.0.0.11:9413 weight 1 check inter 5000 rise 2 fall 3
option httplog
#xxljobadmin负载均衡-一方面用于外网登陆控制台,另一方面应用统一通过该代理接入xxljob集群
listen proxy-xxljobadmin-cluster
bind 0.0.0.0:1111
mode http
balance leastconn
server xxl1 10.0.0.11:1111 weight 1 check inter 5000 rise 2 fall 3
server xxl2 10.0.0.11:1112 weight 1 check inter 5000 rise 2 fall 3
server xxl3 10.0.0.11:1113 weight 1 check inter 5000 rise 2 fall 3
option httplog
#SPARTACUS-AUTU负载均衡-用于网关通过该代理接入认证/授权服务器的验签接口
listen proxy-auth-cluster
bind 0.0.0.0:3001
mode http
balance leastconn
server auth1 10.0.0.10:3001 weight 1 check inter 5000 rise 2 fall 3
server auth2 10.0.0.4:3002 weight 1 check inter 5000 rise 2 fall 3
server auth3 10.0.0.14:3003 weight 1 check inter 5000 rise 2 fall 3
option httplog
#根据域名做负载均衡
#xauth.spartacus.run 认证/授权集群的代理,用于博客端的社交登陆(QQ/微信)
#tiyan.spartacus.run 网关集群的代理,博客端网址、管理端网址的访问前缀
frontend www
bind *:80
mode http
acl is_xauth hdr_beg(host) -i xauth.spartacus.run
acl is_tiyan hdr_beg(host) -i tiyan.spartacus.run
use_backend auth_server if is_xauth
use_backend gateway_server if is_tiyan
backend gateway_server
mode http
balance leastconn
#下面两个配置可保证请求头(x-forwarded-for)中会传递用户的真实IP
option http-server-close
option forwardfor
server gateway1 10.0.0.10:2001 weight 1 check inter 5000 rise 2 fall 3
server gateway2 10.0.0.4:2002 weight 1 check inter 5000 rise 2 fall 3
server gateway3 10.0.0.14:2003 weight 1 check inter 5000 rise 2 fall 3
backend auth_server
mode http
balance leastconn
#下面两个配置可保证请求头(x-forwarded-for)中会传递用户的真实IP
option http-server-close
option forwardfor
server auth1 10.0.0.10:3001 weight 1 check inter 5000 rise 2 fall 3
server auth2 10.0.0.4:3002 weight 1 check inter 5000 rise 2 fall 3
server auth3 10.0.0.14:3003 weight 1 check inter 5000 rise 2 fall 3
#启动haproxy
haproxy -f /apps/haproxy-2.3.9/haproxy.cfg
#查看haproxy是否启动成功
lsof -i:8888
#如何重启:查看haproxy进程pid,杀掉重启
ps -ef | grep haproxy
kill -9 pid
haproxy -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,这里应该知道我在说什么)!
如果搞不定,请加群讨论,扫码关注,发送“加群”