Mysql+Keepalived雙主熱備高可用操作記錄

散盡浮華發表於2017-04-14

 

我們通常說的雙機熱備是指兩臺機器都在執行,但並不是兩臺機器都同時在提供服務。當提供服務的一臺出現故障的時候,另外一臺會馬上自動接管並且提供服務,而且切換的時間非常短。MySQL雙主複製,即互為Master-Slave(只有一個Master提供寫操作),可以實現資料庫伺服器的熱備,但是一個Master當機後不能實現動態切換。使用Keepalived,可以通過虛擬IP,實現雙主對外的統一介面以及自動檢查、失敗切換機制,從而實現MySQL資料庫的高可用方案。之前梳理了Mysql主從/主主同步,下面說下Mysql+keeoalived雙主熱備高可用方案的實施。

Keepalived看名字就知道,保持存活,在網路裡面就是保持線上了,也就是所謂的高可用或熱備,用來防止單點故障(單點故障是指一旦某一點出現故障就會導
整個系統架構的不可用)的發生,那說到keepalived不得不說的一個協議不是VRRP協議,可以說這個協議就是keepalived實現的基礎。
1)Keepalived的工作原理是VRRP(Virtual Router Redundancy Protocol)虛擬路由冗餘協議。在VRRP中有兩組重要的概念:VRRP路由器和虛擬路由器,主控路由器和備份路由器。
2)VRRP路由器是指執行VRRP的路由器,是物理實體,虛擬路由器是指VRRP協議建立的,是邏輯概念。一組VRRP路由器協同工作,共同構成一臺虛擬路由器。 
Vrrp中存在著一種選舉機制,用以選出提供服務的路由即主控路由,其他的則成了備份路由。當主控路由失效後,備份路由中會重新選舉出一個主控路由,來繼
續工作,來保障不間斷服務。

過多內容在這裡就不做詳細介紹了,下面詳細記錄下Mysql+Keepalived雙主熱備的高可用方案的操作記錄

1)先實施Master->Slave的主主同步。主主是資料雙向同步,主從是資料單向同步。一般情況下,主庫當機後,需要手動將連線切換到從庫上。(但是用keepalived就可以自動切換)
2)再結合Keepalived的使用,通過VIP實現Mysql雙主對外連線的統一介面。即客戶端通過Vip連線資料庫;當其中一臺當機後,VIP會漂移到另一臺上,這個過程對於客戶端的資料連線來說幾乎無感覺,從而實現高可用。

環境描述:
mysql的安裝可以參考:http://www.cnblogs.com/kevingrace/p/6109679.html
Centos6.8版本
Master1:182.148.15.238        安裝mysql和keepalived
Master2: 182.148.15.237        安裝mysql和keepalived
VIP:182.148.15.236
   
要實現主主同步,可以先實現主從同步,即master1->master2的主從同步,然後master2->master1的主從同步.
這樣,雙方就完成了主主同步。

注意下面幾點:
1)要保證同步服務期間之間的網路聯通。即能相互ping通,能使用對方授權資訊連線到對方資料庫(防火牆開放3306埠)。
2)關閉selinux。
3)同步前,雙方資料庫中需要同步的資料要保持一致。這樣,同步環境實現後,再次更新的資料就會如期同步了。

可能出現的問題

報錯:
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).
 
解決辦法:
刪除mysql資料目錄下的auto.cnf檔案,重啟mysql服務即可!

另:Keepalived必須使用root賬號啟動!!

一、Mysql主主同步環境部署

---------------master1伺服器操作記錄---------------
在my.cnf檔案的[mysqld]配置區域新增下面內容:
[root@master1 ~]# vim /usr/local/mysql/my.cnf
server-id = 1          
log-bin = mysql-bin      
sync_binlog = 1
binlog_checksum = none
binlog_format = mixed
auto-increment-increment = 2      
auto-increment-offset = 1     
slave-skip-errors = all       
 
[root@master1 ~]# /etc/init.d/mysql restart
Shutting down MySQL. SUCCESS!
Starting MySQL.. SUCCESS!
 
資料同步授權(iptables防火牆開啟3306埠)這樣I/O執行緒就可以以這個使用者的身份連線到主伺服器,並且讀取它的二進位制日誌。
mysql> grant replication slave,replication client on *.* to wang@'182.148.15.%' identified by "wang@123";
Query OK, 0 rows affected (0.00 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
最好將庫鎖住,僅僅允許讀,以保證資料一致性;待主主同步環境部署後再解鎖;
鎖住後,就不能往表裡寫資料,但是重啟mysql服務後就會自動解鎖!
mysql> flush tables with read lock;  //注意該引數設定後,如果自己同步對方資料,同步前一定要記得先解鎖!
Query OK, 0 rows affected (0.00 sec)
 
檢視下log bin日誌和pos值位置
mysql> show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin.000004 |      430 |              | mysql,information_schema |                   |
+------------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec)
 
---------------master2伺服器操作記錄---------------
在my.cnf檔案的[mysqld]配置區域新增下面內容:
[root@master2 ~]# vim /usr/local/mysql/my.cnf
server-id = 2         
log-bin = mysql-bin     
sync_binlog = 1
binlog_checksum = none
binlog_format = mixed
auto-increment-increment = 2      
auto-increment-offset = 2     
slave-skip-errors = all 
 
[root@master2 ~]# /etc/init.d/mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
 
mysql> grant replication slave,replication client on *.* to wang@'182.148.15.%' identified by "wang@123";
Query OK, 0 rows affected (0.00 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
 
mysql> show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin.000003 |      430 |              | mysql,information_schema |                   |
+------------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec)
 
---------------master1伺服器做同步操作---------------
mysql> unlock tables;     //先解鎖,將對方資料同步到自己的資料庫中
mysql> slave stop;
mysql> change  master to master_host='182.148.15.237',master_user='wang',master_password='wang@123',master_log_file='mysql-bin.000003',master_log_pos=430;          
Query OK, 0 rows affected, 2 warnings (0.01 sec)
 
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
 
檢視同步狀態,如下出現兩個“Yes”,表明同步成功!
mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 182.148.15.237
                  Master_User: wang
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 430
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 279
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
        .........................
        Seconds_Behind_Master: 0
        .........................

這樣,master1就和master2實現了主從同步,即master1同步master2的資料。
 
---------------master2伺服器做同步操作---------------
mysql> unlock tables;     //先解鎖,將對方資料同步到自己的資料庫中
mysql> slave stop;
mysql> change  master to master_host='182.148.15.238',master_user='wang',master_password='wang@123',master_log_file='mysql-bin.000004',master_log_pos=430;   
Query OK, 0 rows affected, 2 warnings (0.06 sec)
 
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
 
mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 182.148.15.238
                  Master_User: wang
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 430
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 279
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
        ........................
        Seconds_Behind_Master: 0
        ........................
 
這樣,master2就和master1實現了主從同步,即master2也同步master1的資料。
 
以上表明雙方已經實現了mysql主主同步。
當執行一段時間後,要是發現同步有問題,比如只能單向同步,雙向同步失效。可以重新執行下上面的change master同步操作,只不過這樣同步後,只能同步在此之後的更新資料。下面開始進行資料驗證:
 
-----------------主主同步效果驗證--------------------- 
1)在master1資料庫上寫入新資料
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
 
mysql> create database huanqiu;
Query OK, 1 row affected (0.01 sec)
 
mysql> use huanqiu;
Database changed
 
mysql> create table if not exists haha (
    -> id int(10) PRIMARY KEY AUTO_INCREMENT,
    -> name varchar(50) NOT NULL);
Query OK, 0 rows affected (0.04 sec)
 
mysql> insert into haha values(1,"王士博");
Query OK, 1 row affected (0.00 sec)
 
mysql> insert into haha values(2,"郭慧慧");
Query OK, 1 row affected (0.00 sec)
 
mysql> select * from haha;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | 王士博    |
|  2 | 郭慧慧    |
+----+-----------+
2 rows in set (0.00 sec)
 
然後在master2資料庫上檢視,發現資料已經同步過來了!
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| huanqiu            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)
 
mysql> use huanqiu;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> show tables;
+-------------------+
| Tables_in_huanqiu |
+-------------------+
| haha              |
+-------------------+
1 row in set (0.00 sec)
 
mysql> select * from haha;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | 王士博    |
|  2 | 郭慧慧    |
+----+-----------+
2 rows in set (0.00 sec)
 
2)在master2資料庫上寫入新資料
mysql> create database hehe;
Query OK, 1 row affected (0.00 sec)
 
mysql> insert into huanqiu.haha values(3,"周正"),(4,"李敏");
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
 
然後在master1資料庫上檢視,發現資料也已經同步過來了!
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hehe               |
| huanqiu            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.00 sec)
 
mysql> select * from huanqiu.haha;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | 王士博    |
|  2 | 郭慧慧    |
|  3 | 周正      |
|  4 | 李敏      |
+----+-----------+
4 rows in set (0.00 sec)
 
至此,Mysql主主同步環境已經實現。

二、配置Mysql+Keepalived故障轉移的高可用環境

1)安裝keepalived並將其配置成系統服務。master1和master2兩臺機器上同樣進行如下操作:
[root@master1 ~]# yum install -y openssl-devel
[root@master1 ~]# cd /usr/local/src/
[root@master1 src]# wget http://www.keepalived.org/software/keepalived-1.3.5.tar.gz
[root@master1 src]# tar -zvxf keepalived-1.3.5.tar.gz
[root@master1 src]# cd keepalived-1.3.5
[root@master1 keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived
[root@master1 keepalived-1.3.5]# make && make install
    
[root@master1 keepalived-1.3.5]# cp /usr/local/src/keepalived-1.3.5/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/
[root@master1 keepalived-1.3.5]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@master1 keepalived-1.3.5]# mkdir /etc/keepalived/
[root@master1 keepalived-1.3.5]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@master1 keepalived-1.3.5]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@master1 keepalived-1.3.5]# echo "/etc/init.d/keepalived start" >> /etc/rc.local
    
2)master1機器上的keepalived.conf配置。(下面配置中沒有使用lvs的負載均衡功能,所以不需要配置虛擬伺服器virtual server)
[root@master1 ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[root@master1 ~]# vim /etc/keepalived/keepalived.conf       #清空預設內容,直接採用下面配置:
! Configuration File for keepalived
      
global_defs {
notification_email { 
ops@wangshibo.cn
tech@wangshibo.cn
}
      
notification_email_from ops@wangshibo.cn
smtp_server 127.0.0.1  
smtp_connect_timeout 30
router_id MASTER-HA
}
      
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 eth0      #指定虛擬ip的網路卡介面
    mcast_src_ip 182.148.15.238
    virtual_router_id 51    #路由器標識,MASTER和BACKUP必須是一致的
    priority 101            #定義優先順序,數字越大,優先順序越高,在同一個vrrp_instance下,MASTER的優先順序必須大於BACKUP的優先順序。這樣MASTER故障恢復後,就可以將VIP資源再次搶回來  
    advert_int 1          
    authentication {    
        auth_type PASS  
        auth_pass 1111      
    }
    virtual_ipaddress {     
        182.148.15.236
    }
     
track_script {                
   chk_mysql_port              
}
}
    
編寫切換指令碼。KeepAlived做心跳檢測,如果Master的MySQL服務掛了(3306埠掛了),那麼它就會選擇自殺。Slave的KeepAlived通過心跳檢測發現這個情況,就會將VIP的請求接管
[root@master1 ~]# vim /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
fi

[root@master1 ~]# chmod 755 /opt/chk_mysql.sh
    
啟動keepalived服務
[root@master1 ~]# /etc/init.d/keepalived start
正在啟動 keepalived:                                      [確定]
    
    
4)master2機器上的keepalived配置。master2機器上的keepalived.conf檔案只修改priority為90、nopreempt不設定、real_server設定本地IP。
[root@master2 ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[root@master2 ~]# >/etc/keepalived/keepalived.conf
[root@master2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
      
global_defs {
notification_email { 
ops@wangshibo.cn
tech@wangshibo.cn
}
      
notification_email_from ops@wangshibo.cn
smtp_server 127.0.0.1  
smtp_connect_timeout 30
router_id MASTER-HA
}
      
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 eth0     
    mcast_src_ip 182.148.15.237
    virtual_router_id 51     
    priority 99           
    advert_int 1          
    authentication {    
        auth_type PASS  
        auth_pass 1111      
    }
    virtual_ipaddress {     
        182.148.15.236
    }
     
track_script {                
   chk_mysql_port              
}
}
    
    
[root@master2 ~]# 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
fi

[root@master2 ~]# chmod 755 /opt/chk_mysql.sh
    
[root@master2 ~]# /etc/init.d/keepalived start
正在啟動 keepalived:                                      [確定]
    
    
5)master1和master2兩臺伺服器都要授權允許root使用者遠端登入,用於在客戶端登陸測試!
mysql> grant all on *.* to root@'%' identified by "1234567";
Query OK, 0 rows affected (0.00 sec)
    
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
  
6)在master1和master2兩臺機器上設定iptables防火牆規則,如下:
[root@master1 ~]# cat /etc/sysconfig/iptables
........
-A INPUT -s 182.148.15.0/24 -d 224.0.0.18 -j ACCEPT       #允許組播地址通訊
-A INPUT -s 182.148.15.0/24 -p vrrp -j ACCEPT             #允許VRRP(虛擬路由器冗餘協)通訊
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT    #開放mysql的3306埠
  
[root@master1 ~]# /etc/init.d/iptables restart

三、Mysql+keepalived故障轉移的高可用測試

1)通過Mysql客戶端通過VIP連線,看是否連線成功。
比如,在遠端一臺測試機上連線,通過vip地址可以正常連線(下面的連線許可權要是在服務端提前授權的)
[root@dev-new-test ~]# mysql -h182.148.15.236 -uroot -p123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 60
Server version: 5.6.35-log Source distribution

Copyright (c) 2000, 2013, 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 * from huanqiu.haha;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | 王士博    |
|  2 | 郭慧慧    |
|  3 | 周正      |
|  4 | 李敏      |
+----+-----------+
4 rows in set (0.00 sec)


2)預設情況下,vip是在master1上的。使用"ip addr"命令檢視vip切換情況  
[root@master1 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:3c:25:42 brd ff:ff:ff:ff:ff:ff
    inet 182.148.15.238/27 brd 182.148.15.255 scope global eth0
    inet 182.148.15.236/32 scope global eth0                              //這個32位子網掩碼的vip地址表示該資源目前還在master1機器上
    inet 182.148.15.236/27 brd 82.48.115.255 scope global secondary eth0:0
    inet6 fe80::5054:ff:fe3c:2542/64 scope link 
       valid_lft forever preferred_lft forever

停止master1機器上的mysql服務,根據配置中的指令碼,mysql服務停了,keepalived也會停,從而vip資源將會切換到master2機器上。(mysql服務沒有起來的時候,keepalived服務也無法順利啟動!)
[root@master1 ~]# /etc/init.d/mysql stop
Shutting down MySQL.. SUCCESS! 
[root@master1 ~]# ps -ef|grep mysql
root     25812 21588  0 17:30 pts/0    00:00:00 grep mysql
[root@master1 ~]# ps -ef|grep keepalived
root     25814 21588  0 17:30 pts/0    00:00:00 grep keepalived
[root@master1 ~]# ip addr                   
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:3c:25:42 brd ff:ff:ff:ff:ff:ff
    inet 182.148.15.238/27 brd 182.148.15.255 scope global eth0
    inet 182.148.15.236/27 brd 82.48.115.255 scope global secondary eth0:0
    inet6 fe80::5054:ff:fe3c:2542/64 scope link 
       valid_lft forever preferred_lft forever

如上結果,發現32位子網掩碼的vip沒有了,說明此時vip資源已不在master1機器上了
檢視下master1的系統日誌,如下,會發現vip資源已經切換走了
[root@master1 ~]# tail -f /var/log/messages 
Apr 15 17:17:43 localhost Keepalived_vrrp[23037]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:17:48 localhost Keepalived_vrrp[23037]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:17:48 localhost Keepalived_vrrp[23037]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 182.148.15.236
Apr 15 17:17:48 localhost Keepalived_vrrp[23037]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:17:48 localhost Keepalived_vrrp[23037]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:17:48 localhost Keepalived_vrrp[23037]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:17:48 localhost Keepalived_vrrp[23037]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:30:39 localhost Keepalived_healthcheckers[23036]: Stopped
Apr 15 17:30:39 localhost Keepalived_vrrp[23037]: VRRP_Instance(VI_1) sent 0 priority
Apr 15 17:30:39 localhost Keepalived_vrrp[23037]: VRRP_Instance(VI_1) removing protocol VIPs.

再到master2機器上,發現vip資源的確切換過來了
[root@master2 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:95:1f:6d brd ff:ff:ff:ff:ff:ff
    inet 182.148.15.237/27 brd 182.148.15.255 scope global eth0
    inet 182.148.15.236/32 scope global eth0
    inet6 fe80::5054:ff:fe95:1f6d/64 scope link 
       valid_lft forever preferred_lft forever

檢視master2的系統日誌
[root@master2 ~]# tail -f /var/log/messages 
Apr 15 17:30:41 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:30:41 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:30:41 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:30:41 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 182.148.15.236
Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236

3)再次啟動master1的mysql和keepalived服務。(注意:如果restart重啟mysql,那麼還要啟動下keepalived,因為mysql重啟,根據指令碼會造成keepalived關閉)
注意:一定要先啟動mysql服務,然後再啟動keepalived服務。如果先啟動keepalived服務,按照上面的配置,mysql沒有起來,就會自動關閉keepalived。
[root@master1 ~]# /etc/init.d/mysql start
Starting MySQL.. SUCCESS! 

[root@master1 ~]# /etc/init.d/keepalived start
正在啟動 keepalived:                                      [確定]

啟動這兩個伺服器後,稍微等過一會兒,注意觀察會發現vip資源再次從master2機器上切換回來了。
[root@master1 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:3c:25:42 brd ff:ff:ff:ff:ff:ff
    inet 182.148.15.238/27 brd 182.148.15.255 scope global eth0
    inet 182.148.15.236/32 scope global eth0
    inet 182.148.15.236/27 brd 82.48.115.255 scope global secondary eth0:0
    inet6 fe80::5054:ff:fe3c:2542/64 scope link 
       valid_lft forever preferred_lft forever

[root@master1 ~]# tail -f /var/log/messages 
Apr 15 17:40:41 localhost Keepalived_vrrp[27002]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:40:41 localhost Keepalived_vrrp[27002]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:40:41 localhost Keepalived_vrrp[27002]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:40:41 localhost Keepalived_vrrp[27002]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:40:46 localhost Keepalived_vrrp[27002]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:40:46 localhost Keepalived_vrrp[27002]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 182.148.15.236
Apr 15 17:40:46 localhost Keepalived_vrrp[27002]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:40:46 localhost Keepalived_vrrp[27002]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:40:46 localhost Keepalived_vrrp[27002]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:40:46 localhost Keepalived_vrrp[27002]: Sending gratuitous ARP on eth0 for 182.148.15.236

再看看master2機器,發現vip資源又被恢復後的master1搶過去了
[root@master2 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:95:1f:6d brd ff:ff:ff:ff:ff:ff
    inet 182.148.15.237/27 brd 182.148.15.255 scope global eth0
    inet6 fe80::5054:ff:fe95:1f6d/64 scope link 
       valid_lft forever preferred_lft forever

[root@master2 ~]# tail -f /var/log/messages 
Apr 15 17:30:41 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 182.148.15.236
Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236
Apr 15 17:40:41 localhost Keepalived_vrrp[8731]: VRRP_Instance(VI_1) Received advert with higher priority 101, ours 99
Apr 15 17:40:41 localhost Keepalived_vrrp[8731]: VRRP_Instance(VI_1) Entering BACKUP STATE
Apr 15 17:40:41 localhost Keepalived_vrrp[8731]: VRRP_Instance(VI_1) removing protocol VIPs.

4)同樣,關閉master1機器的keepalived服務,vip資源會自動切換到master2機器上。當master1的keepalived服務恢復後,會將vip資源再次切回來。
[root@master1 ~]# /etc/init.d/keepalived stop
停止 keepalived:                                          [確定]
[root@master1 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:3c:25:42 brd ff:ff:ff:ff:ff:ff
    inet 182.148.15.238/27 brd 182.148.15.255 scope global eth0
    inet 182.148.15.236/27 brd 82.48.115.255 scope global secondary eth0:0
    inet6 fe80::5054:ff:fe3c:2542/64 scope link 
       valid_lft forever preferred_lft forever

檢視master2,發現vip切過來了
[root@master2 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:95:1f:6d brd ff:ff:ff:ff:ff:ff
    inet 182.148.15.237/27 brd 182.148.15.255 scope global eth0
    inet 182.148.15.236/32 scope global eth0
    inet6 fe80::5054:ff:fe95:1f6d/64 scope link 
       valid_lft forever preferred_lft forever

再次恢復master1的keepalived服務,發現vip資源很快油切回來了。
[root@master1 ~]# /etc/init.d/keepalived start
正在啟動 keepalived:                                      [確定]
[root@master1 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:3c:25:42 brd ff:ff:ff:ff:ff:ff
    inet 182.148.15.238/27 brd 182.148.15.255 scope global eth0
    inet 182.148.15.236/32 scope global eth0
    inet 182.148.15.236/27 brd 82.48.115.255 scope global secondary eth0:0
    inet6 fe80::5054:ff:fe3c:2542/64 scope link 
       valid_lft forever preferred_lft forever

在此檢視master2,發現vip資源被切走了
[root@master2 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:95:1f:6d brd ff:ff:ff:ff:ff:ff
    inet 182.148.15.237/27 brd 182.148.15.255 scope global eth0
    inet6 fe80::5054:ff:fe95:1f6d/64 scope link 
       valid_lft forever preferred_lft forever

以上在vip資源切換過程中,對於客戶端連線mysql(使用vip連線)來說幾乎是沒有任何影響的。

------------------------------------溫馨提示(Keepalived的搶佔和非搶佔模式)---------------------------------------

keepalive是基於vrrp協議在linux主機上以守護程式方式,根據配置檔案實現健康檢查。
VRRP是一種選擇協議,它可以把一個虛擬路由器的責任動態分配到區域網上的VRRP路由器中的一臺。
控制虛擬路由器IP地址的VRRP路由器稱為主路由器,它負責轉發資料包到這些虛擬IP地址。
一旦主路由器不可用,這種選擇過程就提供了動態的故障轉移機制,這就允許虛擬路由器的IP地址可以作為終端主機的預設第一跳路由器。

keepalive通過組播,單播等方式(自定義),實現keepalive主備推選。工作模式分為搶佔和非搶佔(通過引數nopreempt來控制)。
1)搶佔模式:
主服務正常工作時,虛擬IP會在主上,備不提供服務,當主服務優先順序低於備的時候,備會自動搶佔虛擬IP,這時,主不提供服務,備提供服務。
也就是說,工作在搶佔模式下,不分主備,只管優先順序。

如上配置,不管keepalived.conf裡的state配置成master還是backup,只看誰的priority優先順序高(一般而言,state為MASTER的優先順序要高於BACKUP)。
priority優先順序高的那一個在故障恢復後,會自動將VIP資源再次搶佔回來!!

2)非搶佔模式:
這種方式通過引數nopreempt(一般設定在advert_int的那一行下面)來控制。不管priority優先順序,只要MASTER機器發生故障,VIP資源就會被切換到BACKUP上。
並且當MASTER機器恢復後,也不會去將VIP資源搶佔回來,直至BACKUP機器發生故障時,才能自動切換回來。

千萬注意: 
nopreempt這個引數只能用於state為backup的情況,所以在配置的時候要把master和backup的state都設定成backup,這樣才會實現keepalived的非搶佔模式!

也就是說:
a)當state狀態一個為master,一個為backup的時候,加不加nopreempt這個引數都是一樣的效果。即都是根據priority優先順序來決定誰搶佔vip資源的,是搶佔模式!
b)當state狀態都設定成backup,如果不配置nopreempt引數,那麼也是看priority優先順序決定誰搶佔vip資源,即也是搶佔模式。
c)當state狀態都設定成backup,如果配置nopreempt引數,那麼就不會去考慮priority優先順序了,是非搶佔模式!即只有vip當前所在機器發生故障,另一臺機器才能接管vip。即使優先順序高的那一臺機器恢復  後也不會主動搶回vip,只能等到對方發生故障,才會將vip切回來。

---------------------------------mysql狀態檢測指令碼優化---------------------------------

上面的mysql監測指令碼有點過於簡單且粗暴,即指令碼一旦監測到Master的mysql服務關閉,就立刻把keepalived服務關閉,從而實現vip轉移!

下面對該指令碼進行優化,優化後,當監測到Master的mysql服務關閉後,就會將vip切換到Backup上(但此時Master的keepalived服務不會被暴力kill)
當Master的mysql服務恢復後,就會再次將VIP資源切回來!

[root@master ~]# cat /opt/chk_mysql.sh
#!/bin/bash
MYSQL=/usr/local/mysql/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=123456
CHECK_TIME=3
 
#mysql  is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0
 
MYSQL_OK=1
 
function check_mysql_helth (){
    $MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "show status;" >/dev/null 2>&1
    if [ $? = 0 ] ;then
    MYSQL_OK=1
    else
    MYSQL_OK=0
    fi
    return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
    let "CHECK_TIME -= 1"
    check_mysql_helth
if [ $MYSQL_OK = 1 ] ; then
    CHECK_TIME=0
    exit 0
fi
if [ $MYSQL_OK -eq 0 ] &&  [ $CHECK_TIME -eq 0 ]
then
    pkill keepalived
    exit 1
fi
sleep 1
done

相關文章