Linux集羣之corosync+pacemaker+drbd實現MySQL高可用

 

一、drbd簡介

  drbd即Distributed Replicated Block Device(分佈式磁盤塊設備),drbd一個基於軟件實現的,不共享任何東西,通過複製的方式在存儲之間構建所謂鏡像模式機制的磁盤,從而使得一個數據可以存儲爲多份, drbd的核心功能是在內核中實現。

 

二、drbd原理

wKioL1Vuyx3y1GSyAAzluG3wNgU194.bmp

  每一個主機都提供一個塊設備,塊的大小是一模一樣的,當主機上的進程需要存儲數據時,需要向內核申請,任何用戶空間的進程都沒有直接操作硬件的權限,事實上驅動磁盤是在內核靠驅動程序來實現的,任何一個進程存儲數據,都是通過發起系統調用來實現的(這就是IO),對於drbd來講,它就是在內核中把系統調用與驅動程序之間添加了一個新的層次,在另外一個節點也是一模一樣的;當進程需要存儲數據時,存儲請求發給內核,內核中有一個層,在接受請求以後,本來應該是通過調度器交由驅動程序往磁盤中寫數據的,但這時它經由drbd層,drbd不對此數據做任何修改,它只是把數據複製一份副本,完成兵分兩路,即drbd將一份數據送往磁盤驅動,由磁盤驅動來完成本地的存儲,另一個份將送往本地的網卡,通過網卡發往對端節點,對端節點接收到數據之後,它知道是drbd通信的,由此就送往drbd模塊,drbd收到以後,把數據送往驅動程序,驅動程序完成對磁盤的操作。

  它不能兩邊同時的寫數據,通常情況下一個爲主的,一個爲從的,進程也只能運行在一個節點上,完成讀寫操作;當一個節點出現故障時,可以把另一個節點提升爲主節點,同樣的可以提供服務,但此時不能往另一個節點同步數據,如果當另一個節點上線時,新上線的只能做爲從節點,來完成數據的同步;

  如果要想讓兩個節點同時工作的話,只有構建成高可用集羣,使用分佈式集羣系統,兩個節點互爲主/主,即爲互備;

 

三、架構佈置

服務器:CentOS 6.6 x86_64;

數據庫IP地址即VIP:172.16.9.100;

兩個節點分別是:node-02、node-03;相應的IP地址分別爲:172.16.9.82,172.16.9.83;

網關服務器:提供時間服務器,網關地址爲:172.16.0.1

MySQL版本:mariadb-5.5.43-linux-x86_64.tar.gz

corosync版本:corosync-1.4.7-1.el6.x86_64

pacemaker版本:pacemaker-1.1.12-4.el6.x86_64

crmsh版本:crmsh-2.1-1.6.x86_64.rpm

crmsh依賴包:pssh-2.3.1-2.el6.x86_64.rpm

drbd版本:drbd84-utils-8.9.1-1.el6.elrepo.x86_64.rpm

drbd內核補丁:kmod-drbd84-8.4.5-504.1.el6.x86_64.rpm

 

四、實驗拓撲

wKioL1Vuy3XyRzCgAAVSnCqdVNE573.bmp


  使用兩臺主機分別在兩臺主機新建一個分區,此分區用於存儲MySQL數據庫文件,在兩個節點分別安裝corosync、pacemaker和drbd用於實現MySQL的高可用,通過crmsh程序對pacemaker進行配置,當其中一個節點出現問題時用於前端訪問的VIP地址將被移到另一個節點上,並掛載drbd同步的分區數據庫存儲文件,然後啓動MySQL數據庫程序,以實現在兩個節點上實現MySQL高可用。

  現在我們要將drbd配置成corosync集羣可用的一個資源,drbd比較的特別它有兩個服務,在兩個節點上都要運行爲主從備份,要把drbd配置成clone資源來進行使用,是一個特殊的clone類型爲主從類型。

 

五、準備工作

  在構建高可用集羣服務器時需要做四個準備工作,分別是:

①節點間時間必須同步:使用ntp協議實現;

②節點間需要通過主機名互相通信,必須解析主機至IP地址;

   (a)建議名稱解析功能使用hosts文件來實現;

   (b)通信中使用的名字與節點名字必須保持一致:“uname -n”命令,或“hostname”展示出的名字保持一致;

③考慮仲裁設備是否會用到;

④建立各節點之間的root用戶能夠基於密鑰認證;

1)配置節點時間同步

配置時間同步使用ntpdate命令,建立一個定時任務,實現週期性的時間同步

[root@node-02 ~]# ntpdate 172.16.0.1
 3Jun 08:56:53 ntpdate[1655]: step time server 172.16.0.1 offset 22520.390088 sec
[root@node-02 ~]# crontab -l
*/3 * * * * /usr/sbin/ntpdate 172.16.0.1&>/dev/null
[root@node-03 ~]# /usr/sbin/ntpdate172.16.0.1
 3Jun 08:57:50 ntpdate[2094]: step time server 172.16.0.1 offset 23311.837688 sec
[root@node-03 ~]# crontab -l
*/3 * * * * /usr/sbin/ntpdate 172.16.0.1&>/dev/null


2)節點間基於主機名互相通信,在/etc/hosts文件中進行配置

[root@node-02 ~]# cat /etc/hosts
127.0.0.1  localhost localhost.localdomain localhost4 localhost4.localdomain4
::1        localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.0.1 server.magelinux.com server
172.16.9.82 node-02 node2
172.16.9.83 node-03 node3
[root@node-03 ~]# cat /etc/hosts
127.0.0.1  localhost localhost.localdomain localhost4 localhost4.localdomain4
::1        localhost localhost.localdomainlocalhost6 localhost6.localdomain6
172.16.0.1 server.magelinux.com server
172.16.9.82 node-02 node2
172.16.9.83 node-03 node3


3)節點之間基於root用戶的密鑰認證

[root@node-02 ~]# ssh-keygen -t rsa 
Generating public/private rsa key pair.
Enter file in which to save the key(/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase):
Enter same passphrase again: 
Your identification has been saved in/root/.ssh/id_rsa.
Your public key has been saved in/root/.ssh/id_rsa.pub.
The key fingerprint is:
93:c1:f9:42:63:cd:2c:98:b3:a9:ef:7e:02:24:db:f2root@node-02
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|      + =       |
|     + O +      |
|  ..  * *       |
|  =  o S .      |
|  oo.   o       |
|  o..           |
|   E.. .        |
|    o+o         |
+-----------------+
[root@node-02 ~]# ssh-copy-id -i.ssh/id_rsa.pub node3
Warning: Permanently added 'node3' (RSA) tothe list of known hosts.
root@node3's password: 
Now try logging into the machine, with"ssh 'node3'", and check in:
 
 .ssh/authorized_keys
 
to make sure we haven't added extra keysthat you weren't expecting.
 
[root@node-03 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key(/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase):
Enter same passphrase again: 
Your identification has been saved in/root/.ssh/id_rsa.
Your public key has been saved in/root/.ssh/id_rsa.pub.
The key fingerprint is:
b7:b1:3b:ca:78:9c:bc:72:0e:a0:18:a6:8d:ac:d0:99root@node-03
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|                 |
|.. .   S o      |
|+* + .  . +     |
|=.E  .o .o      |
|o    .+* ..     |
|.    .==o..     |
+-----------------+
[root@node-03 ~]# 
[root@node-03 ~]#  ssh-copy-id -i .ssh/id_rsa.pub node2
root@node2's password: 
Now try logging into the machine, with"ssh 'node2'", and check in:
 
 .ssh/authorized_keys
 
to make sure we haven't added extra keysthat you weren't expecting.

 

測試節點間是否名密鑰登陸

[root@node-02 ~]# ssh node3
Last login: Wed Jun  3 12:28:06 2015 from 172.16.9.9
[root@node-03 ~]# exit
logout
Connection to node3 closed.
[root@node-02 ~]#
[root@node-03 ~]# ssh node2
Last login: Wed Jun  3 12:41:17 2015 from 172.16.9.9
[root@node-02 ~]# exit
logout
Connection to node2 closed.
[root@node-03 ~]#

 

六、安裝配置HA程序

6.1 安裝corosync和pacemaker程序

  在配置好yum源之後直接安裝corosync和pacemaker程序,分別兩個節點上執行yum 命令安裝

yum install corosync pacemaker -y


6.2 corosync默認配置文件解釋

corosync程序安裝後配置文件所在目錄爲/etc/corosync目錄下,啓動程序/etc/init.d/corosync.

[root@node-02 ~]# cd /etc/corosync/
[root@node-02 corosync]# ls
corosync.conf.example  corosync.conf.example.udpu  service.d uidgid.d
[root@node-02 corosync]# cpcorosync.conf.example corosync.conf
[root@node-02 corosync]# egrep  -v "#|^$" corosync.conf
compatibility: whitetank    #是否兼容whitetank
totem {    #用於定義底層信息層是如何通信的相關屬性
    version:2      #定義版本號
    secauth:off    #是否啓用安全認證功能,啓用後要使用corosync-keygen命令生成密鑰
    threads:0      #工作時所使用的線程數,“0”表示不基於線程模型,而是進程模型
    interface{ #定義多個接口之間,基於哪個地址,哪個多播地址,監聽什麼端口完成多播通信;
        ringnumber:0       #環數,有點類型於TTL值對方是否回傳
        bindnetaddr:192.168.1.0        #多播地址監聽的IP網絡地址
        mcastaddr:239.255.1.1      #多播地址
        mcastport:5405             #多播地址監聽的端口
        ttl:1                  #指明TTL值
    }
}
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
    }
}


6.3 配置pacemaker

  pacemaker與corosync結合運行pacemaker的運行方式有兩種,一種是作爲corosync的插件運行,另一種是以獨立的守護進程運行,以CentOS 6中建議以插件的方式運行,不過這樣日誌中可能會用警告,可以忽略的。在corosync.conf文件後面添加如下內容:

service {
       ver:    0
       name:   pacemaker
       use_mgmtd:yes
}
 
aisexec {
       user:   root
       grout:  root
}


6.4 爲corosync提供密鑰文件,它需要在/dev/random中讀取1024個隨機數

[root@node-02 corosync]# corosync-keygen 
Corosync Cluster Engine Authentication keygenerator.
Gathering 1024 bits for key from/dev/random.
Press keys on your keyboard to generateentropy.
Press keys on your keyboard to generateentropy (bits = 176).

#此時已經卡住了,說沒有這麼多個隨機數,可以在打開一個終端,不斷的敲擊鍵盤,不過這麼有一點的久,你可以在ftp下載一個大的文件,這樣會產生大量的IO。


6.5 corosync+pacemaker最終配置文件

[root@node-02 corosync]# egrep  -v "#|^$" corosync.conf
compatibility: whitetank
totem {
    version:2
    secauth:on
    threads:0
    interface{
        ringnumber:0
        bindnetaddr:172.16.0.0
        mcastaddr:239.255.9.9
        mcastport:5405
        ttl:1
    }
}
logging {
    fileline:off
    to_stderr:no
    to_logfile:yes
    logfile:/var/log/cluster/corosync.log
    to_syslog:no
    debug:off
    timestamp:on
    logger_subsys{
        subsys:AMF
        debug:off
    }
}
service {
       ver:    0
       name:   pacemaker
       use_mgmtd:yes
}
aisexec {
       user:   root
       grout:  root
}


6.6 將配置文件和密鑰文件同步至node3節點

[root@node-02 corosync]# scp authkeycorosync.conf node3:/etc/corosync/
authkey                                                              100%  128    0.1KB/s   00:00    
corosync.conf                                                        100% 2794    2.7KB/s   00:00


6.7 啓動corosync服務

[root@node-02 corosync]# service corosyncstart;ssh node3 'service corosync start'
Starting Corosync Cluster Engine(corosync):               [ OK  ]
Starting Corosync Cluster Engine(corosync): [  OK  ]


6.8 安裝crmsh

  把準備好的程序直接使用yum進行安裝,這樣可以解決依賴關係,在生產環境中只需要選擇一臺節點上進行安裝,在這裏我們在兩個節點上都進行安裝,以方便測試。

[root@node-02 ~]# yum installcrmsh-2.1-1.6.x86_64.rpm pssh-2.3.1-2.el6.x86_64.rpm  -y

 

七、安裝配置drbd

  安裝drbd需要向內核打補丁,所以要下載與內核版本相同的內核補丁,需要在兩個節點都進行安裝。

7.1 安裝drbd程序

  這裏只演示在節點node2上安裝的過程

[root@node-02 ~]# uname  -r
2.6.32-504.el6.x86_64
[root@node-02 ~]# rpm -ivhdrbd84-utils-8.9.1-1.el6.elrepo.x86_64.rpmkmod-drbd84-8.4.5-504.1.el6.x86_64.rpm 
warning: drbd84-utils-8.9.1-1.el6.elrepo.x86_64.rpm:Header V4 DSA/SHA1 Signature, key ID baadae52: NOKEY
Preparing...               ########################################### [100%]
  1:drbd84-utils          ########################################### [ 50%]
  2:kmod-drbd84           ########################################### [100%]
Working. This may take some time ...
Done.


7.2 drbd配置文件global_common.conf解釋

  drbd安裝完成之後將會生成/etc/drbd.conf和/etc/drbd.d/目錄,在drbd.conf文件就寫了兩行包含drbd.d中的文件,所以主配置文件在/etc/drbd.d/目錄中。

[root@node-03 ~]# cd /etc/drbd.d/
[root@node-03 drbd.d]# catglobal_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-countyes;    #是否啓用讓drbd官方進行用戶使用統計,建議爲no
    #minor-count dialog-refresh disable-ip-verification
}
 
common {
    handlers{      #用於定義當集羣發生分裂時採取的處理方法
        #These are EXAMPLE handlers only.
        #They may have severe implications,
        #like hard resetting the node under certain circumstances.
        #Be careful when chosing your poison.
 
        #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{   #定義drbd啓動時雙方等待處理的方式
        #wfc-timeout degr-wfc-timeout outdated-wfc-timeout wait-after-sb
    }
 
    options{   #定義同步時屬性
        #cpu-mask on-no-data-accessible
    }
 
    disk{  #使用的磁盤
        #size on-io-error fencing disk-barrier disk-flushes
        #disk-drain md-flushes resync-rate resync-after al-extents
                # c-plan-ahead c-delay-targetc-fill-target c-max-rate
                # c-min-rate disk-timeout
    }
 
    net{   #網絡相關屬性
        #protocol timeout max-epoch-size max-buffers unplug-watermark
        #connect-int ping-int sndbuf-size rcvbuf-size ko-count
        #allow-two-primaries cram-hmac-alg shared-secret after-sb-0pri
        #after-sb-1pri after-sb-2pri always-asbp rr-conflict
        #ping-timeout data-integrity-alg tcp-cork on-congestion
        #congestion-fill congestion-extents csums-alg verify-alg
        #use-rle
    }
}


7.3 修改global_common.conf文件

[root@node-03 drbd.d]# egrep -v"#|^$" global_common.conf 
global {
    usage-countno;
}
common {
    handlers{
    }
    startup{
    }
    options{
    }
    disk{
        on-io-error detach;         #當磁盤IO錯誤時直接卸載磁盤
    }
    net{
        cram-hmac-alg  "sha1";      #使用通信的加密算法
        shared-secret  "3GQ3aD7DSEY";   #通信的加密密鑰,建議使用opensslrand -base64  #生成
    }
    syncer{
        rate1000M;         #數據同步使用的帶寬
    }
}


7.4 創建分區

  分別在兩個節點上創建兩個分區,分區的大小應該保持一致,只用創建分區不用格式化。如新建一個/dev/sda3大小爲5G。

[root@node-02 ~]# fdisk -l /dev/sda
 
Disk /dev/sda: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665cylinders
Units = cylinders of 16065 * 512 = 8225280bytes
Sector size (logical/physical): 512 bytes /512 bytes
I/O size (minimum/optimal): 512 bytes / 512bytes
Disk identifier: 0x00090ed9
 
  Device Boot      Start         End      Blocks  Id  System
/dev/sda1  *           1          26      204800  83  Linux
Partition 1 does not end on cylinderboundary.
/dev/sda2              26        7859   62914560   8e  Linux LVM
/dev/sda3            7859        8512    5252256   83  Linux


7.5 定義drbd的資源文件

樣本配置文件解釋:

resource web {      #定義資源使用resource關鍵字,web是資源名,不能使用空格字符
  onnode1.magedu.com { #指明在每個節點上的設備屬性
   device    /dev/drbd0;   #設置名是什麼
   disk      /dev/sda5;    #使用哪個磁盤分區設備當作drbd
   address   172.16.100.7:7789;    #此節點將監聽的IP地址和端口
   meta-disk internal;     #指明drbd的源數據存放位置,internal表示磁盤自身
  }
  onnode2.magedu.com {
   device    /dev/drbd0;
   disk      /dev/sda5;
   address   172.16.100.8:7789;
   meta-disk internal;
  }
}


drbd的資源文件需要在/etc/drbd.d/*.res結尾的文件,這裏定義一個mysql.res的文件

[root@node-03 drbd.d]# cat mysql.res
resource mystore {
    device/dev/drbd0;
    disk/dev/sda3;
    meta-diskinternal;
    onnode-02 {
        address 172.16.9.82:7789;
    }
    onnode-03 {
        address172.16.9.83:7789;
    }
}


7.6 把配置的文件同步至另一個節點

[root@node-03 drbd.d]# ls
global_common.conf  mysql.res
[root@node-03 drbd.d]# scp -rpglobal_common.conf mysql.res node2:/etc/drbd.d/
global_common.conf                                 100% 2097     2.1KB/s  00:00    
mysql.res                                         100%  169     0.2KB/s  00:00    
[root@node-02 drbd.d]# ls
global_common.conf  mysql.res


7.7 初始化drbd

  初始化drbd需要在每個節點上都要執行一次,以實現節點間的數據一致性。這裏在node3節點上演示。

[root@node-03 drbd.d]# drbdadm create-mdmystore
initializing activity log
NOT initializing bitmap
Writing meta data...
New drbd meta data block successfullycreated.


7.8 啓動drbd

  啓動drbd時它需要等待對端啓動,雙方纔能啓動成功。

[root@node-02 ~]# service drbd start; sshnode3 'service drbd start'


7.9 查看drbd狀態

[root@node-02 ~]# 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:Secondary/Secondary ds:Inconsistent/Inconsistent C r-----
   ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:5252056

從上面的信息中可以看出此時兩個節點均處於Secondary狀態。於是,我們接下來需要將其中一個節點設置爲Primary。在要設置爲Primary的節點上執行如下命令:

# drbdadm primary --force RESCOURCE

這裏將node2設置爲主節點,操作如下:

[root@node-02 ~]#  drbdadm primary --force mystore
[root@node-02 ~]# 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:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r---n-
   ns:2867864 nr:0 dw:0 dr:2868896 al:0 bm:0 lo:0 pe:2 ua:1 ap:0 ep:1 wo:foos:2385880
    [=========>..........]sync'ed: 54.7% (2328/5128)M
    finish:0:01:04 speed: 37,088 (36,744) K/sec


#查看狀態時發現兩分節點上的磁盤分區已經開始同步了,此時需要等待一下,具體的時間將會根據分區大小來定,同步完狀態如下:

[root@node-02 ~]# 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:5252056 nr:0 dw:0 dr:5252728 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:foos:0


7.10 格式化主節點上的/dev/drbd0

[root@node-02 ~]# mke2fs -t ext4 /dev/drbd
drbd/ drbd0  
[root@node-02 ~]# mke2fs -t ext4 /dev/drbd0
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
328656 inodes, 1313014 blocks
65650 blocks (5.00%) reserved for the superuser
First data block=0
Maximum filesystem blocks=1346371584
41 block groups
32768 blocks per group, 32768 fragments pergroup
8016 inodes per group
Superblock backups stored on blocks: 
    32768,98304, 163840, 229376, 294912, 819200, 884736
 
Writing inode tables: done

                 

八、安裝佈置MySQL數據庫

  MySQL使用的是MairaDB的數據庫,只有在兩個節點中的其中一個節點初始化數據庫就行,因爲兩個節點都是共享提供的一個數據庫文件,如安裝初始化數據庫在node2上進行操作,在node3節點上不用初始化數據庫其它操都是一樣的,這裏就不給出操作過程。

8.1 創建MariaDB運行的用戶

[root@node-02 ~]# useradd -r -u 336 mysql
[root@node-02 ~]# id mysql
uid=336(mysql) gid=336(mysql)groups=336(mysql)


8.2 掛載/dev/drbd0分區到數據庫目錄

[root@node-02 ~]# mkdir /data/data -p
[root@node-02 ~]# mount /dev/drbd0 /data
[root@node-02 ~]# chown -R mysql.mysql /data/data/
#注意:在node3安裝MySQL時需要做如下操作:
[root@node-02 ~]# drbdadm secondary mystore
[root@node-02 ~]# drbd-overview 
 0:mystore/0 Connected Secondary/Secondary UpToDate/UpToDate
把node-03變成主節點:
[root@node-03 ~]# drbdadm primary mystore

然後在進行掛載操作,切記,node3節點上不要初始化數據庫,因爲兩邊的數據已經是同步的。


8.3 解壓MariaDB程序包到/usr/local目錄下

[root@node-2 tools]# tar xfmariadb-5.5.43-linux-x86_64.tar.gz -C /usr/local/


8.4 創建軟鏈接

[root@node-2 tools]# cd /usr/local/
[root@node-2 local]# ln -smariadb-5.5.43-linux-x86_64/ mysql


8.5 初始化數據庫

[root@node-2 local]# cd mysql
[root@node-2 mysql]# chown -R  root.mysql ./*
[root@node-2 mysql]#scripts/mysql_install_db --datadir=/data/data --user=mysql


8.6 提供MySQL的主配置文件

[root@node-2 mysql]# mkdir /etc/mysql
[root@node-2 mysql]# cpsupport-files/my-large.cnf /etc/mysql/my.cnf


8.7 編輯/etc/mysql/my.cnf配置文件

在/etc/mysql/my.cnf配置文件中在[mysqld]標籤中添加數據庫存放目錄。

datadir = /data/data
innodb_file_per_table= on
skip_name_resolve = on


8.8 爲MySQL提供服務腳本

[root@node-2 mysql]# cpsupport-files/mysql.server /etc/rc.d/init.d/mysqld
[root@node-2 mysql]# chmod +x/etc/rc.d/init.d/mysqld
[root@node-2 mysql]# chkconfig --add mysqld
[root@node-2 mysql]# chkconfig mysqld off


8.9 啓動MariaDB服務進行測試

[root@node-02 mysql]# service mysqld start
Starting MySQL....                                         [  OK  ]
[root@node-02 mysql]# bin/mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.43-MariaDB-log MariaDBServer
 
Copyright (c) 2000, 2015, Oracle, MariaDBCorporation Ab and others.
 
Type 'help;' or '\h' for help. Type '\c' toclear the current input statement.
 
MariaDB [(none)]> create database node2;
Query OK, 1 row affected (0.03 sec)
 
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| node2              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.01 sec)
 
MariaDB [(none)]> exit
Bye


8.10 停止MySQL服務,卸載/data

[root@node-02 mysql]# service mysqld stop
Shutting down MySQL.                                       [  OK  ]
[root@node-02 mysql]# umount /data

 

九、配置高可用的MySQL

9.1 初始化配置

[root@node-02 ~]# crm   #切換至crm命令提示符
crm(live)# configure    #切換至配置模式
crm(live)configure# property stonith-enabled=false  #禁用stonith設備,因爲我們這裏沒有stonith設備所有要禁用
crm(live)configure# propertyno-quorum-policy=ignore   #忽略集羣中當節點數小於等於quorum,節點數將無法運行,默認是stop
crm(live)configure# verify  #檢驗語法
crm(live)configure# commit  #提交併保存服務立即生效


9.2 配置VIP資源

crm(live)configure# primitive mysqlipocf:heartbeat:IPaddr \
params ip=172.16.9.100 nic=eth0cidr_netmask=16 \
op monitor interval=10s timeout=20s
 crm(live)configure# verify
#primitive  :配置主資源即基本資源
#mysqlip    :資源名,爲VIP的地址
# ocf:heartbeat:IPaddr  :表示爲ocf風格的heartbeat中的IPaddr,用於設置IP地址
#parmas :參數,即ocf:heartbeat:IPaddr選項中的要進行配置的值
#ip=172.16.9.100    :設置IP地址爲172.6.9.100
#nic    :把VIP設置在哪塊網卡上,可省
#cidr_netmask=16    :使用cidr風格的子網掩碼格式
#op :表示此資源帶的選項
#monitor    :爲監控操作
# interval  :每隔多少時間監控一次
# timeout   :每次監控超時時間


9.3 配置clone的基本資源

任何一個主從或clone資源都必須先是primitive資源,然後在clone,一份叫master,一份叫slave。

crm(live)configure# primitive mystorocf:linbit:drbd params drbd_resource="mystore" \
op monitor role="Master"interval=10s timeout=20s \
op monitor role="Slave"interval=20s timeout=20s \
op start timeout=240s op stop timeout=100s
crm(live)configure# verify


9.4 配置主從資源

crm(live)configure# ms ms_mystor mystormeta clone-max="2" \
clone-node-max="1"master-max="1" master-node-max="1" notify="true"
crm(live)configure# verify
#clone-max : 最多克隆出的資源份數;
#clone-node-max    : 在單個節點上最多運行幾份克隆;
#master-max    : 最多啓動幾份master資源;只能用於在主從資源;
#master-node-max   : 同一個節點最多運行幾份master類型資源;只能用於在主從資源;
#notify    :當一份克隆資源啓動或停止時,是否通知給其它的副本;是布爾值,默認爲false;


9.5 配置掛載文件系統系統

  掛載的必須是主資源,先定義文件系統,然後定義約束主資源在那裏文件系統就在那裏,然後在定義順序約束,只有主節點提升以後才能掛載文件系統.

crm(live)configure# primitive mydataocf:heartbeat:Filesystem \
params device="/dev/drbd0"directory="/data" fstype="ext4" \
op monitor interval=20s timeout=40s opstart timeout=60s \
op stop timeout=60s
crm(live)configure# verify


9.6 定義排列約束

  文件系統資源mydata必須與主從資源ms_mystor的主節點在一起.

crm(live)configure# colocationmydata_with_ms_mystore inf: mydata ms_mystor:Master
crm(live)configure# verify


9.7 定義順序約束

  文件系統資源mydata要在主從資源ms_mystor的主節點啓動之後掛載

crm(live)configure#  order mydata_after_ms_mystor_masterMandatory: ms_mystor:promote mydata:start
crm(live)configure# verify


9.8 定義MySQL資源

crm(live)configure# primitive myserverlsb:mysqld op monitor interval=20s timeout=20s
crm(live)configure# verify


9.9 定義約束mysqlip要和主節點在一起

crm(live)configure# colocationmyip_with_ms_mystor_master inf: mysqlip ms_mystor:Master
crm(live)configure# verify


9.10 定義約束myserver要和mydata在一起

crm(live)configure# colocationmyserver_with_mydata inf: myserver mydata
crm(live)configure# verify


9.11 定義順序啓動約束,myserver要在mydata之後啓動

crm(live)configure# ordermyserver_after_mydata Mandatory: mydata:start myserver:start
crm(live)configure# verify


9.12 定義順序啓動約束,myserver要在myip之後啓動

crm(live)configure# order  myserver_after_myip Mandatory: myip:start myserver:start
crm(live)configure# verify
crm(live)configure# commit


 

十、測試集羣服務

10.1 查看集羣狀態

[root@node-02 ~]# crm status
Last updated: Wed Jun  3 16:45:43 2015
Last change: Wed Jun  3 16:44:22 2015
Stack: classic openais (with plugin)
Current DC: node-02 - partition with quorum
Version: 1.1.11-97629de
2 Nodes configured, 2 expected votes
5 Resources configured
 
 
Online: [ node-02 node-03 ]
 
 mysqlip    (ocf::heartbeat:IPaddr):    Started node-03 
 Master/Slave Set: ms_mystor [mystor]
    Masters: [ node-03 ]
     Slaves: [ node-02 ]
 mydata (ocf::heartbeat:Filesystem):    Started node-03 
 myserver   (lsb:mysqld):   Started node-03

  通過上面的狀態查看,我們已經知道所有的服務運行正常,並運行在node3節點上。


10.2 測試node3節點上的MySQL是否可用

[root@node-03 ~]# ss -tanp|grep":3306"
LISTEN    0      50                        *:3306                     *:*      users:(("mysqld",4367,15))
[root@node-03 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.43-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> create database testnode3;
Query OK, 1 row affected (0.02 sec)
 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testnode3          |
+--------------------+
5 rows in set (0.04 sec)
 
mysql> exit
Bye

在node3節點能正常創建數據庫,也能正常顯示數據庫信息。


10.3 將node3下線,測試node2是否能使用

[root@node-03 ~]# crm node standby 
[root@node-03 ~]# crm node online
[root@node-03 ~]# crm status
Last updated: Wed Jun  3 16:54:43 2015
Last change: Wed Jun  3 16:54:40 2015
Stack: classic openais (with plugin)
Current DC: node-02 - partition with quorum
Version: 1.1.11-97629de
2 Nodes configured, 2 expected votes
5 Resources configured
 
 
Online: [ node-02 node-03 ]
 
 mysqlip    (ocf::heartbeat:IPaddr):    Started node-02 
 Master/Slave Set: ms_mystor [mystor]
    Masters: [ node-02 ]
    Slaves: [ node-03 ]
 mydata (ocf::heartbeat:Filesystem):    Started node-02 
 myserver   (lsb:mysqld):   Started node-02


在node2登錄mysql查看並創建數據庫文件

[root@node-02 ~]# ss -tanp|grep":3306"
LISTEN    0      50                        *:3306                     *:*      users:(("mysqld",6141,15))
[root@node-02 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.43-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               |
| testnode3          |
+--------------------+
5 rows in set (0.05 sec)
 
mysql> create database testnode2;
Query OK, 1 row affected (0.02 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| testnode2          |
| testnode3          |
+--------------------+
6 rows in set (0.00 sec)
 
mysql> exit
Bye


10.4 測試HA MySQL服務是否具有監控能力

[root@node-03 ~]# ss -tanp|grep":3306"
LISTEN    0      50                        *:3306                     *:*      users:(("mysqld",14384,15))
[root@node-03 ~]# killall mysqld
[root@node-03 ~]# killall mysqld
mysqld: no process killed
[root@node-03 ~]# ss -tanp|grep":3306"
[root@node-03 ~]# ss -tanp|grep":3306"
[root@node-03 ~]# ss -tanp|grep":3306"
LISTEN    0      50                        *:3306                     *:*      users:(("mysqld",15471,15))

經過測試MySQL能在意外關閉後幾秒中重啓被啓動。


 

小結:

  一個簡單的MySQL高可用就配置完畢,drbd可以通過分佈式磁盤塊設備提供了共享存儲的可用性,甚至也提供了冗餘的可用性,drbd的可用性和可靠性還是有待考驗的,雖然有中小企業使用了drbd,甚至MySQL官方在實現MySQL高可用都以drbd爲案例講解過drbd;但經過在實際生產環境中的檢驗的反饋來看,drbd偶爾還是會出問題的,到底用不用還是要考慮衡量可以接受數據的損失,及數據的重要性來進行評估。

  不過我們相信drbd會越來越完善、穩定、安全、可靠。。。。。


  歡迎各位觀客爲小烏提出寶貴的意見,小烏等待你。。。。。

 

沒有傘的孩子就必須要努力奔跑。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章