keepalived+mysql双主实现单ip访问

Posted python真好用

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keepalived+mysql双主实现单ip访问相关的知识,希望对你有一定的参考价值。

本文为keepalived vip配置,mysql双主安装部署可以参考之前的mysql双主安装部署

~~

原理:通过配置keepalived将虚拟ip绑定到服务优先级更高的机器上(以下简称master1),当master1的mysql服务挂掉时,通过脚本干掉master1的keepalived服务,使虚拟ip自动绑定到master2。利用keepalived构建虚拟ip,实现mysql双主节点只需要通过一个虚拟ip即可访问,该方案的好处是避免上层的应用改造代码,同时实现了mysql的HA(该方案同样适合mysql的主从架构等等)

~~

环境:centos7 master1 ip:192.168.1.58 master2 ip:192.168.1.95
vip:192.168.1.168

1.master1 master2上都安装keepalive服务

yum install -y keepalived

2.master1和master2上分别配置keepalived.conf(注意:配置不同)

vi /etc/keepalived/keepalived.conf

master1 keepalived配置

! 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 127.0.0.1
   smtp_connect_timeout 30   
   router_id LVS_DEVEL


vrrp_instance VI_1 
    state MASTER
    interface ens192  #实例绑定的网卡
    virtual_router_id 51   #这里设置VRID,这里非常重要,相同的VRID为一个组,他将决定多播的MAC地址
    priority 100   #设置本节点的优先级,优先级高的为master,如另外一个节点配置为90,那此节点就是master
    advert_int 1    #检查间隔,默认为1秒
    authentication 
        auth_type PASS
        auth_pass 1111
    
    virtual_ipaddress    #虚ip
        192.168.1.168
    
    unicast_src_ip 192.168.1.58  #master1 ip
    unicast_peer 
        192.168.1.95             #master2 ip
    


virtual_server 192.168.1.168 3307   #虚ip和端口
    delay_loop 2  #每个2秒检查一次real_server状态
    lb_algo rr
    lb_kind NAT
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP

real_server 192.168.1.58 3307     #实ip和端口
    weight 1
    notify_down /dsj/script/keepalive.sh  #当服务挂掉的时候执行的脚本
    TCP_CHECK 
                    connect_timeout 3     #连接超时时间
                    nb_get_retry 3        #重连次数
                    delay_before_retry 3  #重连间隔时间
                    connect_port 3307     #健康检查端口
            



master2 keepalived配置

! 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 127.0.0.1    smtp_connect_timeout 30    router_id LVS_DEVEL 

vrrp_instance VI_1 
    state BACKUP
    interface ens192
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication 
        auth_type PASS
        auth_pass 1111
    
    virtual_ipaddress 
        192.168.1.168
    
    unicast_src_ip 192.168.1.95
    unicast_peer 
        192.168.1.58
     

virtual_server 192.168.1.168 3307 
    delay_loop 2
    lb_algo rr
    lb_kind NAT
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP
    real_server 192.168.1.95 3307 
        weight 2
        notify_down /dsj/script/keepalive.sh
        TCP_CHECK 
                        connect_timeout 8
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 3307
                
     

3.配置/dsj/script/keepalive.sh

vi /dsj/script/keepalive.sh
chmod 755 /dsj/script/keepalive.sh
#!/bin/sh
pkill keepalived
sleep 10s
systemctl start keepalived
sleep 120s
systemctl start mysqld

4.开启keepalived服务

systemctl start keepalived

查看keepalived状态

可以发现vip已经被绑定到网卡上了
或者

ip addr

也能看到网卡上绑定的vip

测试:
1.master1上使用vip连接mysql,连接成功

mysql -h vip -u user -p

2.关闭master1的mysql服务,可以看到keepalived的服务也被关闭了,这是因为第三步的脚本生效了,此时在查看master2的网卡信息,可以看到vip已经被绑定到了master2上
3.master2上使用vip连接mysql,连接成功

以上是关于keepalived+mysql双主实现单ip访问的主要内容,如果未能解决你的问题,请参考以下文章

keepalived+mysql双主实现单ip访问

keepalived+mysql双主实现单ip访问

Keepalived+MySQL 8.0.17 实现MySQL双主的高可用

Keepalived+MySQL 8.0.17 实现MySQL双主的高可用

MySQL集群之keepalived实现mysql双主高可用

MySQL高可用性之Keepalived+MySQL(双主热备)