利用keepalived構建高可用MySQL-HA
一、mysql 主從複製
參考http://blog.csdn.net/u013378306/article/details/56837766
二、keepalived安裝及配置
注意的地方:
兩臺伺服器設定的vip必須一樣
virtual_router_id 必須一樣
原理:
主服務使用虛擬vip 優先,當主服務關掉後,從服務啟用虛擬vip
mysql 服務地址寫為vip地址
1、192.168.1.201伺服器上keepalived安裝及配置
安裝keepalived
#tar zxvf keepalived-1.1.20.tar.gz
#cd keepalived-1.1.20
#./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686
(注意這裡2.6.18-164.el5-i686版本自己到伺服器的/usr/src/kernels目錄下去看,寫當前伺服器中存在的版本)
#make && make install
[root@master2 ~]#/usr/local/keepalived/sbin/keepalived -D
[root@master2 ~]# ps -aux|grep keepalived
Warning: bad syntax, perhaps a bogus '-'?See /usr/share/doc/procps-3.2.7/FAQ
root 4101 0.3 0.1 35828 632 ? Ss 15:41 0:00/usr/local/keepalived/sbin/keepalived -D
root 4102 0.7 0.4 39988 1620 ? S 15:41 0:00/usr/local/keepalived/sbin/keepalived -D
root 4103 0.5 0.3 39988 1092 ? S 15:41 0:00/usr/local/keepalived/sbin/keepalived -D
root 4106 0.0 0.2 61136 716 pts/0 R+ 15:41 0:00 grep keepalived
配置keepalived
ln -sv /usr/src/kernels/2.6.18-194.el5-x86_64/ /usr/src/Linux
我們自己在新建一個配置檔案,預設情況下keepalived啟動時會去/etc/keepalived目錄下找配置檔案
#mkdir /etc/keepalived
#vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
luwenju@live.cn
}
notification_email_from luwenju@live.cn
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MySQL-ha
}
vrrp_instance VI_1 {
state BACKUP #兩臺配置此處均是BACKUP
interface eth0 #網路卡,可使用ifconfig檢視
virtual_router_id 51
priority 100 #優先順序,另一臺改為90
advert_int 1
nopreempt #不搶佔,只在優先順序高的機器上設定即可,優先順序低的機器不設定
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.200
}
}
virtual_server 192.168.1.200 3306 {
delay_loop 2 #每個2秒檢查一次real_server狀態
lb_algo wrr #LVS演算法
lb_kind DR #LVS模式
persistence_timeout 60 #會話保持時間
protocol TCP
real_server 192.168.1.201 3306 {
weight 3
notify_down /usr/local/MySQL/bin/MySQL.sh #檢測到服務down後執行的指令碼
TCP_CHECK {
connect_timeout 10 #連線超時時間
nb_get_retry 3 #重連次數
delay_before_retry 3 #重連間隔時間
connect_port 3306 #健康檢查埠
}
}
編寫檢測服務down後所要執行的指令碼
#vi /usr/local/MySQL/bin/MySQL.sh
#!/bin/sh
pkill keepalived
#chmod +x /usr/local/MySQL/bin/MySQL.sh
注:此指令碼是上面配置檔案notify_down選項所用到的,keepalived使用notify_down選項來檢查real_server的服務狀態,當發現real_server服務故障時,便觸發此指令碼;我們可以看到,指令碼就一個命令,通過pkillkeepalived強制殺死keepalived程式,從而實現了MySQL故障自動轉移。另外,我們不用擔心兩個MySQL會同時提供資料更新操作,因為每臺MySQL上的keepalived的配置裡面只有本機MySQL的IP+VIP,而不是兩臺MySQL的IP+VIP
啟動keepalived
#/usr/local/keepalived/sbin/keepalived –D
#ps -aux | grep keepalived
測試
找一臺區域網PC,然後去ping MySQL的VIP,這時候MySQL的VIP是可以ping的通的
停止MySQL服務,看keepalived健康檢查程式是否會觸發我們編寫的指令碼
2、192.168.1.202上keepalived安裝及配置
安裝keepalived
#tar zxvf keepalived-1.1.20.tar.gz
#cd keepalived-1.1.20
#./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686
#make && make install
配置keepalived
這臺配置和上面基本一樣,但有三個地方不同:優先順序為90、無搶佔設定、real_server為本機IP
#mkdir /etc/keepalived
#vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
luwenju@live.cn
}
notification_email_from luwenju@live.cn
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MySQL-ha
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.200
}
}
virtual_server 192.168.1.200 3306 {
delay_loop 2
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.1.202 3306 {
weight 3
notify_down /usr/local/MySQL/bin/MySQL.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
編寫檢測服務down後所要執行的指令碼
#vi /usr/local/MySQL/bin/MySQL.sh
#!/bin/sh
pkill keepalived
#注意此處強制停掉可能會導致vip強佔,會導致多服務起都會使用vip,可以改為service keepalived stop
#chmod +x /usr/local/MySQL/bin/MySQL.sh
啟動keepalived
#/usr/local/keepalived/sbin/keepalived –D
#ps -aux | grep keepalived
測試
停止MySQL服務,看keepalived健康檢查程式是否會觸發我們編寫的指令碼
三、測試
MySQL遠端登入測試
我們找一臺安裝有MySQL客戶端的windows,然後登入VIP,看是否能登入,在登入之兩臺MySQL伺服器都要授權允許從遠端登入
MySQL> grant all privileges on *.* to 'root'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MySQL> flush privileges;
Query OK, 0 rows affected (0.00 sec)
使用客戶端登入VIP測試
C:\MySQL\bin>MySQL.exe -uroot -p123456 -h192.168.1.200 -P3306
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 224
Server version: 5.0.89-log Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL>
● keepalived故障轉移測試
※在windows客戶端一直去ping VIP,然後關閉192.168.1.201上的keepalived,正常情況下VIP就會切換到192.168.1.202上面去
※開啟192.168.1.201上的keepalived,關閉192.168.1.202上的keepalived,看是否能自動切換,正常情況下VIP又會屬於192.168.1.201
注:keepalived切換速度還是非常塊的,整個切換過程只需1-3秒
● MySQL故障轉移測試
※在192.168.1.201上關閉MySQL服務,看VIP是否會切換到192.168.1.202上
※開啟192.168.1.201上的MySQL和keepalived,然後關閉192.168.1.202上的MySQL,看VIP是否會切換到192.168.1.201上
下面是用windows客戶端連線的MySQL的VIP,在切換時我執行了一個MySQL查詢命令,從執行show databases到顯示出結果時間為3-5秒(大家可以看到上面有個錯誤提示,不過不用擔心,因為我們的keepalived切換大概為3秒左右,這3秒左右VIP是誰都不屬於的)
MySQL> show databases;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 592
Current database: *** NONE ***
+--------------------+
| Database |
+--------------------+
| information_schema |
| MySQL |
| test |
+--------------------+
3 rows in set (9.01 sec)
後話:世間萬事萬物,都不具備絕對的完美,就像上面的MySQL-HA一樣,keepalived只能做到對3306的健康檢查,但是做不到比如像MySQL複製中的slave-SQL、slave-IO程式的檢查。所以要想做到一些細緻的健康檢查,還得需要藉助額外的監控工具,比如nagios,然後用nagios實現簡訊、郵件報警,從而能夠有效地解決問題。
mysql> GRANT REPLICATION SLAVE ON *.*
-> TO 'repl'@'%.mydomain.com' IDENTIFIED BY 'slavepass';
如果你沒有備份主伺服器,這裡是一個建立備份的快速程式。所有步驟都應該在主伺服器主機上執行。
1. 發出該語句:
mysql> FLUSH TABLES WITH READ LOCK;
2. 仍然加鎖時,執行該命令(或它的變體):
shell> tar zcf /tmp/backup.tar.gz /var/lib/mysql
3. 發出該語句並且確保記錄了以後用到的輸出:
mysql>SHOW MASTER STATUS;
4. 釋放鎖:
mysql> UNLOCK TABLES;
一個可選擇的方法是,轉儲主伺服器的SQL來代替前面步驟中的二進位制複製。要這樣做,你可以在主伺服器上使用mysqldump --master-data,以後裝載SQL轉儲到到你的從伺服器。
另一篇
一、概述
Keepalived看名字就知道,保持存活,在網路裡面就是保持線上了,也就是所謂的高可用或熱備,用來防止單點故障(單點故障是指一旦某一點出現故障就會導致整個系統架構的不可用)的發生,那說到keepalived不得不說的一個協議不是VRRP協議,可以說這個協議就是keepalived實現的基礎。
二、配置實現mysql+keepalived主從切換
網路結構:
VIP :192.168.1.172
MYSQL1:192.168.1.100
MYSQL2:192.168.1.104
其中MYSQL1為主伺服器
MYSQL2為從伺服器
1、mysql主主同步
要實現MySQL+keepalived主從切換,首先要實現的就是兩臺mysql伺服器的主主同步。關於主主同步,可以參考之前的文章《mysql資料庫遠端同步及備份 》
2、安裝keepalived
準備好原始碼包keepalived-1.2.7.tar.gz,然後將其放置在home/software目錄下面:
#tar zxvf keepalived-1.2.7.tar.gz
#cd keepalived-1.2.7
#./configure
出錯:
sudo apt-get install openssl
# apt-get install libpopt-dev
sudo apt-get install libssl-dev
sudo apt-get install libssl0.9.8
#make & make install
3、keepalived主備配置檔案
下面將詳細講述在MYSQL主伺服器上對keepalived的配置。
由於keepalived啟動的時候,預設到/etc目錄下面去讀取配置檔案,所以要先在/etc目錄下建立一目錄keepalived
#mkdir –p /etc/keepalived
#cd /etc/keepalived
#vin keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
380934769@qq.com
}
notification_email_from 380934769@qq.com
smtp_connect_timeout 3
smtp_server 127.0.0.1
router_id MySQL-ha
}
vrrp_script check_run { #宣告vrrp_script 的函式check_run
script "/root/keepalived_check_mysql.sh"
interval 5
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state MASTER #指定主伺服器節點為主節點,備用節點上設定為BACKUP即可
interface eth0 #指定虛擬IP的網路介面
virtual_router_id 88 #VRRP組名,兩個節點的設定必須一樣,以指明各個節點屬於同一VRRP組
priority 100 #主節點的優先順序(1-254之間),備用節點必須比主節點優先順序低。
advert_int 1 #組播資訊傳送間隔,兩個節點設定必須一樣
nopreempt
authentication { #設定驗證資訊,兩個節點必須一致
auth_type PASS
auth_pass skyai1.cublog.cn
}
track_script { #呼叫函式程式碼塊
check_run
}
virtual_ipaddress { #虛擬IP
192.168.1.172
}
}
MYSQL從伺服器的keepalived配置如下:
! Configuration File for keepalived
global_defs {
notification_email {
380934769@qq.com
}
notification_email_from 380934769@qq.com
smtp_connect_timeout 3
smtp_server 127.0.0.1
router_id MySQL-ha
}
vrrp_script check_run {
script "/root/keepalived_check_mysql.sh"
interval 5
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 88
priority 99
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass skyai1.cublog.cn
}
track_script {
check_run
}
virtual_ipaddress {
192.168.1.172
}
}
4、mysql狀態檢測指令碼
在目錄root下建立指令碼keepalived_check_mysql.sh:
#vim /root/keepalived_check_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
5、啟動keepalived
#keepalived start
6、檢視vrrp通讀記錄
#tcpdump vrrp
此時顯示是主伺服器在提供服務。
關閉主伺服器上的mysql
#mysql stop
然後由keepalived配置檔案可以知道,mysql關閉的話,將會執行keepalived_check_mysql.sh這一指令碼。這個指令碼在執行的時候,會判斷mysql的狀態,如果mysql關閉了,將會關閉主伺服器上的keepalived。主伺服器上的keepalived一旦關閉,那麼從伺服器馬上變為主伺服器,為使用者提供服務,如下圖所示:
由上圖可以看到,現在主從伺服器已經切換了。
7、檢視虛擬IP
由於前面所述,主伺服器的mysql已經關閉,從伺服器已經切換為主伺服器,所以在從伺服器上就可以檢視到虛擬IP。
#ip a
由圖中可以看到虛擬IP。
此時,一旦主伺服器上的mysql重啟,然後再啟動keepalived之後,主伺服器又會切換成向使用者提供服務的伺服器。
由圖可知,已經切換成功。
在主伺服器上檢視虛擬IP
#ip a
可以看到虛擬IP。OK,mysql+keepalived自動切換完成。
相關文章
- 利用Keepalived實現MySQL高可用MySql
- Keepalived 架構高可用 Mysql架構MySql
- Keepalived 高可用
- LVS + Keepalived + Nginx基於DR模式構建高可用方案Nginx模式
- 部署MHA+keepalived+ProxySQL高可用架構SQL架構
- PostgreSQL repmgr高可用叢集+keepalived高可用SQL
- nginx+keepalived高可用Nginx
- Keepalived 高可用詳解
- mysql高可用之keepalivedMySql
- MySQL高可用架構:mysql+keepalived實現MySql架構
- 利用KEEPALIVED構建mysql主主複製MySql
- mysql高可用衡搭建(Keepalived)MySql
- keepalived + nginx 實現高可用Nginx
- nginx實現keepalived高可用Nginx
- keepalived高可用負載均衡負載
- MySQL高可用架構之Keepalived+主從架構部署MySql架構
- 構建ORACLE高可用環境Oracle
- keepalived+MySQL實現高可用MySql
- Nginx&Keepalived 實現高可用Nginx
- Keepalived實現服務高可用
- MySQL主主模式+Keepalived高可用MySql模式
- Nginx + Keepalived 高可用叢集部署Nginx
- keepalived 高可用(非搶佔式)
- 高可用服務之Keepalived利用指令碼實現服務的可用性檢測指令碼
- 用 Hystrix 構建高可用服務架構架構
- 利用 K8S 的反親和性構建高可用應用K8S
- 高可用Mysql架構_Haproxy+keepalived+mycat叢集的配置MySql架構
- 搭建Keepalived + Nginx + Tomcat的高可用負載均衡架構NginxTomcat負載架構
- 使用keepalived實現nginx的高可用Nginx
- [Mysql高可用]——雙主互備+keepalivedMySql
- OpenStack的高可用系統構建(1)
- OpenStack的高可用系統構建(2)
- 使用Nginx+Keepalived組建高可用負載平衡Web server叢集Nginx負載WebServer
- LVS+Keepalived高可用負載均衡叢集架構負載架構
- Keepalived實現Nginx負載均衡高可用Nginx負載
- Keepalived雙機熱備實現高可用
- Keepalived高可用叢集工作原理示意圖
- Redis+Keepalived高可用環境部署記錄Redis