keepalived高可用系列~第三篇文章
一 簡介:建立讀寫分離模式
二 keepalived相關配置
vrrp_instance VI_1 {
state MASTER // 可修改
interface eth0
virtual_router_id //兩邊必須一樣
priority 100 //主比從高
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
VIP/24
}
}
virtual_server VIP 3306 {
delay_loop 10
lb_kind DR
nat_mask 255.255.255.0
protocol TCP
real_server REL-IP 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 10
nb_get_retry 3
delay_before_retry 5
}
MISC_CHECK {
misc_path "/etc/keepalived/check_mysql.sh"//檢測指令碼
misc_dynamic
}
}
三 mysql監控指令碼
實現功能 1 檢測mysql down+slave stop
#!/bin/sh
function sql_sqlthread()
{
STATUS=`/usr/local/mysql/bin/mysql -S /tmp/mysql.sock -uroot -ppassword -e "show slave status\G"| grep Running`
IO_env=`echo $STATUS | grep IO | awk -F " " '{print $2}'`
SQL_env=`echo $STATUS | grep IO | awk -F " " '{print $4}'`
if [ "$IO_env" == "No" ] || [ "$SQL_env" == "No" ];then
/etc/init.d/keepalived stop
exit 1;
else
echo "nihao";
fi
}
function mysql_test()
{
STATUS=`/usr/local/mysql/bin/mysqladmin -ppassword -S /tmp/mysql.sock ping | awk '{print $3}'`
if [ "$STATUS" == "alive" ];then
echo "nihao"
sql_sqlthread
else
/etc/init.d/keepalived stop
exit 1;
fi
}
mysql_test
四 進行測試
五 總結
適用場景: 缺少機器但是需要做讀寫分離的場景,可以實現單臺從庫提供服的高可用服務