mysql双主同步这里就不介绍了,请参考以下文章自动化配置
mysql【1】主从同步之人机交互自动化配置脚本-CSDN博客
首先keepalived文件夹含以下文件!
chk_mysql.sh 内容如下
d=`date --date today +%Y%m%d_%H:%M:%S`
status=$(ps -C mysqld --no-heading|wc -l)
echo $status
if [ $status -eq 0 ]; then
systemctl stop mysqld
sleep 1
systemctl start mysqld
sleep 2
status=$(ps -C mysqld --no-heading|wc -l)
if [ $status -eq 0 ]; then
mkdir -p /var/log/
echo "$d mysql服务启动失败!keepalived 将停止服务!"|tee -a /var/log/keepalive.log
systemctl stop keepalived
exit 1
fi
fi
这个脚本的作用,在发现mysql有问题启动失败后,keepalived将自动尝试恢复mysql服务,保证该节点mysql能正常,不需要故障转移,尝试失败后再故障转移!!!
在【主节点】上执行sh keepmaster.sh
设置主节点keepalived完成!
并得到配置文件
把该文件夹所有内容拷贝到【从节点】上
再【从节点】执行 sh keepslave.sh
根据提示选择网卡和设置检测mysql的脚本chk_mysql.sh!
得到配置文件内容并按任意键继续!
keepalived的【从节点】设置完成!!!
最后2节点上执行sh errlog.sh,查看keepalived的运行状态!
OK,keepalived设置成功!
接下来我们测试keepalive自动恢复mysql,
我们停止mysql服务!
等待几秒后,我们再执行sh errlog.sh
可以看到 chk_mysql执行了尝试恢复mysql成功!
执行命令
journalctl -u mysqld -xel
也能看到mysql停止后又自行恢复成功了!!
通过nativecat 使用192.168.31.88虚拟IP也能访问了!!mytest已同步!
现在我们把主节点关机,测试能否故障自动转移
再【次节点】上执行sh errlog.sh
看到此节点改变状态为Master主节点,并设置虚拟IP192.168.31.88到网卡!
通过nativecat 使用192.168.31.88虚拟IP依然能访问!!!
重点来了
keepmaster.sh的内容如下:
clear
cnf=`find / -name "keepalived.conf" `
echo 备份你的配置文件 $cnf
cp $cnf `date +%F`"[master]"${cnf##*/}
listnic=`ifconfig|grep -E "flags"|awk -F ":" '{i++}{print i":"$1}'`
while true;do
clear
echo "请选择要配置的网卡"
for item in $listnic; do
echo $item
done
read -p "输入序号即可:" n
#echo $n
unset i
for item in $listnic; do
let i++
unset nic
if [[ $i -eq $n ]]; then
nic=${item#*:}
break
fi
done
if [[ -z $nic ]]; then
read -p "请输入正确序号!"
else
break
fi
done
echo 当前网卡为:$nic
echo 选择检测脚本
ls -li *.sh|sort -t" " -k 1|awk '{print NR":"$10}'
read -p "" n
sh=`ls -li *.sh|sort -t" " -k 1|awk 'NR=="'${n}'"{print $10}'`
name=${sh%.*}
path=$(pwd)/${sh}
echo 当前检测脚本 $path
chmod 777 $path
read -p "设置虚拟IP可多个,用,逗号隔开" ips
echo 虚拟服务IP为,$ips|tr "[,]" "[\n]"
read -p "输入验证密码" auth_pass
echo 验证密码为:$auth_pass
#$(echo $ips|tr "[,]" "[\n]")
cat >$cnf <<eof
global_defs {
notification_email {
251838045@qq.com
}
notification_email_from 251838045@qq.com
smtp_server smtp.qq.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script $name {
script "$path"
interval 3
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface $nic
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass $auth_pass
}
virtual_ipaddress {
$(echo $ips|tr "[,]" "[\n ]")
}
track_script {
$name
}
}
eof
cat >keepalived.ini <<eof
ips=$ips
auth_pass=$auth_pass
eof
echo 设置keepalive服务
systemctl enable keepalived
systemctl stop keepalived
sleep 2
systemctl start keepalived
systemctl status keepalived
keepslave.sh的内容如下
clear
cnf=`find / -name "keepalived.conf" `
echo 备份你的配置文件 $cnf
cp $cnf `date +%F`"[slave]"${cnf##*/}
listnic=`ifconfig|grep -E "flags"|awk -F ":" '{i++}{print i":"$1}'`
while true;do
clear
echo "请选择要配置的网卡"
for item in $listnic; do
echo $item
done
read -p "输入序号即可:" n
#echo $n
unset i
for item in $listnic; do
let i++
unset nic
if [[ $i -eq $n ]]; then
nic=${item#*:}
break
fi
done
if [[ -z $nic ]]; then
read -p "请输入正确序号!"
else
break
fi
done
echo 当前网卡为:$nic
echo 选择检测脚本
ls -li *.sh|sort -t" " -k 1|awk '{print NR":"$10}'
read -p "" n
sh=`ls -li *.sh|sort -t" " -k 1|awk 'NR=="'${n}'"{print $10}'`
name=${sh%.*}
path=$(pwd)/${sh}
echo 当前检测脚本 $path
chmod 777 $path
if [[ ! -e ./keepalived.ini ]]; then
echo keepalived.ini not exist! please check !!!
exit
fi
while read line;do
eval $line
done <./keepalived.ini
echo 虚拟服务IP为,$ips|tr "[,]" "[\n]"
echo 验证密码为:$auth_pass
#$(echo $ips|tr "[,]" "[\n]")
cat |tee $cnf <<eof
global_defs {
notification_email {
251838045@qq.com
}
notification_email_from 251838045@qq.com
smtp_server smtp.qq.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script $name {
script "$path"
interval 3
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface $nic
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass $auth_pass
}
virtual_ipaddress {
$(echo $ips|tr "[,]" "[\n ]")
}
track_script {
$name
}
}
eof
echo 按任意键继续,ctrl+c退出
read -p ""
echo 设置keepalive服务
systemctl enable keepalived
systemctl stop keepalived
sleep 2
systemctl start keepalived
systemctl status keepalived
errlog.sh的内容如下
clear
if [[ -e /var/log/keepalive.log ]]; then
cat /var/log/keepalive.log
fi
if [[ -e ./keepalived.ini ]]; then
echo ./keepalived.ini
cmd="ip add show|grep -E '"$(cat ./keepalived.ini|awk '/ips/{print $0}'|cut -d= -f 2| tr "," "|")"'"
eval $cmd
fi
echo -e "\033[31m keepalive服务状态 \033[0m"
systemctl status keepalived -l
echo -e "\033[37m keepalive当前系统日志 \033[0m"
journalctl -u keepalived -xel
所需脚本资源皆在这!