在上一篇已经通过mysql-mmm实现对mysql数据库的动态监控,这一篇我们将结合amoeba实现对数据库访问的读写分离,通过keepalived来实现amoeba的高可用[keepalived用于监控主机心跳,软件是否宕掉无法监测,我们默认如果出问题就是amoeba的主机崩溃]
一、本次环境拓扑图如下:
二、环境描述
系统:red hat linux 6(2.6.32)
数据库:mysql 5.1.61(这里直接使用rpm包安装,也不使用最新版本数据库)
MMM:mysql-mmm 2.2.21
DB server地址分配:
192.168.1.100(master)
192.168.1.101(master)
192.168.1.102(slave)
192.168.1.103(slave)
MMM地址:
192.168.1.104(monitor)
虚拟IP地址(vip):
192.168.1.12(write)
192.168.1.13(read)
192.168.1.14(read)
192.168.1.15(read)
amoeba1:192.168.1.112
amoeba1:192.168.1.113
amoeba vip:192.168.1.17(对外的ip地址)
我们这里的配置是基于上一篇的环境,也就是说mysql-mmm和mysql的主从复制都配置好了,下面主要描述amoeba和keepalived
三、配置演示:
Keepalived简介:Keepalived是Linux下面实现VRRP 备份路由的高可靠性运行件,基于Keepalived设计的服务模式能够真正做到主服务器和备份服务器故障时IP瞬间无缝交接,从而提高系统的可用性
1.将需要的包传送到服务器并解压[我这里默认安装包已经上传到服务器家目录]
- # 在192.168.1.112/192.168.1.113上分别解压如下几个包
- [root@client112 ~]# tar -xf keepalived-1.2.9.tar.gz
- [root@client112 ~]# tar -xf jdk-7u15-linux-x64.tar.gz -C /usr/local/
- # 在192.168.1.113
- [root@client113 ~]# tar -xf keepalived-1.2.9.tar.gz
- [root@client113 ~]# tar -xf jdk-7u15-linux-x64.tar.gz -C /usr/local/
2.分别到192.168.1.112/192.168.1.113上配置java环境[这里只演示在112上的操作,113上重复此操作即可]
- [root@client112 ~]# cd /usr/local/
- [root@client112 local]# mkdir java
- # 将解压出来的内容移到java目录
- [root@client112 local]# mv jdk1.7.0_15/* java
- # 设置java环境变量
- [root@client112 local]# echo $JAVA_HOME
- [root@client112 local]# vim /etc/profile
- # 在末尾添加
- export JAVA_HOME=/usr/local/java
- export AMOEBA_HOME=/usr/local/amoeba
- export PATH=$PATH:$AMOEBA_HOME/bin:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
- # 使变量生效
- [root@centos local]# source /etc/profile
3.分别到192.168.1.112/192.168.1.113上配置amoeba[这里只演示在112上的操作,113上重复此操作即可]
(1).配置amoeba中的dbServer.xml(后端mysql 服务器连接配置)
- # 建立相应amoeba目录
- [root@client112 local]# mkdir /usr/local/amoeba
- # 将压缩包解压到指定目录
- [root@client112 local]# tar -xf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba
- [root@client112 local]# cd /usr/local/amoeba/
- [root@client112 local]# cd conf/
- # 配置后端mysql 服务器连接[dbServer.xml]
- [root@client112 conf]# vim dbServers.xml
- .........................(省略)
- <dbServer name="abstractServer" abstractive="true">
- <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">
- <property name="manager">${defaultManager}</property>
- <property name="sendBufferSize">64</property>
- <property name="receiveBufferSize">128</property>
- # 设置 mysql 数据库的端口
- <!-- mysql port -->
- <property name="port">3306</property>
- # 设置缺省的数据库,当连接amoeba时,操作表必须显式的指定数据库名,即采用dbname.tablename的方式,
- # 不支持 use dbname指定缺省库,因为操作会调度到各个后端dbserver
- <!-- mysql schema -->
- <property name="schema">kongzhong</property>
- # 设置amoeba连接后端数据库服务器的账号和密码,需在后端数据库器上创建该用户,并授权amoeba连接
- # 此处需要特别注意:**** 密码默认是注释掉,需要去掉注释!!!
- <!-- mysql user -->
- <property name="user">kongzhong</property>
- <!-- mysql password -->
- <property name="password">kongzhong</property>
- </factoryConfig>
- <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool">
- # 最大连接数[默认]
- <property name="maxActive">500</property>
- # 最大空闲连接数[默认]
- <property name="maxIdle">500</property>
- <property name="minIdle">10</property>
- <property name="minEvictableIdleTimeMillis">600000</property>
- <property name="timeBetweenEvictionRunsMillis">600000</property>
- <property name="testOnBorrow">true</property>
- <property name="testOnReturn">true</property>
- <property name="testWhileIdle">true</property>
- </poolConfig>
- </dbServer>
- # 设置一个后端的dbServer,名为master ,这个可以随便取,但是为了明确其含义,最好给予特殊含义的单词
- # 下面指定的数据库IP地址,为mmm提供给对外访问的虚拟IP地址
- <dbServer name="master" parent="abstractServer">
- <factoryConfig>
- <!-- mysql ip -->
- <property name="ipAddress">192.168.1.12</property>
- </factoryConfig>
- </dbServer>
- <dbServer name="slave1" parent="abstractServer">
- <factoryConfig>
- <!-- mysql ip -->
- <property name="ipAddress">192.168.1.13</property>
- </factoryConfig>
- </dbServer>
- <dbServer name="slave2" parent="abstractServer">
- <factoryConfig>
- <!-- mysql ip -->
- <property name="ipAddress">192.168.1.14</property>
- </factoryConfig>
- </dbServer>
- <dbServer name="slave3" parent="abstractServer">
- <factoryConfig>
- <!-- mysql ip -->
- <property name="ipAddress">192.168.1.15</property>
- </factoryConfig>
- </dbServer>
- <dbServer name="slave4" parent="abstractServer">
- <factoryConfig>
- <!-- mysql ip -->
- <property name="ipAddress">192.168.1.16</property>
- </factoryConfig>
- </dbServer>
- # 指定一个虚拟的dbServer,将上面定义的dbserver加入这个虚拟的dbserver,相当于组成一个组[这里我们将读的数据库组成一个组]
- # 这里 需要将 name="mul..." 改成自己想要取的名字,这个名字也需要有含义,后面会用到
- <dbServer name="virtualslave" virtual="true">
- <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
- <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
- # 选择调度算法 1 是轮询 2 是权重 3 是HA 这里选择1 轮询
- <property name="loadbalance">1</property>
- <!-- Separated by commas,such as: server1,server2,server1 -->
- # 负载均衡,slave1,slave2当成2个服务器进行调度,这模拟量加权的调度算法。
- # 注意这里使用的dbserver必须是已经定义了的,可以写多个,如slave1,slave2
- <property name="poolNames">slave1,slave2,slave3,slave4</property>
- </poolConfig>
- </dbServer>
- </amoeba:dbServers>
(2).配置 Amoeba 监听端口[amoeba.xml]
- [root@client112 conf]# vim amoeba.xml
- .........................(省略)
- <proxy>
- <!-- service class must implements com.meidusa.amoeba.service.Service -->
- <service name="Amoeba for Mysql" class="com.meidusa.amoeba.net.ServerableConnectionManager">
- <!-- port -->
- # 设置amoeba监听的端口(这里如果默认,后面测试需要指定端口,就是这里的端口)
- <property name="port">3306</property>
- <!-- bind ipAddress -->
- <!--
- # 设置监听的接口,如果不设置,则监听所有的IP[选择默认]
- <property name="ipAddress">127.0.0.1</property>
- -->
- .........................(省略)
- <property name="authenticator">
- <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">
- # 提供客户端连接amoeba时需要使用这里设定的账号 (这里的账号密码和amoeba连接后端数据库服务器的密码无关)
- <property name="user">kongzhong123</property>
- # 提供客户端连接amoeba时需要使用这里设定的密码
- <property name="password">kongzhong123</property>
- <property name="filter">
- <bean class="com.meidusa.amoeba.server.IPAccessController">
- <property name="ipFile">${amoeba.home}/conf/access_list.conf</property>
- </bean>
- </property>
- </bean>
- </property>
- </service>
- .........................(省略)
- <queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">
- <property name="ruleLoader">
- <bean class="com.meidusa.amoeba.route.TableRuleFileLoader">
- <property name="ruleFile">${amoeba.home}/conf/rule.xml</property>
- <property name="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property>
- </bean>
- </property>
- <property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property>
- <property name="LRUMapSize">1500</property>
- <property name="defaultPool">master</property>
- <!-- -->
- # 把默认注释掉的读写分离选项,把注释去掉并readpool修改成virtualslave(这个名字,我们前面在dbServer.xml里设置一个读数据库组,这里是作为只读池)
- <property name="writePool">master</property>
- <property name="readPool">virtualslave</property>
- <property name="needParse">true</property>
- </queryRouter>
(3).配置amoeba及JAVA的环境变量[这个上面配置java时配置过,如果没有配置参照下面]
- [root@client112 local]# vim /etc/profile
- # 在末尾添加
- export JAVA_HOME=/usr/local/java
- export AMOEBA_HOME=/usr/local/amoeba
- export PATH=$PATH:$AMOEBA_HOME/bin:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
- # 使变量生效
- [root@centos local]# source /etc/profile
(4)登陆数据库授权相应用户权限
- # 登陆任意一个主master,授权amoeba用到登陆用户(kongzhong需要授权,kongzhong123用于给前端登陆的不需要授权)
- mysql> create database kongzhong;
- mysql> grant all privileges on kongzhong.* to 'kongzhong'@'192.168.1.%' identified by 'kongzhong';
- mysql> flush privileges;
(5).启动amoeba测试
- # 如果确认能够正常启动,就加>>/dev/null 否则还是看一下提示信息
- [root@centos conf]# amoeba start & >>/dev/null
- # 检测启动情况
- [root@centos conf]# netstat -tulnp |grep java
- # 停止amoeba测试指令是否可用
- [root@centos conf]# amoeba stop
(6).利用amoeba登陆测试[任何一台具有mysql客户端,出现以下提示即为正常]
- [root@client100 ~]# mysql -h192.168.1.112 -ukongzhong123 -pkongzhong123
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 1201340161
- Server version: 5.1.45-mysql-amoeba-proxy-2.2.0 Source distribution
- 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> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | kongzhong |
- | test |
- +--------------------+
(7).在192.168.1.113配置amoeba,这里就不再延迟,重复上面配置amoeba的操作
4.安装和配置keepalived
- # keepalived需要openssl依赖包
- [root@client112 ~]# yum -y install openssl* gcc make
- # 建立keepalived安装目录
- [root@client112 ~]# mkdir /usr/local/keepalived
- [root@client112 ~]# cd keepalived-1.2.9
- [root@client112 keepalived-1.2.9]# ./configure --prefix=/usr/local/keepalived
- [root@client112 keepalived-1.2.9]# make
- [root@client112 keepalived-1.2.9]# make install
- # 复制相应文件到指定目录
- [root@client112 keepalived-1.2.9]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
- [root@client112 keepalived-1.2.9]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
- [root@client112 keepalived-1.2.9]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
- [root@client112 keepalived-1.2.9]# mkdir /etc/keepalived
- [root@client112 keepalived-1.2.9]# cd /etc/keepalived/
- # 以上的关于keepalived操作,请重复在192.168.1.113操作一遍
- # 以下操作,根据提示操作
- # 编辑配置文件[192.168.1.112上的配置文件]
- [root@client112 keepalived]# vim keepalived.conf
- bal_defs {
- notification_email
- {
- # 设置报警邮件地址,每行一个
- # 需开启本机sendmail服务[这里可以暂时忽略]
- cloud_data@163.com
- }
- #设置邮件发送地址
- notification_email_from cloud_data@163.com
- #设置smtp server 地址
- smtp_server 127.0.0.1
- #设置连接 smtp server超时时间
- smtp_connect_timeout 30
- #运行Keepalived服务器的一个标志,邮件主题中显示
- router_id client112
- }
- # vrrp实例定义部分
- vrrp_instance VI_1 {
- # 指定keepalived的角色,MASTER表示此主机是主服务器
- # BACKUP表示是备用服务器
- state MASTER
- # 指定HA监测网络的接口
- interface eth0
- # 虚拟路由标志,同一个vrrp实例使用一个唯一标志
- # 即同一个vrrp_instance下,MASTER和BACKUP一致
- virtual_router_id 51
- # 优先级,数字越大优先级越高,MASTER必须大于BACKUP
- priority 100
- # MASTER和BACKUP同步检查间隔,秒
- advert_int 1
- smtp_alert
- # 设定验证类型和密码
- authentication {
- # 验证类型,PASS和HA
- auth_type PASS
- # 验证密码,MASTER和BACKUP密码相同才能进行通信
- auth_pass kongzhong
- }
- # 虚拟ip地址
- virtual_ipaddress
- {
- 192.168.1.17
- }
- }
- # 编辑配置文件[192.168.1.113上的配置文件]
- [root@client113 keepalived]# vim keepalived.conf
- bal_defs {
- notification_email
- {
- cloud_data@163.com
- }
- notification_email_from cloud_data@163.com
- smtp_server 127.0.0.1
- smtp_connect_timeout 30
- router_id client113
- }
- vrrp_instance VI_1 {
- # 这里和192.168.1.112上不同,修改为BACKUP
- state BACKUP
- interface eth0
- virtual_router_id 51
- # 这里和192.168.1.112上不同,修改为80
- priority 80
- advert_int 1
- smtp_alert
- authentication {
- # 这里需要和192.168.1.112上的一样,不然,无法切换的
- auth_type PASS
- auth_pass kongzhong
- }
- virtual_ipaddress
- {
- 192.168.1.17
- }
- }
- # 分别在192.168.1.112和192.168.1.113上启动keepalived
- [root@client112 keepalived]# /etc/init.d/keepalived start
- Starting keepalived: [ OK ]
- # 检查虚拟ip是否绑定网卡[这里ip默认会在优先级比较高的机器上,就是刚才设置的100和80]
- [root@client112 keepalived]# ip add | grep 192.168.1.17
- inet 192.168.1.17/32 scope global eth0
- # 192.168.1.113上启动keepalived
- [root@client113 keepalived]# /etc/init.d/keepalived start
- Starting keepalived: [ OK ]
- # 113上如果需要看到ip需要关闭keepalived,也就是模拟112宕机
- [root@client113 keepalived]# ip add | grep 192.168.1.17
- # 停掉192.168.1.112的keepalived,看看虚拟IP192.168.1.17会不会自动切换到192.168.1.113上
5.现在就可以测试了:
简单测试:
(1).使用amoeba的真实ip登陆
- [root@client100 ~]# mysql -h192.168.1.112 -ukongzhong123 -pkongzhong123
- [root@client100 ~]# mysql -h192.168.1.113 -ukongzhong123 -pkongzhong123
(2).使用keepalived提供的虚拟ip访问
- [root@client100 ~]# mysql -h192.168.1.17 -ukongzhong123 -pkongzhong123
(3).模拟宕掉一台amoeba[即关闭keepalived],看还能否使用keepalived提供的虚拟ip访问
- # 这时虚拟ip应该切换到另外一台好的amoeba上
- [root@client100 ~]# mysql -h192.168.1.17 -ukongzhong123 -pkongzhong123