咱們一般說的雙機熱備是指兩臺機器都在運行,但並非兩臺機器都同時在提供服務。當提供服務的一臺出現故障的時候,另一臺會立刻自動接管而且提供服務,並且切換的時間很是短。MySQL雙主複製,即互爲Master-Slave(只有一個Master提供寫操做),能夠實現數據庫服務器的熱備,可是一個Master宕機後不能實現動態切換。使用Keepalived,能夠經過虛擬IP,實現雙主對外的統一接口以及自動檢查、失敗切換機制,從而實現MySQL數據庫的高可用方案。以前梳理了Mysql主從/主主同步,下面說下Mysql+keeoalived雙主熱備高可用方案的實施。html
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中存在着一種選舉機制,用以選出提供服務的路由即主控路由,其餘的則成了備份路由。當主控路由失效後,備份路由中會從新選舉出一個主控路由,來繼
續工做,來保障不間斷服務。
|
Slave的主主同步。主主是數據雙向同步,主從是數據單向同步。通常狀況下,主庫宕機後,須要手動將鏈接切換到從庫上。(可是用keepalived就能夠自動切換) 2)再結合Keepalived的使用,經過VIP實現Mysql雙主對外鏈接的統一接口。即客戶端經過Vip鏈接數據庫;當其中一臺宕機後,VIP會漂移到另外一臺上,這個過程對於客戶端的數據鏈接來講幾乎無感受,從而實現高可用。 mysql
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)同步前,雙方數據庫中須要同步的數據要保持一致。這樣,同步環境實現後,再次更新的數據就會如期同步了。
|
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).
linux
解決辦法:
刪除mysql數據目錄下的auto.cnf文件,重啓mysql服務便可!
另:Keepalived必須使用root帳號啓動!!
1、Mysql主主同步環境部署git
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]配置區域添加下面內容:
[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主主同步環境已經實現。
|
2、配置Mysql+Keepalived故障轉移的高可用環境sql
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兩臺機器上一樣進行以下操做:
[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
|
3、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地址能夠正常鏈接(下面的鏈接權限要是在服務端提早受權的)
[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的搶佔和非搶佔模式)---------------------------------------vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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狀態檢測腳本優化---------------------------------bash
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
|
上面的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
|
« 上一篇:更換Ubuntu源爲國內源的操做記錄
» 下一篇:Linux下防護DDOS攻擊的操做梳理服務器