在上一篇文章中描述了MySQL HA on Azured 設計思路,本篇文章中將描述具體的部署,每個元件的安裝和配置。
整體的設計架構如下:
下面將是所有元件的安裝配置過程,所有的虛擬機器是CentOS 6.5的作業系統。Azure上虛擬機器的新建、Vnet的配置等本文就不再涉及。如有需要,請參考張磊同學的部落格:
http://www.cnblogs.com/threestone
-
配置Azure Internal Load Balance及新增硬碟
本文采用Xplate CLI部署Internal Load balancer,其地址為靜態的10.1.1.200,Distribution模式採用sourceIP。
- 首先建立ILB:
azure service internal-load-balancer add -a 10.1.1.200 -t Subnet-2 mysql-ha mysql-ha-ilb
info: service internal-load-balancer add command OK
- 建立Endpoint和LoadBalanceSet
azure vm endpoint create -n mysql -o tcp -t 3306 -r tcp -b mysql-ha-lbs -i mysql-ha-ilb -a sourceIP mysql-ha1 3306 3306
info: vm endpoint create command OK
azure vm endpoint create -n mysql -o tcp -t 3306 -r tcp -b mysql-ha-lbs -i mysql-ha-ilb -a sourceIP mysql-ha2 3306 3306
info: vm endpoint create command OK
- 檢視ILB
- 固定兩臺VM的DIP地址
azure vm static-ip set mysql-ha1 10.1.1.6
azure vm static-ip set mysql-ha2 10.1.1.7
- 為每臺VM新增20G的硬碟
azure vm disk attach-new mysql-ha1 20 https://portalvhds68sv75c8hs05m.blob.core.chinacloudapi.cn/hwmysqltest/mysqlha1sdc.vhd
azure vm disk attach-new mysql-ha2 20 https://portalvhds68sv75c8hs05m.blob.core.chinacloudapi.cn/hwmysqltest/mysqlha2sdc.vhd
-
在Server上安裝部署DRBD
- 安裝依賴包
yum -y install gcc rpm-build kernel-devel kernel-headers flex ncurses-devel
- 下載DRBD的安裝包並安裝
wget http://elrepo.org/linux/elrepo/el5/x86_64/RPMS/drbd84-utils-8.4.3-1.el5.elrepo.x86_64.rpm
wget http://elrepo.org/linux/elrepo/el6/x86_64/RPMS/kmod-drbd84-8.4.3-1.el6_4.elrepo.x86_64.rpm
yum -y install drbd84-utils-8.4.3-1.el5.elrepo.x86_64.rpm
yum install -y kmod-drbd84-8.4.3-1.el6_4.elrepo.x86_64.rpm
- 關閉SELinux
setenforce 0
或者修改配置檔案/etc/sysconfig/selinux
SELINUX=disabled
- 修改配置檔案
vim /etc/drbd.d/mysql.res
resource mysql {
protocol C;
device /dev/drbd0;
disk /dev/sdc1;
meta-disk internal;
on mysql-ha1 {
address 10.1.1.6:7789;
}
on mysql-ha2 {
address 10.1.1.7:7789;
}
}
- 修改hosts檔案
為保證DRBD的工作正常,修改hosts檔案,把DRBD叢集的機器都加入到hosts檔案中
vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.1.1.6 mysql-ha1
10.1.1.7 mysql-ha2
- 給/dev/sdc分割槽
注意此時不要進行格式化。具體方法不再累述。
- 建立DRBD的工作盤
在兩臺VM上執行drbdadm create-md mysql,分別建立DRBD的工作盤
在mysql-ha1上執行:drbdadm -- --overwrite-data-of-peer primary mysql,使這臺vm的成為DRBD組的Primary。此時通過drbd-overview檢視DRBD的狀態,可以看到兩塊磁碟此時正在進行同步:
0:mysql/0 SyncSource Primary/Secondary UpToDate/Inconsistent C r-----
[>....................] sync'ed: 0.6% (20372/20476)M
此時在主節點上進行格式化:mkfs.ext4 /dev/drbd0
至此,DRBD安裝配置完成。這時,只能在Primary的VM上mount這塊DRBD的盤,在secondary的VM上只能看到這塊盤,但不能對它進行操作。
- 在Server上安裝部署Corosync、Pacemaker和CRM
如前文所述,Corosync整個叢集Message Layer,Pacemaker是叢集的大腦,而CRM軟體是叢集的編輯器。叢集的安裝部署過程如下:
- 新增epel的yum源
在國內訪問國外的epel源因為受到GFW的影響,不能訪問。所以選擇國內yahoo的yum源。具體操作如下:
vim /etc/yum.repos.d/epel.repo
[epel]
name=epel
baseurl=http://mirrors.sohu.com/fedora-epel/6/$basearch
enabled=1
gpgcheck=0
-
按照下面的順序安裝
yum install -y corosync pacemaker -y
yum install cluster-glue -y
wget http://download.opensuse.org/repositories/network:/ha-clustering:/Stable/CentOS_CentOS-6/src/crmsh-2.1-1.6.src.rpm
wget http://download.opensuse.org/repositories/network:/ha-clustering:/Stable/CentOS_CentOS-6/src/pssh-2.3.1-4.2.src.rpm
yum install python-devel python-setuptools gcc make gcc-c++ rpm-build python-lxml cluster-glue-libs-devel pacemaker-libs-devel asciidoc autoconf automake redhat-rpm-config -y
rpmbuild --rebuild crmsh-2.1-1.6.src.rpm
rpmbuild --rebuild pssh-2.3.1-4.2.src.rpm
cd /root/rpmbuild/RPMS/x86_64/
yum install * -y
-
配置Corosync配置檔案
其中的member配置和transport: udpu就是指定採用udp單播(unicast)的配置。
在所有節點做full mesh的節點配置:
vim /etc/corosync/corosync.conf
compatibility: whitetank
totem {
version: 2
secauth: off
threads: 0
interface {
member{
memberaddr: 10.1.1.6
}
member{
memberaddr: 10.1.1.7
}
ringnumber: 0
bindnetaddr: 10.1.1.0
mcastport: 5405
ttl: 1
}
transport: udpu
}
logging {
fileline: off
to_stderr: no
to_logfile: yes
logfile: /var/log/cluster/corosync.log
to_syslog: yes
debug: off
timestamp: on
logger_subsys {
subsys: AMF
debug: off
}
}
amf {
mode: disabled
}
service {
ver: 1
name: pacemaker
use_mgmtd: no
use_logd: no
}
aisexec {
user: root
group: root
}
- 生成corosync的金鑰檔案
mv /dev/random /dev/random.bak
ln -s /dev/urandom /dev/random
corosync-keygen
scp /etc/corosync/authkey mysql-ha2:/etc/corosync
- 啟動corosync和pacemaker服務
service corosync start
ssh mysql-ha2 'service corosync start'
service pacemaker start
ssh mysql-ha2 'service pacemaker start'
- 安裝Azure元件,建立mdfilb服務
安裝Azure cli:
yum install nodejs –y
yum install npm –y
npm install azure-cli –g
在/etc/init.d裡建立檔案mdfilb
#!/bin/sh
start()
{
echo -n $"Starting Azure ILB Modify: "
/root/mdfilb.sh
echo
}
stop()
{
echo -n $"Shutting down Azure ILB Modify: "
echo
}
MDFILB="/root/mdfilb.sh"
[ -f $MDFILB ] || exit 1
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 3
start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0
vim /root/mdfilb.sh
azure vm endpoint delete mysql-ha2 mysql
azure vm endpoint create -i mysql-ha-ilb -o tcp -t 3306 -k 3306 -b mysql-ha-lbs -r tcp -a sourceIP mysql-ha1 3306 3306
但實測下來,VM對ILB對修改結果不穩定。
- 通過crm對pacemaker進行編排
- crm configure 進入編輯介面,對兩個全域性引數進行調整:
property stonith-enabled=false
property no-quorum-policy=ignore
- 編排
業務的編排前,需要把兩臺VM重新啟動。
- DRBD服務
crm configure
primitive mysqldrbd ocf:linbit:drbd params drbd_resource="mysql" op start timeout=240 op stop timeout=240
ms mydrbd mysqldrbd meta master-max="1" master-node-max="1" clone-max="2" clone-node-max="1" notify="true"
verify
commit
- 檔案掛載服務
primitive mystore ocf:heartbeat:Filesystem params device=/dev/drbd0 directory=/mydata fstype=ext4 op start timeout=60 op stop timeout=60
verify
commit
保證檔案掛載和DRBD在一臺機器上,同時保證啟動順序
colocation mystore_with_mysqldrbd inf: mystore mydrbd:Master
order mystore_after_ms_mydrbd mandatory: mydrbd:promote mystore:start
verify
commit
- 更改ILB的服務
primitive mdfilb lsb:mdfilb
要保證與檔案掛載在一個伺服器上,同時保證啟動順序
colocation mdfilb_with_mystore inf: mdfilb mystore
order mdfilb_after_mystore mandatory: mystore mdfilb
verify
commit
- MySQL服務
primitive mysqld lsb:mysqld
要保證與檔案掛載在一個伺服器上,同時保證啟動順序
colocation mysqld_with_mystore inf: mysqld mystore
order mysqld_after_mdfilb mandatory: mdfilb mysqld
verify
commit
至此,所有的配置都配置完成。在測試中,VM控制ILB的部分不穩定,可以視情況選擇是否部署。
可以檢視狀態:
在mysql-ha1上:
[root@mysql-ha1 /]# crm status
Last updated: Sun Dec 6 03:33:21 2015
Last change: Sun Dec 6 02:57:11 2015
Stack: classic openais (with plugin)
Current DC: mysql-ha2 - partition with quorum
Version: 1.1.11-97629de
2 Nodes configured, 2 expected votes
5 Resources configured
Online: [ mysql-ha1 mysql-ha2 ]
Master/Slave Set: mydrbd [mysqldrbd]
Masters: [ mysql-ha2 ]
Slaves: [ mysql-ha1 ]
mystore (ocf::heartbeat:Filesystem): Started mysql-ha2
mdfilb (lsb:mdfilb): Started mysql-ha2
mysqld (lsb:mysqld): Started mysql-ha2
[root@mysql-ha1 /]# drbd-overview
0:mysql/0 Connected Secondary/Primary UpToDate/UpToDate C r-----
[root@mysql-ha1 /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 29G 3.3G 24G 13% /
tmpfs 1.7G 30M 1.7G 2% /dev/shm
/dev/sdb1 50G 180M 47G 1% /mnt/resource
[root@mysql-ha1 /]# service mysqld status
mysqld is stopped
在mysql-ha2上:
[root@mysql-ha2 ~]# crm status
Last updated: Sun Dec 6 03:36:30 2015
Last change: Sun Dec 6 02:57:11 2015
Stack: classic openais (with plugin)
Current DC: mysql-ha2 - partition with quorum
Version: 1.1.11-97629de
2 Nodes configured, 2 expected votes
5 Resources configured
Online: [ mysql-ha1 mysql-ha2 ]
Master/Slave Set: mydrbd [mysqldrbd]
Masters: [ mysql-ha2 ]
Slaves: [ mysql-ha1 ]
mystore (ocf::heartbeat:Filesystem): Started mysql-ha2
mdfilb (lsb:mdfilb): Started mysql-ha2
mysqld (lsb:mysqld): Started mysql-ha2
[root@mysql-ha2 ~]# drbd-overview
0:mysql/0 Connected Primary/Secondary UpToDate/UpToDate C r----- /mydata ext4 20G 193M 19G 2%
[root@mysql-ha2 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 29G 3.2G 24G 12% /
tmpfs 1.7G 45M 1.7G 3% /dev/shm
/dev/sdb1 50G 180M 47G 1% /mnt/resource
/dev/drbd0 20G 193M 19G 2% /mydata
[root@mysql-ha2 ~]# service mysqld status
mysqld (pid 2367) is running...
[root@mysql-ha2 ~]# cd /mydata/
[root@mysql-ha2 mydata]# ls
hengwei ibdata1 ib_logfile0 ib_logfile1 lost+found mysql test
此時在前端通過mysql客戶端訪問10.1.1.200的3306埠,在兩臺機器隨意關停機的情況下,可以快速的恢復連線,保持業務的連續性!