首页 > 代码库 > keepalived+mysql 高可用集群

keepalived+mysql 高可用集群

mysql 为主主模式参考 https://my.oschina.net/sanmuyan/blog/877373

192.168.100.129 mysql 主节点/keepalived 主节点
192.168.100.130 mysql 备节点/keepalived 备节点
192.168.100.150 虚拟IP

主备节点

yum install keepalived -y
vim  /etc/keepalived/keepalived.conf
! Configuration File for keepalived

vrrp_instance VI_1 {
    state MASTER    #备节点设置为BACKUP
    interface eth0
    virtual_router_id 51 #主备保持一致
    priority 100 #备节点90
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.100.150  #虚拟IP
    }
}

virtual_server 192.168.100.150 3306 { #虚拟IP
    delay_loop 6
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP

    real_server 192.168.100.129 3306 { #真实IP 备节点为192.168.100.130
    weight 3
    notify_down /root/mysql.sh   #如果本地IP 3306端口宕掉 则执行该脚本(杀死keepalived进程)
    TCP_CHECK {
    connect_timeout 3   
    nb_get_retry 3       
    delay_before_retry 3 
  }
}
[root@localhost ~]# vim mysql.sh
#!/bin/bash
pkill keepalived

[root@localhost ~]# chmod +x mysql.sh 

实验

技术分享

 

说明keepalived已经开始工作

关闭主服务器  短暂的超时备服务器接管了虚拟IP

技术分享

 

keepalived进程正在运行

[root@localhost ~]# ps -el | grep keepalived
1 S     0  16274      1  0  80   0 - 27926 poll_s ?        00:00:00 keepalived
5 S     0  16275  16274  0  80   0 - 28487 poll_s ?        00:00:00 keepalived
5 S     0  16276  16274  0  80   0 - 28457 poll_s ?        00:00:00 keepalived
[root@localhost ~]# systemctl stop mysqld  关闭mysql服务

keepalived 进程已经被kill

技术分享

 

 

keepalived+mysql 高可用集群