编辑/etc/keepalived/keepalived.conf文件,添加以下内容:
global_defs { router_id mysql_ha_slave }
vrrp_script chk_mysql { script "/etc/keepalived/check_mysql.sh" interval 2 weight -20 fall 1 rise 1 }
vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 200 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.91.200/24 } track_script { chk_mysql } }
|
创建/etc/keepalived/check_nginx_port.sh文件,添加以下内容:
cat > /etc/keepalived/check_mysql.sh <<EOF #!/bin/bash
# 检查MySQL服务是否在运行 mysql_status=$(systemctl is-active mysqld)
if [ "$mysql_status" = "active" ]; then exit 0 # MySQL服务正常 else exit 1 # MySQL服务异常 fi EOF
|
设置脚本权限:
chmod +x /etc/keepalived/check_mysql.sh
|
启动keepalived:
systemctl start keepalived systemctl enable keepalived
|
查看日志:
tail -f /var/log/messages
|