keepalive +mysql主從複製實現高可用

安裝keepalivemysql

 

安裝依賴插件:sql

yum install -y gcc openssl-devel popt-devel

wget -q http://www.keepalived.org/software/keepalived-1.2.13.tar.gz tar -zxvf keepalived-1.2.13.tar.gz cd keepalived-1.2.13 ./configure && make && make install cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ mkdir /etc/keepalived cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/sbin/keepalived /usr/sbin/ chkconfig --add keepalived chkconfig --level 345 keepalived on

 

在master 主機上的配置bash

 

cat /etc/keepalived/keepalived.conf vrrp_script chk_mysql_port { #檢測mysql服務是否在運行。有不少方式,好比進程,用腳本檢測等等 script "/opt/chk_mysql.sh" #這裏經過腳本監測 interval 2 #腳本執行間隔,每2s檢測一次 weight -5                    #腳本結果致使的優先級變動,檢測失敗(腳本返回非0)則優先級 -5 fall 2                    #檢測連續2次失敗纔算肯定是真失敗。會用weight減小優先級(1-255之間) rise 1 #檢測1次成功就算成功。但不修改優先級 } vrrp_instance VI_1 { state MASTER interface ens33 #指定虛擬ip的網卡接口,不必定是eth0根據ifconfig肯定 virtual_router_id 51 #路由器標識,MASTER和BACKUP必須是一致的 priority 100 #定義優先級,數字越大,優先級越高,在同一個vrrp_instance下,MASTER的優先級必須大於BACKUP的優先級。這樣MASTER故障恢復後,就能夠將VIP資源再次搶回來 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.11.25 } track_script { chk_mysql_port } }

 

在slave上的配置spa

cat /etc/keepalived/keepalived.conf vrrp_script chk_mysql_port { script "/opt/chk_mysql.sh" interval 2 weight -5 fall 2 rise 1 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.11.25 } track_script { chk_mysql_port } }

檢測腳本的配置插件

 

cat /opt/chk_mysql.sh #!/bin/bash counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l) if [ "${counter}" -eq 0 ]
then
   ## /etc/init.d/keepalived stop
service keepalived stop
else echo "running..." >> /opt/keepalived-running-info.log sleep 5000 fi

注:
chk_mysql.sh 這個文件要有執行權限 chom -x chk_mysql.sh
要安裝netstat 這個命令
 
 

 

# 在master和slave上執行 //sudo /etc/init.d/keepalived start

service keepalived start //keepalived啓動code

service keepalived stop //keepalive 中止router

相關文章
相關標籤/搜索