当前位置:   article > 正文

keepalived与mysql8主主高可用部署_mysql8 集群+keepalived

mysql8 集群+keepalived

一、keepalived配置文件地址

vim /etc/keepalived/keepalived.conf:

二、keepalived配置文件内容

主从配置大体一样,修改文件提示的地方即可。

global_defs {  
   notification_email {  
         28903817@sohu.com  
   }
   notification_email_from 28903817@sohu.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30  
   router_id mysql_ha01     #成为从节点修改为mysql_ha02
   script_user root
}
vrrp_instance VI_1 {
   state BACKUP
   interface ens33  #配置哪个网卡去进行心跳包的传输
   virtual_router_id 51 #虚拟路由标识,这个标识是一个数字(取值在0-255之间,用来区分多个instance的VRRP组播),同一个vrrp实例使用唯一的标识,确保和master2相同,同网内不同集群此项必须不同,否
  
   priority 100   #成为主节点 从节点可小于100。
   nopreempt #不抢占资源
   advert_int 1 #心跳包的频率 1秒一次
   authentication {
   #两台主机通信心跳包的加密
      auth_type PASS
      auth_pass 1111
   }
   virtual_ipaddress {
   192.168.244.100/24   #虚拟ip(类似于--proxy-address)
   }
	notify_master "/etc/keepalived/notify.sh master"
 	notify_backup "/etc/keepalived/notify.sh backup"
 	notify_fault "/etc/keepalived/notify.sh fault"
}
virtual_server 192.168.244.100 3306 {
   delay_loop 2 #每个2秒检查一次real_server状态
   lb_algo rr #LVS算法
   lb_kind DR  #LVS算法
   persistence_timeout 60  #会话保持时间
   protocol TCP
   real_server 192.168.244.168 3306 {
      weight 3   # 配置节点权值,数字越大权重越高
      notify_down /etc/keepalived/bin/mysql.sh #down后执行的脚本
      TCP_CHECK {
         connect_timeout 3    #连接超时时间
         nb_get_retry 3       #重连次数
         delay_before_retry 3   #重连间隔时间
         connect_port 3306   #健康检查端口
         }
   }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

三、mysql文件内容

#!/bin/bash
pkill keepalived
  • 1
  • 2

四、设置mysql文件权限

chmod -R 777 /etc/keepalived/bin/mysql.sh

五、keepalived实现状态切换email通知

1. 安装mailx

yum install -y mailx

2.修改配置文件

vim /etc/mail.rc

set bsdcompat
set from=82436965@qq.com                #邮箱地址
set smtp=smtp.qq.com
set smtp-auth-user=82436965@qq.com
set smtp-auth-password=kpuhuqvppcedjjeh   #需要开启邮箱pop3
set smtp-auth=login
set ssl-verify=ignore
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.测试发送邮件

echo “hello” | mail -s “mail test” 82436965@qq.com

4.配置脚本文件

vim /etc/keepalived/notify.sh

#!/bin/bash
contact='82436965@qq.com'
notify() {
mailsubject="$(hostname) to be $1, vip floating"
mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
echo "$mailbody" | mail -s "$mailsubject" $contact

}
case $1 in
master)
notify master
;;
backup)
notify backup
;;
fault)
notify fault
;;
*)
echo "Usage: $(basename $0) {master|backup|fault}"
exit 1
;;
esac
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

5.配置脚本文件权限

chmod /etc/keepalived/notify.sh

6.模拟master故障

killall keepalived

7.查看邮件

在这里插入图片描述

注意事项

Configuration file ‘/etc/keepalived/keepalived.conf’ is not a regular non-executable file

排查:查看keepalived.conf 的权限
ll /etc/keepalived/keepalived.conf

解决方案:
#chmod 644 /etc/keepalived/keepalived.conf

注:keepalived的配置文件的权限只能是644,否则就行报错

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/709271
推荐阅读
相关标签
  

闽ICP备14008679号