在部署mysql 主主高可用时,使用haproxy进行负载,在服务部使用的情况下发现服务器cpu占比高,负载也高,因此急需解决这个问题。
文章目录
1. haproxy 简介
1.1. 基本概念
HAProxy(High Availability Proxy)是一个开源的高性能负载均衡器和反向代理服务器。它使用C语言编写,具备高并发(一万以上)和高性能的特点,特别适用于需要处理大量并发连接的场景。
1.2. 主要功能
负载均衡:HAProxy可以智能地将流量分发到多个后端服务器,实现负载均衡,避免单点故障,提高系统的可用性和性能。
反向代理:支持TCP和HTTP反向代理,能够代理客户端的请求到后端服务器,并返回服务器的响应给客户端。
高可用性:通过自动故障切换和会话保持等功能,确保服务的连续性和稳定性。
安全保护:HAProxy的运行模式可以保护后端服务器不被直接暴露到网络上,增加安全性。
1.3. 主要特性
支持多种负载均衡算法:如轮询、加权轮询等,满足不同场景下的需求。
强大的ACL支持:用于访问控制,提高系统的安全性。
高性能:能够处理大量的并发连接,单位时间内处理的最大请求数高,最大处理能力可达10Gbit/s。
易集成:可以很简单安全地整合至用户当前的架构中。
2. haproxy 应用场景
2.1. 负载均衡
Web服务器负载均衡:HAProxy可以将用户请求分发到多个Web服务器,避免单点故障,实现高可用性和负载均衡,从而提高应用的吞吐量和响应速度12。
数据库服务器负载均衡:将查询请求分配到多个数据库实例,平衡数据库负载,提高性能和可靠性1。
2.2. 高可用性集群
HAProxy通过健康检查机制,确保流量只发送到健康的服务器实例。当某台后端服务器发生故障时,HAProxy可以自动将流量切换到其他健康的服务器上,实现故障转移和冗余,保证系统的高可用性12。
2.3. 反向代理
作为反向代理服务器,HAProxy可以隐藏真实的服务器信息,将客户端的请求代理到后端服务器,并将响应返回给客户端。这种方式提供了额外的安全性和缓存功能2。
2.4. API网关
在微服务架构中,HAProxy可以作为入口点,分发请求到不同的服务,实现API网关的功能1。
2.5. SSL终端
HAProxy支持SSL/TLS终端,能够处理加密的HTTPS流量,并将解密后的流量转发到后端服务器,简化后端服务器的配置2。
3.haproxy cpu占用特别高 解决前现状
3.1 解决前部署配置文件
cat > haproxy.cfg << EOF
global
maxconn 4000
nbthread 2
daemon
defaults
log global
log 127.0.0.1 local3
mode http
option tcplog
option dontlognull
retries 10
option redispatch
maxconn 2000
timeout connect 30s
timeout client 10m
timeout server 10m
#timeout http-keep-alive 10s
timeout check 10s
######## 监控界面配置 #################
listen admin_stats
#监控界面的访问的IP和端口
bind 0.0.0.0:8888
#访问协议
mode http
#URI相对地址
stats uri /dbs
#统计报告格式
stats realm Global\ statistics
#登陆帐户信息
stats auth admin:admin
# 隐藏HAProxy的版本号
stats hide-version
# 管理界面,如果认证成功了,可通过webui管理节点
stats admin if TRUE
# 统计页面自动刷新时间
stats refresh 30s
listen mysql
bind 0.0.0.0:3306
mode tcp
#负载均衡算法(轮询算法)
#轮询算法:roundrobin
#权重算法:static-rr
#最少连接算法:leastconn
#请求源IP算法:source
balance roundrobin
# 监控检查需要一个无密码的账号
# mysql健康检查 haproxy为mysql登录用户名(需要在实体数据有这个账户,且无密码)
# option mysql-check user haproxy
server s1 mysql-master1:3306 check weight 1 maxconn 2000 inter 5000 rise 2 fall 2
server s2 mysql-master2:3306 check weight 1 maxconn 2000 inter 5000 rise 2 fall 2 backup
EOF
cat > dokcer-stark-haproxy.yml << EOF
version: '3'
networks:
liuhm-net:
external: true
services:
haproxy:
image: haproxy:alpine
hostname: haproxy
volumes:
- "${PWD}/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg"
- "/etc/localtime:/etc/localtime:ro"
restart: always
privileged: true
ports:
- 8888:8888
- 3306:3306
networks:
- liuhm-net
deploy:
mode: global
EOF
cpu 飙升
3.2.解决后
- 修改了镜像版haproxy:2.9.1-alpine
- 增加了配置 tune.disable-zero-copy-forwarding
- github 解决问题地址
cat > haproxy.cfg << EOF
global
maxconn 4000
nbthread 2
daemon
## tune.disable-zero-copy-forwarding必须配置,不然cpu占比很高
tune.disable-zero-copy-forwarding
defaults
log global
log 127.0.0.1 local3
mode http
option tcplog
option dontlognull
retries 10
option redispatch
maxconn 2000
timeout connect 30s
timeout client 10m
timeout server 10m
#timeout http-keep-alive 10s
timeout check 10s
######## 监控界面配置 #################
listen admin_stats
#监控界面的访问的IP和端口
bind 0.0.0.0:8888
#访问协议
mode http
#URI相对地址
stats uri /dbs
#统计报告格式
stats realm Global\ statistics
#登陆帐户信息
stats auth admin:admin
# 隐藏HAProxy的版本号
stats hide-version
# 管理界面,如果认证成功了,可通过webui管理节点
stats admin if TRUE
# 统计页面自动刷新时间
stats refresh 30s
listen mysql
bind 0.0.0.0:3306
mode tcp
#负载均衡算法(轮询算法)
#轮询算法:roundrobin
#权重算法:static-rr
#最少连接算法:leastconn
#请求源IP算法:source
balance roundrobin
# 监控检查需要一个无密码的账号
# mysql健康检查 haproxy为mysql登录用户名(需要在实体数据有这个账户,且无密码)
# option mysql-check user haproxy
server s1 mysql-master1:3306 check weight 1 maxconn 2000 inter 5000 rise 2 fall 2
server s2 mysql-master2:3306 check weight 1 maxconn 2000 inter 5000 rise 2 fall 2 backup
EOF
cat > dokcer-stark-haproxy.yml << EOF
version: '3'
networks:
liuhm-net:
external: true
services:
haproxy:
image: haproxy:2.9.1-alpine
hostname: haproxy
volumes:
- "${PWD}/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg"
- "/etc/localtime:/etc/localtime:ro"
restart: always
privileged: true
ports:
- 8888:8888
- 3306:3306
networks:
- liuhm-net
deploy:
mode: global
EOF
实际配置后没有飙升