1. 程式人生 > >heartbeat v1(CRM)+DRBD實現數據庫服務器高可用集群搭建

heartbeat v1(CRM)+DRBD實現數據庫服務器高可用集群搭建

drbd mariadb heartbeat


一. 方案簡介


本方案采用Heartbeat雙機熱備軟件來保證數據庫的高穩定性和連續性,數據的一致性由DRBD這個工具來保證。默認情況下只有一臺mysql在工作,當主mysql服務器出現問題後,系統將自動切換到備機上繼續提供服務,當主數據庫修復完畢,又將服務切回繼續由主mysql提供服務。

二. 方案優缺點

優點:安全性高、穩定性高、可用性高,出現故障自動切換,

缺點:只有一臺服務器提供服務,成本相對較高。不方便擴展。可能會發生腦裂。

三. 方案架構圖

技術分享


四. 方案適用場景

本方案適用於數據庫訪問量不太大,短期內訪問量增長不會太快,對數據庫可用性要求非常高的場景。

配置HA集群前提:

1、節點之間時間必須同步;

建議使用ntp協議進行;

2、節點之間必須要通過主機名互相通信;

建議使用hosts文件;

通信中使用的名字必須與其節點為上“uname -n”命令展示出的名字保持一致;

3、如果是2個節點,需要仲裁設備;

4、節點之間彼此root用戶能基於ssh密鑰方式進行通信;

註意:定義為集群服務中的任意資源都不能開機自動啟動,因為它們將由CRM啟動;

具體設置參考前文:http://dengaosky.blog.51cto.com/9215128/1964550


五. 方案實現

  1. 實驗環境介紹

    技術分享

2.DRBD的安裝

DRBD介紹

官方站點:http://www.drbd.org/

DRBD(DistributedReplicatedBlockDevice)是一個基於塊設備級別在遠程服務器直接同步和鏡像數據的軟件,用軟件實現的、無共享的、服務器之間鏡像塊設備內容的存儲復制解決方案。它可以實現在網絡中兩臺服務器之間基於塊設備級別的實時鏡像或同步復制(兩臺服務器都寫入成功)/異步復制(本地服務器寫入成功),相當於網絡的RAID1,由於是基於塊設備(磁盤,LVM邏輯卷),在文件系統的底層,所以數據復制要比cp命令更快。DRBD已經被MySQL官方寫入文檔手冊作為推薦的高可用的方案之一.

獲取DRBD軟件程序,CentOS 6.6的內核版本是2.6.32-504

[[email protected] ~]# uname -r

2.6.32-504.el6.x86_64

DRBD已經合並到linux kernel2.6.33及以後內核版本中,這裏直接安裝管理工具即可,若內核版本低於2.6.33時請額外安裝DRBD內核模塊,且與管理工具版本保持一致。

kmod-drbd84-8.4.5-504.1.el6.x86_64.rpm

drbd84-utils-8.9.1-1.el6.elrepo.x86_64.rpm

# rpm -ivhdrbd84-utils-8.9.1-1.el6.elrepo.x86_64.rpmkmod-drbd84-8.4.5-504.1.el6.x86_64.rpm

3.DRBD的配置

可以參考我的前一篇文章:http://dengaosky.blog.51cto.com/9215128/1964590

我這裏就只帖配置了!

配置之前需要先使用fdisk對/dev/sdb進行分區。

# echo -n -e"n\np\n3\n\n+1G\nw\n" |fdisk /dev/sda

# partx -a /dev/sda

BLKPG: Device or resource busy

error adding partition 1

BLKPG: Device or resource busy

error adding partition 2

BLKPG: Device or resource busy

error adding partition 3

配置文件的內容如下(node1和node2的配置一樣):

[[email protected] drbd.d]# cat global_common.conf

# DRBD is the result of over a decade ofdevelopment by LINBIT.

# In case you need professional servicesfor DRBD or have

# feature requests visithttp://www.linbit.com

global {

usage-count no;

# minor-count dialog-refresh disable-ip-verification

}

common {

handlers {

# These are EXAMPLE handlersonly.

# They may have severeimplications,

# like hard resetting the nodeunder certain circumstances.

# Be careful when chosing yourpoison.

pri-on-incon-degr"/usr/lib/drbd/notify-pri-on-incon-degr.sh;/usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ;reboot -f";

pri-lost-after-sb "/usr/lib/drbd/notify-pri-lost-after-sb.sh;/usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ;reboot -f";

local-io-error"/usr/lib/drbd/notify-io-error.sh;/usr/lib/drbd/notify-emergency-shutdown.sh; echo o > /proc/sysrq-trigger ;halt -f";

# fence-peer"/usr/lib/drbd/crm-fence-peer.sh";

# split-brain"/usr/lib/drbd/notify-split-brain.sh root";

# out-of-sync"/usr/lib/drbd/notify-out-of-sync.sh root";

# before-resync-target"/usr/lib/drbd/snapshot-resync-target-lvm.sh -p 15 -- -c 16k";

# after-resync-target/usr/lib/drbd/unsnapshot-resync-target-lvm.sh;

}

startup {

# wfc-timeout degr-wfc-timeoutoutdated-wfc-timeout wait-after-sb

}

options {

# cpu-maskon-no-data-accessible

}

disk {

on-io-error detach;

# size on-io-error fencingdisk-barrier disk-flushes

# disk-drain md-flushesresync-rate resync-after al-extents

# c-plan-ahead c-delay-targetc-fill-target c-max-rate

# c-min-rate disk-timeout

}

net {

protocol C;

cram-hmac-alg "sha1";

shared-secret"password";

# protocol timeoutmax-epoch-size max-buffers unplug-watermark

# connect-int ping-intsndbuf-size rcvbuf-size ko-count

# allow-two-primariescram-hmac-alg shared-secret after-sb-0pri

# after-sb-1pri after-sb-2prialways-asbp rr-conflict

# ping-timeoutdata-integrity-alg tcp-cork on-congestion

# congestion-fillcongestion-extents csums-alg verify-alg

# use-rle

}

syncer {

rate 1000M;

}

}

資源配置文件:

[[email protected] drbd.d]# cat mystore.res

resource mystore {

on node1.stu31.com {

device /dev/drbd0;

disk /dev/sda3;

address 172.16.31.10:7789;

meta-disk internal;

}

on node2.stu31.com {

device /dev/drbd0;

disk /dev/sda3;

address 172.16.31.11:7789;

meta-disk internal;

}

}

復制一份到node2:

[[email protected] drbd.d]# scp *node2:/etc/drbd.d/

4. DRBD的管理維護

創建DRBD資源

配置好drbd以後,就需要使用命令在node1和node2上創建配置的drbd資源,使用如下命令:

[[email protected] drbd.d]# drbdadm create-mdmystore # mystore為配置文件中定義的資源名

[[email protected] drbd.d]# drbdadm create-mdmystore

DRBD的啟動和停止

#/etc/rc.d/init.d/drbd start #啟動drbd

#/etc/rc.d/init.d/drbd stop #停止drbd

#/etc/rc.d/init.d/drbd restart #重啟drbd

查看DRBD狀態

#watch -n 1 ‘cat /proc/drbd‘

#/etc/init.d/drbd status

以上兩種方式都可以查看drbd的狀態

5. 設置當前節點為主節點,並進行格式化和掛載

由於默認沒有主次節點之分,因而需要設置兩個主機的主次節點,選擇需要設置為主節點的主機,然後執行如下

node1為主節點

#強制設置主節點

[[email protected] ~]# drbdadm primary --force mystore

[[email protected] ~]# drbd-overview

0:mystore/0 SyncSource Primary/Secondary UpToDate/Inconsistent

[====>...............] sync‘ed: 29.0% (754064/1059216)K


完成後查看節點狀態:

[[email protected] ~]# cat /proc/drbd

version: 8.4.5 (api:1/proto:86-101)

GIT-hash:1d360bde0e095d495786eaeb2a1ac76888e4db96 build by [email protected],2015-01-02 12:06:20

0:cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----

ns:1059216 nr:0 dw:0 dr:1059888 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:foos:0

6.格式化存儲

[[email protected] ~]# mke2fs -t ext4 /dev/drbd0

掛載到一個目錄:

[[email protected] ~]# mount /dev/drbd0 /mnt

復制一個文件到mnt:

[[email protected] ~]# cp /etc/issue /mnt

卸載存儲:

[[email protected] ~]# umount /mnt

7.切換主節點為備節點,將node2提升為主節點

節點1設置為備節點:

[[email protected] ~]# drbdadm secondary mystore

[[email protected] ~]# drbd-overview

0:mystore/0 Connected Secondary/Secondary UpToDate/UpToDate


提升節點2為主節點:

[[email protected] ~]# drbdadm primary mystore

[[email protected] ~]# drbd-overview

0:mystore/0 Connected Primary/Secondary UpToDate/UpToDate

掛載文件系統,查看文件是否存在:

[[email protected] ~]# mount /dev/drbd0 /mnt

[[email protected] ~]# ls /mnt

issue lost+found


卸載存儲:

[[email protected] ~]# umount /mnt

註意:

(1)mount drbd設備以前必須把設備切換到primary狀態。

(2)兩個節點中,同一時刻只能有一臺處於primary狀態,另一臺處於secondary狀態。

(3)處於secondary狀態的服務器上不能加載drbd設備。

(4)主備服務器同步的兩個分區大小最好相同,這樣不至於浪費磁盤空間,因為drbd磁盤鏡像相當於網絡raid 1。

8.初始化安裝mariadb/mysql數據庫

我們將drbd設備作為數據庫的數據存儲目錄,我們在本地創建文件夾,將drbd0掛載到此文件夾。

[[email protected] ~]# mkdir /mydata

[[email protected] ~]# drbdadm primary all

[[email protected] ~]# drbd-overview

0:mystore/0 Connected Primary/Secondary UpToDate/UpToDate

[[email protected] ~]# mount /dev/drbd0 /mydata

[[email protected] ~]# ls /mydata/

issue lost+found

掛載成功!

初始化安裝mariadb必須在主節點進行:

創建用戶mysql管理數據庫及配置數據存儲目錄權限為mysql,兩個節點都需要創建用戶

# groupadd -r -g 306 mysql

# useradd -r -g 306 -u 306 mysql


創建數據存放目錄:

[[email protected] ~]# mkdir /mydata/data

[[email protected] ~]# chown -R mysql:mysql/mydata/data/

獲取mariadb數據庫的二進制安裝包:

mariadb-5.5.40-linux-x86_64.tar.gz

解壓安裝包到/usr/local:

[[email protected] ~]# tar xf mariadb-5.5.40-linux-x86_64.tar.gz -C /usr/local/

創建軟鏈接:

[[email protected] ~]# cd /usr/local

[[email protected] local]# ln -sv mariadb-5.5.40-linux-x86_64/ mysql

`mysql‘ -> `mariadb-5.5.40-linux-x86_64/‘

進入安裝目錄:

[[email protected] local]# cd mysql

[[email protected] mysql]# pwd

/usr/local/mysql

[[email protected] mysql]# chown -R root:mysql ./*


初始化安裝mariadb:

[[email protected] mysql]#scripts/mysql_install_db --user=mysql --datadir=/mydata/data


安裝完成後查看數據目錄:

[[email protected] mysql]# ls /mydata/data/

aria_log.00000001 aria_log_control mysql performance_schema test

安裝成功!

mariadb配置文件的存放,如果我們希望一個節點的配置文件更改後,備節點同步更新,那麽配置文件需要存放在drbd存儲上是最合適的!

[[email protected] mysql]# cp support-files/my-large.cnf /mydata/data/my.cnf

[[email protected] mysql]# vim /mydata/data/my.cnf

[mysqld]

port = 3306

datadir = /mydata/data

socket = /tmp/mysql.sock

skip-external-locking

key_buffer_size = 256M

max_allowed_packet = 1M

table_open_cache = 256

sort_buffer_size = 1M

read_buffer_size = 1M

read_rnd_buffer_size = 4M

myisam_sort_buffer_size = 64M

thread_cache_size = 8

query_cache_size= 16M

# Try number of CPU‘s*2 forthread_concurrency

thread_concurrency = 8

innodb_file_per_table = on

skip_name_resolve = on

在本地創建軟鏈接指向配置文件目錄:

系統默認存在一個,不需要就刪除:

[[email protected] mysql]# ls /etc/my.cnf

/etc/my.cnf

[[email protected] mysql]# rm -rf /etc/my.cnf

[[email protected] mysql]# ln -sv /mydata/data/my.cnf /etc/my.cnf

`/etc/my.cnf‘ -> `/mydata/data/my.cnf‘

服務腳本的創建:關閉開機自啟動

[[email protected] mysql]# cp support-files/mysql.server/etc/init.d/mysqld

[[email protected] mysql]# chkconfig --add mysqld

[[email protected] mysql]# chkconfig mysqld off

啟動服務測試:

[[email protected] mysql]# service mysqld start

Starting MySQL. [ OK ]


登錄mysql創建數據庫:

[[email protected] mysql]#/usr/local/mysql/bin/mysql

Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MariaDB connection id is 2

Server version: 5.5.40-MariaDB-log MariaDBServer

Copyright (c) 2000, 2014, Oracle, MontyProgram Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ toclear the current input statement.

MariaDB [(none)]> create databasetestdb;

Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| test |

| testdb |

+--------------------+

5 rows in set (0.00 sec)

MariaDB [(none)]> \q

Bye

停止數據庫服務:

[[email protected] mysql]# service mysqld stop

Shutting down MySQL. [ OK ]

現在開始到節點2配置mariadb數據庫:

先降級節點1為從節點:

卸載存儲:

[[email protected] mysql]# umount /mydata


降級:

[[email protected] mysql]# drbdadm secondarymystore

[[email protected] mysql]# drbd-overview

0:mystore/0 Connected Secondary/Secondary UpToDate/UpToDate

將節點2升級為主節點:

[[email protected] ~]# drbdadm primary mystore

[[email protected] ~]# drbd-overview

0:mystore/0 Connected Primary/Secondary UpToDate/UpToDate

創建drbd設備掛載目錄:

[[email protected] ~]# mkdir /mydata

[[email protected] ~]# chown -R mysql:mysql/mydata

掛載drbd設備:

[[email protected] ~]# mount /dev/drbd0 /mydata

[[email protected] ~]# ls /mydata/

data issue lost+found

[[email protected] ~]# ls /mydata/data/

aria_log.00000001 ib_logfile0 mysql node1.stu31.com.err testdb

aria_log_control ib_logfile1 mysql-bin.000001 performance_schema

ibdata1 my.cnf mysql-bin.index test

數據是同步了的!

解壓二進制程序安裝包:

[[email protected] ~]# tar xf mariadb-5.5.40-linux-x86_64.tar.gz -C /usr/local

創建軟鏈接:

[[email protected] ~]# cd /usr/local

[[email protected] local]# ln -sv mariadb-5.5.40-linux-x86_64/ mysql

`mysql‘ ->`mariadb-5.5.40-linux-x86_64/‘

[[email protected] local]# cd mysql

[[email protected] mysql]# chown -R root:mysql ./*

創建服務腳本:只需要服務腳本了,其它都已經在node1上配好了。

[[email protected] mysql]# cp support-files/mysql.server/etc/init.d/mysqld

[[email protected] mysql]# chkconfig --add mysqld

配置文件需要軟鏈接至/etc目錄下:

[[email protected] mysql]# rm /etc/my.cnf

rm: remove regular file `/etc/my.cnf‘? y

[[email protected] mysql]# ln -sv/mydata/data/my.cnf /etc/my.cnf

`/etc/my.cnf‘ -> `/mydata/data/my.cnf‘

啟動mysqld服務:

[[email protected] mysql]# service mysqld start

Starting MySQL... [ OK ]

查看在節點1上建立的數據庫testdb是否存在:

[[email protected] mysql]#/usr/local/mysql/bin/mysql

Welcome to the MariaDB monitor. Commands end with ; or \g.

Your MariaDB connection id is 2

Server version: 5.5.40-MariaDB-log MariaDBServer

Copyright (c) 2000, 2014, Oracle, MontyProgram Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ toclear the current input statement.

MariaDB [(none)]> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| test |

| testdb |

+--------------------+

5 rows in set (0.09 sec)

#授權遠程用戶登錄

MariaDB [(none)]> grant all on *.* to‘root‘@‘172.16.%.%‘ identified by ‘oracle‘;

Query OK, 0 rows affected (0.03 sec)

MariaDB [(none)]> flush privileges;

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> \q

Bye

安裝成功!

停止mysqld服務:

[[email protected] mysql]# service mysqld stop

Shutting down MySQL.. [ OK ]


卸載DRBD設備:

[[email protected] mysql]# umount /dev/drbd0


關閉DRBD服務:

[[email protected] mysql]# service drbd stop

Stopping all DRBD resources: .

[[email protected] mysql]# service drbd stop

Stopping all DRBD resources: .

關閉數據庫服務的自啟動:兩個節點都需要設置

# chkconfig mysqld off

下面就可以進行heartbeat的安裝了!

###########################################################################################

9. 遷移mysql的數據到drdb(假設你的mysql已經在該服務器上安裝好了)

DRBD已經安裝並且能正常同步了,那麽我們接下來要做的就是將本機的已安裝的mysql的數據遷移到drbd上,具體的數據遷移步驟如下:

a) 關閉node1和node2的mysql,/etc/rc.d/init.d/mysqld stop

b) 在node1上創建存放數據庫數據的目錄:mkdir -pv /mydata/data

c) 在node1將配置文件放到drbd目錄中:

#mv /etc/my.cnf /mydata/data

刪除node1上的/etc/my.cnf,rm -f /etc/my.cnf

在node1和node2上執行如下命令創建軟鏈接。

#ln -s /mydata/data/my.cnf /etc/my.cnf

d) 修改/etc/my.cnf的數據目錄指向/mydata/data/

e) 將原來的mysql數據文件移動到/mydata/data/

f) 執行chown -R mysql.mysql /mydata/data/

g) 啟動mysql

10. 手工切換DRBD

在沒有安裝配置drbd之前,drbd是不能自動切換的,我們可以寫出過程來加深對drbd的理解,也更能明白heartbeat的工作流程,下面是手工切換的步驟:

a) 在node1上停止mysql,/etc/rc.d/init.d/mysqld stop。

b) 在node1上執行umount /dev/drbd0。

c) 在node1上執行drbdadm secondary all切換到從模式。當兩個節點都是secondary模式的時候才可以將node2設置成primary。

d) 在node2上執行drbdadm primary all,將node2提升為主模式,並觀察/proc/drbd是否正常。

e) 在node2上執行mount /dev/drbd0 /mydata掛在文件系統。

f) 啟動node2的mysql,/etc/rc.d/init.d/mysqld start。

註意:node1和node2上的mysql用戶的uid和gid要一樣。不然切換後會導致mysql數據目錄的屬主不正確而啟動失敗。

11. 主從切換

主切換成從,需要先卸載文件系統,再執行降級為從的命令:

#umount /dev/drbd0

#drbdadm secondary all

從切換成主,要先執行升級成主的命令然後掛在文件系統:

#drbdadm primary all #如果不成功drbdsetup/dev/drbd0 primary -o

#mount /dev/drbd0 /drbd/

12. DRBD腦裂後的處理

當DRBD出現腦裂後,會導致drbd兩邊的磁盤不一致,處理方法如下:

在確定要作為從的節點上切換成secondary,並放棄該資源的數據:

#drbdadm secondary mystore

#drbdadm -- --discard-my-data connect mystore


在要作為primary的節點重新連接secondary(如果這個節點當前的連接狀態為WFConnection的話,可以省略),使用如下命令連接:

#drbdadm connect mystore

###########################################################################################

13. heartbeat的安裝

在DRBD調試沒有問題之後,就可以開始安裝和配置heartbeat了。需要確認DRBD和數據庫服務都是關閉的。

Heartbeat介紹

官方站點:http://linux-ha.org/wiki/Main_Page

heartbeat可以資源(VIP地址及程序服務)從一臺有故障的服務器快速的轉移到另一臺正常的服務器提供服務,heartbeat和keepalived相似,heartbeat可以實現failover功能,但不能實現對後端的健康檢查.

獲取heartbeat程序組:

[[email protected] heartbeat2]# ls

heartbeat-2.1.4-12.el6.x86_64.rpm

heartbeat-gui-2.1.4-12.el6.x86_64.rpm

heartbeat-ldirectord-2.1.4-12.el6.x86_64.rpm

heartbeat-pils-2.1.4-12.el6.x86_64.rpm

heartbeat-stonith-2.1.4-12.el6.x86_64.rpm

安裝程序包組:

node1和node2都需要安裝;

前提條件需要安裝如下依賴包:

[[email protected] heartbeat2]# yum install -ynet-snmp-libs libnet PyXML


安裝heartbeat套件程序:

[[email protected] heartbeat2]# rpm -ivh heartbeat-2.1.4-12.el6.x86_64.rpm heartbeat-stonith-2.1.4-12.el6.x86_64.rpm heartbeat-pils-2.1.4-12.el6.x86_64.rpm heartbeat-gui-

2.1.4-12.el6.x86_64.rpm

Preparing... ########################################### [100%]

1:heartbeat-pils ########################################### [ 25%]

2:heartbeat-stonith ########################################### [ 50%]

3:heartbeat ########################################### [ 75%]

4:heartbeat-gui ########################################### [100%]

14. Heartbeat的配置

Hearbeat的配置主要包括三個配置文件,authkeys,ha.cf和haresources的配置,下面就分別來看!

復制配置文件模版到/etc/ha.d目錄下:

[[email protected] ~]# cd /usr/share/doc/heartbeat-2.1.4/

[[email protected] heartbeat-2.1.4]# cp auth keysha.cf haresources /etc/ha.d/

Authkerys的配置

這個文件用來配置密碼認證方式,支持3種認證方式,crc,md5和sha1,從左到右安全性越來越高,消耗的資源也越多。因此如果heartbeat運行在安全的網路之上,比如私網,那麽可以將驗


證方式設置成crc,master和backup的authkeys配置一樣。我的authkeys文件配置如下:

[[email protected] ~]# vim /etc/ha.d/authkeys

auth 2

2 sha1 password123

ha.cf的配置

需要先設置一下rsyslog:

配置rsyslog記錄heartbeat的日誌

[[email protected] ~]# vim /etc/rsyslog.conf

local0.* /var/log/heartbeat.log

[[email protected] ~]# scp /etc/rsyslog.conf node2:/etc/rsyslog.conf

rsyslog.conf 100%3203 3.1KB/s 00:00

ha.cf的配置文件:

[[email protected] ha.d]# cat /etc/ha.d/ha.cf|grep -v ^#

#定義heartbeat的日誌記錄

logfacility local0

#設定心跳(檢測)時間為2秒

keepalive 2

#設置死亡時間為15秒

deadtime 30

warntime 10

initdead 120

#采用組播的方式傳遞信息

mcast eth0 225.135.121.21 694 1 0

#當Primary機器發生故障切換到Secondary機器後Primary恢復後是否進行切回操作

auto_failback on

node node1.stu31.com

node node2.stu31.com

ping 172.16.0.1

haresources的配置

haresources用來設置master的主機名、虛擬IP、服務以及磁盤掛載等,node1和node2的配置是一樣的,下面的mysqld需要做成服務,放在/etc/rc.d/init.d/目錄下,配置配置如下:

[[email protected] ha.d]# cat/etc/ha.d/haresources |grep -v ^#

node1.stu31.com IPaddr::172.16.31.180/24/eth0/172.16.31.255

drbddisk::mystore Filesystem::/dev/drbd0::/mydata::ext4 mysqld

註:該文件內IPaddr,Filesystem等腳本存放路徑在/etc/ha.d/resource.d/下,也可在該目錄下存放服務啟動腳本(例如:mysqld),將相同腳本名稱添到/etc/ha.d/haresources內容中,從而跟隨heartbeat啟動而啟動該腳本。

#cp /etc/init.d/mysqld /etc/ha.d/resource.d/

IPaddr::172.16.31.180/24/eth0:用IPaddr腳本配置浮動VIP

drbddisk::mystore:用drbddisk腳本實現DRBD主從節點資源組的掛載和卸載

Filesystem::/dev/drbd0::/mydata::ext4:用Filesystem腳本實現磁盤掛載和卸載

設置完成後,將authkeys文件的權限改為400或600,再復制配置文件到節點2:

[[email protected] ha.d]# chmod 400 authkeys

[[email protected] ha.d]# scp authkeys ha.cf haresources node2:/etc/ha.d/

authkeys 100% 678 0.7KB/s 00:00

ha.cf 100% 10KB 10.3KB/s 00:00

haresources 100%6011 5.9KB/s 00:00

先啟動drbd服務:

兩個節點都啟動drbd服務:

#service drbd start

#設置主節點為node1

[[email protected] ha.d]# drbd-overview

0:mystore/0 Connected Primary/Secondary UpToDate/UpToDate

不用掛載。

啟動heartbeat服務:

[[email protected] ha.d]# service heartbeat start;ssh node2 ‘service heartbeat start‘

logd is already running

Starting High-Availability services:

2015/01/04_10:38:05 INFO: Resource is stopped

Done.

logd is already running

Starting High-Availability services:

2015/01/04_10:38:05 INFO: Resource is stopped

Done.

需要註意的問題:

(1).在啟動Heartbeat服務之前,一定要先啟動drbd服務。

(2).用chkconfig mysqld off關閉mysqld服務自啟動,這個服務的啟動與關閉應由heartbeat負責執行。

查看掛載:

[[email protected] ha.d]# mount

/dev/mapper/vg0-root on / type ext4 (rw)

proc on /proc type proc (rw)

sysfs on /sys type sysfs (rw)

devpts on /dev/pts type devpts(rw,gid=5,mode=620)

tmpfs on /dev/shm type tmpfs (rw)

/dev/sda1 on /boot type ext4 (rw)

/dev/mapper/vg0-usr on /usr type ext4 (rw)

/dev/mapper/vg0-var on /var type ext4 (rw)

none on /proc/sys/fs/binfmt_misc typebinfmt_misc (rw)

/dev/drbd0 on /mydata type ext4 (rw)

[[email protected] ha.d]# ls /mydata

data issue lost+found

查看監聽端口是否啟動:

[[email protected] ha.d]# ss -tunl |grep 3306

tcp LISTEN 0 50 *:3306 *:*

查看drbd狀態:

[[email protected] ha.d]# drbd-overview

0:mystore/0 Connected Primary/Secondary UpToDate/UpToDate /mydata ext4 987M 31M 904M4%

查看heartbeat的日誌文件:

[[email protected] ha.d]# tail/var/log/heartbeat.log

Jan 4 12:09:20 node1 Filesystem[4905]: INFO: Resource is stopped

Jan 4 12:09:20 node1 ResourceManager[4659]: info: Running/etc/ha.d/resource.d/Filesystem /dev/drbd0 /mydata ext4 start

Jan 4 12:09:20 node1 ResourceManager[4659]: debug: Starting/etc/ha.d/resource.d/Filesystem /dev/drbd0 /mydata ext4 start

Jan 4 12:09:20 node1 Filesystem[4986]: INFO: Running start for /dev/drbd0 on/mydata

Jan 4 12:09:20 node1 Filesystem[4986]: INFO: Starting filesystem check on/dev/drbd0

Jan 4 12:09:20 node1 Filesystem[4975]: INFO: Success

Jan 4 12:09:20 node1 ResourceManager[4659]: debug:/etc/ha.d/resource.d/Filesystem /dev/drbd0 /mydata ext4 start done. RC=0

Jan 4 12:09:20 node1 ResourceManager[4659]: info: Running/etc/ha.d/resource.d/mysqld start

Jan 4 12:09:20 node1 ResourceManager[4659]: debug: Starting/etc/ha.d/resource.d/mysqld start

Jan 4 12:09:22 node1 ResourceManager[4659]: debug:/etc/ha.d/resource.d/mysqld start done.RC=0

客戶端連接數據庫服務器進行測試:

[[email protected] ~]# mysql -h 172.16.31.180 -uroot -poracle

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.5.40-MariaDB-log MariaDBServer

Copyright (c) 2000, 2013, Oracle and/or itsaffiliates. All rights reserved.

Oracle is a registered trademark of OracleCorporation and/or its

affiliates. Other names may be trademarksof their respective

owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ toclear the current input statement.

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| test |

| testdb |

+--------------------+

5 rows in set (0.08 sec)

mysql> \q

Bye

15.Heartbeat+DRBD測試

環境搭建好以後,就需要進行周密的測試,看是否實現了預期的功能:

停掉master的heartheat看看是否能正常切換。

停掉master的網絡或者直接將master系統shutdown,看看能否正常切換。

啟動master的heartbeat看看是否能正常切換回來。

重新啟動master看看能否切換過程是否OK。

註意:這裏說的切換是指是否已經將mysql停掉、是否卸載了文件系統等等。

我們將節點1的heartbeat服務關閉:

[[email protected] ha.d]# service heartbeat stop

Stopping High-Availability services:

Done.

查看DRBD狀態:成了備節點

[[email protected] ha.d]# drbd-overview

0:mystore/0 Connected Secondary/Primary UpToDate/UpToDate

mysql服務關閉:查看mysqld服務監聽端口:

[[email protected] ha.d]# ss -tunl |grep 3306

到node2查看:

heartbeat服務狀態:

[[email protected] ~]# service heartbeat status

heartbeat OK [pid 4296 et al] is running onnode2.stu31.com [node2.stu31.com]...

DRDB服務狀態:

[[email protected] ~]# drbd-overview

0:mystore/0 Connected Primary/Secondary UpToDate/UpToDate /mydata ext4 987M 31M 904M4%

mysqld服務監聽端口啟動:

[[email protected] ~]# ss -tunl |grep 3306

tcp LISTEN 0 50 *:3306 *:*


文件系統掛載成功:

[[email protected] ~]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/vg0-root 20G 507M 19G 3% /

tmpfs 499M 0 499M 0% /dev/shm

/dev/sda1 190M 28M 153M 16% /boot

/dev/mapper/vg0-usr 9.8G 3.0G 6.4G 32% /usr

/dev/mapper/vg0-var 20G 255M 19G 2% /var

/dev/drbd0 987M 31M 904M 4% /mydata

再次進行遠程連接:

[[email protected] ~]# mysql -h 172.16.31.180 -uroot -poracle

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.5.40-MariaDB-log MariaDBServer

Copyright (c) 2000, 2013, Oracle and/or itsaffiliates. All rights reserved.

Oracle is a registered trademark of OracleCorporation and/or its

affiliates. Other names may be trademarksof their respective

owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ toclear the current input statement.

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| test |

| testdb |

+--------------------+

5 rows in set (0.07 sec)

mysql> \q

Bye


在node1上再次啟動heartbeat服務:

[[email protected] ha.d]# service heartbeat start

Starting High-Availability services:

2015/01/04_13:33:07 INFO: Resource is stopped

Done.

[[email protected] ha.d]# drbd-overview

0:mystore/0 Connected Secondary/Secondary UpToDate/UpToDate

[[email protected] ha.d]# drbd-overview

0:mystore/0 Connected Primary/Secondary UpToDate/UpToDate /mydata ext4 987M 31M 904M 4%

[[email protected] ha.d]# ss -tunl |grep 3306

tcp LISTEN 0 50 *:3306 *:*


節點1恢復後會自動成為主節點。

測試成功。

至此,一個heartbeat+DRBD+mysql構成的高可用集群就完成了!


本文出自 “眼眸刻著你的微笑” 博客,請務必保留此出處http://dengaosky.blog.51cto.com/9215128/1964598

heartbeat v1(CRM)+DRBD實現數據庫服務器高可用集群搭建