MariaDB GaleraCluster簡介及雙主HA實踐
分布式數據庫/數據庫集群系統中數據庫復制是十分重要的,常用的有以下兩種架構:Master/SlaveReplication架構(左)和Multi-masterReplication架構(右)。

What is GaleraCluster?
Galera Cluster is awrite-set replication service PRovider in the formof the dlopenable library. It provides synchronous replication andsupports multi-master replication. Galera Cluster is capable ofunconstrained parallel applying (that is,“parallel replication”),multicast replication and automatic node provisioning.
The primary focus ofGalera Cluster is data consistency. Transactions are either appliedto every node or not at all. Galera Cluster is not a clustermanager, a load balancer or a cluster monitor. What it does it keepdatabases synchronized provided that they were properly configuredand synchronized in the beginning.
而GaleraCluster提供了并行的復制機制,能實現同步的,多主的復制方式。因此具有很多實用特性:(1)可以在任意節點上進行讀寫(2)自動剔除故障節點(3)自動加入新節點(4)真正行級別的并發復制(5)客戶端連接跟操作單數據庫的體驗一致。
在數據復制方面有同步和異步兩種方式,而同步方式的優勢十分明顯:(1)高可用(當節點宕機時數據不會丟失,數據復制一致性高,實現不復雜);(2)具有較高的性能;(3)跨集群。但是缺點是隨著節點數量的增加,其事務的響應時間,沖突的概率和死鎖率會呈指數增長。(因此像MySQL和PostgreSQL都采用異步復制方式)


?DatabaseManagement System (DBMS) The databaseserver that runs on the individual node. Galera Cluster can useMySQL, MariaDB or Percona XtraDB.
?wsrepAPI The interface andthe responsibilities for the database server and replicationprovider. It consists of:
?wsrephooks The integrationwith the database server engine for write-setreplication.
?dlopen()Thefunction that makes the wsrep provider available to the wsrephooks.
?Galera ReplicationPlugin The plugin thatenables write-set replication service functionality.
?GroupCommunication plugins(GCS plugins) The various groupcommunication systems available to Galera Cluster. Forinstance,gcommand Spread..
State SnapshotTransfer (SST)
SST是集群提供給節點用來從一個節點到其它節點對一個完整數據的拷貝傳輸功能。GaleraCluster可以使用邏輯方法(mysqldump)和物理方法(rsync,rsync_wan,xtrabackup等)。

如下圖,對此本文設計了一個基于freeradius認證的VPN集群系統,其核心的數據庫采用MariaDB雙主GaleraCluster實現,同時借助keepalive和haproxy實現負載均衡。

設VIP地址為192.168.10.99
節點 | Hostname | FQDN | Ipaddress | 備注(CentOS7) |
Master | radiusone | radiusone.ctgu.cn | 192.168.10.91 | Keepalive,haproxy,mariadb |
Slave | radiustwo | radiustwo.ctgu.cn | 192.168.10.92 | Keepalive,haproxy,mariadb |
說明:由于是雙機配置,相同的操作或配置以#開頭,否則[root@radiusone/two~]# 開頭。
# cat/etc/hosts
127.0.0.1
::1
192.168.10.91
192.168.10.92
# yum install -yepel-release
為了安裝galeramariadb-galera-common mariadb-galera-server mariadb-libs mariadbmariadb-galera-common,方便起見,這里使用RDO的源。
#wgethttps://repos.fedorapeople.org/repos/openstack/openstack-kilo/rdo-release-kilo-1.noarch.rpm
# rpm -ivhrdo-release-kilo-1.noarch.rpm
# yum update-y
# yum install -y wgetnet-tools
# vim/etc/sysconfig/selinux
SELINUX=disabled
# setenforce0
設置防火墻
# iptables -A INPUT-p tcp -m state --state NEW -m multiport --dports3306,3307,4567,4568,4444 -j ACCEPT
#iptables-save
# systemctl enableiptables
# cat/etc/sysconfig/iptables
# sampleconfiguration for iptables service
# you can edit thismanually or use system-config-firewall
# please do not askus to add additional ports/services to this defaultconfiguration
*filter
:INPUT ACCEPT[0:0]
:FORWARD ACCEPT[0:0]
:OUTPUT ACCEPT[0:0]
-A INPUT -m state--state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -jACCEPT
-A INPUT -i lo -jACCEPT
-A INPUT -p tcp -mstate --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp--dport 3306 -j ACCEPT
-A INPUT -p tcp--dport 3307 -j ACCEPT
-A INPUT -p tcp--dport 4567 -j ACCEPT
-A INPUT -p tcp--dport 4568 -j ACCEPT
-A INPUT -p tcp--dport 4444 -j ACCEPT
-A INPUT -j REJECT--reject-with icmp-host-prohibited
-A FORWARD -j REJECT--reject-with icmp-host-prohibited
COMMIT
# systemctl restartiptables
# systemctl stopfirewalld
# systemctl disablefirewalld
# yum install -ymariadb mariadb-libs mariadb-devel mariadb-galera-commonmariadb-galera-server
# systemctl enable
# systemctl enable
[root@radiusone ~]#systemctl restart mariadb
[root@radiusone ~]#mysql_secure_installation
設置root用戶遠程使用密碼passWord訪問
[root@radiusone ~]#mysql -u root –p
MariaDB [(none)]>grant all privileges on *.* to 'root'@'%' identified by 'password'with grant option;
MariaDB[(none)]>

# vim/etc/my.cnf.d/galera.cnf +27
bind-address=0.0.0.0
[root@radiusone ~]#vim /etc/my.cnf.d/server.cnf
[mysqld]
datadir=/var/lib/mysql
query_cache_size=0
binlog_format=ROW
default_storage_engine=innodb
innodb_autoinc_lock_mode=2
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_provider_options="gcache.size=1G"
wsrep_cluster_address=gcomm://
#wsrep_cluster_address=gcomm://192.168.10.91,192.168.10.92
wsrep_cluster_name='dbcluster'
wsrep_node_address='192.168.10.91'
wsrep_node_name='radiusone'
#wsrep_sst_method=xtrabackup
wsrep_sst_method=rsync
wsrep_sst_auth=username:Password
字段說明
wsrep_provider – Ifyou are running on 32bit it will be in the /usr/lib otherwise on64bit systems it will be in /usr/lib64
wsrep_cluster_address– Include all theaddresses of the servers that will exist in the cluster. You canuse hostnames if you wish
wsrep_cluster_name –This is the name of the cluster. All servers in the cluster mustmatch names
wsrep_node_address– The IP address of thisserver
wsrep_node_name– The name of thisserver
wsrep_sst_auth – Thisis the username and password used to authenticate xtrabackup. Setthis the same on all machines
[root@radiustwo ~]#vim /etc/my.cnf.d/server.cnf
[mysqld]
datadir=/var/lib/mysql
query_cache_size=0
binlog_format=ROW
default_storage_engine=innodb
innodb_autoinc_lock_mode=2
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_provider_options="gcache.size=1G"
wsrep_cluster_address=gcomm://192.168.10.91
#wsrep_cluster_address=gcomm://192.168.10.91,192.168.10.92
wsrep_cluster_name='dbcluster'
wsrep_node_address='192.168.10.92'
wsrep_node_name='radiustwo'
#wsrep_sst_method=xtrabackup
wsrep_sst_method=rsync
wsrep_sst_auth=username:Password
[root@radiusone ~]#systemctl restart mariadb
[root@radiustwo ~]#systemctl restart mariadb
【按照下文附錄中的Mariadb-galeraHA集群啟動順序操作】
# yum install -ykeepalived
# vim/etc/sysctl.conf
net.ipv4.tcp_keepalive_intvl=1
net.ipv4.tcp_keepalive_time=5
net.ipv4.tcp_keepalive_probes=5
net.ipv4.ip_forward=1
net.ipv4.ip_nonlocal_bind=1
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-arptables=1
# sysctl-p
配置keepalive forVIP 192.168.10.99
[root@radiusone ~]#cat /etc/keepalived/keepalived.conf
! Configuration Filefor keepalived
global_defs{
}
vrrp_scriptcheck_haproxy {
}
vrrp_instance VI_1{
}
[root@radiustwo ~]#cat /etc/keepalived/keepalived.conf
! Configuration Filefor keepalived
global_defs{
}
vrrp_scriptcheck_haproxy {
}
vrrp_instance VI_1{
}
# systemctl restartkeepalived
# systemctl enablekeepalived
可以查看當前VIP192.168.10.99在radiusone[MASTER]主機上
[root@radiusone ~]#ip a
2: eth0: mtu 1500qdisc pfifo_fast state UP qlen 1000
[root@radiustwo ~]#ip a
2: eth0: mtu 1500qdisc pfifo_fast state UP qlen 1000
當停止radiusone上的keepalive時,VIP會自動漂移至radiustwo上
[root@radiusone ~]#systemctl stop
[root@radiustwokeepalived]# ip a
2: eth0: mtu 1500qdisc pfifo_fast state UP qlen 1000
[root@radiusone ~]#mysql -h 192.168.10.99 -u root -p
# yum install-y haproxy
# cat/etc/haproxy/haproxy.cfg
global
defaults
# systemctl starthaproxy
添加數據庫服務
# vim/etc/haproxy/haproxy.cfg
listengalera_cluster
# systemctl restarthaproxy
# systemctlenable


重點要檢查的字段
SHOW GLOBAL STATUSLIKE’wsrep_%’;
(1)SHOW GLOBAL STATUSLIKE ’wsrep_cluster_state_uuid’; 集群中每個節點的值必須一致。
(2)SHOW GLOBAL STATUSLIKE ’wsrep_cluster_size’; 顯示集群的規模,此例應該顯示”2”。
(3)SHOW GLOBAL STATUSLIKE ’wsrep_cluster_status’; 顯示”Primary”為正常。
(4)SHOW GLOBAL STATUSLIKE ’wsrep_ready’; 顯示”ON”為正常。
(5) SHOW GLOBALSTATUS LIKE ’wsrep_connected’; 顯示”ON”為正常。
(6) SHOW GLOBALSTATUS LIKE ’wsrep_local_state_comment’; 顯示”Joined”為正常。
至此,MariaDBGalera雙主Cluster構建完成,接下來即可參考官方文檔完成freeradius和daloradius以及vpn-l2tp的部署,實現基于freeradius認證的VPN集群系統。
https://mariadb.com/kb/en/mariadb/starting-and-stopping-mariadb/
(1)先啟動radiusone節點mariadb,并且vim/etc/my.cnf.d/server.cnf配置文件中
wsrep_cluster_address=gcomm://
#wsrep_cluster_address=gcomm://192.168.10.91,192.168.10.92
(2)再啟動radiustwo節點mariadb,并且vim/etc/my.cnf.d/server.cnf配置文件中
#wsrep_cluster_address=gcomm://
wsrep_cluster_address=gcomm://192.168.10.91,192.168.10.92
(3)重啟radiusone節點mariadb,并且vim/etc/my.cnf.d/server.cnf配置文件中
#wsrep_cluster_address=gcomm://
wsrep_cluster_address=gcomm://192.168.10.91,192.168.10.92
(4)查看端口(VIP在radiusone上面)
[root@radiusone ~]#netstat -anp | grep 3306
tcp
tcp
tcp
[root@radiusone ~]#netstat -anp | grep 3307
tcp
tcp
[root@radiustwo ~]#netstat -anp | grep 3306
tcp
[root@radiustwo ~]#netstat -anp | grep 3307
tcp
檢查mariadb集群的狀態
# mysql -u root–p
MariaDB [(none)]>SHOW STATUS LIKE 'wsrep%';
https://mariadb.com/kb/en/mariadb/galera-cluster-status-variables/
(1)關閉或者重啟slave節點radiustwo,檢查能否使用VIP:3307正常訪問數據庫;【通過】
(2)關閉或者重啟master節點radiusone,檢查能否使用VIP:3307正常訪問數據庫;【通過】
(3)將master(radiusone)和slave(radiustwo)節點全部關閉,啟動后,檢查能否使用VIP:3307正常訪問數據庫。【按照上面集群啟動的順序來操作】
(1)查看日志如果有以下類似錯誤:
# tailf/var/log/mariadb/mariadb.log
160203 18:31:37[Warning] WSREP: no nodes coming from prim view, prim notpossible
160203 18:31:37[Note] WSREP:view(view_id(NON_PRIM,4c674ade-ca61-11e5-a9f8-7e298e07ba
33,1) memb{
} joined {
} left {
} partitioned{
})
160203 18:31:38[Warning] WSREP: last inactive check more than PT1.5S ago(PT3.50687S)
, skippingcheck
160203 18:32:07[Note] WSREP: view((empty))
160203 18:32:07[ERROR] WSREP: failed to open gcomm backend connection: 110: failedto
160203 18:32:07[ERROR] WSREP: gcs/src/gcs_core.c:gcs_core_open():202: Failed toopen
backend connection:-110 (Connection timed out)
160203 18:32:07[ERROR] WSREP: gcs/src/gcs.c:gcs_open():1291: Failed to openchannel '
dbcluster' at'gcomm://192.168.10.91': -110 (Connection timed out)
160203 18:32:07[ERROR] WSREP: gcs connect failed: Connection timed out
160203 18:32:07[ERROR] WSREP: wsrep::connect() failed: 7
160203 18:32:07[ERROR] Aborting
160203 18:32:07[Note] WSREP: Service disconnected.
160203 18:32:08[Note] WSREP: Some threads may fail to exit.
160203 18:32:08[Note] /usr/libexec/mysqld: Shutdown complete
多半是因為防火墻的問題,使用iptables-F關閉防火墻,再次重啟集群,檢查HA是否正常。
# sudo iptables-F
(2)Mariadb無法重啟,報如下錯誤
160203 21:03:26[Note] Plugin 'FEEDBACK' is disabled.
160203 21:03:26[Note] Server socket created on IP: '0.0.0.0'.
160203 21:03:26[ERROR] Can't start server: listen() on TCP/IP port: Addressalready in use
160203 21:03:26[ERROR] listen() on TCP/IP failed with error 98
160203 21:03:26[ERROR] Aborting
16020321:03:26
16020321:03:27
160203 21:03:27[Note] /usr/libexec/mysqld: Shutdown complete'
原因是3306端口被占用,使用命令netstat-anp | grep 3306查看可知被haproxy監聽listengalera_cluster端口占用。
解決方法:
關閉haproxy,重啟mariadb集群成功后,再開啟haproxy。
# iptables-F
# systemctl stophaproxy
# systemctl restartmariadb
# systemctl restarthaproxy
參考:
http://serverfault.com/questions/623054/centos-7-freeradius-fails-to-start-on-boot-due-to-priority
或者
將haproxy監聽端口改為其它端口,比如3307。但是其它應用(比如freeradius和daloradius)在配置數據庫是也要將端口設為3307。再或者是將各節點的mariadb配置為其它端口,修改
listengalera_cluster
# vim/etc/my.cnf.d/galera.cnf +27
bind-address=0.0.0.0
主要參考:http://galeracluster.com/documentation-webpages/galera-documentation.pdf
cp /usr/share/zoneinfo/Asia/Shanghai/etc/localtime
|
新聞熱點
疑難解答