Atlas简介
Atlas是由 Qihoo 360公司Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目。它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基础上,修改了大量bug,添加了很多功能特性。目前该项目在360公司内部得到了广泛应用,很多MySQL业务已经接入了Atlas平台,每天承载的读写请求数达几十亿条。同时,有超过50家公司在生产环境中部署了Atlas,超过800人已加入了我们的开发者交流群,并且这些数字还在不断增加。
官方链接:https://github.com/Qihoo360/Atlas
主要功能
1.读写分离
2.从库负载均衡
3.IP过滤
4.自动分表
5.DBA可平滑上下线DB
6.自动摘除宕机的DB
接下来将测试验证以上功能。。。。。。
安装部署
]# rpm -ivh Atlas-2.1.el6.x86_64.rpm
Preparing... ########################################### [100%]
1:Atlas ########################################### [100%]
编辑配置文件
Atlas运行需要依赖一个配置文件(test.cnf)。在运行Atlas之前,需要对该文件进行配置。Atlas的安装目录是/usr/local/mysql-proxy,进入安装目录下的conf目录,可以看到已经有一个名为test.cnf的默认配置文件,我们只需要修改里面的某些配置项,不需要从头写一个配置文件。
]# pwd
/usr/local/mysql-proxy/conf
]# vim test.cnf
[mysql-proxy]
#带#号的为非必需的配置项目
#管理接口的用户名
admin-username = user
#管理接口的密码
admin-password = pwd
#注:使用的主从环境:Master:192.168.1.100,Slave:192.168.1.200
#Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔
proxy-backend-addresses = 192.168.1.100:3306
#Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔
proxy-read-only-backend-addresses = 192.168.1.200:3306
#用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码!
pwds = root:5AaUuB6tXYU=
#设置Atlas的运行方式,设为true时为守护进程方式,设为false时为前台方式,一般开发调试时设为false,线上运行时设为true
daemon = true
#设置Atlas的运行方式,设为true时Atlas会启动两个进程,一个为monitor,一个为worker,monitor在worker意外退出后会自动将其重启,设为false时只有worker,没有monitor,一般开发调试时设为false,线上运行时设为true
keepalive = true
#工作线程数,对Atlas的性能有很大影响,可根据情况适当设置
event-threads = 1
#日志级别,分为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 = REALTIME
#实例名称,用于同一台机器上多个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
关于配置文件参数详细说明可以参考:
https://github.com/Qihoo360/Atlas/wiki/Atlas%E7%9A%84%E5%AE%89%E8%A3%85
启动atlas服务
]# /usr/local/mysql-proxy/bin/mysql-proxyd test start
OK: MySQL-Proxy of test is started
注意:
(1). 运行文件是:mysql-proxyd(不是mysql-proxy)。
(2). test是conf目录下配置文件的名字,也是配置文件里instance项的名字,三者需要统一。
查看Atlas进程
]# ps -ef | grep mysql-proxy|grep -v grep
root 7186 1 0 14:28 ? 00:00:00 /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/test.cnf
root 7187 7186 0 14:28 ? 00:00:00 /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/test.cnf
连接Atlas管理界面
]# mysql -P2345 -uuser -ppwd -h 127.0.0.1
……
mysql> select * from t;
ERROR 1105 (07000): use 'SELECT * FROM help' to see the supported commands
mysql> 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", ... |
| REMOVE BACKEND $backend_id | example: "remove backend 1", ... |
+----------------------------+---------------------------------------------------------+
7 rows in set (0.00 sec)
mysql> select * FROM backends;
#可以看到1.100为rw,1.200为ro
功能测试
1.Atlas读写分离功能
#连接Atlas,进行增删改查,查看sql_log日志
#注意如果atlas和mysql处于同一台机器,连接atlas时必须指定-h 127.0.0.1或者机器的IP
因为不指定-h的时候mysql连接走的unix socket连接,此时-P参数无效,-P后面指定任意端口都是连到的本机的mysql实例
~]# mysql -P1234 -uroot -pyougou -h192.168.1.200
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1827336327
Server version: 5.0.81-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, 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 tables;
+------------------+
| Tables_in_yougou |
+------------------+
| t |
+------------------+
1 row in set (0.00 sec)
mysql> select * from t;
+------+
| x |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+------+
5 rows in set (0.01 sec)
mysql> delete from t where x=5;
Query OK, 1 row affected (0.05 sec)
mysql> insert into t select 5;
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> update t set x=8 where x=5;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from t;
+------+
| x |
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 8 |
+------+
5 rows in set (0.00 sec)
mysql> create table y(y int);
Query OK, 0 rows affected (0.61 sec)
mysql> drop table y;
Query OK, 0 rows affected (0.28 sec)
#对应的sql_log日志:
~]# tail -f /usr/local/mysql-proxy/log/sql_test.log
#可以看到所有的增删改查操作都去master上,select去slave上
2.Atlas从库负载均衡
架构环境
Master 192.168.1.100:3306
Slave 192.168.1.200:3306, 192.168.1.200:3307, 192.168.1.200:3308
#此处实验环境资源有限,使用的mysqld_multi启动多个mysql实例,关于mysqld_multi的使用参考:http://blog.itpub.net/27000195/viewspace-1419251/
Atlas配置文件
#其他参数与上面一致,主要修改proxy-read-only-backend-addresses为:
重启atlas服务
~]# /usr/local/mysql-proxy/bin/mysql-proxyd test restart
OK: MySQL-Proxy of test is stopped
OK: MySQL-Proxy of test is started
测试atlas负载均衡
#在1.200上连接1.200的atlas,进行数据库操作,查看atlas的sql_log日志
~]# tail -f /usr/local/mysql-proxy/log/sql_test.log
可以看到读得操作都是在(slave)1.200上,并且3306端口会联系读2次(配置了3306@2权重为2),才轮到3307和3308端口,从而实现了读负载均衡和读写分离
3.IP过滤
通过配置client-ips来限制允许连接Atlas的客户端的IP,可以是精确IP,也可以是IP段,以逗号分隔,若不设置该项则允许所有IP连接,否则只允许列表中的IP连接
client-ips = 127.0.0.1, 192.168.1
设置client-ips为127.0.0.1
重启atlas服务
]# /usr/local/mysql-proxy/bin/mysql-proxyd test restart
OK: MySQL-Proxy of test is stopped
OK: MySQL-Proxy of test is started
IP过滤验证
#本机连接正常
#使用1.100连接1.200的atlas,被拒绝
从而实现了客户端IP过滤功能
4.自动分表
配置分表参数
#此处将数据库yougou下的表parttable按照字段ID分位10张表
重启atlas使配置生效
创建子表
#yougoyu数据库中创建parttable_0….10共10张子表
生成测试数据
#本来打算使用procedure插入测试数据,但是报错,如下:
create procedure insertparttable()
begin
declare i int;
set i=0;
while i<100 do
insert into parttable values(i,i*100);
set i=i+1;
end while;
end;//
mysql> call insertparttable();
ERROR 1146 (42S02): Table 'yougou.parttable' doesn't exist
所以只有写明ID列位int数字,来生成:
~]# vim insertparttable.sql
insert into parttable values(1,100);
insert into parttable values(i,i*100);
0<=i<=100
……
测试atlas自动分表
#插入数据
mysql> source insertparttable.sql
#监控sql_log,发现atlas自动将id%10的结果插入到对应的子表中
#查询数据
#对应的sql_log
如上验证了atlas的简单分表功能,感觉目前可用性较差
5.上下线DB
#上线
#下线
6.自动摘除宕机的DB
#正常状态
#将3308端口的mysql停止
#连接atlas,执行查询,并且查看log
可以看到atlas的log提示标记为down,sql_log中发现不在去3308端口读
#此时通过atlas管理环境查看状态,发现3308的state为down
Keepalived+Atlas架构实践
架构图
Mysql主从:一主三从
Atlas:2个节点做HA
Keepalived:提供VIP防止Atlas单点故障
Mysql主从配置
省略……
Keepalived安装
http://www.keepalived.org/download.html 下载url
#安装keepalived
tar zxvf keepalived-1.2.13.tar -C /opt/
cd /opt/keepalived-1.2.13
./configure --prefix=/usr/local/keeplived
make;makeinstall 或 make && makeinstall
#编译keepalived时报错
++++++++++++++++++++++++++++++++++++++++
configure: error:
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files. !!!
++++++++++++++++++++++++++++++++++++++++
解决方法:
rpm -ivh e2fsprogs-devel-1.39-23.el5.x86_64.rpm
rpm -ivh keyutils-libs-devel-1.2-1.el5.x86_64.rpm
rpm -ivh libsepol-devel-1.15.2-2.el5.x86_64.rpm
rpm -ivh libselinux-devel-1.33.4-5.5.el5.x86_64.rpm
rpm -ivh krb5-devel-1.6.1-36.el5.x86_64.rpm
rpm -ivh zlib-devel-1.2.3-3.x86_64.rpm
rpm -ivh openssl-devel-0.9.8e-12.el5.x86_64.rpm
==============================================
#新建一个配置文件,默认情况下keepalived启动时会去/etc/keepalived
]# mkdir -p /etc/keepalived
]# cp -p /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
编辑配置文件
#Atlas1(1.100)上
~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
yu.jx@yougou.com
}
notification_email_from yu.jx@yougou.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id Atlas_ha
}
vrrp_instance Atlas_ha{
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.254
}
}
virtual_server 192.168.1.254 1234{
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.1.100 1234{
weight 3
notify_down /usr/local/mysql-proxy/bin/Atlas.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 1234
}
}
#Atlas2(1.200)与atlas1几乎一致,如下:
~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
yu.jx@yougou.com
}
notification_email_from yu.jx@yougou.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id Atlas_ha
}
vrrp_instance Atlas_ha{
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.254
}
}
virtual_server 192.168.1.254 1234{
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.1.200 1234{
weight 3
notify_down /usr/local/mysql-proxy/bin/Atlas.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 1234
}
}
}
创建notify_down脚本
~]# cat /usr/local/mysql-proxy/bin/Atlas.sh
#!/bin/sh
pkill keepalived
~]# chmod +x /usr/local/mysql-proxy/bin/Atlas.sh
启动keepalived
~]# /usr/local/keepalived/sbin/keepalived –D
~]#ps aux | grep keepalived
Atlas自动切换测试
使用客户端连接VIP(1.254):1234,假设VIP此时绑定在atlas A节点上
查看atlas A节点上altlas的log可以看到用户在使用当前atlas A连接数据库,关闭此节点上的atlas
发现VIP切换到另外一个节点atlas B,并且在atlas B上查看atlas的log,发现此时客户端已经切换到atlas B上从而实现了Atlas的HA功能
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/27000195/viewspace-1421262/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/27000195/viewspace-1421262/