构建高可用服务器之三 Keepalive冗余Mysql

构建高可用服务器之三 Keepalive冗余Mysql,第1张

构建高可用服务器之三Keepalive冗余Mysql

实验目的:

通过Keepalive构建Mysql的高可用性。Mysql已经占了先机,当一个Mysql宕机时,不会影响对Mysql的调用。

实验环境:

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

标识主机名IP地址

MasterMysqlMaster192.168.1.3

来自Myslq从站192.168.1.4

见<<建立Discuz论坛的两种方法>:>

见<<Mysql三部曲的三个主从案例>:>

实验步骤:

一、在主mysql上安装配置Keepalive

[root@master ~]# bash keepalive_install.sh

“脚本”内容如下:

[root@master ~]# 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的内核模块

[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/keepalivedreload Reloading 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

“脚本”的内容如下,与主机上执行的脚本一致(严格来说不是脚本,只是安装命令放在一起)。

[root@slave ~]# 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 [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/keepalivedreload Reloading keepalived:                                      [  OK  ] [root@slave ~]# cat/data/sh/keepalive_stop.sh #!/bin/bash /etc/init.d/keepalived stop [root@slave ~]#

三。将Discuz论坛数据库的连接IP更改为VIP

可以直接访问192.168.1.3关于主的论坛,也可以直接访问192.168.1.4关于从的论坛。现在我们仍然可以通过直接访问VIP(虚拟IP)192.168.1.77来访问论坛。

此时,由于discuz调用主服务器上的数据库IP,我们应该让它调用VIP192.168.1.77,这样才能保证它在主服务器挂机时自动切换到从服务器。

上的

[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

[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


四。测试

从上面两个图可以明显看出此时VIP在主上。

在主服务器上手动模拟mysql关闭


VIP自动浮动到slave


论坛仍然可以访问



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/782474.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-04
下一篇 2022-05-04

发表评论

登录后才能评论

评论列表(0条)

保存