环境拓扑
chengAlived
#!/bin/bash function checkModelone(){ echo "重新进行获取" wget 192.168.158.147:3306 -O mysql echo "获取成功" echo "进行判断..." grep "5.7.17" mysql > /dev/null if [ $? -eq 0 ]; then echo "mysql正在运行..." echo "检查keepalived进程是否存在..." ps aux|grep keepalived |grep -v grep if [ $? -ne 0 ] then echo "keepalived进程丢失" echo "准备重启keepalived..." /usr/local/keepalived/sbin/keepalived -f /etc/keepalived/keepalived.conf echo "重启成功" else echo "keepalived 正在运行..." fi else echo "mysql端口异常..." echo "准备杀除keepalived进程进行漂移" chengkill keepalived echo "漂移作业完毕" fi } function checkModeltwo(){ checkModelone } while [ true ]; do /bin/sleep 10 if [ -d "mysql" ];then touch mysql else if [ -s ./mysql ]; then echo "文件不为空" checkModelone du -sh mysql else echo "文件为空" checkModeltwo fi fi done
运行规则:nohup ./chengAlived.sh &
说明+:
运行后, 会判断是否存在名字为"mysql"的文件, 否则新建, 把wget后的内容写进这个文件, 通过3306端口使用grep判断mysql版本号, 对keepalived漂移进行管理.
讲解+
第一台机器mysql主机+vip
第二台机器keepalived+lvs
启动脚本
nohup ./chengAlive.sh &
进程
ps aux | grep chengAlive.sh [root@localhost test]# ps aux | grep chengAlive.sh root 16407 0.0 0.1 113128 1412 pts/1 S 15:27 0:00 /bin/bash ./chengAlive.sh root 16410 0.0 0.1 112676 1008 pts/1 R+ 15:27 0:00 grep --color=auto chengAlive.sh
日志(mysql处于正常开启状态)
模拟宕机
假如mysql挂掉了(这是158.147)
然后keepalived查看脚本日志信息(158.149)
上面,,由于mysql已挂, kill掉keepalived, 实现漂移操作
抢占模拟
如果mysql重新启动(158.147)
再看keepalive(158.149)脚本日志
重新自动启动keepalive.抢占成功.
完毕.