初識MariaDB之9——keepalive+GTID雙主實現高可用
傳統的主從復制架構下,盡管可以使用半同步的方式進行讀寫分離,但是主節點仍然存在單點隱患,在規模不大的情況下可以采用keepalive+雙主的模式對主節點進行高可用保護,客戶端通過VIP訪問MySQL服務器
二、實現方式
keepalive是通過vrrp協議實現,之前有過介紹這裏不多贅述,要註意的是在雲主機是禁用vrrp協議的。本次實驗采用CentOS7.4,數據庫版本為MariaDB-10.2.14,2臺MySQL服務器互為主從,172.16.10.30/24為keepalived服務的Master主機,172.16.10.40/24為keepalived服務的Backup主機,可以使用半同步的方式保證數據一致性,缺點是始終有一個服務器處於待機狀態
三、實驗目的
采用keepalived+雙主模型對MySQL服務器做高可用,模擬A主機宕機時B主機繼續對外提供服務,當A主機上線後,重新成為Master節點
四、操作步驟
1.將MasterA與MasterB互為主從
(1)編輯AB主機配置文件並啟動MySQL服務
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip_name_resolve=ON
relay_log=mysql-relaylog
relay_log_index=mysql-relaylog
relay_log_purge=OFF
slow_query_log=ON
server-id=10
innodb_file_per_table=ON
binlog_format=ROW
log_bin=mysql-binlog
log_slave_updates=ON
gtid_strict_mode=ON
(2)在A主機上創建復制賬號並導入數據庫
MariaDB [(none)]> grant replication slave on *.* to 'bak'@'172.16.10.%' identified by 'bakpassword';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> source /root/hellodb.sql;
MariaDB [hellodb]> show global variables like 'gtid%';
+------------------------+---------+
| Variable_name | Value |
+------------------------+---------+
| gtid_binlog_pos | 0-10-37 |
| gtid_binlog_state | 0-10-37 |
| gtid_current_pos | 0-10-37 |
| gtid_domain_id | 0 |
| gtid_ignore_duplicates | OFF |
| gtid_slave_pos | |
| gtid_strict_mode | ON |
+------------------------+---------+
(3)將B主機的Master指向A主機
MariaDB [(none)]> CHANGE MASTER TO master_host='172.16.10.30', master_port=3306, master_user='bak', master_password='bakpassword',master_use_gtid=current_pos;
MariaDB [(none)]> start slave;
MariaDB [(none)]> show global variables like 'gtid%';
+------------------------+---------+
| Variable_name | Value |
+------------------------+---------+
| gtid_binlog_pos | 0-10-37 |
| gtid_binlog_state | 0-10-37 |
| gtid_current_pos | 0-10-37 |
| gtid_domain_id | 0 |
| gtid_ignore_duplicates | OFF |
| gtid_slave_pos | 0-10-37 |
| gtid_strict_mode | ON |
+------------------------+---------+
(4)將A主機的Master指向B主機
MariaDB [(none)]> CHANGE MASTER TO master_host='172.16.10.40', master_port=3306, master_user='bak', master_password='bakpassword',master_use_gtid=current_pos;
MariaDB [(none)]> start slave;
此時雙主模型已經構建完成,主機B之所以不用創建復制賬號是因為已將A創建賬號是的語句同步了過來
2.安裝配置keepalived
(1)AB主機安裝keepalived
(2)編輯A主機的故障轉移腳本
[root@host3 ~]# vim /etc/keepalived/chk_mysql.sh
#!/bin/bash
mysqlStr=/usr/bin/mysql
hostIP=172.16.10.30
chkUser=chk
chkPassword=chkpassword
mysqlPort=3306
$mysqlStr -h$hostIP -u$chkUser -p$chkPassword -P$mysqlPort -e "show global variables like '%gtid%';" > /dev/null 2>&1
if [ $? != 0 ];then
/usr/bin/systemctl stop keepalived.service
fi
(3)編輯A主機配置文件,啟動服務,讓A成為對外提供服務的主機
[root@host3 ~]# vim /etc/keepalived/keepalived.conf
vrrp_script chk_mysql {
script "/etc/keepalived/chk_mysql.sh"
interval 10
}
vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 100
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.10.100
}
track_script {
chk_mysql
}
}
[root@host3 ~]# systemctl start keepalived.service
(4)編輯A主機配置文件,啟動服務
[root@host4 ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state BACKUP
nopreempt
interface ens32
virtual_router_id 100
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.10.100
}
}
[root@host4 ~]# systemctl start keepalived.service
五、效果驗證
(1)在AB任意主機上創建一個可以遠程管理的賬號
MariaDB [hellodb]> grant all on *.* to 'chk'@'172.16.10.%' identified by 'chkpassword';
MariaDB [hellodb]> flush privileges;
(2)再使用另一個主機通過VIP發現當前gtid_binlog_pos在B主機上,說明之前對外提供服務的是B主機
[root@host5 ~]# mysql -h172.16.10.100 -uchk -pchkpassword -P3306 -e"show global variables like '%gtid%';"
+------------------------+-----------------+
| Variable_name | Value |
+------------------------+-----------------+
| gtid_binlog_pos | 0-20-42 |
| gtid_binlog_state | 0-10-40,0-20-42 |
| gtid_current_pos | 0-20-42 |
| gtid_domain_id | 0 |
| gtid_ignore_duplicates | OFF |
| gtid_slave_pos | 0-20-42 |
| gtid_strict_mode | ON |
| wsrep_gtid_domain_id | 0 |
| wsrep_gtid_mode | OFF |
+------------------------+-----------------+
(3)從遠程節點通過VIP對數據庫執行任意DML操作後,發現gtid_binlog_pos回到了A主機,說明A主機已持有VIP
[root@host5 ~]# mysql -h172.16.10.100 -uchk -pchkpassword -P3306 -e"delete from hellodb.students where stuid=11;"
[root@host5 ~]# mysql -h172.16.10.100 -uchk -pchkpassword -P3306 -e"show global variables like '%gtid%';"
+------------------------+-----------------+
| Variable_name | Value |
+------------------------+-----------------+
| gtid_binlog_pos | 0-10-43 |
| gtid_binlog_state | 0-20-42,0-10-43 |
| gtid_current_pos | 0-10-43 |
| gtid_domain_id | 0 |
| gtid_ignore_duplicates | OFF |
| gtid_slave_pos | 0-10-43 |
| gtid_strict_mode | ON |
| wsrep_gtid_domain_id | 0 |
| wsrep_gtid_mode | OFF |
+------------------------+-----------------+
(4)停止A主機MySQL服務,模擬故障,再通過VIP執行DML操作後發現gtid_binlog_pos回到了B主機,此時B主機對外提供服務,故障已切換
[root@host3 ~]# systemctl stop keepalived.service
[root@host5 ~]# mysql -h172.16.10.100 -uchk -pchkpassword -P3306 -e"delete from hellodb.students where stuid=7;"
[root@host5 ~]# mysql -h172.16.10.100 -uchk -pchkpassword -P3306 -e"show global variables like '%gtid%';"
+------------------------+-----------------+
| Variable_name | Value |
+------------------------+-----------------+
| gtid_binlog_pos | 0-20-45 |
| gtid_binlog_state | 0-10-44,0-20-45 |
| gtid_current_pos | 0-20-45 |
| gtid_domain_id | 0 |
| gtid_ignore_duplicates | OFF |
| gtid_slave_pos | 0-20-45 |
| gtid_strict_mode | ON |
| wsrep_gtid_domain_id | 0 |
| wsrep_gtid_mode | OFF |
+------------------------+-----------------+
(5)重啟A主機MySQL服務,再重啟keepalived服務,A主機重新持有VIP,此時再通過VIP執行DML操作後發現gtid_binlog_pos回到A主機,說明A主機重新持有VIP,至此所有操作完成
補充說明:
以本文為例,當A主機的MySQLd服務停止後,其keepalived服務也會跟著停止,重啟MySQLd服務,keepalived服務並不會跟著啟動,必須手動啟動,否則主機A將無法持有VIP
初識MariaDB之9——keepalive+GTID雙主實現高可用