keepalived+雙主架構部署
在高可用集群環境中,keepalived使用的是VIP,利用keepalived自帶的服務監控功能和自定義腳本來實現MYSQL故障時自帶切換。
Keepalived基於VRRP協議,虛擬冗余路由協議,所謂vrrp的優先級就是它會根據優先級來確定其他在集群中的地位,用0-255來表示,數字越小則表示優先級越低,數值越大表示優先級越高。當值為0,代表著master放棄持有VIP;該值為255時,表示當前master的優先級最高並蚩尤VIP。
1、 搭建GTID+row模式雙主環境
IP |
主機名 |
作用 |
數據庫版本 |
操作系統版本 |
172.16.20.32 |
mastera |
master |
Mysql5.7.20 |
Redhat6.7 |
172.16.20.34 |
masterb |
master |
Mysql5.7.20 |
Redhat6.7 |
172.16.20.30 |
|
VIP |
|
|
1.1. MySQL GTID環境安裝
此處參照以前論壇內容:
http://www.cnblogs.com/hmwh/p/9011377.html
https://www.cnblogs.com/hmwh/p/9011377.html
1.2. 分別再主備庫上創建同步賬號
創建主從復制賬號
create user ‘rep‘@‘172.16.20.%‘ identified by ‘mysql‘;
grant replication slave on *.* to ‘rep‘@‘172.16.20.%‘;
show grants for ‘rep‘@‘172.16.20.%‘;
flush privileges;
所有主機創建管理賬號:
create user ‘zs‘@‘172.16.20.%‘ identified by ‘123456‘;
grant all privileges on *.* to ‘zs‘@‘172.16.20.%‘;
flush privileges;
配置主從復制:
change master to master_host=‘172.16.20.32‘,master_port=3307,master_user=‘rep‘,master_password=‘mysql‘,master_auto_position=1;
start slave;
show slave status\G;
無問題後再在主庫配置復制關系:
change master to master_host=‘172.16.20.34‘,master_port=3307,master_user=‘rep‘,master_password=‘mysql‘,master_auto_position=1;
start salve;
show slave status\G;
1.3分別安裝keepalived軟件包
由於我的沒有keepalived包
yum install keepalived
下載地址:
http://rpmfind.net/linux/rpm2html/search.php?query=keepalived&submit=%E6%90%9C%E7%B4%A2...+&system=&arch=
rpm -ivh keepalived-1.2.13-5.el6_6.x86_64.rpm
1.4配置MYSQL數據庫檢測腳本
兩臺服務器都配置檢測腳本:內容根據情況自行修改
cd /etc/keepalived/
vim checkmysql.sh
內容如下:
#!/bin/bash
mysqlstr=/usr/local/mysql5.7/bin/mysql
host=172.16.2.32
user=zs
password=123456
port=3307
#####mysql service machine status health is 1,others 0
mysql_status=1
#####check mysql status#########
$mysqlstr -h $host -u $user -ppassword -Pport -e "show status;" > /dev/null 2>&1
if [ $? = 0 ];then
echo "mysql_status=1"
exit 0
else
/etc/init.d/keepalived stop
fi
chmod 775 checkmysql.sh
1.5兩臺機器上修改keepalived配置文件
主庫的:
vim keepalived.conf
vrrp_scripts vs_mysql_100 {
scripts "/etc/keepalived/checkmysql.sh"
interval 10
}
vrrp_instance VI_100 {
state BACKUP
nopreempt
interface eth0
virtual_router_id 100
priority 100 --註意,30的為100,34的為90。
advert_int 5
authentication {
auth_type PASS
auth_pass 1314
}
track_script {
vs_mysql_100
}
virtual_ipaddress {
172.16.20.30
}
}
備庫的:
vim keepalived.conf
vrrp_scripts vs_mysql_101 {
scripts "/etc/keepalived/checkmysql.sh"
interval 10
}
vrrp_instance VI_101 {
state BACKUP
nopreempt
interface eth0
virtual_router_id 100
priority 90
advert_int 5
authentication {
auth_type PASS
auth_pass 1314
}
track_script {
vs_mysql_101
}
virtual_ipaddress {
172.16.20.30
}
}
1.6 啟動keepalived
32服務器先啟動。
/etc/init.d/keepalived start
/etc/init.d/keepalived status
ps -ef |grep keep
tail -100f /var/log/messages
172.16.20.32優先級高,keepalived的狀態已經由backup升級為master。
ip addr
34服務器啟動keepalived進程:
/etc/init.d/keepalived start
/etc/init.d/keepalived status
ps -ef |grep keep
tail -100f /var/log/messages
查看日誌為backup 模式
管理用戶測試登陸:
查看的UUID為主庫32的。
備庫keepalived 啟動了,並沒有VIP。
1.7故障演練
關閉32。
可以看到VIP飄到34服務器上。
且keepalived狀態由backup升級為master。
keepalived+雙主架構部署