千家信息网

MySQL高可用之keepalived方案的示例分析

发表于:2025-02-21 作者:千家信息网编辑
千家信息网最后更新 2025年02月21日,这篇文章主要为大家展示了"MySQL高可用之keepalived方案的示例分析",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"MySQL高可用之keepal
千家信息网最后更新 2025年02月21日MySQL高可用之keepalived方案的示例分析

这篇文章主要为大家展示了"MySQL高可用之keepalived方案的示例分析",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"MySQL高可用之keepalived方案的示例分析"这篇文章吧。



实验环境
mysql master : 192.168.111.52
mysql slave : 192.168.111.53
keepalived vip : 192.168.111.60
、搭建过程
1. mysql双主的构建
① 互相 change master 即可,此处省略该过程,着重讲下keepalived

2. keepalived相关
① yum -y install keepalived 安装keepalived
② root@192.168.111.52:~# keepalived -v
Keepalived v1.2.7 (02/21,2013)

③ 编辑 /etc/keepalived/keepalived.conf

点击(此处)折叠或打开

  1. root@192.168.111.52:keepalived# cat keepalived.conf

  2. ! Configuration File for keepalived


  3. global_defs {##全局配置

  4. notification_email {

  5. horand_gc@163.com #####邮件接收者

  6. }

  7. notification_email_from dba@163.com #####邮件发送者

  8. smtp_server smtp.163.com #####SMTP服务器

  9. smtp_connect_timeout 30

  10. router_id haMySQL #####routerID 同一组keepalived设置为相同

  11. }


  12. vrrp_script chk_mysql {##健康检测相关配置

  13. script "/etc/keepalived/chk_mysql.sh" #####设置脚本 或者直命令 返回 0 则表示成功 >0 则表示失败, 详情可以后面的脚本内容

  14. interval 1 #####检测间隔

  15. weight 2 #####检测返回失败之后优先级会减少 2 (如果 master优先级100 , slave 优先级 99 ,master检测失败则优先级为100-2 < 99 ,则slave会提升为主)

  16. }


  17. vrrp_instance VI_1 {

  18. state MASTER ##### 设为master

  19. interface eth2 ##### 网卡设定

  20. virtual_router_id 51 ##### 针对该 instance的虚拟id , 同一组 instance设置相同

  21. priority 100 ##### 优先级设定

  22. advert_int 1 ##### 检测时间间隔

  23. authentication {

  24. auth_type PASS ##### 同一组instance之间的认证方式为 PASS ,pass 为7777 ,必须相同(防止 有用户恶意伪造 vrrp)

  25. auth_pass 7777

  26. }

  27. virtual_ipaddress {

  28. 192.168.111.60 ##### 设置虚拟ip ,可以设置多个

  29. }


  30. track_script {

  31. chk_mysql ##### 表示该instance 使用chk_mysql进行相关检测

  32. }

  33. ##### 以下配置 在该实例 转换为 master,slave,或者出错的时候执行的脚本(可以设置邮件通知,或者处理一些其他问题)

  34. # notify_master "/etc/keepalived/change_master.sh"

  35. # notify_slave "/etc/keepalived/change_slave.sh"

  36. # notify_fault "/etc/keepalived/change_fault.sh"

  37. }


  38. root@192.168.111.53:keepalived# cat keepalived.conf

  39. ! Configuration File for keepalived


  40. global_defs {

  41. notification_email {

  42. horand_gc@163.com

  43. }

  44. notification_email_from dba@163.com

  45. smtp_server smtp.163.com

  46. smtp_connect_timeout 30

  47. router_id haMySQL

  48. }


  49. vrrp_script chk_mysql {

  50. script "/etc/keepalived/chk_mysql.sh"

  51. interval 1

  52. weight 2

  53. }


  54. vrrp_instance VI_1 {

  55. state BACKUP ##### 该主机作为备机 BACKUP

  56. interface eth2

  57. virtual_router_id 51

  58. priority 99 ##### 优先级设置 小于 master

  59. advert_int 1

  60. authentication {

  61. auth_type PASS

  62. auth_pass 7777

  63. }

  64. virtual_ipaddress {

  65. 192.168.111.60

  66. }


  67. track_script {

  68. chk_mysql

  69. }

  70. }


  71. root@192.168.111.52:keepalived# cat chk_mysql.sh

  72. #!/bin/bash


  73. num=`ps -ef |grep mysqld | grep -v grep | wc -l` ##### 查看mysqld进程数量 , 正常情况有一个root起的mysqld_safe守护进程,还有一个属于mysql用户的mysqld进程

  74. [[ $num -eq 2 ]] && exit 0 || exit 1

3. 故障模拟
① 启动 keepalived

点击(此处)折叠或打开

  1. root@192.168.111.52:keepalived# tail /var/log/messages

  2. Apr 29 13:45:24 localhost Keepalived_healthcheckers[24184]: Registering Kernel netlink command channel

  3. Apr 29 13:45:24 localhost Keepalived_healthcheckers[24184]: Opening file '/etc/keepalived/keepalived.conf'.

  4. Apr 29 13:45:24 localhost Keepalived_healthcheckers[24184]: Configuration is using : 7417 Bytes

  5. Apr 29 13:45:24 localhost Keepalived_vrrp[24185]: Opening file '/etc/keepalived/keepalived.conf'.

  6. Apr 29 13:45:24 localhost Keepalived_vrrp[24185]: Configuration is using : 65552 Bytes

  7. Apr 29 13:45:24 localhost Keepalived_vrrp[24185]: Using LinkWatch kernel netlink reflector...

  8. Apr 29 13:45:24 localhost Keepalived_healthcheckers[24184]: Using LinkWatch kernel netlink reflector...

  9. Apr 29 13:45:25 localhost Keepalived_vrrp[24185]: VRRP_Script(chk_mysql) succeeded

  10. Apr 29 13:45:25 localhost Keepalived_vrrp[24185]: VRRP_Instance(VI_1) Transition to MASTER STATE

  11. Apr 29 13:45:26 localhost Keepalived_vrrp[24185]: VRRP_Instance(VI_1) Entering MASTER STATE


  12. root@192.168.111.53:keepalived# tailf /var/log/messages

  13. Apr 29 13:49:27 localhost Keepalived_healthcheckers[32569]: Registering Kernel netlink reflector

  14. Apr 29 13:49:27 localhost Keepalived_healthcheckers[32569]: Registering Kernel netlink command channel

  15. Apr 29 13:49:27 localhost Keepalived_vrrp[32570]: Opening file '/etc/keepalived/keepalived.conf'.

  16. Apr 29 13:49:27 localhost Keepalived_vrrp[32570]: Configuration is using : 65550 Bytes

  17. Apr 29 13:49:27 localhost Keepalived_vrrp[32570]: Using LinkWatch kernel netlink reflector...

  18. Apr 29 13:49:27 localhost Keepalived_healthcheckers[32569]: Opening file '/etc/keepalived/keepalived.conf'.

  19. Apr 29 13:49:27 localhost Keepalived_healthcheckers[32569]: Configuration is using : 7415 Bytes

  20. Apr 29 13:49:27 localhost Keepalived_vrrp[32570]: VRRP_Instance(VI_1) Entering BACKUP STATE

  21. Apr 29 13:49:27 localhost Keepalived_healthcheckers[32569]: Using LinkWatch kernel netlink reflector...

  22. Apr 29 13:49:27 localhost Keepalived_vrrp[32570]: VRRP_Script(chk_mysql) succeeded

ip a 可以查看到 vip 192.168.111.60 在 192.168.111.52(master)上

② 关闭 111.52上面的mysql

点击(此处)折叠或打开

  1. root@192.168.111.52:keepalived# mysqladmin shutdown

  2. root@192.168.111.52:keepalived# tailf /var/log/messages

  3. Apr 29 14:19:30 localhost Keepalived_vrrp[24862]: VRRP_Script(chk_mysql) failed

  4. Apr 29 14:19:32 localhost Keepalived_vrrp[24862]: VRRP_Instance(VI_1) Received higher prio advert

  5. Apr 29 14:19:32 localhost Keepalived_vrrp[24862]: VRRP_Instance(VI_1) Entering BACKUP STATE


  6. root@192.168.111.53:keepalived# tailf /var/log/messages

  7. Apr 29 14:19:55 localhost Keepalived_vrrp[566]: VRRP_Instance(VI_1) forcing a new MASTER election

  8. Apr 29 14:19:56 localhost Keepalived_vrrp[566]: VRRP_Instance(VI_1) Transition to MASTER STATE

  9. Apr 29 14:19:57 localhost Keepalived_vrrp[566]: VRRP_Instance(VI_1) Entering MASTER STATE

ip a 可以查看到 vip 192.168.111.60 在 192.168.111.53(master)上

③ 启动111.52上面的mysql

点击(此处)折叠或打开

  1. root@192.168.111.52:keepalived# mysqld_safe & ; tailf /var/log/messages

  2. Apr 29 14:24:21 localhost Keepalived_vrrp[24862]: VRRP_Script(chk_mysql) succeeded

  3. Apr 29 14:24:22 localhost Keepalived_vrrp[24862]: VRRP_Instance(VI_1) forcing a new MASTER election

  4. Apr 29 14:24:23 localhost Keepalived_vrrp[24862]: VRRP_Instance(VI_1) Transition to MASTER STATE

  5. Apr 29 14:24:24 localhost Keepalived_vrrp[24862]: VRRP_Instance(VI_1) Entering MASTER STATE


  6. root@192.168.111.53:keepalived# tailf /var/log/messages

  7. Apr 29 14:24:45 localhost Keepalived_vrrp[566]: VRRP_Instance(VI_1) Received higher prio advert

  8. Apr 29 14:24:45 localhost Keepalived_vrrp[566]: VRRP_Instance(VI_1) Entering BACKUP STATE

ip a 可以查看到 vip 192.168.111.60 在 192.168.111.52(master)上,也就是说111.52会持续通过track_script的脚本检查 ,若成功的话会恢复原来的优先级100 ,便把vip抢过来了(若不希望优先级高的直接上来直接抢占vip的话 需要再instance 里面配置 nopreempt ,backup无需设置)

虚拟server
以上是实验是通过 vrrp_script以及trace_script 实现优先级变换来实现故障转移的,现在看下 通过虚拟server怎么实现mysql的高可用
① 配置

点击(此处)折叠或打开

  1. root@192.168.111.52:keepalived# cat /etc/keepalived/keepalived.conf

  2. ! Configuration File for keepalived


  3. global_defs {

  4. notification_email {

  5. tab@taomee.com

  6. }

  7. notification_email_from dba@taomee.com

  8. smtp_server smtp.shidc.taomee.com

  9. smtp_connect_timeout 30

  10. router_id haMySQL

  11. }


  12. vrrp_script chk_mysql {

  13. script "/etc/keepalived/chk_mysql.sh"

  14. interval 1

  15. weight 2

  16. }


  17. vrrp_instance VI_1 {

  18. state MASTER

  19. interface eth2

  20. virtual_router_id 51

  21. priority 100

  22. nopreempt

  23. advert_int 1

  24. authentication {

  25. auth_type PASS

  26. auth_pass 7777

  27. }

  28. virtual_ipaddress {

  29. 192.168.111.60

  30. }


  31. # track_script {

  32. # chk_mysql

  33. # }

  34. }


  35. virtual_server 192.168.111.60 3306 {

  36. delay_loop 6

  37. persistence_timeout 300

  38. protocol TCP



  39. real_server 192.168.111.52 3306 {

  40. weight 1

  41. notify_down /etc/keepalived/kill_self.sh

  42. TCP_CHECK {

  43. tcp_port 3306

  44. connect_timeout 3

  45. }

  46. }


  47. }


  48. root@192.168.111.53:keepalived# cat /etc/keepalived/keepalived.conf

  49. ! Configuration File for keepalived


  50. global_defs {

  51. notification_email {

  52. tab@taomee.com

  53. }

  54. notification_email_from dba@taomee.com

  55. smtp_server smtp.shidc.taomee.com

  56. smtp_connect_timeout 30

  57. router_id haMySQL

  58. }


  59. vrrp_script chk_mysql {

  60. script "/etc/keepalived/chk_mysql.sh"

  61. interval 1

  62. weight 2

  63. }


  64. vrrp_instance VI_1 {

  65. state BACKUP

  66. interface eth2

  67. virtual_router_id 51

  68. priority 99

  69. advert_int 1

  70. authentication {

  71. auth_type PASS

  72. auth_pass 7777

  73. }

  74. virtual_ipaddress {

  75. 192.168.111.60

  76. }


  77. # track_script { #########这里先注释掉 通过追踪脚本的检查

  78. # chk_mysql

  79. # }

  80. }


  81. virtual_server 192.168.111.60 3306 {

  82. delay_loop 6

  83. persistence_timeout 300

  84. protocol TCP



  85. real_server 192.168.111.53 3306 {### 真实 服务

  86. weight 1 #### 权重,用来多真实服务 均衡使用

  87. notify_down /etc/keepalived/kill_self.sh ####在检查该服务不可用时执行该脚本(用来杀死 keepalived 实现 vip 飘逸)

  88. TCP_CHECK {

  89. tcp_port 3306 #### 检查端口 继承 real_server 192.168.111.53 3306 {### 真实 服务

  90. connect_timeout 3 #### tcp超时时间

  91. }

  92. }


  93. }


  94. root@192.168.111.53:keepalived# cat /etc/keepalived/kill_self.sh

  95. #!/bin/bash

  96. killall keepalived



②启动keepalived

点击(此处)折叠或打开

  1. root@192.168.111.52:keepalived# keepalived

  2. root@192.168.111.52:keepalived# tailf /var/log/messages

  3. Apr 29 14:48:22 localhost Keepalived_vrrp[20482]: Opening file '/etc/keepalived/keepalived.conf'.

  4. Apr 29 14:48:22 localhost Keepalived_vrrp[20482]: Configuration is using : 64590 Bytes

  5. Apr 29 14:48:22 localhost Keepalived_vrrp[20482]: Using LinkWatch kernel netlink reflector...

  6. Apr 29 14:48:22 localhost Keepalived_healthcheckers[20481]: IPVS: Scheduler not found

  7. Apr 29 14:48:22 localhost kernel: IPVS: Scheduler module ip_vs_ not found

  8. Apr 29 14:48:22 localhost Keepalived_healthcheckers[20481]: IPVS: Service not defined

  9. Apr 29 14:48:22 localhost Keepalived_healthcheckers[20481]: Using LinkWatch kernel netlink reflector...

  10. Apr 29 14:48:22 localhost Keepalived_healthcheckers[20481]: Activating healthchecker for service [192.168.111.52]:3306

  11. Apr 29 14:48:23 localhost Keepalived_vrrp[20482]: VRRP_Instance(VI_1) Transition to MASTER STATE

  12. Apr 29 14:48:24 localhost Keepalived_vrrp[20482]: VRRP_Instance(VI_1) Entering MASTER STATE


  13. root@192.168.111.53:keepalived# keepalived

  14. root@192.168.111.53:keepalived# tailf /var/log/messages

  15. Apr 29 14:48:51 localhost Keepalived_healthcheckers[25092]: Configuration is using : 11673 Bytes

  16. Apr 29 14:48:51 localhost Keepalived_vrrp[25093]: Opening file '/etc/keepalived/keepalived.conf'.

  17. Apr 29 14:48:51 localhost Keepalived_vrrp[25093]: Configuration is using : 64568 Bytes

  18. Apr 29 14:48:51 localhost Keepalived_vrrp[25093]: Using LinkWatch kernel netlink reflector...

  19. Apr 29 14:48:51 localhost Keepalived_vrrp[25093]: VRRP_Instance(VI_1) Entering BACKUP STATE

  20. Apr 29 14:48:51 localhost kernel: IPVS: Scheduler module ip_vs_ not found

  21. Apr 29 14:48:51 localhost Keepalived_healthcheckers[25092]: IPVS: Scheduler not found

  22. Apr 29 14:48:51 localhost Keepalived_healthcheckers[25092]: IPVS: Service not defined

  23. Apr 29 14:48:51 localhost Keepalived_healthcheckers[25092]: Using LinkWatch kernel netlink reflector...

  24. Apr 29 14:48:51 localhost Keepalived_healthcheckers[25092]: Activating healthchecker for service [192.168.111.53]:3306

此时 ip a 命令可以查看虚拟ip 111.60 在111.52(master) 上

③关闭 111.52上的mysql

点击(此处)折叠或打开

  1. root@192.168.111.52:keepalived# mysqladmin shutdown

  2. 2017-04-29T07:07:38.121123Z mysqld_safe mysqld from pid file /opt/mysql/mysqld.pid ended

  3. [1]+ Done mysqld_safe

  4. root@192.168.111.52:keepalived# tailf /var/log/messages

  5. Apr 29 15:07:31 localhost Keepalived_healthcheckers[23405]: TCP connection to [192.168.111.52]:3306 failed !!!

  6. Apr 29 15:07:31 localhost Keepalived_healthcheckers[23405]: Removing service [192.168.111.52]:3306 from VS [192.168.111.60]:3306

  7. Apr 29 15:07:31 localhost Keepalived_healthcheckers[23405]: IPVS: Service not defined

  8. Apr 29 15:07:31 localhost Keepalived_healthcheckers[23405]: Executing [/etc/keepalived/kill_self.sh] for service [192.168.111.52]:3306 in VS [192.168.111.60]:3306

  9. Apr 29 15:07:31 localhost Keepalived_healthcheckers[23405]: Lost quorum 1-0=1 > 0 for VS [192.168.111.60]:3306

  10. Apr 29 15:07:31 localhost Keepalived_healthcheckers[23405]: Remote SMTP server [0.0.0.0]:25 connected.

  11. Apr 29 15:07:31 localhost Keepalived[23404]: Stopping Keepalived v1.2.7 (02/21,2013)

  12. Apr 29 15:07:31 localhost Keepalived_healthcheckers[23405]: IPVS: No such service

  13. Apr 29 15:07:31 localhost Keepalived_vrrp[23406]: VRRP_Instance(VI_1) sending 0 priority


  14. root@192.168.111.53:keepalived# tailf /var/log/messages

  15. Apr 29 15:07:32 localhost Keepalived_vrrp[26815]: VRRP_Instance(VI_1) Transition to MASTER STATE

  16. Apr 29 15:07:33 localhost Keepalived_vrrp[26815]: VRRP_Instance(VI_1) Entering MASTER STATE

此时 ip a 命令可以看到虚拟ip 111.60在111.53(新master)上

以上是"MySQL高可用之keepalived方案的示例分析"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

0