利用keepalived构建高可用MySQL-HA,保证两台MySQL数据的一致性,然后用keepalived实现虚拟IP,通过keepalived自带的服务监控功能来实现MySQL故障时自动切换。
使用MySQL双master+keepalived是一种非常好的解决方案,在MySQL-HA环境中,MySQL互为主从关系,这样就保证了两台MySQL数据的一致性,然后用keepalived实现虚拟IP,通过keepalived自带的服务监控功能来实现MySQL故障时自动切换。
下面,我把即将上线的一个生产环境中的架构与大家分享一下,看一下这个架构中,MySQL-HA是如何实现的,环境拓扑如下
一、安装keepalived
(1)源码编译安装
wget http://www.keepalived.org/software/keepalived-1.1.17.tar.gz
uname -r (查看内核版本,下面的要和查看到的内核版本一样)
ln -s/usr/src/kernels/2.6.32-358.23.2.el6.x86_64/ /usr/src/linux (如果/usr/src/kernels下面没有内容,可使用yum-y install kernel-devel安装即可)
2)安装依赖包
yum -y install openssl-devel librarie* popt-devel
3)安装主要软件
tar -zxvf ipvsadm-1.24.tar.gz
cd ipvsadm-1.24
make && make install
cd ..
tar -zxvf keepalived-1.1.17.tar.gz
cd keepalived-1.1.17
./configure
make && make install
编译的时候出现这个提示,说明keepalived和内核结合了,如果不是这样的,需要加上这个参数./configure --with-kernel-
dir=/kernel/path
Keepalived configuration
------------------------
Keepalived version : 1.1.17
Compiler : gcc
Compiler flags : -g -O2
Extra Lib : -lpopt -lssl -lcrypto
UseIPVS Framework : Yes
IPVS sync daemon support : Yes
Use VRRP Framework : Yes
Use LinkWatch : No
Use Debug flags : No
(2)yum配置安装
yum -y install ipvsadm
yum -y install keepalived
进行安装即可,用yum安装完直接进行修改配置文件即可
keepalived-master配置
vi /etc/keepalived/keepalived.conf
!Configuration File for keepalived global_defs { router_id LVS_DEVEL } # VIP1 vrrp_instance VI_1 { state MASTER interface eth0 lvs_sync_daemon_inteface eth0 virtual_router_id 60 priority 100 advert_int 5 authentication { auth_type PASS auth_pass 1122 } virtual_ipaddress { 192.168.40.170 } } virtual_server 192.168.40.170 3306 { delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 60 protocol TCP real_server 192.168.40.131 3306 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } eal_server 192.168.40.132 3306 { # weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } }
keepalived-backup配置
!Configuration File for keepalived global_defs { router_id LVS_DEVEL } # VIP1 vrrp_instance VI_1 { state BACKUP interface eth0 lvs_sync_daemon_inteface eth0 virtual_router_id 60 priority 90 advert_int 5 authentication { auth_type PASS auth_pass 1122 } virtual_ipaddress { 192.168.40.170 } } virtual_server 192.168.40.170 3306 { delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 60 protocol TCP real_server 192.168.40.131 3306 { # weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } real_server 192.168.40.132 3306 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } }
二、mysql主从配置(可以参考我的博客)
# 主服务器上
[mysqld]
server-id = 10
log-bin = mysql-bin
relay-log = relay-mysql
relay-log-index = relay-mysql.index
auto-increment-increment = 2
auto-increment-offset = 1
# 从服务器上
[mysqld]
server-id = 20
log-bin = mysql-bin
relay-log = relay-mysql
relay-log-index = relay-mysql.index
auto-increment-increment = 2
auto-increment-offset = 2
3、如果此时两台服务器均为新建立,且无其它写入操作,各服务器只需记录当前自己二进制日志文件及事件位置,以之作为另外的服务器复制起始位置即可
server1|mysql> SHOW MASTER STATUS\G
************************** 1. row ***************************
File: mysql-bin.000001
Position: 710
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
server2|mysql> SHOW MASTER STATUS\G
mysql> SHOW MASTER STATUS\G
*************************** 1. row ***************************
File: mysql-bin.000003
Position: 811
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
4、各服务器接下来指定对另一台服务器为自己的主服务器即可:
server1|mysql> CHANGE MASTER TO ...,MASTER_LOG_FILE=‘mysql-bin.000003‘, MASTER_LOG_POS=811
server2|mysql> CHANGE MASTER TO ...,MASTER_LOG_FILE=‘mysql-bin.000001‘, MASTER_LOG_POS=710
测试myql集群故障
用vip 192.168.40.170 链接
停止启动一台mysql数据库测试
测试mysql 连接:
查看ipvsadm -L -n
以上就是k+M 高可用。结构需要用脚本调试。