使用keepalived给mycat实现高可用
机器准备:
mysql机器:192.168.186.162 192.168.186.163
mycat: 192.168.186.161 192.168.186.160
client: 192.168.186.123
实验拓扑图:
(为了方便实验,以下全部使用yum安装)
关闭所有机器的防火墙和selinux
部署MySQL:192.168.186.162 192.168.186.163
yum -y install mariadb-server mariadb
systemctl start mariadb
mysql -uroot -p # (默认没有密码,回车直接登录)
#以下是数据库内的操作
grant all on *.* to 'mycat'@'192.168.186.161' identified by 'mycat';
grant all on *.* to 'mycat'@'192.168.186.160' identified by 'mycat';
create database shared;
use shared;
create table slaver( id int );
insert into slaver values (162); #在写的机器插入162,在读的机器插入163,待会可以验证mycat读写分离是否正常
insert into slaver values (163);
【(为了检测实验结果,这里就不配置主从了)】
部署mycat: 192.168.186.161 192.168.186.160
1.安装java环境
# tar xf jdk-8u181-linux-x64.tar.gz -C /usr/local/ # ln -s /usr/local/jdk1.8.0_181/ /usr/local/java# vim /etc/profile.d/java.shexport JAVA_HOME=/usr/local/javaexport PATH=$JAVA_HOME/bin:$PATH# . /etc/profile.d/java.sh# java -versionjava version "1.8.0_181"Java(TM) SE Runtime Environment (build 1.8.0_181-b13)Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
2.安装mycat
# tar xf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/
3.配置server.xml
#vim server.xml...<user name="mycat"> <property name="password">mycat</property><property name="schemas">TESTDB</property> </user>...<user name="user"> <property name="password">user</property><property name="schemas">TESTDB</property><property name="readOnly">true</property></user>...
4.配置schema.xml, 这个文件用来配置分库分表及读写分离.
#vim schema.xml
echo wrapper.startup.timeout=300 >> /usr/local/mycat/conf/wrapper.conf
5.启动mycat服务
#/usr/local/mycat/bin/mycat start &
6.验证
连接工作端口,client端连接:
mysql -umycat -pmycat -h192.168.186.161 -P8066
#操作数据库
use TESTDB;
select * from slaver; (多次执行只能看见163)
insert into slaver values (111); (回到192.168.186.162的数据库可以看到111,但是192.168.186.163上没有111)
【达到上述效果则说明mycat配置成功】
部署keepalived: 192.168.186.161 192.168.186.160
yum -y install keepalived
vim /etc/keepalived/keepalived.conf
161配置如下:
! Configuration File for keepalivedglobal_defs {
notification_email {
acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.200.1smtp_connect_timeout 30router_id LVS_DEVELvrrp_skip_check_adv_addrvrrp_strictvrrp_garp_interval 0vrrp_gna_interval 0
}vrrp_script mycat {
script "/usr/bin/netstat -ntlp|grep 8066"interval 3weight -30fall 2rise 1
}
vrrp_instance VI_1 {
state MASTERinterface ens33virtual_router_id 51priority 100advert_int 1authentication {
auth_type PASSauth_pass 1111}virtual_ipaddress {
192.168.186.100}track_script {
mycat}
}
160配置如下:
! Configuration File for keepalivedglobal_defs {
notification_email {
acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.200.1smtp_connect_timeout 30router_id LVS_DEVELvrrp_skip_check_adv_addrvrrp_strictvrrp_garp_interval 0vrrp_gna_interval 0
}vrrp_instance VI_1 {
state BACKUPinterface ens33virtual_router_id 51priority 100advert_int 1authentication {
auth_type PASSauth_pass 1111}virtual_ipaddress {
192.168.186.100}
}
systemctl start keepalived
iptables -t filter -D INPUT 1【哪台机器获取VIP,iptables会自动添加防火墙,所以要删掉,否则无法连接】
测试:
开启服务之后,VIP在192.168.186.161
client端连接:
mysql -umycat -pmycat -h192.168.186.100 -P8066
连接成功证明keepalivd部署成功
测试VIP是否跳转
在161的机器上:
pkill java
到160查看
ip a
看到有192.168.186.100
client端连接:
mysql -umycat -pmycat -h192.168.186.100 -P8066
连接成功,证明keepalivd+mycat部署成功,实验到此结束!