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

四 进行测试

五 总结

  适用场景: 缺少机器但是需要做读写分离的场景,可以实现单台从库提供服的高可用服务

猜你喜欢

转载自www.cnblogs.com/danhuangpai/p/10143332.html
今日推荐