Keepalived雙機熱備

1、環境

192.168.56.11

192.168.56.12

已配製好雙主複製

虛擬IP預定爲 192.168.56.51

2、下載

http://www.keepalived.org/software/keepalived-1.2.13.tar.gz

3、安裝

1) 安裝openssl

yum install openssl openssl-devel

2)安裝keepalived

tar zxvf keepalived-1.2.13.tar.gz 
./configure --prefix=/usr/local/keepalived
make && make install

4、配製


1)建立啓動腳本

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
chmod +x /etc/init.d/keepalived


2)設置路徑

PATH=/usr/local/keepalived/sbin:/usr/local/mysql/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin


3)設置啓動參數



sed -i 's@^.*KEEPALIVED_OPTIONS.*=.*$@KEEPALIVED_OPTIONS="-D -f /usr/local/keepalived/etc/keepalived/keepalived.conf"@g' /usr/local/keepalived/etc/sysconfig/keepalived


4)啓動

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
service keepalived restart


5、配製腳本

cat /usr/local/keepalived/etc/keepalived/keepalived.conf
! Configuration File for keepalived

vrrp_script vs_mysql {
    script "/usr/local/keepalived/checkMySQL.sh -h 192.168.56.11 -P 3306"
    interval 3
}

vrrp_instance vip1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 5
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        vs_mysql
    }
    virtual_ipaddress {
        192.168.56.51
    }
}

cat /usr/local/keepalived/checkMySQL.sh
#!/bin/bash
# checkMySQL.sh
# script "/etc/keepalived/checkMySQL.sh -h 192.168.11.82 -P 3306"

function usage()
{
  echo "usage:"
  echo "example:# checkMySQL.sh -h 192.168.11.82 -P 3306"
}

function isHaveMySQL()
{
  mysqldNum=$(ps -ef | egrep -i "mysqld" | grep $1 | egrep -iv "mysqld_safe" | grep -v grep | wc -l)
  mysqlPortNum=$(netstat -tunlp | grep $1 | wc -l)
  echo $1   $mysqldNum   $mysqlPortNum
  if (( $mysqldNum >= 1  )) && (( $mysqlPortNum >= 1 ))
  then
    return 0
  else
    return 1
  fi
  return 1
}


while getopts "h:P:" option
do
    case "$option" in
        h)
            dbhost="$OPTARG";;
        P)
            dbport="$OPTARG";;
        \?)
            usage
            exit 1;;
    esac
done

if [ "-$dbhost" = "-" ]; then
	usage
	exit 1
fi

if [ "-$dbport" = "-" ]; then
	usage
	exit 1
fi

res=$(isHaveMySQL $dbport)
exit $?
檢測腳本中實際未用到ip

6、測試

先後在11與12上執行service keepalived restart啓動服務
注意:不要同時啓動,要有時間間隔
可以看到11進入主模式


而12進入備用狀態



這時通過vip訪問mysql,顯示連接到了11
G:\Tools\Console2>mysql -utest -ptest -h192.168.56.51
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.21-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select @@hostname;
+------------+
| @@hostname |
+------------+
| mysql11    |
+------------+
1 row in set (0.00 sec)


如果停止11的mysql,可以看到主備切換


12切換到了主模式

再次通過sql確認
mysql> select @@hostname;
ERROR 2013 (HY000): Lost connection to MySQL server during query
mysql> select @@hostname;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    7
Current database: *** NONE ***

+------------+
| @@hostname |
+------------+
| mysql12    |
+------------+
1 row in set (0.01 sec)
經過一次錯誤提示後,連接到了12


啓動11的mysql,11會自動切換至主模式







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章