因为TiDB是无状态,所以在多个TiDB节点前会利用负载均衡软件做TiDB节点的高可用,这里是基于HAProxy软件做负载均衡,HAProxy 官网:http://www.haproxy.org
功能
- 负载均衡:L4(TCP) 和 L7(HTTP/HTTPS) 两种模式,支持 RR(roundrobin) / 静态 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 的状态
一、关闭SElinux、配置防火墙
1、vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存退出
setenforce 0 #使配置立即生效
2、vi /etc/sysconfig/iptables #编辑
-A RH-Firewall-1-INPUT -d 224.0.0.18 -j ACCEPT #允许组播地址通信
-A RH-Firewall-1-INPUT -p vrrp -j ACCEPT #允许VRRP(虚拟路由器冗余协)通信
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT #允许80端口通过防火墙
:wq! #保存退出
/etc/init.d/iptables restart #重启防火墙使配置生效
二、安装HAProxy
1、创建HAProxy运行账户和组
groupadd haproxy #添加haproxy组
useradd -g haproxy haproxy -s /bin/false #创建nginx运行账户haproxy并加入到haproxy组,不允许haproxy用户直接登录系统
2、安装
[root@ip-172-16-30-86 ~]# yum install -y gcc
[root@ip-172-16-30-86 opt]# tar zxvf haproxy-1.8.19.tar.gz
[root@ip-172-16-30-86 haproxy-1.8.19]# cd haproxy-1.8.19/
[root@ip-172-16-30-86 haproxy-1.8.19]# uname -r
3.10.0-862.el7.x86_64
#编译 uname -r #查看系统内核版本号
[root@ip-172-16-30-86 haproxy-1.8.19]# make TARGET=linux3100 CPU=x86_64 PREFIX=/usr/local/haprpxy
#安装
[root@ip-172-16-30-86 haproxy-1.8.19]# make install PREFIX=/usr/local/haproxy
参数说明:
TARGET=linux3100
使用uname -r查看内核,如:2.6.18-371.el5,此时该参数就为linux26
kernel 大于2.6.28的用:TARGET=linux2628
CPU=x86_64 #使用uname -r查看系统信息,如x86_64 x86_64 x86_64 GNU/Linux,此时该参数就为x86_64
PREFIX=/usr/local/haprpxy #/usr/local/haprpxy为haprpxy安装路径
3、设置HAProxy
mkdir -p /usr/local/haproxy/conf #创建配置文件目录
mkdir -p /etc/haproxy #创建配置文件目录
touch /usr/local/haproxy/conf/haproxy.cfg #创建配置文件
ln -s /usr/local/haproxy/conf/haproxy.cfg /etc/haproxy/haproxy.cfg #添加配置文件软连接
cp -r /opt/haproxy-1.8.19/examples/errorfiles /usr/local/haproxy/errorfiles #拷贝错误页面
ln -s /usr/local/haproxy/errorfiles /etc/haproxy/errorfiles #添加软连接
mkdir -p /usr/local/haproxy/log #创建日志文件目录
touch /usr/local/haproxy/log/haproxy.log #创建日志文件
ln -s /usr/local/haproxy/log/haproxy.log /var/log/haproxy.log #添加软连接
cp /opt/haproxy-1.8.19/examples/haproxy.init /etc/rc.d/init.d/haproxy #拷贝开机启动文件
chmod +x /etc/rc.d/init.d/haproxy #添加脚本执行权限
chkconfig haproxy on #设置开机启动
ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin #添加软连接
4、配置haproxy.cfg参数
TiDB or MySQL 需要使用 L4 (OSI) TCP 层模式
cp /usr/local/haproxy/conf/haproxy.cfg /usr/local/haproxy/conf/haproxy.cfg-bak #备份
vi /usr/local/haproxy/conf/haproxy.cfg #编辑,修改
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
log 127.0.0.1 local2 info ###[err warning info debug]
chroot /usr/local/haproxy
pidfile /var/run/haproxy.pid ###haproxy的pid存放路径,启动进程的用户必须有权限访问此文件
maxconn 4000 ###最大连接数,默认4000
user haproxy
group haproxy
daemon ###创建1个进程进入deamon模式运行。此参数要求将运行模式设置为"daemon"
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http ###默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
log global ###采用全局定义的日志
option dontlognull ###不记录健康检查的日志信息
option httpclose ###每次请求完毕后主动关闭http通道
option httplog ###日志类别http日志格式
option forwardfor ###如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
option redispatch ###serverId对应的服务器挂掉后,强制定向到其他健康的服务器
timeout connect 10000 #default 10 second timeout if a backend is not found
timeout client 300000 ###客户端连接超时
timeout server 300000 ###服务器连接超时
maxconn 60000 ###最大连接数
retries 3 ###3次连接失败就认为服务不可用,也可以通过后面设置
####################################################################
listen stats
bind 0.0.0.0:1099 #监听端口
stats refresh 30s #统计页面自动刷新时间
stats uri /stats #统计页面url,查看负载均衡状态页面IP:1099/stats
stats realm Haproxy Manager #统计页面密码框上提示文本
stats auth admin:admin #统计页面用户名和密码设置 (账号密码登陆)
#stats hide-version #隐藏统计页面上HAProxy的版本信息
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
######配置网页服务器负载均衡###############################
#frontend main
# bind 0.0.0.0:8888
# 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 ###满足策略要求,则响应策略定义的backend页面
# default_backend dynamic ###不满足则响应backend的默认页面
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#backend static
# balance roundrobin ###负载均衡模式轮询
# server static 127.0.0.1:8888 check ###后端服务器定义
#backend dynamic ###负载均衡后台服务器
# balance roundrobin
# server webserver1 172.16.30.86:8888 check maxconn 2000
# server webserver2 172.16.30.89:8888 check maxconn 2000
#backend static
# balance roundrobin ###负载均衡模式轮询
# server static 127.0.0.1:8888 check ###后端服务器定义
###########################################################
#######配置TiDB实现负载均衡##############################
listen tidb-cluster
bind 0.0.0.0:5001
mode tcp
balance roundrobin
server ip-172-16-30-86 172.16.30.86:5000 check inter 2000 rise 2 fall 3
server ip-172-16-30-89 172.16.30.89:5000 check inter 2000 rise 2 fall 3
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
#errorloc 503 http://www.osyunwei.com/404.html
errorfile 403 /etc/haproxy/errorfiles/403.http
errorfile 500 /etc/haproxy/errorfiles/500.http
errorfile 502 /etc/haproxy/errorfiles/502.http
errorfile 503 /etc/haproxy/errorfiles/503.http
errorfile 504 /etc/haproxy/errorfiles/504.http
:wq! #保存退出
service haproxy start #启动
service haproxy stop #关闭
service haproxy restart #重启
5、设置HAProxy日志
vi /etc/sysconfig/syslog 编辑修改添加
SYSLOGD_OPTIONS="-r -m 0"
相关解释说明:要是存在SYSLOGD_OPTIONS="-m 0"则改成 SYSLOGD_OPTIONS="-r -m 0"
-r:打开接受外来日志消息的功能,其监控514 UDP端口;
-x:关闭自动解析对方日志服务器的FQDN信息,这能避免DNS不完整所带来的麻烦;
-m:修改syslog的内部mark消息写入间隔时间(0为关闭),例如240为每隔240分钟写入一次"--MARK--"信息;
-h:默认情况下,syslog不会发送从远端接受过来的消息到其他主机,而使用该选项,则把该开关打开,所有
接受到的信息都可根据syslog.conf中定义的@主机转发过去
vi /etc/rsyslog.conf 编辑修改
$ModLoad imudp
$UDPServerRun 514 #开启udp日志
local1.* /var/log/haproxy.log
local2.* /var/log/haproxy.log
local3.* /var/log/haproxy.log
#重启rsyslog
service rsyslog restart
6.重启Haproxy
# systemctl restart haproxy.service
# ss -tnl|grep 500
LISTEN 0 32768 *:5001 *:*
LISTEN 0 32768 :::5000 :::
验证是否可以haproxy登陆
[root@ip-172-16-30-86 log]# mysql -uroot -p -h172.16.30.86 -P5001
Enter password:
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 4
Server version: 5.7.10-TiDB-v2.1.4 MySQL Community Server (Apache License 2.0)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| INFORMATION_SCHEMA |
| PERFORMANCE_SCHEMA |
| TPCH_001 |
| bike |
| bikeshare |
| dm_meta |
| dminc |
| dmmysql |
| dmtidb |
| marvin |
| mysql |
| test |
| tidb |
+--------------------+
13 rows in set (0.00 sec)
日志输出/var/log/haproxy.log,info级别
[root@ip-172-16-30-86 ~]# tail -f /var/log/haproxy.log
2019-03-06T16:21:24+08:00 localhost haproxy[49672]: Stopping proxy stats in 0 ms.
2019-03-06T16:21:24+08:00 localhost haproxy[49672]: Stopping proxy tidb-cluster in 0 ms.
2019-03-06T16:21:24+08:00 localhost haproxy[49672]: Proxy stats stopped (FE: 114 conns, BE: 57 conns).
2019-03-06T16:21:24+08:00 localhost haproxy[49672]: Proxy tidb-cluster stopped (FE: 2 conns, BE: 2 conns).
2019-03-06T16:21:24+08:00 localhost haproxy[80393]: Proxy stats started.
2019-03-06T16:21:24+08:00 localhost haproxy[80393]: Proxy tidb-cluster started.
2019-03-06T16:21:31+08:00 localhost haproxy[80394]: 192.168.198.113:60791 [06/Mar/2019:16:21:31.617] stats stats/<STATS> 0/0/0/0/0 200 16101 - - LR-- 1/1/0/0/0 0/0 "GET /stats HTTP/1.1"
2019-03-06T16:21:32+08:00 localhost haproxy[80394]: 192.168.198.113:60794 [06/Mar/2019:16:21:32.600] stats stats/<NOSRV> 0/-1/-1/-1/0 503 212 - - SC-- 1/1/0/0/0 0/0 "GET /favicon.ico HTTP/1.1"
2019-03-06T16:22:02+08:00 localhost haproxy[80394]: 192.168.198.113:60845 [06/Mar/2019:16:22:02.602] stats stats/<STATS> 0/0/0/0/0 200 16277 - - LR-- 1/1/0/0/0 0/0 "GET /stats HTTP/1.1"
2019-03-06T16:22:03+08:00 localhost haproxy[80394]: 192.168.198.113:60848 [06/Mar/2019:16:22:03.607] stats stats/<NOSRV> 0/-1/-1/-1/0 503 212 - - SC-- 1/1/0/0/0 0/0 "GET /favicon.ico HTTP/1.1"
7.浏览器打开haproxy的监控页面
如下:http://172.16.30.86:1099/stats/ 说明:1080即haproxy配置文件中监听端口,stats 即haproxy配置文件中的监听名称,账号密码admin/admin
版权声明:本文为博主原创文章,未经博主允许不得转载。



