HAProxy
HAProxy
是一款高性能的负载均衡器,开源
,支持TCP以及HTTP协议的应用,既支持四层负载均衡又支持七层负载均衡
一、配置
安装:
yum install haproxy -y
配置:
vim /etc/haproxy/haproxy.cfg
修改的内容:
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main *:80
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js
# use_backend static if url_static
default_backend web
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
balance roundrobin
server static 127.0.0.1:4331 check
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend web
balance roundrobin
server web01 10.1.1.31:80 check
server web02 10.1.1.32:80 check
修改的地方:
frontend main *:80
...
# use_backend static if url_static
default_backend web
backend web
...
启动服务:
systemctl start haproxy
systemctl status haproxy
定义Web管理界面:
# 定义Web管理界面
listen statistics
bind *:9090 # 定义监听端口
mode http # 默认使用协议
stats enable # 启用stats
stats uri /hadmin?stats # 自定义统计页面的URL,默认为/haproxy?stats
stats auth admin:admin # 统计页面用户名和密码设置
stats hide-version # 隐藏统计页面上HAProxy的版本信息
stats refresh 30s # 统计页面自动刷新时间
stats admin if TRUE # 如果认证通过就做管理功能,可以管理后端的服务器
stats realm Hapadmin # 统计页面密码框上提示文本,默认为Haproxy\ Statistics
访问。5
http://IP:9090/hadmin?stats
二、HAProxy 常用算法
balance roundrobin # 轮询,软负载均衡基本都具备这种算法
balance static-rr # 根据权重,建议使用 => server ... weight 权重值
balance leastconn # 最少连接者先处理,建议使用
balance source # 根据请求源IP,建议使用(类似IP_HASH)
balance uri # 根据请求的URI
balance url_param # 根据请求的URl参数
balance hdr(name) # 根据HTTP请求头来锁定每一次HTTP请求
balance rdp-cookie(name) # 根据cookie(name)来锁定并哈希每一次TCP请求
三、HAProxy 数据库做负载均衡
适用于双主双从
,对双主
进行负载均衡
配置:
vim /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode tcp
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main *:3306
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js
# use_backend static if url_static
default_backend mysql
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
balance roundrobin
server static 127.0.0.1:4331 check
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend mysql
balance roundrobin
server mysql01 10.1.1.33:3306 check
server mysql02 10.1.1.34:3306 check
修改的地方:
defaults
mode tcp
frontend main *:3306
...
# use_backend static if url_static
default_backend mysql
backend mysql
...
测试:
mysql -h10.1.1.40 -udsshop -p