CentOS利用Keepalived构建双主MySQL+双机热备

前端之家收集整理的这篇文章主要介绍了CentOS利用Keepalived构建双主MySQL+双机热备前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

之前的文章介绍了如何配置MysqL双主互备,见http://www.linuxidc.com/Linux/2013-05/83784.htm

这里介绍如何配合前者实现Keepalived双机热备

系统环境:CentOS6.3 x64

MysqL版本:MysqL-5.6.10

Keepalived版本:keepalived-1.2.7



MysqL-VIP:192.168.7.253

MysqL-master1:192.168.7.201

MysqL-master2:192.168.7.249



1.在MysqL-master1:192.168.7.201服务器上keepalived安装及配置



编译安装,实际以本机kernel版本为准



# wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz

# tar zxvf keepalived-1.2.7.tar.gz

# cd keepalived-1.2.7

# ./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-

279.el6.x86_64

# make && make install



新建一个配置文件,默认keepalived启动会去/etc/keepalived目录下寻找配置文件



# mkdir /etc/keepalived



# vi /etc/keepalived/keepalived.conf

------------------

global_defs {



notification_email {



lzyangel@126.com



}



notification_email_from lzyangel@126.com



smtp_server stmp.126.com



smtp_connect_timeout 30



router_id MysqL-ha



}



vrrp_instance VI_1{



# 两台配置此处均是BACKUP



state BACKUP



# 注意网卡接口



interface eth0



virtual_router_id 51



# 优先级,另一台改为90



priority 100



advert_int 1



# 不主动抢占资源



nopreempt



authentication {



auth_type PASS



auth_pass 1111



}



virtual_ipaddress {



192.168.7.253



}



}







virtual_server 192.168.7.253 3306 {



# 每个2秒检查一次real_server状态



delay_loop 2



# LVS算法



lb_algo wrr



# LVS模式



lb_kind DR



# 会话保持时间



persistence_timeout 60



protocol TCP



real_server 192.168.7.201 3306 {



weight 3



# 检测到服务down后执行的脚本



notify_down /etc/rc.d/keepalived.sh



TCP_CHECK {



# 连接超时时间



connect_timeout 10



# 重连次数



nb_get_retry 3



# 重连间隔时间



delay_before_retry 3



# 健康检查端口



connect_port 3306



}



}

----------------------





编写检测服务down后所要执行的脚本



# vi /etc/rc.d/keepalived.sh



-------------

#!/bin/sh

pkill keepalived

-------------



# chmod +x /etc/rc.d/keepalived.sh



注:此脚本是上面配置文件notify_down选项所用到的,keepalived使用notify_down选项来检查real_server的服务状态。

当发现real_server服务故障时,便触发此脚本.

我们可以看到,脚本就一个命令:

通过pkill keepalived强制杀死keepalived进程,从而实现了MysqL故障自动转移.

另外,我们不用担心两个MysqL会同时提供数据更新操作,因为每台MysqL上的keepalived的配置里面只有本机MysqL的IP+VIP,而不是两台MysqL的IP+VIP.



启动keepalived

# /usr/local/keepalived/sbin/keepalived -D

查看连接状态

# ps -aux | grep keepalived

返回:

---------------



root 25348 0.0 0.0 36792 720 ? Ss 07:15 0:00



/usr/local/keepalived/sbin/keepalived -D

root 25349 0.5 0.1 40968 2016 ? S 07:15 0:00



/usr/local/keepalived/sbin/keepalived -D

root 25350 0.0 0.1 40968 1340 ? S 07:15 0:00



/usr/local/keepalived/sbin/keepalived -D



--------------------

测试



找一台局域网PC,然后去ping MysqL的VIP,这时候MysqL的VIP是可以ping的通的



停止MysqL服务,看keepalived健康检查程序是否会触发我们编写的脚本,去kill掉keepalived进程

# service MysqLd stop

# ps -aux | grep keepalived

无返回结果

2.在MysqL-master2:192.168.7.249服务器上keepalived安装及配置

编译安装,实际以本机kernel版本为准

# wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz

# tar zxvf keepalived-1.2.7.tar.gz

# cd keepalived-1.2.7

# ./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-

279.el6.x86_64

# make && make install

新建一个配置文件,默认keepalived启动会去/etc/keepalived目录下寻找配置文件

# mkdir /etc/keepalived

# vi /etc/keepalived/keepalived.conf

------------------

global_defs {

notification_email {

lzyangel@126.com

}

notification_email_from lzyangel@126.com

smtp_server stmp.126.com

smtp_connect_timeout 30

router_id MysqL-ha

}

vrrp_instance VI_1{

# 两台配置此处均是BACKUP

state BACKUP

# 注意网卡接口

interface eth0

virtual_router_id 51

# 优先级,另一台改为90

priority 90

advert_int 1

# 不主动抢占资源

nopreempt

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.7.253

}

}

virtual_server 192.168.7.253 3306 {

# 每个2秒检查一次real_server状态

delay_loop 2

# LVS算法

lb_algo wrr

# LVS模式

lb_kind DR

# 会话保持时间

persistence_timeout 60

protocol TCP

real_server 192.168.7.249 3306 {

weight 3

# 检测到服务down后执行的脚本

notify_down /etc/rc.d/keepalived.sh

TCP_CHECK {

# 连接超时时间

connect_timeout 10

# 重连次数

nb_get_retry 3

# 重连间隔时间

delay_before_retry 3

# 健康检查端口

connect_port 3306

}

}

----------------------

编写检测服务down后所要执行的脚本

# vi /etc/rc.d/keepalived.sh

-------------

#!/bin/sh

pkill keepalived

-------------

# chmod +x /etc/rc.d/keepalived.sh

启动keepalived

# /usr/local/keepalived/sbin/keepalived -D

# ps -aux | grep keepalive

至此配置完成,网站后台只需要配置MysqL-VIP:192.168.7.253虚拟地址即可

这里实际连接的master数据库,以哪台master先开MysqL服务为准。

当master1:192.168.7.201 MysqL端口挡掉或者服务器异常关闭,keepalived会自动跳转到master2:192.168.7.249.

因为两台数据库的数据时同步的,用户访问的是MysqL-VIP:192.168.7.253虚拟地址,所以网站数据连接会无缝透明转接到master2服务器,实现双机热备+数据同步功能。保证网站数据库的实时可用性。

注:当某一台master服务器挂掉恢复后,需同时打开MysqL服务和keepalived服务,保证另一台服务器如果挂掉会无缝转接。

所以建议MysqL与keeplived设置服务开机启动。

# chkconfig MysqLd on

# vi /etc/rc.local

添加

---------------

/usr/local/keepalived/sbin/keepalived -D

猜你在找的CentOS相关文章