1. 程式人生 > >Mysql+Keepalived(vrrp_script)雙主熱備高可用

Mysql+Keepalived(vrrp_script)雙主熱備高可用

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

1

2

3

4

5

6

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會漂移到另一臺上,這個過程對於客戶端的資料連線來說幾乎無感覺,從而實現高可用。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

環境描述:

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)同步前,雙方資料庫中需要同步的資料要保持一致。這樣,同步環境實現後,再次更新的資料就會如期同步了。

可能出現的問題

1

2

3

4

5

6

7

報錯:

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主主同步環境部署

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

---------------master1伺服器操作記錄---------------

在my.cnf檔案的[mysqld]配置區域新增下面內容:

[[email protected] ~]# 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      

  

[[email protected] ~]# /etc/init.d/mysql restart

Shutting down MySQL. SUCCESS!

Starting MySQL.. SUCCESS!

  

資料同步授權(iptables防火牆開啟3306埠)這樣I/O執行緒就可以以這個使用者的身份連線到主伺服器,並且讀取它的二進位制日誌。

mysql> grant replication slave,replication client on *.* to [email protected]'182.148.15.%' identified by "[email protected]";

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]配置區域新增下面內容:

[[email protected] ~]# 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

  

[[email protected] ~]# /etc/init.d/mysql restart

Shutting down MySQL.. SUCCESS!

Starting MySQL.. SUCCESS!

  

mysql> grant replication slave,replication client on *.* to [email protected]'182.148.15.%' identified by "[email protected]";

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='[email protected]',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='[email protected]',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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

1)安裝keepalived並將其配置成系統服務。master1和master2兩臺機器上同樣進行如下操作:

[[email protected] ~]# yum install -y openssl-devel

[[email protected] ~]# cd /usr/local/src/

[[email protected] src]# wget http://www.keepalived.org/software/keepalived-1.3.5.tar.gz

[[email protected] src]# tar -zvxf keepalived-1.3.5.tar.gz

[[email protected] src]# cd keepalived-1.3.5

[[email protected] keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived

[[email protected] keepalived-1.3.5]# make && make install

     

[[email protected] keepalived-1.3.5]# cp /usr/local/src/keepalived-1.3.5/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/

[[email protected] keepalived-1.3.5]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

[[email protected] keepalived-1.3.5]# mkdir /etc/keepalived/

[[email protected] keepalived-1.3.5]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

[[email protected] keepalived-1.3.5]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

[[email protected] keepalived-1.3.5]# echo "/etc/init.d/keepalived start" >> /etc/rc.local

     

2)master1機器上的keepalived.conf配置。(下面配置中沒有使用lvs的負載均衡功能,所以不需要配置虛擬伺服器virtual server)

[[email protected] ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak

[[email protected] ~]# vim /etc/keepalived/keepalived.conf       #清空預設內容,直接採用下面配置:

! Configuration File for keepalived

       

global_defs {

notification_email {

[email protected]

[email protected]

}

       

notification_email_from [email protected]

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的請求接管

[[email protected] ~]# 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

 

[[email protected] ~]# chmod 755 /opt/chk_mysql.sh

     

啟動keepalived服務

[[email protected] ~]# /etc/init.d/keepalived start

正在啟動 keepalived:                                      [確定]

     

     

4)master2機器上的keepalived配置。master2機器上的keepalived.conf檔案只修改priority為90、nopreempt不設定、real_server設定本地IP。

[[email protected] ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak

[[email protected] ~]# >/etc/keepalived/keepalived.conf

[[email protected] ~]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

       

global_defs {

notification_email {

[email protected]

[email protected]

}

       

notification_email_from [email protected]

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             

}

}

     

     

[[email protected] ~]# 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

 

[[email protected] ~]# chmod 755 /opt/chk_mysql.sh

     

[[email protected] ~]# /etc/init.d/keepalived start

正在啟動 keepalived:                                      [確定]

     

     

5)master1和master2兩臺伺服器都要授權允許root使用者遠端登入,用於在客戶端登陸測試!

mysql> grant all on *.* to [email protected]'%' 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防火牆規則,如下:

[[email protected] ~]# 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埠

   

[[email protected] ~]# /etc/init.d/iptables restart

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

1)通過Mysql客戶端通過VIP連線,看是否連線成功。

比如,在遠端一臺測試機上連線,通過vip地址可以正常連線(下面的連線許可權要是在服務端提前授權的)

[[email protected]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切換情況 

[[email protected] ~]# 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服務也無法順利啟動!)

[[email protected] ~]# /etc/init.d/mysql stop

Shutting down MySQL.. SUCCESS!

[[email protected] ~]# ps -ef|grep mysql

root     25812 21588  0 17:30 pts/0    00:00:00 grep mysql

[[email protected] ~]# ps -ef|grep keepalived

root     25814 21588  0 17:30 pts/0    00:00:00 grep keepalived

[[email protected] ~]# 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資源已經切換走了

[[email protected] ~]# 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資源的確切換過來了

[[email protected] ~]# 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的系統日誌

[[email protected] ~]# 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。

[[email protected] ~]# /etc/init.d/mysql start

Starting MySQL.. SUCCESS!

 

[[email protected] ~]# /etc/init.d/keepalived start

正在啟動 keepalived:                                      [確定]

 

啟動這兩個伺服器後,稍微等過一會兒,注意觀察會發現vip資源再次從master2機器上切換回來了。

[[email protected] ~]# 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 sc