標籤:blank target border mysql style
DRBD:Distributed Replicated Block Device 分布式複製塊裝置,原理圖如下
650) this.width=650;" style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/9/287547_1412844391tzrq.png" "564" height="352" />
DRBD有主雙架構和雙主架構的,當處於主從架構時,這個裝置一定只有一個節點是可以讀寫的,另外的節點是不可讀的,連掛載都不可能,只有一個節點是主的,其它節點都是從的。當做為主主架構時,需要達到幾個條件,1.在高可用叢集中啟用DRBD; 2. 啟用分布式檔案鎖功能,即需要把磁碟格式化為叢集檔案系統(如GFS2,OCFS2等);3. 把DRBD做成資源。
資料的預存程序:當某個進程儲存資料時在核心中通過DRBD模組複製一分通過TCP/IP通訊端發送到從節點上去,從節點的DRBD在通訊端上接收資料並通過DRBD模組儲存到磁碟,並把結果返回給主節點 。但是核心傳輸的速度很快,而TCP報文的傳輸速度相對來說要慢得多,於是會把資料存入TCP協議棧的發送緩衝區,然後從緩衝區慢慢傳遞到從節點上。這個資料轉送過程有三個模型:
A : 非同步模型,當核心把DRBD模組中複製的資料已經以報文方式發送到本地的TCP協議棧的緩衝區,至是從節點的資料是否已經傳送到,儲存是否完整,無法保證;
B : 半同步模型,報文已經發送到從節點的通訊端上,已經接收,並發往核心,但從節點的預存程序沒有回應過程。
C:同步模型,報文發往從節點,從節點已經接收,並發往核心,核心已經把資料報文儲存到磁碟,並把結果返回給主節點,這個是預設的資料轉送模型,可以保證資料的完整性。
安裝環境:
centos6.5,安裝corosync和pacemaker過程請參見上一篇部落格,上一個環境安裝的配置介面是crmsh,這裡安裝的是pcs,安裝pcs比較簡單,#yum install pcs –y 即可
安裝完成後叢集狀態如下:
650) this.width=650;" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/9/287547_1412844392JIDf.png" "427" height="239" />
現在看到兩個叢集節點mysql1 mysql2線上,pacemaker是做為外掛程式在使用的,沒有stonith裝置,只有兩個節點,所以quorum也要忽略
#pcs property set stonith-enabled=false
# pcs property set no-quorum-policy=ignore
#pcs config show 查看設定檔
650) this.width=650;" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/9/287547_1412844392vMiQ.png" "412" height="391" />
顯示錯誤還沒有叢集設定檔,stonith已經禁用,quorum已經忽略
安裝drbd:
根據官方文檔,drbd在核心2.6.33版本以後就已經裝載進了核心,不需要再額外安裝drbd,只需要安裝drbdadm管理軟體就行,我這裡用的是centos6.5,核心版本是2.6.32,所以還需要安裝
650) this.width=650;" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/9/287547_1412844392VPz4.png" "184" height="36" />
drbd版本有8.1 8.2 8.3 8.4,我這裡用的是已經做好的rpm包進行安裝,下載目錄:ftp://rpmfind.net/linux/atrpms/
把drbd的兩個rpm包放在/root目錄下,然後執行# yum --nogpgcheck localinstall *.rpm
--nogpgcheck 不執行gpg檢測,因為在centos裡面沒有認證檔案
650) this.width=650;" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/9/287547_1412844393pHgZ.png" "700" height="132" />
在兩個節點上安裝完成。載入DRBD模組
[[email protected] ~]# modprobe drbd[[email protected] ~]# lsmod | grep drbddrbd 325978 0 libcrc32c 1246 1 drbd
建立一個新的磁碟分割做為DRBD的共用磁碟,我這裡是xen虛擬機器添加的一個磁碟/dev/xvdb,建立的磁碟為/dev/xvdb1
[[email protected] drbd.d]# fdisk /dev/xvdbDevice contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabelBuilding a new DOS disklabel with disk identifier 0x218cde3e.Changes will remain in memory only, until you decide to write them.After that, of course, the previous content won‘t be recoverable.Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to switch off the mode (command ‘c‘) and change display units to sectors (command ‘u‘).Command (m for help): nCommand action e extended p primary partition (1-4)pPartition number (1-4): 1First cylinder (1-2610, default 1): Using default value 1Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): Using default value 2610Command (m for help): pDisk /dev/xvdb: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x218cde3e Device Boot Start End Blocks Id System/dev/xvdb1 1 2610 20964793+ 83 LinuxCommand (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.
在mysql2節點上執行同樣的操作。
配置drbd:
在/etc目錄下有一個檔案drbd.conf 主設定檔,裡面主要調用了/etc/drbd.d/目錄下的設定檔
650) this.width=650;" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/9/287547_14128443934qAo.png" "499" height="82" />
配置/etc/drbd.d/global_common.conf
global {usage-count no;# minor-count dialog-refresh disable-ip-verification}common { protocol C;handlers { 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-timeout outdated-wfc-timeout wait-after-sb wfc-timeout 120; degr-wfc-timeout 120; }options {# cpu-mask on-no-data-accessible}disk { on-io-error detach; fencing resource-only; }net { cram-hmac-alg "sha1"; shared-secret "drbd1";} syncer { rate 1000M; }}
在/etc/drbd.d/目錄下再建立一個磁碟的資源
#vim /etc/drbd.d/stone.res
resource mysql{ on mysql1 { device /dev/drbd0; disk /dev/xvdb1; address 10.204.80.86:7789; meta-disk internal; } on mysql2 { device /dev/drbd0; disk /dev/xvdb1; address 10.204.80.85:7789; meta-disk internal; }}
把這兩個檔案複製到mysql2節點上
然後在兩個節點上初始化資源,分別執行
#drbdadm create-md mysql --這個資源是在stone.res檔案中定義的資源
650) this.width=650;" style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/9/287547_1412844394w8z8.png" "520" height="181" />
顯示初始化成功
啟動drbd服務
#service drbd start
查看drbd狀態
#cat /proc/drbd
650) this.width=650;" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/9/287547_1412844394kQfT.png" "637" height="82" />
上面的資訊兩個節點都處於Secondary 狀態,將mysql1節點設定為Primary
#drbdadm primary --force mysql
650) this.width=650;" style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/9/287547_1412844394Isvt.png" "644" height="165" />
如上所示,資料同步過程已經開始,過一段時間後查看drbd狀態
650) this.width=650;" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/9/287547_1412844395l045.png" "647" height="86" />
顯示資源已經是主從狀態
建立檔案系統:
#mke2fs –t ext4 –L drbd /dev/drbd0 #格式化為ext4檔案系統,並指定label名為drbd
650) this.width=650;" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/9/287547_1412844395FP4S.png" "567" height="393" />
建立/data目錄,並掛載分區
#mkdir /data
#mount /dev/drbd0 /data
650) this.width=650;" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/9/287547_1412844396xEO4.png" "395" height="230" />
掛載成功
在/data目錄中建立 a,b,c,d四個檔案,然後再手動切換Primary/Secondary節點
在節點mysql1上執行
#cd /data && touch {a,b,c,d}#umount /data#drbdadm secondary mysql
650) this.width=650;" style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/9/287547_1412844396sveM.png" "662" height="182" />
可以看到drbd的狀態又重新變為secondary /secondary狀態
在mysql2節點上把drbd節點設定為Primary 狀態,建立/data 目錄 ,並掛載
#drbdadm primary mysql#cat /proc/drbd#mkdir /data#mount /dev/drbd0 /data#ls /data
650) this.width=650;" style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/9/287547_1412844396tQaq.png" "641" height="170" />
可以看到在mysql1節點上建立的a,b,c,d四個檔案在mysql2節點上依然存在,drbd共用儲存已經Ok
安裝mysql,建立叢集資源,安裝mysql過程略
配置資源資源
pcs建立資源可以用 #pcs ? 可以查看協助資訊
# pcs resource create mysqlip ipaddr ip=10.204.80.89 cidr_netmask=24 nic=eth0
添加DRBD資源,提供drbd的RA目前由OCF歸類為linbit,其路徑為/usr/lib/ocf/resource.d/linbit/drbd。drbd 需要同時運行在兩個節點上,但只能有一個節點(primary/secondary模型)是Master,而另一個節點為Slave;因此,它是一種比較特殊的叢集資源,其資源類型為多狀態(Multi-state)clone類型,即主機節點有Master和Slave之分,且要求服務剛啟動時兩個節點都處於slave狀態。
#pcs resource create mysqldrbd ocf:linbit:drbd params drbd_resource=mysql op monitor role=Master interval=50s timeout=30s op monitor role=Slave interval=60s timeout=30s
#pcs resource master ms_mysqldrbd mysqldrbd meta master-max="1" master-node-max="1" clone-max="2" clone-node-max="1" notify="true"
把drbd定義為clone資源,查看資源狀態#pcs resource show
650) this.width=650;" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/9/287547_1412844397IyZ9.png" "442" height="108" />
但做為檔案系統還需要掛載,把drbd掛載到/data目錄
# pcs resource create drbdfs ocf:heartbeat:Filesystem params device="/dev/drbd0" directory="/data" fstype="ext4"
查看資源狀態
650) this.width=650;" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/9/287547_1412844397h4xr.png" "418" height="121" />
檔案系統掛載drbdfs必須和Master mysqldrbd在同一節點上,必須先啟動mysqldrbd然後才能掛載drbdfs檔案系統,所以必須定義資源約束
# pcs constraint colocation add drbdfs with master MS_mysqldrbd
# pcs constraint order promote MS_mysqldrbd then drbdfs ###drbd的資源添加完成
# pcs resource create MySQLD lsb:mysqld op monitor interval=20 timeout=20 on-fail=restart ###添加MySQLD 服務資源完成,定義MySQLD的資源約束,必須drbdfs檔案系統先掛載然後才能啟動MySQLD服務
# pcs constraint order drbdfs then MySQLD
650) this.width=650;" style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/9/287547_1412844397dCZQ.png" "618" height="43" />
把mysqlip ,drbdfs 和MySQLD服務定義到同一個資源群組
# pcs resource group add myservice mysqlip drbdfs MySQLD
查看資源狀態
650) this.width=650;" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/9/287547_1412844398ZQLL.png" "488" height="358" />
定義資源群組的約束
#pcs constraint order mysqlip then promote MS_mysqldrbd then drbdfs then MySQLD
定義位置約束
#pcs constraint location
查看叢集的配置資訊
#pcs config show
[[email protected] /]# pcs config showCluster Name: Corosync Nodes: Pacemaker Nodes: mysql1 mysql2 Resources: Master: ms_mysqldrbd Meta Attrs: master-max=1 master-node-max=1 clone-max=2 clone-node-max=1 notify=true Resource: mysqldrbd (class=ocf provider=linbit type=drbd) Attributes: drbd_resource=mysql Operations: monitor interval=50s role=Master timeout=30s (mysqldrbd-monitor-interval-50s-role-Master) monitor interval=60s role=Slave timeout=30s (mysqldrbd-monitor-interval-60s-role-Slave) Group: myservice Resource: mysqlip (class=ocf provider=heartbeat type=IPaddr) Attributes: ip=10.204.80.89 Operations: monitor interval=60s (mysqlip-monitor-interval-60s) Resource: drbdfs (class=ocf provider=heartbeat type=Filesystem) Attributes: device=/dev/drbd0 directory=/data fstype=ext4 Operations: monitor interval=60s (drbdfs-monitor-interval-60s) Resource: MySQLD (class=lsb type=mysqld) Operations: monitor on-fail=restart interval=20 timeout=20 (MySQLD-monitor-interval-20)Stonith Devices: Fencing Levels: Location Constraints:Ordering Constraints: promote ms_mysqldrbd then start drbdfs (Mandatory) (id:order-ms_mysqldrbd-drbdfs-mandatory) start drbdfs then start MySQLD (Mandatory) (id:order-drbdfs-MySQLD-mandatory)Colocation Constraints: drbdfs with ms_mysqldrbd (INFINITY) (rsc-role:Started) (with-rsc-role:Master) (id:colocation-drbdfs-ms_mysqldrbd-INFINITY-1)Cluster Properties: cluster-infrastructure: classic openais (with plugin) dc-version: 1.1.10-14.el6_5.3-368c726 expected-quorum-votes: 2 no-quorum-policy: ignore stonith-enabled: false
cosync+pacemaker+DRBD共用儲存實現mysql高可用叢集