当前位置:Gxlcms > 数据库问题 > Lvs+Keepalived+Mysql

Lvs+Keepalived+Mysql

时间:2021-07-01 10:21:17 帮助过:16人阅读

7.1.1503 (Core) [root@node1 ~]# 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

安装

[root@node1 ~]# yum install -y ipvsadm keepalived
[root@node2 ~]# yum install -y ipvsadm keepalived

主负载配置

[root@node1 ~]# vim /etc/keepalived/keepalived.conf 

修改配置文件

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   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
         }
    }
}

备负载配置

[root@node2 ~]# vim /etc/keepalived/keepalived.conf 

修改配置文件

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   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配置)

[root@node1 ~]# 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

启动服务

[root@node1 ~]# systemctl start keepalived
[root@node2 ~]# systemctl start keepalived

测试

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

[root@node1 ~]# systemctl stop keepalived

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

[root@node1 ~]# systemctl start keepalived

 

Lvs+Keepalived+Mysql

标签:smt   back   pass   delay   mysql   mys   inter   启动服务   install   

人气教程排行