实验目的:

   通过Keepalive来构建Mysql的高可用。Mysql已做主从,当一台Mysql宕机的时候不影响对Mysql的调用。

实验环境:

  两台Mysql服务器(基于LAMP且已做主从),两台Mysql服务器都通过Apache发布Discuz论坛(discuz调用的是主mysql地址)

身份            主机名      IP地址

Mysql           master      192.168.1.3

Myslq           slave        192.168.1.4

LAMP平台请参见

Mysql主从参见

实验步骤:

一、在主mysql上安装配置keepalive

[root@master ~]# bash keepalive_install.sh

脚本内容如下:

[root@master ~]# cat keepalive_install.sh#!/bin/bashyum -y  install kernel-devel openssl-devel  popt-devel &&wgethttp://www.keepalived.org/software/keepalived-1.1.15.tar.gz &&tar -zxf keepalived-1.1.15.tar.gz &&cd keepalived-1.1.15 &&./configure--with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/ &&make && make install &&DIR=/usr/local/cp $DIR/etc/rc.d/init.d/keepalived  /etc/rc.d/init.d/  &&cp $DIR/etc/sysconfig/keepalived /etc/sysconfig/&& mkdir -p /etc/keepalived &&cp $DIR/sbin/keepalived/usr/sbin/&&cp/usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ &&/etc/init.d/keepalived restartif [ $? -eq 0 ];then        echo -e '\e[32mInstall Keepalive Successfully!\e[0m'else        echo -e '\e[31mInstall Keepalive Failure!\e[0m'fi

加载ip_vs的内核模块

[root@master ~]# modprobe ip_vs[root@master~]# modprobe ip_vs_wrr[root@master~]# modprobe ip_vs_rr

修改keepalived.conf配置文件如下:

[root@master ~]# vim /etc/keepalived/keepalived.conf[root@master ~]# cat/etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs {  notification_email {         654001593@qq.com   }  notification_email_from root@localhost.com  smtp_server 127.0.0.1  smtp_connect_timeout 30  router_id LVS_DEVEL} vrrp_instance VI_1 {   state MASTER   interface eth0   virtual_router_id 51   priority 100   advert_int 1   authentication {       auth_type PASS       auth_pass 1111    }   virtual_ipaddress {         192.168.1.77    }} virtual_server 192.168.1.77 3306 {   delay_loop 6   lb_algo wrr   lb_kind DR   persistence_timeout 50   protocol TCP    real_server 192.168.1.3 3306 {       weight 1         notify_down/data/sh/ keepalive_stop.sh       TCP_CHECK {           connect_timeout 3           nb_get_retry 3           delay_before_retry 3             connect_port 3306       }    }}
[root@master ~]# mkdir -p /data/sh[root@master ~]# vim/data/sh/keepalive_stop.sh[root@master ~]# chmod +x /data/sh/keepalive_stop.sh[root@master ~]# /etc/init.d/keepalivedreloadReloading keepalived:                                      [  OK  ][root@master ~]# cat/data/sh/keepalive_stop.sh#!/bin/bash/etc/init.d/keepalived stop[root@master ~]#

二、在从mysql上安装配置keepalive

[root@slave ~]# bash keepalive_install.sh

脚本内容如下,同master上执行的脚本一致(非严格意义上的脚本,安装命令放一起而已)

[root@slave ~]# cat keepalive_install.sh#!/bin/bashyum -y install  kernel-developenssl-devel  popt-devel &&wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz&&tar -zxf keepalived-1.1.15.tar.gz &&cd keepalived-1.1.15 &&./configure--with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/ &&make && make install &&DIR=/usr/local/cp $DIR/etc/rc.d/init.d/keepalived  /etc/rc.d/init.d/  &&cp $DIR/etc/sysconfig/keepalived/etc/sysconfig/ && mkdir -p /etc/keepalived &&cp $DIR/sbin/keepalived/usr/sbin/&&cp/usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ &&/etc/init.d/keepalived restartif [ $? -eq 0 ];then        echo -e '\e[32mInstall Keepalive Successfully!\e[0m'else        echo -e '\e[31mInstall Keepalive Failure!\e[0m'fi[root@slave ~]#

加载ip_vs的内核模块

[root@master ~]# modprobe ip_vs[root@master ~]# modprobe ip_vs_wrr[root@master ~]# modprobe ip_vs_rr

修改keepalived.conf配置文件如下

[root@slave ~]# vim/etc/keepalived/keepalived.conf[root@slave ~]# cat/etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs {  notification_email {         654001593@qq.com   }  notification_email_from root@localhost.com  smtp_server 127.0.0.1  smtp_connect_timeout 30  router_id LVS_DEVEL} vrrp_instance VI_1 {   state BACKUP    !此处同master不一样   interface eth0   virtual_router_id 51   priority 90     !优先级比master上低   advert_int 1    authentication{       auth_type PASS       auth_pass 1111    }   virtual_ipaddress {         192.168.1.77    }} virtual_server 192.168.1.77 3306 {   delay_loop 6   lb_algo wrr   lb_kind DR   nat_mask 255.255.255.0   persistence_timeout 50    protocol TCP    real_server 192.168.1.4 3306 { !Slave上的IP       weight 1         notify_down/data/sh/keepalive_stop.sh       TCP_CHECK {           connect_timeout 3           nb_get_retry 3           delay_before_retry 3             connect_port 3306       }    }}
[root@slave ~]#root@slave ~]# mkdir -p /data/sh[root@slave ~]# vim/data/sh/keepalive_stop.sh[root@slave ~]# chmod +x/data/sh/keepalive_stop.sh[root@slave ~]# /etc/init.d/keepalivedreloadReloading keepalived:                                      [  OK  ][root@slave ~]# cat/data/sh/keepalive_stop.sh#!/bin/bash/etc/init.d/keepalived stop[root@slave ~]#

三、修改Discuz论坛数据库连接IPVIP

直接访问master上的192.168.1.3论坛是可以访问的,且直接访问slave上的192.168.1.4论坛也是可以访问的,现在我们直接访问VIP(虚拟IP) 192.168.1.77依旧可以访问论坛。

这个时候因为discuz调用的是master上的数据库IP,所以我们应该让其调用VIP     192.168.1.77,这样才能保证在主挂的时候自动切换到从上。

master

[root@master ~]#sed -i 's/192.168.1.3/192.168.1.77/'/usr/local/apache/htdocs/config/config_global.php[root@master ~]#/usr/local/apache/bin/apachectl restart

slave

[root@slave ~]#sed -i 's/192.168.1.3/192.168.1.77/'/usr/local/apache/htdocs/config/config_global.php[root@slave ~]#/usr/local/apache/bin/apachectl restart

四、测试

上边两个图很明显可以看出此时VIPmaster上。

手动模拟master上的mysql关闭

VIP自动漂到slave

论坛依旧可以访问