若您的預算無法購買共用儲存設備且對於 Downtime 的要求並沒有那麼高,那麼您可以考慮 OpenSource 的 Active/Standby 高可用性 (HA, High Availability) 解決方案,本次實作為採用?[DRBD]?配合?[heartbeat]?及?[MySQL]?來建置高可用性資料庫,[DRBD]?為利用網路來傳輸二台主機的共用資料區達到資料同步,而?[heartbeat]?則是監控二台主機間的心跳 (Heartbeat) 判斷 Active/Standby Node 之間是否能互相存取得到對方,以便當其中一台主機因不可預期因素損壞時達到服務即時接手 (約 1 ~ 3 分鐘) 提供高可用性服務之目的。
If your budget does not buy common storage devices and the requirements for Downtime are not as high as xff0c; then you can consider OpenSource 'Active/Standby 'tyre' (HA, High Availabilty) solutions xff0c;
OS、HDD、Package
- CentOS 5.4 32 bit (Linux 2.6.18-164.el5) * 2台
- 每台 HDD *2顆
- /dev/hda 20GB (System)
- /dev/hdb 10GB (Database Files)
- Install Package
- drbd83-8.3.8-1.el5.centos
- kmod-drbd83-8.3.8-1.el5.centos
- heartbeat-pils-2.1.3-3.el5.centos
- heartbeat-ldirectord-2.1.3-3.el5.centos
- heartbeat-2.1.3-3.el5.centos
- heartbeat-stonith-2.1.3-3.el5.centos
- mysql-server-5.0.77-4.el5_5.3
- 每台 HDD *2顆
Networking
- Hostname: node1.weithenn.org (Default Primary Node)
- Host IP (eth0): 10.10.25.121/24
- Heartbeat IP (eth1): 192.168.1.1/24
- Hostname: node2.weithenn.org (Default Secondary Node)
- Host IP (eth0): 10.10.25.122/24
- Heartbeat IP (eth1): 192.168.1.2/24
Cluster
- Cluster IP (eth0:0): 10.10.25.120/24 (哪台主機接手為 Primary Node 後會自動設定此網卡及 IP Address)
- Cluster Reousrce Name: ha
步驟1.關閉 IPTables 及 SELinux
安裝時將 CentOS 安裝於 hda 而 hdb 先不用理它後續步驟會進行處理,我們要為高可用性環境來準備基礎的設定,請先請二台主機的防火牆 (Iptables) 及 SELinux 關閉以利建置過程能順利執行。(Node1 及 Node2 都必須設定)
Install the Centos on hda and hdb will handle #xff0c without it; we're going to prepare the base setup for the high-availability environment & #xff0c; please request the Iptables and SELinux to shut down so that the construction process can proceed smoothly. (Node1 and Node2 must be set)
步驟2.設定網路資訊
設定好二台主機的 IP Address、Netmask、Default Gateway、DNS 及最重要的 Hostnames,設定完成後資訊如下(Node1 及 Node2 都必須設定)
Setup two host IP Address, Netmask, Default Gateway, DNS and the most important Hostnames, after setup the following information (Node1 and Node2 have to be set)
- node1.weithenn.org (Default Primary Node)
- Host IP (eth0): 10.10.25.121/24
- Heartbeat IP (eth1): 192.168.1.1/24
- node2.weithenn.org (Default Secondary Node)
- Host IP (eth0): 10.10.25.122/24
- Heartbeat IP (eth1): 192.168.1.2/24
因為高可用性對於主機的 FQDN 的設定非常要求,請將主機名稱加入 /etc/hosts 內以便 DNS 運作出現問題時也能保持主機運作正常,此檔案在 node1 及 node2 都必須設定,設定完成後請交互測試能否使用 FQDN 去 ping 到對方主機。(Node1 及 Node2 都必須設定)
xff0c; please add hostname to /etc/hosts so that DNS can keep host running normal while doing the current problem & #xff0c; this file has to be set xff0c for node1 and node2; ask for an interactive test if FQDN can be used to ping to another host when the configuration is complete. (Node1 and Node2 have to be set)
步驟3.建立分割區於 hdb
為 hdb 硬碟建立分割區,此顆硬碟即為屆時二台主機要同步資料的硬碟,也是屆時我們存放 MySQL 資料庫檔案的地方。(Node1 及 Node2 都必須建立)
Create partitions for hdb hard disks & #xff0c; this hard disk is the harddisk #xff0c where the data are to be synchronized by the second host in the current year; it is also where the MySQL database files are stored. (Node1 and Node2 must be created)
建立分割區完成後使用指令 fdisk -l 確定 partition talbe 狀態
Use command fdisk-l to confirm party level status when creating partitions
建立分割區完成後請建立 /db 資料夾,此資料夾也就是屆時 hdb 空間要掛載於此資料夾。 (Node1 及 Node2 都必須建立)
Please create /db folders & #xff0c after the partition has been created; this folder is due to be mounted on this folder with hdb space. (Node1 and Node2 have to be created)
步驟4.安裝相關套件
使用?[YUM]?安裝 DRBD、Heartbeat、MySQL 等相關套件。(Node1 及 Node2 都必須安裝)
Use? ?
安裝好後修改 mysql 設定檔,將 mysql 資料庫存放路徑由預設的 /var/lib/mysql 修改至屆時會執行硬碟資料同步的 /db。(Node1 及 Node2 都必須設定)
Modifying mysql configuration file & #xff0c when installed; modifying mysql repository path by default /var/lib/mysql will execute hard disk data synchronization/ db by the end of the term. (Node1 and Node2 have to be set)
步驟5.修改 drbd 設定檔
修改 drbd 設定檔,設定檔內容中若您的 CentOS 為 32 bit 則安裝 heartbeat 後路徑為 /usr/lib/heartbeat,若 CentOS 為 64 bit 則 heartbeat 安裝後路徑為 /usr/lib64/heartbeat。(Node1 及 Node2 都必須設定)
Modify the drbd configuration file & #xff0c; install the heartbeat after / usr/ lib/ heartbeat& #xff0c in the configuration file content if your CentOS is 32 bit; if the DentOS is 64 bit after / usr/ lib64/ heartbeat. (Node1 and Node2 have to be set)
完成後請調整 drbd 執行檔權限以便後續指令能順利執行,若此步驟省略的話後續執行過程會出現錯誤。(Node1 及 Node2 都必須設定)
After completion, adjust the permissions of the drbd executable so that the subsequent command can run xff0c without delay; there will be an error in running the process after this step has been omitted. (Node1 and Node2 have to be set)
步驟6.載入 drbd 模組並建立 resource
使用指令 modprobe 指令來載入 drbd 模組,載入完成後使用指令 drbdadm create-md 來建立 drbd resource,由於我們在 drbd 設定檔中指令 resource 名稱為 ha 所以指令便輸入 ha。(Node1 及 Node2 都必須設定)
Use the command modprobe command to load the drbd module & #xff0c; use the command drbdadm Create-md to create drbd resources, after loading; since we command the source name ha in the drbd configuration file, the command must be set. (Node1 and Node2 must be set)
啟動完成後可以使用 service drbd status 指令來查看目前 drbd 的狀態,當 node1 啟動 drbd 服務而 node2 尚未啟動時會發現狀態為 Secondary/Unknown,當 node2 啟動 drbd 服務後則會發現狀態為 Secondary/Secondary,而 ds 狀態為 Inconsistent 表示二台主機資料尚未同步。
The current status of drbd & #xff0c can be viewed using the service drbd status command after startup has been completed; when node1 starts drbd service and node2 is not started, the status is Second/ Unknown, when node2 starts the drbd service, the status is Condary/Secondary, and ds is Inconsistent indicates that the host data has not yet been synchronized.
步驟1.初始化二台主機 hdb 資料
確定 Node1 及 Node2 主機都可偵測到對方 (Secondary/Secondary) 後我們設定 Node1 為 Primary Node,並使二台主機開始同步 hdb 硬碟資料 (屆時的 /dev/drbd0),此時查看 drbd 狀態可發現同步的百分比及進度。 (僅 Node1 主機執行)
Make sure that Node1 and Node2 hosts can detect each other (Secondary/Secondary) and we set Node1 as Prime Node, and allow the two hosts to start synchdb hard disk data (end /dev/drbd0) & #xff0c; then check the percentage and progress of drbd status to detect synchronization. (Node1 host only)
當同步完成後可發現二台主機的 ds 狀態都為 UpToDate 表示同步完成,二台主機擁有相同且最新的資料。
When synchronisation is completed, the ds status of the two hosts is found to be UpToDate for synchronization & #xff0c; the two hosts have the same and up-to-date data.
在 Node1 主機執行看到下列狀態 (此台為 Primary Node)
sees the following state on the Node1 host (this is Primary Node)
在 Node2 主機執行看到下列狀態 (此台為 Secondary Node)
sees the following state on the Node2 host (this is Secondary Node)
步驟2.初始化 MySQL 資料庫
Node1 及 Node2 主機都完成資料同步作業後 (即 /dev/drbd0 完成初始化) 首先使用指令 mkfs.ext3 指令來格式化 /dev/drbd0,格式化完成後將 /dev/drbd0 掛載至 /db (僅 Node1 主機執行)
Use command mkfs.ext3 for formatting /dev/drd0, load /dev/drbd0 to /db after formatting has been completed (i. e. /dev/ drdd0 completed)
將 /dev/drbd0 順利掛載到 /db 後啟動 MySQL 服務來確定資料庫相關檔案是否能順利寫入 /db 資料夾內。(僅 Node1 主機執行)
Mount /dev/drbd0 smoothly to / db after starting MySQL service to determine whether database-related files can be easily written in /db folders. (Node1 host only)
步驟3.停止 MySQL 服務及卸載
確定 MySQL 服務可順利寫入 /db 後,便可準備設定 Heartbeat 部份但在設定以前請先將 Node1 主機中 MySQL 服務停止及 /dev/drbd0 卸載並把 Node1 先退回為 Secondary Node,以避免設定 Heartbeat 過程中受到影響 (僅 Node1 主機執行)
Make sure that mySQL service is easy to write / /db after & #xff0c; prepare to configure the HeartBeat part, but before setting, please unload MySQL from Node1 host and /dev/drbd0 and return Node1 first to Western Node& #xff0c; avoid setting the HeartBeat process affected (only Node1 host is running)
步驟1.設定 Heartbeat 設定檔 - ha.cf
設定 ha.cf 設定檔其中 Node1 及 Node2 在設定檔內容不同處僅 ucast 所指定的 IP Address, Node1 必須指定 eth0 為 Node2 的 10.10.25.122 而 eth1 為 192.168.1.2,而 Node2 則指定 eth0 為 Node1 的 10.10.25.121 而 eth1 為 192.168.1.1。(Node1 及 Node2 都必須設定)
Configure Hade1 and Node2 of the config file only IP Address, Node1 must specify eth0 as 10.10.25.122 of Node2 and eth1 as 192.168.1.2, while Node2 designates eth0 as Node1 10.10.25.121 and eth1 as 192.168.1.1 where the content of the profile is different. (Node1 and Node2 must be set)
步驟2.設定 Heartbeat 設定檔 - haresources
設定 haresource 設定檔 Node1 及 Node2 設定檔內容一模一樣,表示預設使用 Node1 主機擔任 Primary Node 角色,此設定檔內容可分為五段來看。(Node1 及 Node2 都必須設定)
Configure haresource configuration files Node1 and Node2 profiles with identical content & #xff0c; for default use of Node1 hosts as Primary Node roles & #xff0c; this profile content can be viewed in five paragraphs. (Node1 and Node2 have to be set)
- 指定 Primary Node 的 FQDN 此例為 node1.weithenn.org
- 指定 Cluster IP Address 此例為 10.10.25.120
- 指定 Cluster Resource Name 此例為 drbddisk::ha
- 指定 Cluster Device、Mount Point、File System Type 此例為 Filesystem::/dev/drbd0::/db::ext3
- 指定 Service 此例為 mysql
此次實作的 haresources 設定檔內容如下
The current implementation of the haresources profile is described below.
步驟3.設定 Heartbeat 設定檔 - authkeys
此設定檔為 Cluster Node 之間的密碼,也就是主機必須具備此一密碼檔才會被認為是同一個 Cluster 中的 Node,此次使用 sha1 編碼方式並使用 urandom 指令將一堆亂數寫入檔案內當作 Cluster 密碼,此步驟在 Node1 主機執行完成後請利用 scp 指令將檔案複製到 Node2 主機中以便保持密碼檔案一致。
This configuration file is the password & #xff0c between Cluster Node; the host must have this password file to be considered the same Node, from the Cluster; this time, using the ssha1 encoding and the urandom command to write a pile of random numbers into the file as the Closter password & #xff0c; this step is taken at the end of the Node1 host, using the Scp command to copy the file to the Node2 host in order to keep the password file consistent.
步驟4.設定 Heartbeat 服務設定檔 - mysql
因為預設的 Heartbeat 服務設定檔中預設並沒有 mysql 因此我們只好自已編寫,屆時 Secondary Node 接手 Primary Node 服務時並是依據此檔案進行。(Node1 及 Node2 都必須設定)
Because mysql is not foreseen in the default Heartbeat service configuration file, we have to compose & #xff0c; then Western Node takes over the Primary Node service on the basis of this file. (Node1 and Node2 have to be set)
步驟5.新增 Heartbeat 服務
將 Heartbeat 服務新增至系統中,請注意此服務必須比 DRBD 還要慢啟動否則會因為 DRBD 服務尚未啟動 (/db 尚未掛載) 而產生錯誤。 (Node1 及 Node2 都必須設定)
Newly add the Heartbeat service to the system & #xff0c; please note that this service must start more slowly than the DRBD or it will be wrong because the DRBD service has not started (/ db has not been loaded). (Node1 and Node2 have to be set)
步驟6.啟動 Heartbeat 服務
完成後在 Node1 及 Node2 主機啟動 Hertbeat 服務 (Node1 及 Node2 都必須設定)
Start Hertbeat services on Node1 and Node2 hosts after completion (Node1 and Node2 have to be set)
當二台主機都啟動 Heartbeat 服務後,因為 haresources 的設定 Node1 主機會成為 Cluster 中的 Primary Node 因此 Node1 主機會擁有下列資源
When both hosts start Heartbeat & #xff0c; because of the haresources settings Node1 master opportunities became Primary Node in Cluster, node1 masters have the following resources:
在測試 HA 機制時您可使用下列指令來即時 (每 1 秒更新) 觀察 DRBD 的 Primary/Secondary 狀態
The following instructions can be used to observe DRBD's Primary/Secondary status immediately (updating every 1 second) while testing the HA scheme:
在測試切換 Node 以前您可在 Primary Node 建立 database 待 Secondary Node 接手後查看剛才建立的資料庫是否也成功轉移過來
Before testing Node, you can set up a database in Primary Node and see if the database just created has been successfully transferred after it has been taken over.
測試1.手動切換 Node
要手動切換 Node 只要在 Primary Node 上將 heartbeat 服務停止即可,當原來的 Primary Node 的 heartbeat 服務再次啟動後並不會將主權搶回 (因為 ha.cf 設定檔中 auto_failback 為 off),若您在 ha.cf 設定檔中 auto_failback 為 on 則當原來的 Primary Node (現在為 Secondary Node) 的 Heartbeat 服務再次啟動後會將主權搶回。
To switch Node manually, you can simply stop the hearingbeat service on the Primary Node file & #xff0c; when the original Primary Node heartbeat service is restarted, it will not take back the ownership (because the auto_failback off) & #xff0c in the configuration file; if you re-activate the original Primary Node (now Secondary Node) Heartbeat service in the ha.cf configuration file, it will.
Primary Node 為 Node1 主機,執行 heartbeat 服務重新啟動
當上述指令執行時您會看到 Node1 主機中 DRBD 狀態變化為 Primary/Secondary >> Secondary/Secondary >> Secondary/Primary,當上述指令執行完成後狀態如下
When this command is executed, you will see a change in the DRBD status in Node1 host to Primary/Secondary & gt; > Security/Secondary & gt; > Security/Primary& #xff0c; when the command is executed as follows:
此時在 Node2 主機您可看到接手 Primary Node 角色 (掛載 /dev/drbd0 至 /db、接手 mysqld 服務、啟動 Cluster IP 10.10.25.120 及網卡 eth0:0)
On the Node2 host you can see the assumed Primary Node role (mount / dev/ drd0 to / db, take over mysqld service, start Cluster IP 10.10.25.120 and eth0:0)
測試2.當主機網路斷線
模擬當網路斷線時是否會自動切換 Node,您可將插在 eth0 網路線拔掉或在 Primary Node 上執行 ifdown eth0 指令去關閉 eth0 網卡來進行 Node 切換測試。
Simulates whether Node, is automatically switched when the network is disconnected; you can deactivate the eth0 network or execute the edown eth0 command to close the eth0 card for the Node switch test.
- 拔除 eth0 上的網路線或執行 ifdown eth0 指令來關閉網卡連線
- Primary Node 切換為 Secondary Node,而原 Secondary Node 接手為 Primary Node
測試3.當網路卡及交換器損壞
若您有多片網卡時可設定網卡 Bonding (NIC Teaming) 功能並接在不同的實體網路交換器上,如此一來即可預防網路卡損壞或網路交換器損壞而造成的斷線問題。
If you have more than one Internet card, you can configure the web card Bonding (NIC Teaming) functionality and connect it to a different entity network exchanger & #xff0c; so that you can pre-empt the disconnection problems caused by the loss of the Internet card or the loss of the network exchanger.
機器運作日子一久難免會遇到機器損壞或其它因素而需要停機,當然單台停機時我們可利用停止 Heartbeat 服務來達成切換 Node 的目的進而維修硬體,若遇到特殊情況需要將二台主機都停機的情況下正確的開機/關機流程是必要的,以下便是說明正確的開機/關機流程。
The machine runs for a long time with machine damage or other factors requiring a shutdown & #xff0c; of course, when a single station stops, we can use the Heartbeat service to switch Node to hardware & #xff0c; the correct start/off process is necessary if special circumstances require both hosts to be shut down & #xff0c; the following is the correct start/off process.
正確開機流程
當二台 Node 主機都關機而預設 Node1 為 Primary Node 的情況下,正確的開機流程為:
When both Node hosts are switched off and Node1 is set to be Primary Node xff0c; the correct startup process is xff1a;
- Power On Node1 主機
- 過約 1 分鐘後再 Power On Node2 主機
- 當 Node1 主機開機流程運作到 DRBD 程序時會等待 60 秒 (等待 Secondary Node 回應),在數秒期間 Node2 主機開機流程也運作到 DRBD 程序時會去找 Node1 主機進行溝通
- Node1 順利開機完成,Node2 也開機完成
- 登入 Node1、Node2 主機查看 DRBD 狀態及相關資訊 (掛載 /dev/drbd0 至 /db、接手 mysqld 服務、啟動 Cluster IP 10.10.25.120 及網卡 eth0:0)
正確關機流程
當遇到不可抗力因素必須要二台主機都關機且 Node1 為 Primary Node 的情況下,正確的關機流程為:
When a force majeure factor is encountered, both hosts must be shut down and Node1 must be Primary Node xff0c; the correct shutdown process is xff1a;
- 在 Node1 主機執行 service heartbeat stop 指令將 Heartbeat 服務關閉
- 確認 Node2 主機接手成為 Primary Node 且相關服務運作正常
- 將 Node1 主機關機 (此時在 Node2 主機看到的 DRBD 狀態為 Primary/Unknown)
- 在 Node2 主機上執行 service mysqld stop 指令將 MySQL 服務關閉
- MySQL 服務關閉完成後即可將 Node2 主機關機
- 之後開機時再遵照上述的?正確開機流程?步驟即可順利讓 Cluster 再度運作
因為出現不同的設定需求 (加裝網卡所以要設定網卡為 Bonding 模式、更換 IP Address、伺服器要換地方),因此除了上述的正常開機/關機流程是不夠的,順便記錄一下整個流程:
xff0c due to different configuration needs (install the webcards as Bonding mode, change IP Address, change the server to a different location); therefore, the normal start-up/cut-off process mentioned above is not enough xff0c; log the entire process xff1a;
- 先將 Node1、Node2 開機啟動 drbd、heartbeat 服務關閉
- 於 Node1 主機將 heartbeat 服務停止,確認 Node2 主機接手 HA 服務後即可將 Node1 主機關機
- 於 Node2 主機將 mysqld 服務停止後即可關機
- 將 Node1、Node2 退出機櫃後搬移至新地點,上架前先安裝擴充的網卡此時因為開機沒有啟動 HA 服務所以沒有開機順序的問題
- 開機完成後設定 Node1、Node2 的網卡 Bonding 模式及修改 IP Address 並測試容錯機制是否運作
- 修改 drbd、heartbeat 相關設定檔內容 (IP Address)
- 將 Node1、Node2 開機啟動 drbd、heartbeat 服務啟用後先將 Node1 重新開機約 1 分鐘後再將 Node2 重新開機
- 開機完成後確定 HA 機制是否運作並測試 HA 機制
- 完成改設定及移機要求
[DRBD - Software Development for High Availability Clusters]
Q1.'ha' ignored, since this host (node2.weithenn.org) is not mentioned with an 'on' keyword.?
Error Meaage:
執行指令 drbdadm create-md ha 時出現如下錯誤訊息
The following error message occurred while executing drbdadm code-md ha
Ans:
因為在 drbd 設定檔 drbd.conf 中 on 本來寫的是 node1、node2 而以,將設定檔內容改為 FQDN 名稱 node1.weithenn.org、node2.weithenn.org 後即可正常執行指令。
Because in drbd configuration file drbd.conf on & #xff0c instead of node1, node2 originally written, the command can be properly executed after changing the profile content to FQDN name node1.weithenn.org, node2.weithenn.org.
Q2.drbdadm create-md ha: exited with code 20?
Error Meaage:
執行指令 drbdadm create-md ha 時出現如下錯誤訊息
The following error message occurred while executing drbdadm code-md ha
Ans:
因為忘了執行 fdisk /dev/hdb 指令建立分割區所造成,如下將 /dev/hdb 建立分割區後指令即可正常執行
& #xff0c as a result of forgetting to execute fdisk/dev/hdb command to create partitions; the following commands will be properly executed when /dev/hdb creates partitions
Q3.drbdadm create-md ha: exited with code 40?
Error Meaage:
執行指令 drbdadm create-md ha 時出現如下錯誤訊息
The following error message occurred while executing drbdadm code-md ha
Ans:
使用 dd 指令將一些資料塞到 /dev/hdb 後再執行 drbdadm create-md ha 指令即可順利執行
Use dd command to stuff some data to /dev/ hdb before executing drbdamcdate-md ha
Q1.DRBD 狀態始終是 Secondary/Unknown?
Q1. DRBD status is always Secondary/Unknown?
Error Meaage:
Node1、Node2 主機啟動 DRBD 後狀態始終是 Secondary/Unknown
Node1, Node2 Host has been in status since the launch of the DRBD
Ans:
因為 Node1、Node2 沒有打開相對應的 Port,請開啟相對應的 Port 或先把 IPTables 服務關閉即可。
Node1, Node2 does not open the matching Port, please open the matching Port or close the IPtables service first.
注册有任何问题请添加 微信:MVIP619 拉你进入群
打开微信扫一扫
添加客服
进入交流群
发表评论