企业级应用,持久层架构方案二(双主同步高可用二)

Posted itall

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了企业级应用,持久层架构方案二(双主同步高可用二)相关的知识,希望对你有一定的参考价值。

  这是企业级应用,持久层架构方案的第二篇。在上一篇:企业级应用,持久层架构方案一(双主同步高可用)中。已经准备好了两台mysql数据库节点:hadoop001、hadoop002。两个节点互为主备,实现舒双主同步高可用,如何叫做双主同步高可用呢?其实要分为两个问题:一个是双主同步,互为主备;另一个是高可用。那么在上一篇中已经实现了双主互为主备,本篇通过keepalived虚拟ip实现高可用:

1.安装keepalived

  1.1.hadoop001节点

#安装keepalived
yum install keepalived -y

#启动keepalived服务
/etc/init.d/keepalived start
-------------------------------------------
[[email protected] anginx]# /etc/init.d/keepalived start
正在启动 keepalived:                                      [确定]
[[email protected] anginx]# ps -ef |grep keepalived
root      15723      1  0 00:59 ?        00:00:00 /usr/sbin/keepalived -D
root      15724  15723  0 00:59 ?        00:00:00 /usr/sbin/keepalived -D
root      15725  15723  0 00:59 ?        00:00:00 /usr/sbin/keepalived -D
root      15731  15622  0 00:59 pts/1    00:00:00 grep keepalived
[[email protected] anginx]#

#设置开机自启动
echo "/etc/init.d/keepalived start" >>/etc/rc.local

#关闭keepalived服务
/etc/init.d/keepalived stop

 

  1.2.hadoop002节点

  说明:参考hadoop001节点安装

2.配置keepalived

 2.1.hadoop001节点

#编辑keepalived配置文件
vi /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 mysql_ha


vrrp_instance VI_1 
    state BACKUP
    interface eth1
    virtual_router_id 55
    priority 100
    advert_int 1
    nopreempt             #不主动抢占资源,只在master这台优先级高的设置,backup不设置
    authentication 
        auth_type PASS
        auth_pass server123
    
    virtual_ipaddress 
        192.168.80.40 dev eth1 label eth1:1
    


virtual_server 192.168.80.40 3306 
 delay_loop 2
 #lb_algo rr              #LVS算法,用不到,我们就关闭了
 #lb_kind DR              #LVS模式,如果不关闭,备用服务器不能通过VIP连接主MySQL
 persistence_timeout 60  #同一IP的连接60秒内被分配到同一台真实服务器
 protocol TCP
 real_server 192.168.80.30 3306    #检测本地mysql,backup也要写检测本地mysql
 weight 3
 notify_down /usr/local/develop/shell/mysql.sh    #当mysq服down时,执行此脚本,杀死keepalived实现切换
 TCP_CHECK 
 connect_timeout 3    #连接超时
 nb_get_retry 3       #重试次数
 delay_before_retry 3 #重试间隔时间
  

...........................................................

 

 2.2.hadoop002节点

#编辑keepalived配置文件
vi /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 mysql_ha


vrrp_instance VI_1 
    state BACKUP
    interface eth1
    virtual_router_id 55
    priority 90
    advert_int 1
    authentication 
        auth_type PASS
        auth_pass server123
    
    virtual_ipaddress 
        192.168.80.40 dev eth1 label eth1:1
    


virtual_server 192.168.80.40 3306 
 delay_loop 2
 #lb_algo rr              #LVS算法,用不到,我们就关闭了
 #lb_kind DR              #LVS模式,如果不关闭,备用服务器不能通过VIP连接主MySQL
 persistence_timeout 60  #同一IP的连接60秒内被分配到同一台真实服务器
 protocol TCP
 real_server 192.168.80.31 3306    #检测本地mysql,backup也要写检测本地mysql
 weight 3
 notify_down /usr/local/develop/shell/mysql.sh    #当mysq服down时,执行此脚本,杀死keepalived实现切换
 TCP_CHECK 
 connect_timeout 3    #连接超时
 nb_get_retry 3       #重试次数
 delay_before_retry 3 #重试间隔时间
  

...........................................................

 

3.编写切换脚本

#mysql.sh脚本
cd /usr/local/develop/shell

vi mysql.sh
-----------------------------------------------------------
#!/bin/bash
pkill keepalived

#给脚本添加执行权限
chmod u+x mysql.sh

#重新启动keepalived
/etc/init.d/keepalived restart

4.测试方案

  

1.分别在hadoop001、hadoop002节点启动mysql服务
2.分别在hadoop001、hadoop002节点启动keepalived服务
3.通过虚拟ip:192.168.80.40连接mysql服务,并进行操作,检查是否正常
4.假设当前虚ip是落在hadoop001节点
5.停止hadoop001节点的mysql服务,再次进行相关mysql操作,检查是否正常

 

以上是关于企业级应用,持久层架构方案二(双主同步高可用二)的主要内容,如果未能解决你的问题,请参考以下文章

keepalived实现mysql双主架构

MySQL双主(主主)架构方案

MySQL双主(主主)架构方案

案例二(构建双主高可用HAProxy负载均衡系统)

初识MariaDB之9——keepalive+GTID双主实现高可用

mysql双主模式方案