mysql keepalived 双机热备,keepalived+Nginx+mysql做双机热备

Nginx+mysql的安装网上多的是,mysql互备等,这里就不写了。

nginx1:192.168.6.155

nginx2:192.168.2.156

最新的1.2.3安装过程有错误 找不到解决方法 就下了个1.2.2的

1.安装配置Keepalived下载所需要的软件

wget http://keepalived.org/software/keepalived-1.2.2.tar.gz

wget http://rpm5.org/files/popt/popt-1.16.tar.gz

2.安装popt

编译keepalived时需要popt,否则会报以下错误:configure: error: Popt libraries is required

tar -zxvf popt-1.16.tar.gz

cd popt-1.16

./configure

make

make install

3.安装keepalived

tar -zxvf keepalived-1.2.2.tar.gz

cd keepalived-1.2.2

./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-194.8.1.el5-i686/

make

make install

#cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

#cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

#cp /usr/local/sbin/keepalived /usr/sbin/

4.修改配置文件为以下内容:

vim /usr/local/keepalived/etc/keepalived/keepalived.conf

主站服务器

! Configuration File for keepalived

global_defs {

router_id LVS_DEVEL

}

vrrp_script Monitor_Nginx {

script "/root/shell/monitor_nginx.sh" #根据自己的实际路径放置monitor_nginx.sh    也可添加多一个check_mysql.sh检测mysql

interval 2

weight 2

}

vrrp_instance VI_1 {

state MASTER

interface eth0

virtual_router_id 51

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1234

}

track_script {

Monitor_Nginx

}

virtual_ipaddress {

192.168.6.133

}

}

注:备机的keepalived的安装和上面一样,只要把配置文件改为以下(把MASTER改为BACKUP)

! Configuration File for keepalived

global_defs {

router_id LVS_DEVEL

}

vrrp_script Monitor_Nginx {

script "/root/scripts/monitor_nginx.sh"

interval 2

weight 2

}

vrrp_instance VI_1 {

state BACKUP            #改为BACKUP

interface eth0

virtual_router_id 51

priority 30            #比MASTER数值要低

advert_int 1

authentication {

auth_type PASS

auth_pass 1234

}

track_script {

Monitor_Nginx

}

virtual_ipaddress {

192.168.6.133

}

}

注:monitor_nginx.sh为监控nginx进程的脚本,内容如下

#!/bin/bash

if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]

then

/usr/local/nginx/sbin/nginx

sleep 5

if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]

then

killall keepalived

fi

fi

mysql状态检测脚本/root/shell/keepalived_check_mysql.sh

vim  /root/shell/keepalived_check_mysql.sh

#!/bin/bash

MYSQL=/usr/local/mysql/bin/mysql

MYSQL_HOST=localhost

MYSQL_USER=root

MYSQL_PASSWORD=9ai9.net

CHECK_TIME=3

#mysql  is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0

MYSQL_OK=1

function check_mysql_helth (){

$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "show status;" >/dev/null 2>&1

if [ $? = 0 ] ;then

MYSQL_OK=1

else

MYSQL_OK=0

fi

return $MYSQL_OK

}

while [ $CHECK_TIME -ne 0 ]

do

let "CHECK_TIME -= 1"

check_mysql_helth

if [ $MYSQL_OK = 1 ] ; then

CHECK_TIME=0

exit 0

fi

if [ $MYSQL_OK -eq 0 ] &&  [ $CHECK_TIME -eq 0 ]

then

/etc/init.d/keepalived stop

exit 1

fi

sleep 1

done

赋予执行权限

chmod 755 /root/shell/keepalived_check_mysql.sh

monitor_nginx.sh也一样需要赋予执行权限

5.启动keepalived

/usr/local/keepalived/sbin/keepalived -D -f /usr/local/keepalived/etc/keepalived/keepalived.conf

或者用service keepalived start来启动 service keepalived status来查看状态

用这种 /etc/rc.d/init.d/keepalived start方式来启动有问题 不知道为什么

测试步骤

1.    访问VIP看是否能够正常访问后端的tomcat

2.    停止其中一个tomcat看是否能将访问转到另一台上

3.    停止两台nginx上任何一个nginx进程看监控进程脚本是否会自动启动nginx

4.    停止任何一台nginx上的keepalived进程看另一台是否接管vip

比如停止Master上的keepalived,例如如下killall keepalived,查看BACKUP机器是否已经接管,如果BACKUP接管后,BACKUP机器日志会是出下情况

eth0: mtu 1500 qdisc pfifo_fast qlen 1000

link/ether 00:0c:29:03:9f:88 brd ff:ff:ff:ff:ff:ff

inet 192.168.6.155/24 brd 192.168.6.255 scope global eth0

inet 192.168.6.133/32 scope global eth0    inet6 fe80::20c:29ff:fe03:9f88/64 scope link

valid_lft forever preferred_lft forever

注意:

vrrp协议 使用224.0.0.18地址组播 需要添加到防火墙 以为centos为例:

-A RH-Firewall-1-INPUT -d 224.0.0.18 -j ACCEPT

(责任编辑:ken)

你可能感兴趣的:(mysql,keepalived,双机热备)