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

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

1.安装keepalived

  1.1.hadoop001节点

#安装keepalived
yum install keepalived -y

#启动keepalived服务
/etc/init.d/keepalived start
-------------------------------------------
[root@hadoop02 anginx]# /etc/init.d/keepalived start
正在启动 keepalived:                                      [确定]
[root@hadoop02 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
[root@hadoop02 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 {
     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 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 {
     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 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操作,检查是否正常

 

posted @ 2019-06-13 18:55  小杨【0和1】  阅读(242)  评论(0编辑  收藏  举报