keepalived+MySQL雙主實現高可用性

    使用MySQL主主複製技術+Keepalived是一種簡單、便捷的解決方案,在高可用集羣環境中,keepalived使用虛擬VIP,使用Keepalived自帶的服務監控功能和自定義腳原本實現MySQL故障時自動切換,很是靈活。若是有一臺MySQL服務器死機,或工做出現故障,keepalived將檢測到,並將有故障的MySQL服務器從系統中去除。當MySQL服務器工做正常時,則自動將MySQL服務器加入到服務器集羣中,無需人工干預。下面環境爲debugo0一、debugo02兩個節點,使用CentOS 6.6系統。MySQL server的版本是Percona Server 5.6.21,已經配置好了兩邊的MySQL實例。html

1. 配置MySQL Multi-Master Replication

編輯my.cnf文件,添加相關複製選項。python

vim /etc/my.cnf
server-id=1002
log-bin=mysql-bin
relay-log=mysqld-relay-bin
binlog_format=mixed
replicate_ignore_db=information_schema
auto-increment-increment=2
auto-increment-offset=1

#debugo01
mysql> show master status \G
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 648
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.01 sec)
mysql> grant replication slave,replication client on *.* to rep@'%' identified by 'debugo';
mysql> flush privileges;
#debugo02
mysql> grant replication slave,replication client on *.* to rep@'%' identified by 'debugo';
mysql> flush privileges;
mysql> CHANGE MASTER TO MASTER_HOST='debugo01', MASTER_USER='rep', MASTER_PASSWORD='debugo', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=648;
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

#debugo01
mysql> CHANGE MASTER TO MASTER_HOST='debugo02', MASTER_USER='rep', MASTER_PASSWORD='debugo', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=426;
mysql> start slave;

檢查日誌,沒有錯誤說明配置成功。mysql

2. 配置keepalived

CentOS 6.6的光盤中已經帶了keepalived-1.2.13-4的軟件包,經過yum安裝便可:
yum install keepalived
完成後編輯keepalived.conf文件ios

vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   router_id Keepalived_MySQL
}
 
vrrp_script check_run {
    script "/etc/keepalived/check_MySQL.sh"
    interval 5
}
 
vrrp_sync_group VG1{
    group {
        VI_1
    }
}
 
vrrp_instance VI_1 {
    state BACKUP
    interface eth1
    virtual_router_id 51
    priority 100
    advert_int 1
    nopreempt
    track_script {
        check_run
    }
    authentication {
        auth_type PASS
        auth_pass debugo
    }
    virtual_ipaddress {
        10.6.2.200
    }
}

配置說明(參考官方文檔,Keepalived+LVS 集羣的部署keepalived.conf 文件詳解)
    keepalived 的全局配置文件對keepalived起效的配置,而VRRPD 是keepalived 的核心,LVS配置只是須要使用keepalived來配置和管理LVS時須要使用,固然若是是隻用keepalived 配置高可用集羣是,LVS模塊就不須要配置。配置文件都是以塊的形式組織的,每一個模塊的配置都須要用 { } 的範圍以內
在keepalived 的主配置文件中主要分爲三個文本模塊分別是:
全局模塊(Global Configuration),VRRPD配置模塊(VRRP 實例模塊),LVS模塊(虛擬服務模塊);
1. 全局配置(Global Configuration) sql

    包括兩個部分: 即全局定義(global definition) 和靜態地址路由(Static ipaddress /routes)。 全局定義:主要是設置Keeplived 的通知機制和標識,同時也不建議用該方法進行狀態監控,進行狀態監控時nagios 是不二的選擇。靜態地址路由:該處是配置靜態地址和靜態路由的,換句話說不隨着Vrrp instance的開關的變化而變化,固然VIP不是static IPaddress,會隨着VRRPD而添加和刪除,設置靜態地址和靜態路由的格式如:src $SRC_IP to $DST_IP dev $SRC_DEVICE 或者src $SRC_IP to $DST_IP via $GW dev $SRC_DEVICE2. VRRPD 配置包括三個部分: 分別是VRRP腳本(VRRP scripts) . VRRP同步組(syncgriuzation group) 和VRRP 實例(VRRP Instance):
2. VRRP scripts:vim

    腳本會安週期性執行,每秒( 或者按照本身設定的時間間隔),被監控的實例會記錄退出的代碼,這些腳本被至少有被監控的實例的權重爲o的實例才能執行,所以任意腳本可能的聲明都不會被系統所記錄,若是沒有特殊的設置,權重等於2,就意味着那些被監控的實例的優先級會被增長2,反過來,若是失敗,則那些實例的優先級會被減去2
3. syncgriuzation group:bash

若是不使用 VRRP Sync Groups 若是keepalived 主機有兩個網段,每一個網段開啓一個VRRP 實例,若是對外的網段出現問題,VRRPD認爲本身仍然認爲健康,所以 Master和Backup 相互切換,從而致使服務不能正常使用,同時高可用集羣也不能正常運行,Sync group 就是爲了解決該問題
注意:
router_id : 標識當前節點.兩個節點便可以相同,也能夠不一樣。
virtual_router_id : 這個標識是同一個vrrp實例使用惟一的標識。即同一個vrrp_stance,MASTER和BACKUP的virtual_router_id是一致的,同時在整個vrrp內是惟一的。
而priority 通常MASTER能夠設置大一些。
vrrp_script中,MySQL的檢測腳本以下,當檢測到MySQL失敗後,virtual_ipaddress會飄逸到BACKUP上。服務器

vim /etc/keepalived/check_MySQL.sh
#!/bin/bash
MYSQL=/usr/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
CHECK_COUNT=5

counter=1
while true
do
        mysql -h $MYSQL_HOST -u $MYSQL_USER -e "show status;"  >/dev/null 2>&1
        i=$?
        ps aux | grep mysqld | grep -v grep > /dev/null 2>&1
        j=$?
        if [ $i = 0 ] && [ $j = 0 ]
        then
                exit 0
        else
                if [ $i = 1 ] && [ $j = 0 ]
                then
                        exit 0
                else
                        if [ $counter -gt $CHECK_COUNT ]
                        then
                                break
                        fi
                let counter++
                continue
                fi
        fi
        done
/etc/init.d/keepalived stop
exit 1

3.測試

啓動keepalived
service keepalived start
此時,在/var/log/messages中能夠看到按期check_run腳本執行成功:ide

2
3
.....
Jan 21 19:58:35 debugo02 Keepalived_vrrp[22065]: VRRP_Script(check_run) succeeded
......

下面關閉debugo01(MASTER)的MySQL
service mysql stop測試

ebugo01 keepalived log
an 21 20:00:23 debugo01 Keepalived[25202]: Stopping Keepalived v1.2.13 (10/15,2014)
Jan 21 20:00:23 debugo01 Keepalived_vrrp[25204]: VRRP_Instance(VI_1) sending 0 priority
Jan 21 20:00:23 debugo01 Keepalived_vrrp[25204]: VRRP_Instance(VI_1) removing protocol VIPs.

#debugo02 keepalived log
Jan 21 19:58:35 debugo02 Keepalived_vrrp[22065]: VRRP_Script(check_run) succeeded
Jan 21 20:00:24 debugo02 Keepalived_vrrp[22065]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jan 21 20:00:24 debugo02 Keepalived_vrrp[22065]: VRRP_Group(VG1{) Syncing instances to MASTER state
Jan 21 20:00:25 debugo02 Keepalived_vrrp[22065]: VRRP_Instance(VI_1) Entering MASTER STATE
Jan 21 20:00:25 debugo02 Keepalived_vrrp[22065]: VRRP_Instance(VI_1) setting protocol VIPs.
Jan 21 20:00:25 debugo02 Keepalived_vrrp[22065]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 10.6.2.200
Jan 21 20:00:25 debugo02 Keepalived_healthcheckers[22064]: Netlink reflector reports IP 10.6.2.200 added
Jan 21 20:00:30 debugo02 Keepalived_vrrp[22065]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 10.6.2.200

檢查IP地址:

#debugo02

ip addr show

......

    inet 10.6.2.200/32 scope global eth1

......

切換成功^^

相關文章
相關標籤/搜索