实验目的:
通过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平台请参见<<搭建Discuz论坛的两种方式>>
Mysql主从参见<<构建Mysql三部曲之三 主从案例>>
实验步骤:
一、在主mysql上安装配置keepalive
[[email protected] ~]# bash keepalive_install.sh
“脚本”内容如下:
[[email protected] ~]# cat keepalive_install.sh #!/bin/bash yum -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 restart if [ $? -eq 0 ];then echo -e ‘\e[32mInstall Keepalive Successfully!\e[0m‘ else echo -e ‘\e[31mInstall Keepalive Failure!\e[0m‘ fi
加载ip_vs的内核模块
[[email protected] ~]# modprobe ip_vs [[email protected]~]# modprobe ip_vs_wrr [[email protected]~]# modprobe ip_vs_rr
修改keepalived.conf配置文件如下:
[[email protected] ~]# vim /etc/keepalived/keepalived.conf [[email protected] ~]# cat/etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] 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 } } }
[[email protected] ~]# mkdir -p /data/sh [[email protected] ~]# vim/data/sh/keepalive_stop.sh [[email protected] ~]# chmod +x /data/sh/keepalive_stop.sh [[email protected] ~]# /etc/init.d/keepalivedreload Reloading keepalived: [ OK ] [[email protected] ~]# cat/data/sh/keepalive_stop.sh #!/bin/bash /etc/init.d/keepalived stop [[email protected] ~]#
二、在从mysql上安装配置keepalive
[[email protected] ~]# bash keepalive_install.sh
“脚本”内容如下,同master上执行的脚本一致(非严格意义上的脚本,安装命令放一起而已)。
[[email protected] ~]# cat keepalive_install.sh #!/bin/bash yum -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 restart if [ $? -eq 0 ];then echo -e ‘\e[32mInstall Keepalive Successfully!\e[0m‘ else echo -e ‘\e[31mInstall Keepalive Failure!\e[0m‘ fi [[email protected] ~]#
加载ip_vs的内核模块
[[email protected] ~]# modprobe ip_vs [[email protected] ~]# modprobe ip_vs_wrr [[email protected] ~]# modprobe ip_vs_rr
修改keepalived.conf配置文件如下
[[email protected] ~]# vim/etc/keepalived/keepalived.conf [[email protected] ~]# cat/etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] 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 } } }
[[email protected] ~]# [email protected] ~]# mkdir -p /data/sh [[email protected] ~]# vim/data/sh/keepalive_stop.sh [[email protected] ~]# chmod +x/data/sh/keepalive_stop.sh [[email protected] ~]# /etc/init.d/keepalivedreload Reloading keepalived: [ OK ] [[email protected] ~]# cat/data/sh/keepalive_stop.sh #!/bin/bash /etc/init.d/keepalived stop [[email protected] ~]#
三、修改Discuz论坛数据库连接IP为VIP
直接访问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上
[[email protected] ~]#sed -i ‘s/192.168.1.3/192.168.1.77/‘/usr/local/apache/htdocs/config/config_global.php [[email protected] ~]#/usr/local/apache/bin/apachectl restart
在slave上
[[email protected] ~]#sed -i ‘s/192.168.1.3/192.168.1.77/‘/usr/local/apache/htdocs/config/config_global.php [[email protected] ~]#/usr/local/apache/bin/apachectl restart
四、测试
上边两个图很明显可以看出此时VIP在master上。
手动模拟master上的mysql关闭
VIP自动漂到slave上
论坛依旧可以访问