利用HAProxy代理SQL Server的AlwaysOn辅助副本

利用HAProxy代理SQL Server的AlwaysOn辅助副本

公司最近数据库升级到SQL Server2014 ,并部署了alwayson高可用集群

机房内有三套程序需要读取数据库

第一套:主程序,读写数据库,连接主副本

第二套:报表程序,读报表,连接辅助副本

第三套:历史库程序,读历史库,连接辅助副本

 

软件环境

机器环境

 

架构图

为什麽需要使用HAProxy?

之前机房里面有2000个终端,这些终端是一个很小的嵌入式设备,第二套报表程序原来是使用直连数据库IP(10.11.10.36)来连接数据库

但这样有一个弊端,当36这台辅助副本宕机,那么报表程序就瘫痪了,因为2000个终端要更改数据库连接需要烧写程序到终端里面非常耗费时间

可能要几天时间

 

最后决定使用HAProxy做负载均衡和TCP连接重定向

使用HAProxy有几个好处

1、前端不需要后端数据库的实际IP,当需要升级后端数据库,比如打补丁的时候特别方便

2、HAProxy能够自动检测后端数据库服务,探测1433端口是否存活,如果1433端口出问题,能够自动重定向连接到37这台辅助副本

3、减轻单台读库压力,使用RR轮询算法,请求均衡分发到36和37这两台辅助副本,减轻36这台机器的压力

 

 


HAProxy相关配置步骤

 

#yum安装,版本是1.5.4

yum install -y haproxy.x86_64

 

 

#编辑rsyslog 文件,修改为-c 2 -r -x -m

vi /etc/sysconfig/rsyslog
SYSLOGD_OPTIONS="-c 2 -m 0 -r -x"

 

 


#编辑rsyslog.conf 文件添加两行local3.*  和local0.*

vi /etc/rsyslog.conf
local7.*                                                /var/log/boot.log
local3.*                                                /var/log/haproxy.log
local0.*                                               /var/log/haproxy.log

 

 

 

#重启rsyslog服务

service rsyslog restart

 

 


# 编辑haproxy配置文件 下面以mssql从库负载均衡为例

vi /etc/haproxy/haproxy.cfg

global
log 127.0.0.1 local2 
chroot /var/lib/haproxy 
pidfile /var/run/haproxy.pid 
maxconn 6000 
user haproxy 
group haproxy 
daemon 
#stats socket /var/lib/haproxy/stats     
stats socket /var/run/haproxy.sock mode 666 level admin
stats timeout 2m


defaults 
mode http 
log                 127.0.0.1:514       local3                     
option                  dontlognull
#option http-server-close
#option forwardfor       except 127.0.0.0/8
option redispatch
retries 3 
timeout http-request 10s 
timeout connect 10s 
timeout client 1m 
timeout server 1m 
timeout http-keep-alive 10s
timeout check 10s
maxconn 6000

 

 

listen stats 
mode http
bind *:2080
stats enable
stats refresh 30s
stats uri /haproxyadminstats
stats realm HAProxy\ Statistics
stats auth admin:admin
stats admin if TRUE


listen mssql :1433
mode tcp
balance roundrobin
server mssqldb1 10.11.10.36:1433 weight 1 maxconn 6000 check port 1433 inter 2000 rise 2 fall 2
server mssqldb2 10.11.10.37:1433 weight 1 maxconn 6000 check port 1433 inter 2000 rise 2 fall 2

 

 


#检查配置文件是否有语法错误

haproxy -f /etc/haproxy/haproxy.cfg -c
Configuration file is valid

 


#启动haproxy

/etc/init.d/haproxy start

 

 

#检查haproxy是否在监听

netstat -lntp

 

注意:Centos机器只需要使用一个网口,不需要额外增加网口


#打开后台管理界面
http://10.11.30.47:2080/haproxyadminstats

 

HAProxy提供了一个后台管理界面

 

查看haproxy的日志

cat /var/log/haproxy.log


测试验证

使用SSMS2016来连接HAProxy的IP

10.11.10.39

现在是连接到GZC-SQL03这台机

 

现在把 GZC-SQL03这台机的SQL服务停了

HAProxy已经探测到 GZC-SQL03这台机的SQL服务停了

 再次点击一下执行按钮,可以发现已经重定向到 GZC-SQL02这台机

 

 

虽然经过HAProxy这一层,但是性能方面也不算太差


 

HAProxy的通信方式

通信方式类似于LVS的NAT模式

LVS的NAT模式(调度器将请求的目标ip即vip地址改为Real server的ip, 返回的数据包也经过调度器,调度器再把源地址修改为vip)

 

 


总结

线上环境使用HAProxy已经差不多1个月,到现在没有出现过问题,比较稳定

对于HAProxy原理上的东西这里就不叙述了,网上有很多资料

 

参考文章:

http://www.cnblogs.com/dehai/p/4885021.html

 

如果是每个业务使用不同端口,可以使用下面的配置文件

比如报表使用1433端口,BI抽取数据使用2433端口

vi /etc/haproxy/haproxy.cfg
global
    log         127.0.0.1 local2 
    chroot      /var/lib/haproxy       
    pidfile     /var/run/haproxy.pid     
    maxconn     6000                               
    user        haproxy           
    group       haproxy           
    daemon
    #stats socket /var/lib/haproxy/stats 
    stats socket /var/run/haproxy.sock mode 666 level admin
    stats timeout 2m

    


defaults         
    mode                    http               
    log                     global                         
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3                 
    timeout http-request    10s        
    timeout connect         10s               
    timeout client          1m                 
    timeout server          1m                 
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 6000               





listen stats     
    mode http
    bind *:2080
    stats enable
    stats refresh 30s
    stats uri   /haproxyadminstats
    stats realm  HAProxy\ Statistics
    stats auth   admin:admin
    stats admin if TRUE
    
    
listen mssql :1433
    mode tcp
    balance roundrobin
    server mssqldb1 10.11.10.36:1433  weight 1 maxconn 6000 check port 1433 inter 2000  rise 2 fall 2
    server mssqldb2 10.11.10.37:1433  weight 1 maxconn 6000 check port 1433 inter 2000  rise 2 fall 2
    
    
listen mssql2 :2433
    mode tcp
    balance leastconn
    server mssqldb3 10.11.10.37:1433   maxconn 6000 check port 1433 inter 2000  rise 2 fall 2

 

 

如有不对的地方,欢迎大家拍砖o(∩_∩)o 

本文版权归作者所有,未经作者同意不得转载。

posted @ 2016-08-30 09:53  桦仔  阅读(2621)  评论(12编辑  收藏  举报