Keepalived-mysql高可用架构(主主热备)
博客专区 > 阿dai 的博客 > 博客详情
Keepalived-mysql高可用架构(主主热备)
阿dai 发表于3个月前
Keepalived-mysql高可用架构(主主热备)
  • 发表于 3个月前
  • 阅读 21
  • 收藏 1
  • 点赞 0
  • 评论 0

Keepalived-mysql高可用架构(主主热备)

开始搭建该架构前已安装好mysql且主主热备已搭建完成,搭建方法详见:https://my.oschina.net/u/3497124/blog/1527854

master:192.168.8.131
backup:192.168.8.132
VIP:192.168.8.100

安装Keepalived工具

分别在master和backup服务器上安装Keepalived工具:

[root@adailinux ~]# yum install -y keepalived

开始配置

配置master服务器:

配置Keepalived:

[root@adailinux ~]# vim /etc/keepalived/keepalived.conf
! Configuration File forkeepalived
global_defs {
notification_email {
adai@test.com
 }
notification_email_from  root@test.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MYSQL_HA      #标识,双主相同
 }
vrrp_instance VI_1 {
 state BACKUP           #两台都设置BACKUP
 interface ens33         
 virtual_router_id 51       #主备相同
 priority 100           #优先级,backup设置90
 advert_int 1           
 nopreempt             #不主动抢占资源,只在master这台优先级高的设置,backup不设置
 authentication {      
 auth_type PASS 
 auth_pass 123456
 }
 virtual_ipaddress {
 192.168.8.100
 }
}
virtual_server 192.168.8.100 3306 {
 delay_loop 2
 #lb_algo rr              #LVS算法,用不到,我们就关闭了
 #lb_kind DR              #LVS模式,如果不关闭,备用服务器不能通过VIP连接主MySQL
 persistence_timeout 50  #同一IP的连接60秒内被分配到同一台真实服务器
 protocol TCP
 }
 real_server 192.168.8.131 3306 {   #检测本地mysql,backup也要写检测本地mysql
 weight 3
 notify_down /usr/local/sbin/keepalived/mysql.sh    #当mysq服down时,执行此脚本,杀死keepalived实现切换
 TCP_CHECK { 
 connect_timeout 3    #连接超时
nb_get_retry 3       #重试次数
 delay_before_retry 3 #重试间隔时间
  }
} 

创建mysql.sh脚本:  
[root@adailinux ~]# mkdir /usr/local/sbin/keepalived
[root@adailinux ~]# vim /usr/local/sbin/keepalived/mysql.sh
#!/bin/bash
pkill keepalived

更改文件权限:
[root@adailinux ~]# chmod  755 /usr/local/sbin/keepalived/mysql.sh

启动Keepalived:
[root@adailinux ~]# systemctl start keepalived

配置backup服务器:

配置Keepalived:

! Configuration File forkeepalived
global_defs {
notification_email {
adai@test.com
 }
notification_email_from  root@test.com
smtp_server 127.0.0.1
smtp_connect_timeout 30 
router_id MYSQL_HA      #标识,双主相同
 }
vrrp_instance VI_1 {    
 state BACKUP           #两台都设置BACKUP
 interface ens33             
 virtual_router_id 51       #主备相同
 priority 90           #优先级,backup设置90
 advert_int 1           
# nopreempt             #不主动抢占资源,只在master这台优先级高的设置,backup不设置
 authentication {
 auth_type PASS 
 auth_pass 123456
 }
 virtual_ipaddress {
 192.168.8.100
 }
}
virtual_server 192.168.8.100 3306 {
 delay_loop 2
 #lb_algo rr              #LVS算法,用不到,我们就关闭了
 #lb_kind DR              #LVS模式,如果不关闭,备用服务器不能通过VIP连接主MySQL
 persistence_timeout 50  #同一IP的连接60秒内被分配到同一台真实服务器
 protocol TCP
 }
 real_server 192.168.8.132 3306 {   #检测本地mysql,backup也要写检测本地mysql
 weight 3
 notify_down /usr/local/sbin/keepalived/mysql.sh    #当mysq服down时,执行此脚本,杀死keepalived实
现切换
 TCP_CHECK {
 connect_timeout 3    #连接超时
nb_get_retry 3       #重试次数 
 delay_before_retry 3 #重试间隔时间
  }
}

创建mysql.sh文件:
[root@adailinux ~]# mkdir /usr/local/sbin/keepalived
[root@adailinux ~]# vim /usr/local/sbin/keepalived/mysql.sh
#!/bin/bash
pkill keepalived

更改权限:
[root@adailinux ~]# chmod 755 /usr/local/sbin/keepalived/mysql.sh

启动Keepalived:  
[root@adailinux ~]# systemctl start keepalived

说明: backup服务器与master服务器Keepalived配置文件的区别:只修改priority为90、nopreempt不设置、real_server设置本地IP。

测试

如果服务器宕机,Keepalived服务肯定回停止工作,所以在此通过开/关Keepalived来模拟宕机状态:

宕机前:

查看master服务器IP状态:
[root@adailinux ~]# ip add
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:8a:1b:5c brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.131/24 brd 192.168.8.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.8.100/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::65d2:adc:20d3:8c74/64 scope link 
       valid_lft forever preferred_lft forever

查看backup服务器IP状态:  
[root@adailinux ~]# ip add
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:af:b7:9f brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.132/24 brd 192.168.8.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::76c1:b882:4f26:608c/64 scope link 
       valid_lft forever preferred_lft forever

说明: 此时VIP在master服务器上,说明有master对外提供服务。

master服务器宕机后:

[root@adailinux ~]# systemctl stop keepalived

查看master服务器IP:
[root@adailinux ~]# ip add
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:8a:1b:5c brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.131/24 brd 192.168.8.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::65d2:adc:20d3:8c74/64 scope link 
       valid_lft forever preferred_lft forever

查看backup服务器IP:
[root@adailinux ~]# ip add
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:af:b7:9f brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.132/24 brd 192.168.8.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.8.100/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::76c1:b882:4f26:608c/64 scope link 
       valid_lft forever preferred_lft forever

即,此时有backup服务器接管master服务器对外提供服务。

恢复master服务器Keepalived服务:

[root@adailinux ~]# systemctl start keepalived
[root@adailinux ~]# ip add
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:8a:1b:5c brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.131/24 brd 192.168.8.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::65d2:adc:20d3:8c74/64 scope link 
       valid_lft forever preferred_lft forever

此时VIP并没有回到master服务器上,原因是master在进行Keepalived的时候加入了参数nopreemt。

以上服务器切换过程也可以通过查看日志/var/logs/messages来了解。

参考:
MySQL高可用性之Keepalived+Mysql:http://lizhenliang.blog.51cto.com/7876557/1362313

共有 人打赏支持
粉丝 39
博文 157
码字总数 226276
×
阿dai
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: