Keepalive+Atlas+DRBD+Heartbeat+mysql配置系列(三):keepalive+atlas实现mysql读写分离

post by rocdk890 / 2019-3-30 15:00 Saturday linux技术
系统:centos 7(64位)
环境:
主atlas ip: 10.0.2.3
备atlas ip: 10.0.2.4
keepalive vip: 10.0.2.2
mysql vip: 10.0.2.10

1.安装atlas
wget --no-check-certificate https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm
rpm -ivh Atlas-2.2.1.el6.x86_64.rpm

2.配置atlas(主备都一样的配置)
先在mysql主从里都添加个账atlas的账号:
GRANT ALL PRIVILEGES ON *.* TO 'atlas'@'%' IDENTIFIED BY '123456';
flush privileges;


给atlas账号的密码加密:
/usr/local/mysql-proxy/bin/encrypt 123456
RePBqJ+5gI4=

vim /usr/local/mysql-proxy/conf/test.cnf
[mysql-proxy]

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

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

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

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

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

#用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码!
#pwds = user1:+jKsgB3YAG8=, user2:GS+tr4TPgqc=
pwds=  atlas:RePBqJ+5gI4=, root:TWbz0dlu35U=

#设置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 = ON

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

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

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

#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


3.给atlas创建启动脚本
vim /etc/init.d/atlas
#!/bin/sh
#
#atlas:    Atlas Daemon
#
# chkconfig:    - 90 25
# description:  Atlas Daemon
#
# Source function library.
start()
{
        echo -n $"Starting atlas: "
        /usr/local/mysql-proxy/bin/mysql-proxyd test start
        echo
}
stop()
{
        echo -n $"Shutting down atlas: "
        /usr/local/mysql-proxy/bin/mysql-proxyd test stop
        echo
}
ATLAS="/usr/local/mysql-proxy/bin/mysql-proxyd"
[ -f $ATLAS ] || exit 1
# See how we were called.
case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        restart)
                stop
                sleep 2
                start
                ;;
        *)
                echo $"Usage: $0 {start|stop|restart}"
                exit 1
esac
exit 0

保存后执行:
chmod +x /etc/init.d/atlas
chkconfig atlas on

4.启动主备atlas
service atlas start

5.测试atlas
mysql -h 10.0.2.3 -P 2345 -u admin -p

mysql> SELECT * FROM backends;
+-------------+----------------+-------+------+
| backend_ndx | address        | state | type |
+-------------+----------------+-------+------+
|           1 | 10.0.2.10:3306 | up    | rw   |
|           2 | 10.0.2.8:3306  | up    | ro   |
|           3 | 10.0.2.9:3306  | up    | ro   |
+-------------+----------------+-------+------+
3 rows in set (0.00 sec)

下面模拟程序连接atlas:
mysql -h 10.0.2.3 -P 3306 -u root -p

mysql> SHOW VARIABLES LIKE 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 2     |
+---------------+-------+
1 row in set (0.00 sec)

mysql> SHOW VARIABLES LIKE 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 3     |
+---------------+-------+
1 row in set (0.00 sec)

mysql> SHOW VARIABLES LIKE 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 2     |
+---------------+-------+
1 row in set (0.00 sec)

从上面的结果可以看到mysql读写分离成功.

6.安装Keepalived实现Atlas高可用 (atlas主备都要安装)
yum -y install keepalived

7.配置
atlas主(10.0.2.3)上keepalive的配置:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
	router_id LVS_DEVEL
}

vrrp_script Monitor_Atlas {
	script "/etc/keepalived/monitor_atlas.sh"
	interval 2
	weight 2
}

vrrp_instance VI_1 {
	state MASTER
	interface eth0
	virtual_router_id 51
		mcast_src_ip 10.0.2.3
	priority 100
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
    }
    virtual_ipaddress {
    	10.0.2.2
    }
}

atlas备(10.0.2.4)上keepalive的配置:
! Configuration File for keepalived
global_defs {
	router_id LVS_DEVEL
}

vrrp_script Monitor_Atlas {
	script "/etc/keepalived/monitor_atlas.sh"
	interval 2
	weight 2
}

vrrp_instance VI_1 {
	state BACKUP
	interface eth0
	virtual_router_id 51
		mcast_src_ip 10.0.2.4
	priority 95
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
    }
    virtual_ipaddress {
    	10.0.2.2
    }
}

cat /etc/keepalived/monitor_atlas.sh
#!/bin/bash
if  [ $(ps -ef |grep 'mysql-proxy' |grep -v 'grep' |wc -l) -eq "0" ];then
	/etc/init.d/atlas start
    sleep 5
    if [ $(ps -ef |grep 'mysql-proxy' |grep -v 'grep' |wc -l) -eq "0"  ];then
	systemctl stop keepalived
	systemctl restart network
    fi
fi

8.修改/etc/sysconfig/keepalived
vi /etc/sysconfig/keepalived
# Options for keepalived. See `keepalived --help' output and keepalived(8) and
# keepalived.conf(5) man pages for a list of all options. Here are the most
# common ones :
#
# --vrrp               -P    Only run with VRRP subsystem.
# --check              -C    Only run with Health-checker subsystem.
# --dont-release-vrrp  -V    Dont remove VRRP VIPs & VROUTEs on daemon stop.
# --dont-release-ipvs  -I    Dont remove IPVS topology on daemon stop.
# --dump-conf          -d    Dump the configuration data.
# --log-detail         -D    Detailed log messages.
# --log-facility       -S    0-7 Set local syslog facility (default=LOG_DAEMON)
#
 
#KEEPALIVED_OPTIONS="-D"
KEEPALIVED_OPTIONS="-D -d -S 0"               ##在最后添加此行

9.修改/etc/rsyslog.conf
vi /etc/rsyslog.conf
##在最后添加此行:
local0.*                                                /var/log/keepalived.log

systemctl restart rsyslog

10.验证
这里就只说说怎么弄,具体的可以看你们自己选择:
1)可以把主的atlas给停了,看看keepalive是否能切换.
2)当然也可以只停keepalive,来验证是否可以连接atlas.
夜空- 本站版权
1、本站所有主题由该文章作者发表,该文章作者与夜空享有文章相关版权
2、其他单位或个人使用、转载或引用本文时必须同时征得该文章作者和夜空的同意
3、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
5、原文链接:blog.slogra.com/post-758.html

标签: centos mysql 安装 centos7 keepalive atlas 读写分离