Redis + Twemproxy(五)之 HAProxy

Twemproxy 主要功能在于数据分片和代理,当大并发访问 Redis 时,性能瓶颈必定在于 Twemproxy,因为所有请求最先访问 Twemproxy,然后分片到后台不同 Redis 集群。

HAProxy 优点:

  • 最高可以同时维护40000~50000个并发连接,单位时间内处理最大的请求数为20000,最大数据处理能力可达10GBPS;
  • 支持多余8种负载均衡算法,同时也支持 session 保持;
  • 支持虚拟主机功能,可以工作在4、7层(支持多网段);
  • 拥有服务器性能监控工具;
  • 支持 TCP 协议的负载均衡转发,可以对 MySQL 读进行负载均衡,对后端的MySQL节点进行检测和负载均衡。

一、安装 HAProxy

Deb 包下载地址:https://pkgs.org/download/haproxy
默认安装目录:
在这里插入图片描述
安装后,发现服务已自动启动:
在这里插入图片描述

二、备份配置文件

> cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg_bak

三、修改配置文件

> vim /etc/haproxy/haproxy.cfg
# 全局配置段
global
	log /dev/log	local0
	log /dev/log	local1 notice
	chroot /var/lib/haproxy	# 安全方面的设置,用于改变程序执行时所参考的根目录位置。如果 HAProxy 被攻破了,也只能得到一个假的根环境
	stats socket /run/haproxy/admin.sock mode 660 level admin
	stats timeout 30s
	user haproxy	# 运行 HAProxy 进程的用户
	group haproxy	# 运行 HAProxy 进程的组
	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/
	# An alternative list with additional directives can be obtained from
	#  https://mozilla.github.io/server-side-tls/ssl-config-generator/?server=haproxy
	ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS
	ssl-default-bind-options no-sslv3

# 为 frontend、listen、backend 提供默认配置
defaults
	log	global
	mode	http				# mode { tcp|http|health },tcp:基于layer4实现代理,可代理mysql, pgsql, ssh, ssl等协议;http:仅当代理的协议为http时使用;health:工作为健康状态检查的响应模式,当连接请求到达时回应“OK”后即断开连接。
	option	httplog				# 日志类别,采用httplog  
	option	dontlognull			# 不记录健康检查日志信息 
        timeout connect 5000	# 连接超时
        timeout client  50000	# 客户端超时
        timeout server  50000	# 服务器超时
	errorfile 400 /etc/haproxy/errors/400.http	# HAProxy 错误页面
	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  appli1-rewrite
        bind 0.0.0.0:10001
        cookie  SERVERID rewrite	# cookie 中的 SERVERID 重写
        balance roundrobin			# 负载均衡算法
        option  abortonclose		# 当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接;一般长时间处理的服务器(连接数据库等),需要关闭该选项
        option  redispatch			# 当 real server 对应的服务器挂掉后,强制定向到其他健康的服务器  
        retries 3					# 服务器连接失败后的重试次数
        maxconn 2000				# 最大并发连接数
        timeout connect 5000		# 创建连接的超时时长
        timeout client  50000		# 客户端的最长非活动时间。如果在规定时间范围内,客户端没有向 HAProxy 请求资源,则 HAProxy 将会主动切断与客户端的连接
        timeout server  50000		# 后端服务器响应超时时间。如果在规定时间范围内,后端服务器还未给 HAProxy 返回,则会报502(后端服务器超时)

# 定义一个完整的代理
listen  proxy_status 
        bind :16001
        mode tcp	
        balance roundrobin			# 后端服务器组内的服务器调度算法
        server tw_proxy_1 192.168.255.128:22121 check inter 10s	# check inter 10s 是检测心跳频率 
        server tw_proxy_2 192.168.177.128:22121 check inter 10s

# 前端
frontend admin_stats				# 指定 servername
        bind :7777
        mode http
        stats enable				# 启用统计页
        option httplog			
        maxconn 10					# 最大并发连接数
        stats refresh 30s			# 自动刷新时间间隔
        stats uri /admin			# 自定义stats page uri,访问的接口,默认 /haproxy?stats
        stats auth mldn:java		# 认证时的账号和密码,可使用多个,默认不需要认证
        stats hide-version			# 隐藏版本信息
        stats admin if TRUE			# stats admin { if | unless } <cond>:启用stats page中的管理功能

四、重启 HAProxy

> systemctl restart haproxy

五、访问管理控制平台

访问网址:http://192.168.255.128:7777/admin
输入配置文件指定的账号与密码:mldn / java

在这里插入图片描述

六、测试

测试通过 16001 端口访问后端 Redis:
在这里插入图片描述

发布了112 篇原创文章 · 获赞 22 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/u010601662/article/details/104792170