Lvs+Keepalived+Mysql

Posted 答案

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lvs+Keepalived+Mysql相关的知识,希望对你有一定的参考价值。

环境

[[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core) 
[[email protected] ~]# uname -a 
Linux node1 3.10.0-229.el7.x86_64 #1 SMP Fri Mar 6 11:36:42 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

主机

   IP         NAME      Describe
10.0.0.10     node1       主负载
10.0.0.11     node2       备负载
10.0.0.12     node3        节点
10.0.0.13     node4        节点
10.0.0.14     node5        节点
10.0.0.20 VIP

安装

[[email protected] ~]# yum install -y ipvsadm keepalived
[[email protected] ~]# yum install -y ipvsadm keepalived

主负载配置

[[email protected] ~]# vim /etc/keepalived/keepalived.conf 

修改配置文件

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state MASTER
    interface eno16777736
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.20/24 dev eno16777736 label eno16777736:0
    }
}

virtual_server 10.0.0.20 3306 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 10.0.0.12 3306 {
        weight 1
         MISC_CHECK {
         misc_path "/etc/keepalived/check.sh 10.0.0.12"
         misc_dynamic
         }
    }
    real_server 10.0.0.13 3306 {
        weight 1
         MISC_CHECK {
         misc_path "/etc/keepalived/check.sh 10.0.0.13"
         misc_dynamic
         }
    }
    real_server 10.0.0.14 3306 {
        weight 1
         MISC_CHECK {
         misc_path "/etc/keepalived/check.sh 10.0.0.14"
         misc_dynamic
         }
    }
}

备负载配置

[[email protected] ~]# vim /etc/keepalived/keepalived.conf 

修改配置文件

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL_BACKUP
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state BACKUP
    interface eno16777736
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.20/24 dev eno16777736 label eno16777736:0
    }
}

virtual_server 10.0.0.20 3306 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
protocol TCP

    real_server 10.0.0.12 3306 {
        weight 1
         MISC_CHECK {
         misc_path "/etc/keepalived/check.sh 10.0.0.12"
         misc_dynamic
         }
    }

    real_server 10.0.0.13 3306 {
        weight 1
         MISC_CHECK {
         misc_path "/etc/keepalived/check.sh 10.0.0.13"
         misc_dynamic
         }
    }
    real_server 10.0.0.14 3306 {
        weight 1
         MISC_CHECK {
         misc_path "/etc/keepalived/check.sh 10.0.0.14"
         misc_dynamic
         }
    }
}

自定义脚本做健康检查(node1和node2配置)

[[email protected] ~]# vim /etc/keepalived/check.sh

插入以下配置

#!/bin/bash

mysql -uwsrep -pnx111111 -h$1 -e "select 1;" &>/dev/null
ret=$?
if [ $ret -eq 0 ];then
    exit 0
else
    exit 1
fi

启动服务

[[email protected] ~]# systemctl start keepalived
[[email protected] ~]# systemctl start keepalived

测试

关闭主负载keepalived,查看VIP是否漂移到备负载

[[email protected] ~]# systemctl stop keepalived

启动主负载keepalived,查看VIP是否漂移回来

[[email protected] ~]# systemctl start keepalived

 


以上是关于Lvs+Keepalived+Mysql的主要内容,如果未能解决你的问题,请参考以下文章

LVS+Keepalived实现MySQL高可用

mysql主从之LVS+keepalived+双主MySQL 负载均衡

LVS+Keepalived实现mysql的负载均衡

MySQL 高可用:mysql+Lvs+Keepalived 负载均衡及故障转移

MySQL主主复制+LVS+Keepalived实现MySQL高可用性

Lvs+Keepalived+Mysql