mysql中间件-atlas介绍

一 。atlas简介

Atlas是由 Qihoo 360公司Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目。它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基础上,修改了大量bug,添加了很多功能特性。目前该项目在360公司内部得到了广泛应用,很多MySQL业务已经接入了Atlas平台,每天承载的读写请求数达几十亿条。同时,有超过50家公司在生产环境中部署了Atlas,超过800人已加入了我们的开发者交流群,并且这些数字还在不断增加。(简介内容摘自github官网 https://github.com/Qihoo360/Atlas/wiki/Atlas%E7%9A%84%E6%9E%B6%E6%9E%84)

1》主要功能:

1.读写分离,2.从库负载均衡,3.IP过滤,4.自动分表,5.DBA可平滑上下线DB,6.自动摘除宕机的DB

2》系统主要架构:

tlas是一个位于应用程序与MySQL之间中间件。在后端DB看来,Atlas相当于连接它的客户端,在前端应用看来,Atlas相当于一个DB。Atlas作为服务端与应用程序通讯,它实现了MySQL的客户端和服务端协议,同时作为客户端与MySQL通讯。它对应用程序屏蔽了DB的细节,同时为了降低MySQL负担,它还维护了连接池。Atlas的整体架构,可参考下面这两幅图:



3》线程模型

Atlas启动后会创建多个线程,其中一个为主线程,其余为工作线程。主线程负责监听所有的客户端连接请求,工作线程只监听主线程的命令请求。

如下图所示,主线程接收到客户端的连接请求,将该请求的相关信息封装为一个名为CON的结构,再把该结构推入一个异步队列。然后通过round-robin方式选择一个工作线程,向其发送一个字节的数据包以激活它。工作线程在收到主线程的激活指令后,从异步队列中取出CON结构,开始处理客户端的请求。


4》Atlas配合LVS使用的架构

下图是一个可以参考的整体架构,LVS前端做负载均衡,两个Atlas做HA,防止单点故障。LVS周期性地对后端Atlas的存活检测有两种方式,一是直接去探测端口是否可连接,二是执行一个脚本,这个脚本会去尝试连接Atlas,通过脚本的返回值来决定每个后端是否可用。Atlas有两种运行状态,通常为online,可通过发信号将其置为offline。Atlas检测到来请求的IP是LVS的网卡IP时,如果处于online状态,就向LVS的检测脚本返回online,如果处于offline状态,就向脚本返回offline。比如我现在因为某种原因需要重启一台Atlas,但直接重启势必导致瞬间的SQL请求全部失败,对前端应用造成影响。因此我先发下线信号将Atlas置为offline状态,当LVS的检测脚本发现返回值是offline时,便将这台Atlas摘除,从此时开始便没有新的请求导向这台Atlas。等到已经打向这台Atlas的SQL请求处理完毕后(这是一个很短的时间),就可以安全重启Atlas而不必担心对前端造成影响了。


二。atlas安装和读写分离

 1》atlas安装过程(参考https://github.com/Qihoo360/Atlas/wiki/Atlas%E7%9A%84%E5%AE%89%E8%A3%85)

    这里已经安装了 mysql的双主双从 参考(http://blog.csdn.net/liaomin416100569/article/details/78580382)

 环境:
   数据库:mariadb  系统centos7

[html]  view plain  copy
  1. 主:192.168.58.147:3306"   
  2.  从1:192.168.58.149:3306   
  3. 主备:192.168.58.151:3306  
  4.  从2:192.168.58.150:3306   
下载最新版rpm包 (https://github.com/Qihoo360/Atlas/releases) el5是centos5版本 el6是centos6以上版本

wget https://github.com/Qihoo360/Atlas/releases/download/sharding-1.0.1/Atlas-sharding_1.0.1-el6.x86_64.rpm
安装 
rpm -ivh Atlas-sharding_1.0.1-el6.x86_64.rpm
默认安装目录位于
 
usr/local/mysql-proxy
 2》配置读写分离
 在/usr/local/mysql-proxy/cnf目录下有个test.cnf文件 有关于所有配置的信息 
[mysql-proxy]

#带#号的为非必需的配置项目

#管理接口的用户名
admin-username = admin

#管理接口的密码
admin-password = admin

#Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔
proxy-backend-addresses = 192.168.58.147:3306,192.168.58.151:3306

#Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔
#proxy-read-only-backend-addresses = 192.168.58.149:3306,192.168.58.150:3306

#用户名与其对应的加密过的MySQL密码,密码使用/usr/local/mysql-proxy/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码!我的后端mysql的密码时root  ./encrypt root 获取密码
pwds = root:DAJnl8cVzy8=

#设置Atlas的运行方式,设为true时为守护进程方式,设为false时为前台方式,一般开发调试时设为false,线上运行时设为true,true后面不能有空格。
daemon = true

#设置Atlas的运行方式,设为true时Atlas会启动两个进程,一个为monitor,一个为worker,monitor在worker意外退出后会自动将其重启,设为false时只有worker,没有monitor,一般开发调试时设为false,线上运行时设为true,true后面不能有空格。
keepalive = true

#工作线程数,对Atlas的性能有很大影响,可根据情况适当设置
event-threads = 8

#日志级别,分为message、warning、critical、error、debug五个级别
log-level = message

#日志存放的路径
log-path = /usr/local/mysql-proxy/log

#SQL日志的开关,可设置为OFF、ON、REALTIME,OFF代表不记录SQL日志,ON代表记录SQL日志,REALTIME代表记录SQL日志且实时写入磁盘,默认为OFF
#sql-log = OFF

#慢日志输出设置。当设置了该参数时,则日志只输出执行时间超过sql-log-slow(单位:ms)的日志记录。不设置该参数则输出全部日志。
#sql-log-slow = 10

#实例名称,用于同一台机器上多个Atlas实例间的区分
#instance = test

#Atlas监听的工作接口IP和端口
proxy-address = 0.0.0.0:1234

#Atlas监听的管理接口IP和端口
admin-address = 0.0.0.0:2345

#分表设置,此例中person为库名,mt为表名,id为分表字段,3为子表数量,可设置多项,以逗号分隔,若不分表则不需要设置该项
#tables = person.mt.id.3

#默认字符集,设置该项后客户端不再需要执行SET NAMES语句
#charset = utf8

#允许连接Atlas的客户端的IP,可以是精确IP,也可以是IP段,以逗号分隔,若不设置该项则允许所有IP连接,否则只允许列表中的IP连接
#client-ips = 127.0.0.1, 192.168.1

#Atlas前面挂接的LVS的物理网卡的IP(注意不是虚IP),若有LVS且设置了client-ips则此项必须设置,否则可以不设置
#lvs-ips = 192.168.1.1
启动atlas
 
./mysql-proxyd test start
查看发现启动了两个端口
[root@bogon bin]# netstat -nlp | grep mysql
tcp        0      0 0.0.0.0:2345            0.0.0.0:*               LISTEN      9103/mysql-proxy    
tcp        0      0 0.0.0.0:1234            0.0.0.0:*               LISTEN      9103/mysql-proxy 
其中 1234是mysql客户端连接的端口   2345是管理端口
使用装有mysql客户端的电脑连接
mysql -uroot -proot -h192.168.58.148 -P1234
操作数据库 测试是否自动同步读写 出现问题参考(https://github.com/Qihoo360/Atlas/wiki/Atlas%E7%9A%84%E8%BF%90%E8%A1%8C%E5%8F%8A%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98)

2345管理端口可以动态管理 后端服务器的添加和删除,管理密码的处理等  使用mysql客户端登录使用 select * from help查看

所有命令帮助 登录密码就是test.cnf中指定的admin用户名和密码

[root@node1 ~]# mysql -h192.168.58.148 -P2345 -uadmin -padmin
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.99-agent-admin


Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


MySQL [(none)]> show databases;
ERROR 1105 (07000): use 'SELECT * FROM help' to see the supported commands
MySQL [(none)]> select * from help
    -> ;
+---------------------------------------+---------------------------------------------------------+
| command                               | description                                             |
+---------------------------------------+---------------------------------------------------------+
| SELECT * FROM help                    | shows this help                                         |
| SELECT * FROM backends                | lists the backends and their state                      |
| SET OFFLINE $backend_id               | offline backend server, $backend_id is backend_ndx's id |
| SET ONLINE $backend_id                | online backend server, ...                              |
| ADD MASTER $backend                   | example: "add master 127.0.0.1:3306", ...               |
| ADD SLAVE $backend                    | example: "add slave 127.0.0.1:3306", ...                |
| ADD GMASTER $group_id $backend        | example: "add gmaster 1 127.0.0.1:3306", ...            |
| ADD GSLAVE $group_id $backend         | example: "add gslave 1 127.0.0.1:3306", ...             |
| REMOVE BACKEND $backend_id            | example: "remove backend 1", ...                        |
| REMOVE GBACKEND $group_id $backend_id | example: "remove gbackend 1 1", ...                     |
| SELECT * FROM clients                 | lists the clients                                       |
| ADD CLIENT $client                    | example: "add client 192.168.1.2", ...                  |
| REMOVE CLIENT $client                 | example: "remove client 192.168.1.2", ...               |
| SELECT * FROM pwds                    | lists the pwds                                          |
| ADD PWD $pwd                          | example: "add pwd user:raw_password", ...               |
| ADD ENPWD $pwd                        | example: "add enpwd user:encrypted_password", ...       |
| REMOVE PWD $pwd                       | example: "remove pwd user", ...                         |
| SAVE CONFIG                           | save the backends to config file                        |
| SELECT VERSION                        | display the version of Atlas                            |
+---------------------------------------+---------------------------------------------------------+
19 rows in set (0.00 sec)


三。atlas高可用

  atlas使用lvs或者haproxy进行四层代理 达到高可用(lvs安装可以使用简易安装工具 piranha)

这里lvs就是反向代理 可以使用nat模型
模拟环境 
 除之前四台mysql外 另外添加两台主机 配置atlas

192.168.58.146 atlas
192.168.58.148 atlas
192.168.58.147 lvs
关闭146和148防火墙和selinux
 
systemctl stop firewalld
atlas安装过程参考章节2  相同的配置ok  配置完成后测试在任意mysql客户端的环境连接
[root@node1 ~]# mysql -uroot -proot -h192.168.58.146 -P1234
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 5.0.81-log MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> exit
Bye
[root@node1 ~]# mysql -uroot -proot -h192.168.58.148 -P1234
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.0.81-log MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> 

具体配置原理

 147上安装lvs(参考 http://blog.csdn.net/liaomin416100569/article/details/73300692
yum -y install ipvsadm
虚拟机上添加一个虚拟网卡 参考上面博客 (ip是  192.168.86.116【是vip 对外提供服务的ip】 window主机是 192.168.86.112 所以生成的虚拟ip是86段
添加lvs集群配置
ipvsadm -A -t 192.168.86.116:3333 -s rr
ipvsadm -a -t 192.168.86.116:3333 -r 192.168.58.146:1234 -m
ipvsadm -a -t 192.168.86.116:3333 -r 192.168.58.148:1234 -m  
147设置网卡间转发 (修改 /etc/sysctl.conf ) 添加
net.ipv4.ip_forward = 1 
使用命令 sysctl -p 生效
将 目标机器146和148修改为静态ip 同时设置网关为 147 配置如下

146修改配置(其他参数默认)

BOOTPROTO=static
IPADDR=192.168.58.146
GATEWAY=192.168.58.147
148同理配置

BOOTPROTO=static
IPADDR=192.168.58.148
GATEWAY=192.168.58.147
分别重启网卡
 
service network restart
在window上使用mysql客户端 访问vip 连接 3333端口测试是否能连接atlas并且测试是否目标机器轮询
C:\Users\jiaozi>mysql -uroot -proot -h192.168.86.116 -P3333
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 5.0.81-log MariaDB Server

Copyright (c) 2000, 2011, 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> select user();
+---------------------+
| user()              |
+---------------------+
| [email protected] |
+---------------------+
1 row in set (0.00 sec)

mysql> exit
Bye

C:\Users\jiaozi>mysql -uroot -proot -h192.168.86.116 -P3333
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.0.81-log MariaDB Server

Copyright (c) 2000, 2011, 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> select user();\
+---------------------+
| user()              |
+---------------------+
| [email protected] |
+---------------------+
1 row in set (0.00 sec)

四。atlas分表分库
 参考 github (https://github.com/Qihoo360/Atlas/wiki/Atlas-Sharding)

猜你喜欢

转载自blog.csdn.net/liaomin416100569/article/details/79195796