KING 博主等级

一帆风顺 ⛵️⛵️⛵️

keepalived 实现mysql的高可用

钟晓川
2024-10-14 / 0 点赞 / 168 阅读

安装keepalived

  • Ubuntu
apt install keepalived
  • CentOS
yum install -y keepalived

检测脚本

vim /etc/keepalived/monitor.sh

#!/bin/bash

dt=`date '+%Y-%m-%d %H:%M:%S'`

mysqlmasterprocess=`docker ps | grep "mysql-master" | grep -ivE "grep|cron" | awk '{ print $1}'`
if [ -z $mysqlmasterprocess ]
then
    # docker start mysql-master
    # echo "[$dt] docker start mysql-master success" >> logs.log
    systemctl stop keepalived
    echo "192.168.98.137 mysql已关闭, keepalived以成功关闭" | mail -s "mysql服务关闭了" 1028504601@qq.com
fi

主机和备机 keepalived 配置

vim /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 LVS_DEVEL
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script check_java {
    script "/etc/keepalived/monitor_proxy.sh"
    interval 1
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.98.10
    }
    track_script {
        check_java
    }
}

virtual_ipaddress 配置虚拟IP

track_script 自定义检测脚本

Keepalived用户指南

0