全部都是按照网上博客的做法
服务器环境:ubuntu18.04
以下全部为root操作
- 安装keepaalive: apt-get install keepalived
- 去/etc/keepalived目录下新建keepalived.conf,并设置内容
global_defs { router_id MySQL-HA //主备一致 } vrrp_script check_run { script "/etc/keepalived/service_check.sh" //手动在该目录创建一个脚本文件,文件内容稍后附上 interval 3 weight -20 } vrrp_instance VI_1 { state MASTER //当前为主,备填写的是BACKUP interface enp2s0 //这里的网卡名称稍后截图 viual_router_id 90 //主备一致 priority 100 //当前为主,备填写的比该值低 advert_int 1 nopreempt authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.0.90 //虚拟出的ip地址 } track_script { check_run //调用上面定义的方法名 } } virtual_server 192.168.0.90 3306 { delay_loop 6 lb_algo wcl lb_kind DR #nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 192.168.0.88 3306 { weight 1 notify_down /etc/keepalived/service_down.sh TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } }
其中的interface 通过命令ifconfig获取到的
-
配置文件中涉及到的检查脚本
首先在对应目录变为
root@Vostro-3670:/etc/keepalived# ll
总用量 28
drwxr-xr-x 2 root root 4096 9月 29 11:07 ./
drwxr-xr-x 131 root root 12288 9月 29 10:41 ../
-rw-r--r-- 1 root root 1809 9月 29 11:07 keepalived.conf
-rwxrwxrwx 1 root root 1087 9月 29 08:51 service_check.sh*
-rwxrwxrwx 1 root root 38 9月 27 16:04 service_down.sh*
脚本 service_check.sh
#!/bin/bash
MYSQL=/usr/bin/mysql
MYSQL_HOST=192.168.0.88
MYSQL_USER='root'
MYSQL_PASSWORD='mypasswd'
CHECK_TIME=3
#mysql is working MYSQL_OK is 0 , mysql down MYSQL_OK is 1
MYSQL_OK=1
function check_mysql_helth (){
$MYSQL -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASSWORD -e "show status;" > /dev/null 2>&1
if [ $? = 0 ] ;then
MYSQL_OK=0
else
MYSQL_OK=1
fi
return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -= 1"
check_mysql_helth
if [ $MYSQL_OK = 0 ] ; then
CHECK_TIME=0
echo "OK"
exit 0
fi
if [ $MYSQL_OK -eq 1 ] && [ $CHECK_TIME -eq 1 ]
then
#/etc/init.d/keepalived stop
echo "failed"
pkill keepalived
exit 1
fi
sleep 1
done
service_down.sh
#!/bin/sh
pkill keepalived
最后我查看日志,只有这点信息
然后用ip a 看到对应网卡里也只有本机的静态IP