HA-DRBD Heartbeat 建置 MySQL 高可用性

资讯 2024-07-09 阅读:28 评论:0
美化布局示例

欧易(OKX)最新版本

【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   全球官网 大陆官网

币安(Binance)最新版本

币安交易所app【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

火币HTX最新版本

火币老牌交易所【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

若您的預算無法購買共用儲存設備且對於 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; , ; ; ; ; ; < ””””?rel_rel_rel_rel_rel_rel_rel_ret>?rel_rel_rel_rel_rel_rel_rel_rel_rel_rel_rel_rel_rel_rel_rel_rel_rel_

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

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)

 #service iptables stop
 #sestatus
 SELinux status:                 disabled

步驟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)

 #cat /etc/hosts
 127.0.0.1       localhost
 ::1             localhost6.localdomain6 localhost6
 10.10.25.121    node1.weithenn.org node1
 10.10.25.122    node2.weithenn.org node2

步驟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 /dev/hdb                                                      //準備為 hdb 建立分割區
 The number of cylinders for this disk is set to 20805.
 There is nothing wrong with that, but this is larger than 1024,
 and could in certain setups cause problems with:
 1) software that runs at boot time (e.g., old versions of LILO)
 2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
 Command (m for help): n                                              //鍵入 n 表示要建立分割區
 Command action
    e   extended
    p   primary partition (1-4)
 p                                                                    //鍵入 p 表示建立主要分割區
 Partition number (1-4): 1                                            //鍵入 1 為此主要分割區代號
 First cylinder (1-20805, default 1):                                 //開始磁柱值,按下 enter 即可
 Using default value 1
 Last cylinder or +size or +sizeM or +sizeK (1-20805, default 20805): //結束磁柱值,按下 enter 即可
 Using default value 20805
 Command (m for help): w                                              //鍵入 w 表示確定執行剛才設定
 The partition table has been altered!
 Calling ioctl() to re-read partition table.
 Syncing disks.
 [root@node1 yum.repos.d]# partprobe                                  //使剛才的 partition table 變更生效

建立分割區完成後使用指令 fdisk -l 確定 partition talbe 狀態

Use command fdisk-l to confirm party level status when creating partitions

 #fdisk -l
 Disk /dev/hda: 21.4 GB, 21474754560 bytes
 255 heads, 63 sectors/track, 2610 cylinders
 Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot      Start         End      Blocks   Id  System
 /dev/hda1   *           1          13      104391   83  Linux
 /dev/hda2              14        2610    20860402+  8e  Linux LVM
 Disk /dev/hdb: 10.7 GB, 10737377280 bytes
 16 heads, 63 sectors/track, 20805 cylinders
 Units = cylinders of 1008 * 512 = 516096 bytes
    Device Boot      Start         End      Blocks   Id  System
 /dev/hdb1               1       20805    10485688+  83  Linux

建立分割區完成後請建立 /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)

 #mkdir /db

步驟4.安裝相關套件

使用?[YUM]?安裝 DRBD、Heartbeat、MySQL 等相關套件。(Node1 及 Node2 都必須安裝)

Use? ?

 #yum -y install drbd83 kmod-drbd83
 #yum -y install heartbeat
 #yum -y install heartbeat heartbeat-ldirectord heartbeat-pils heartbeat-stonith
 #yum -y install mysql-server

安裝好後修改 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)

 #vi /etc/my.cnf
 [mysqld]
 datadir=/var/lib/mysql                      //預設值
 datadir=/db                                 //修改後
 socket=/var/lib/mysql/mysql.sock
 user=mysql
 ...略...

步驟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)

 #vi /etc/drbd.conf
 global {
   minor-count 64;
   usage-count yes;
 }
 common {
   syncer { rate 1000M; }
 }
 resource ha {
   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-local-io-error.sh; /usr/lib/drbd/notify-emergency-shutdown.sh; echo o > /proc/sysrq-trigger ; halt -f";
     fence-peer "/usr/lib/heartbeat/drbd-peer-outdater -t 5";
     pri-lost "/usr/lib/drbd/notify-pri-lost.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b > /proc/sysrq-trigger ; reboot -f";
     split-brain "/usr/lib/drbd/notify-split-brain.sh root";
     out-of-sync "/usr/lib/drbd/notify-out-of-sync.sh root";
   }
   startup {
     wfc-timeout 60;
     degr-wfc-timeout 120;
     outdated-wfc-timeout 2;
   }
   disk {
     on-io-error detach;
     fencing resource-only;
   }
   syncer {
     rate 1000M;
   }
   on node1.weithenn.org {
     device /dev/drbd0;
     disk /dev/hdb1;
     address 192.168.1.1:7788;
     meta-disk internal;
   }
   on node2.weithenn.org {
     device /dev/drbd0;
     disk /dev/hdb1;
     address 192.168.1.2:7788;
     meta-disk internal;
   }
 }

完成後請調整 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)

 #chgrp haclient /sbin/drbdsetup
 #chmod o-x /sbin/drbdsetup
 #chmod u+s /sbin/drbdsetup
 #chgrp haclient /sbin/drbdmeta
 #chmod o-x /sbin/drbdmeta
 #chmod u+s /sbin/drbdmeta

步驟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)

 #modprobe drbd                                    //載入 drbd 模組
 #lsmod|grep drbd                                  //確認 drbd 模組是否載入
 drbd                  228528  0
 #dd if=/dev/zero of=/dev/hdb1 bs=1M count=100     //把一些資料塞到 hdb 內 (否則 create-md 時有可能會出現錯誤)
 #drbdadm create-md ha                             //建立 drbd resource
 #service drbd start                               //啟動 drbd 服務
 #chkconfig drbd on                                //設定 drbd 開機時自動啟動

啟動完成後可以使用 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.

 #service drbd status
 drbd driver loaded OK; device status:
 version: 8.3.8 (api:88/proto:86-94)
 GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by mockbuild@builder10.centos.org, 2010-06-04 08:04:16
 m:res  cs         ro                   ds                         p  mounted  fstype
 0:ha   Connected  Secondary/Secondary  Inconsistent/Inconsistent  C 

步驟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)

 #drbdadm -- --overwrite-data-of-peer primary ha
 #service drbd status
 drbd driver loaded OK; device status:
 version: 8.3.8 (api:88/proto:86-94)
 GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by mockbuild@builder10.centos.org, 2010-06-04 08:04:16
 m:res  cs          ro                 ds                     p             mounted  fstype
 ...    sync'ed:    5.7%               (9660/10236)M          delay_probe:
 0:ha   SyncSource  Primary/Secondary  UpToDate/Inconsistent  C

當同步完成後可發現二台主機的 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)

 #service drbd status
 drbd driver loaded OK; device status:
 version: 8.3.8 (api:88/proto:86-94)
 GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by mockbuild@builder10.centos.org, 2010-06-04 08:04:16
 m:res  cs         ro                 ds                 p  mounted  fstype
 0:ha   Connected  Primary/Secondary  UpToDate/UpToDate  C

在 Node2 主機執行看到下列狀態 (此台為 Secondary Node)

sees the following state on the Node2 host (this is Secondary Node)

 #service drbd status
 drbd driver loaded OK; device status:
 version: 8.3.8 (api:88/proto:86-94)
 GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by mockbuild@builder10.centos.org, 2010-06-04 08:04:16
 m:res  cs         ro                 ds                 p  mounted  fstype
 0:ha   Connected  Secondary/Primary  UpToDate/UpToDate  C

步驟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)

 #mkfs.ext3 /dev/drbd0
 mke2fs 1.39 (29-May-2006)
 Filesystem label=
 OS type: Linux
 Block size=4096 (log=2)
 Fragment size=4096 (log=2)
 1310720 inodes, 2621333 blocks
 131066 blocks (5.00%) reserved for the super user
 First data block=0
 Maximum filesystem blocks=2684354560
 80 block groups
 32768 blocks per group, 32768 fragments per group
 16384 inodes per group
 Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
 Writing inode tables: done
 Creating journal (32768 blocks): done
 Writing superblocks and filesystem accounting information: done
 This filesystem will be automatically checked every 30 mounts or
 180 days, whichever comes first.  Use tune2fs -c or -i to override.
 #mount /dev/drbd0 /db
 #df -h
 Filesystem            Size  Used Avail Use% Mounted on
 /dev/mapper/VolGroup00-LogVol00
                       19G  2.7G   15G  16% /
 /dev/hda1              99M   12M   82M  13% /boot
 tmpfs                 252M     0  252M   0% /dev/shm
 /dev/drbd0            9.9G  151M  9.2G   2% /db

將 /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)

 #service mysqld start
 #ll /db
 total 20544
 -rw-rw---- 1 mysql mysql 10485760 Nov  1 14:48 ibdata1
 -rw-rw---- 1 mysql mysql  5242880 Nov  1 14:48 ib_logfile0
 -rw-rw---- 1 mysql mysql  5242880 Nov  1 14:48 ib_logfile1
 drwx------ 2 mysql mysql    16384 Nov  1 14:46 lost+found
 drwx------ 2 mysql mysql     4096 Nov  1 14:48 mysql             //預設資料庫
 drwx------ 2 mysql mysql     4096 Nov  1 14:48 test              //測試資料庫

步驟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)

 #service mysqld stop                                             //停止 mysql 服務
 #umount /dev/drbd0                                               //卸載 /dev/drbd0
 #drbdadm secondary ha                                            //在 node1 主機執行將其降為 Secondary Node
 #service drbd status                                             //確認二台主機都為 Secondary Node
 drbd driver loaded OK; device status:
 version: 8.3.8 (api:88/proto:86-94)
 GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by mockbuild@builder10.centos.org, 2010-06-04 08:04:16
 m:res  cs         ro                   ds                 p  mounted  fstype
 0:ha   Connected  Secondary/Secondary  UpToDate/UpToDate  C

步驟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)

 #vi /etc/ha.d/ha.cf
 debugfile /var/log/ha-debug                   //heartbeat 除錯記錄檔
 logfile /var/log/ha-log                       //heartbeat 記錄檔
 logfacility local0                            //記錄檔的記錄等級
 autojoin none
 ucast eth0 10.10.25.122                       //此為 Node1 主機的設定,若是 Node2 主機請設定為 10.10.25.121
 ucast eth1 192.168.1.2                        //此為 Node1 主機的設定,若是 Node2 主機請設定為 192.168.1.1
 ping 10.10.25.254                             //IP 連線偵測用,設定區網內的 Gateway (當網路或 Heartbeat 失效時測試用)
 respawn hacluster /usr/lib/heartbeat/ipfail
 respawn hacluster /usr/lib/heartbeat/dopd
 apiauth dopd gid=haclient uid=hacluster
 udpport 694                                   //使用 UDP Protocol 及 694 Port 來通訊
 warntime 5                                    //網路發生狀況時告警時間 5 秒
 deadtime 15                                   //網路斷線狀況發生 15 秒則判定網路出問題 (Secondary Node 準備接手 Primary Node 的服務)
 initdead 60
 keepalive 2                                   //每 2 秒 Node 互相偵測一次 (使用 ping)
 node node1.weithenn.org
 node node2.weithenn.org
 auto_failback off                             //當 Primary Node 發生問題 Secondary Node 接手後若本來的 Primary Node 修復後是否要把目前的 Primary Node 搶回

步驟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)

  1. 指定 Primary Node 的 FQDN 此例為 node1.weithenn.org
  2. 指定 Cluster IP Address 此例為 10.10.25.120
  3. 指定 Cluster Resource Name 此例為 drbddisk::ha
  4. 指定 Cluster Device、Mount Point、File System Type 此例為 Filesystem::/dev/drbd0::/db::ext3
  5. 指定 Service 此例為 mysql

此次實作的 haresources 設定檔內容如下

The current implementation of the haresources profile is described below.

 #vi /etc/ha.d/haresources
 node1.weithenn.org 10.10.25.120 drbddisk::ha Filesystem::/dev/drbd0::/db::ext3 mysql

步驟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.

 #( echo -ne "auth 1
1 sha1 "; dd if=/dev/urandom bs=512 count=1 | openssl md5) > /etc/ha.d/authkeys
 #cat /etc/ha.d/authkeys
 auth 1
 1 sha1 71461fc5e160d7846c2f4b524f952128
 #chmod 600 /etc/ha.d/authkeys
 #scp /etc/ha.d/authkeys node2:/etc/

步驟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)

 #vi /etc/ha.d/resource.d/mysql                    //內容如下
 #!/bin/bash
 . /etc/ha.d/shellfuncs
 case "$1" in
 start)
   res=`/etc/init.d/mysqld start`
   ret=$?
   ha_log $res
   exit $ret
   ;;
 stop)
   res=`/etc/init.d/mysqld stop`
   ret=$?
   ha_log $res
   exit $ret
   ;;
 status)
   if [[ `ps -ef | grep '[m]ysqld'` > 1 ]]; then
      echo "running"
   else
      echo "stopped"
   fi
   ;;
 *)
   echo "Usage: mysqld {start|stop|status}"
   exit 1
   ;;
 esac
 exit 0
 #chmod 755 /etc/ha.d/resource.d/mysql           //將此檔案權限設定為可執行

步驟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)

 #chkconfig --add heartbeat
 #ll /etc/rc3.d/S7*
 lrwxrwxrwx 1 root root 14 Nov  1 14:25 /etc/rc3.d/S70drbd -> https://blog.csdn.net/liaoyuanzi/article/init.d/drbd
 lrwxrwxrwx 1 root root 19 Nov  1 15:01 /etc/rc3.d/S75heartbeat -> https://blog.csdn.net/liaoyuanzi/article/init.d/heartbeat
 #chkconfig heartbeat on

步驟6.啟動 Heartbeat 服務

完成後在 Node1 及 Node2 主機啟動 Hertbeat 服務 (Node1 及 Node2 都必須設定)

Start Hertbeat services on Node1 and Node2 hosts after completion (Node1 and Node2 have to be set)

 #service heartbeat start

當二台主機都啟動 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:

 * 掛載 /dev/drbd0 至 /db  (Node2 主機此時無法掛載 /dev/drbd0 至 /db)
 * 啟動 mysqld 服務
 * 擁有 Cluster IP 10.10.25.120 (eth0:0)

在測試 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:

 watch -n 1 service drbd status

在測試切換 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.

 #mysqladmin -u root password 'weithenn'                 //設定 MySQL 管理者密碼為 weithenn
 #mysqladmin -p create testdb1                           //建立名稱為 testdb1 的資料庫 (會要求輸入 MySQL 管理者密碼)
 #mysqlshow -p                                           //顯示資料庫 (會要求輸入 MySQL 管理者密碼)

測試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 服務重新啟動

Primary Node for Node1 host & #xff0c; reboot for startbeat service

 [root@node1 ~]#service heartbeat restart
 Stopping High-Availability services:
                                                           [  OK  ]
 Waiting to allow resource takeover to complete:
                                                           [  OK  ]
 Starting High-Availability services:
 2010/11/03_11:31:41 INFO:  Resource is stopped
                                                           [  OK  ]

當上述指令執行時您會看到 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:

 [root@node1 ~]#service drbd status
 drbd driver loaded OK; device status:
 version: 8.3.8 (api:88/proto:86-94)
 GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by mockbuild@builder10.centos.org, 2010-06-04 08:04:16
 m:res  cs         ro                 ds                 p  mounted  fstype
 0:ha   Connected  Secondary/Primary  UpToDate/UpToDate  C

此時在 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)

 [root@node2 ~]#service drbd status                                         //接手 /dev/drbd0
 drbd driver loaded OK; device status:
 version: 8.3.8 (api:88/proto:86-94)
 GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by mockbuild@builder10.centos.org, 2010-06-04 08:04:16
 m:res  cs         ro                 ds                 p  mounted  fstype
 0:ha   Connected  Primary/Secondary  UpToDate/UpToDate  C  /db      ext3
 [root@node2 ~]# service mysqld status                                      //接手 mysqld 服務
 mysqld (pid 10101) is running...
 [root@node2 ~]# ifconfig eth0:0                                            //接手 Cluster IP
 eth0:0    Link encap:Ethernet  HWaddr 00:03:FF:1E:7B:82
           inet addr:10.10.25.120  Bcast:10.10.25.255  Mask:255.255.255.0
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
           Interrupt:11 Base address:0xc000

測試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.

  1. 拔除 eth0 上的網路線或執行 ifdown eth0 指令來關閉網卡連線
  2. 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;

  1. Power On Node1 主機
  2. 過約 1 分鐘後再 Power On Node2 主機
  3. 當 Node1 主機開機流程運作到 DRBD 程序時會等待 60 秒 (等待 Secondary Node 回應),在數秒期間 Node2 主機開機流程也運作到 DRBD 程序時會去找 Node1 主機進行溝通
  4. Node1 順利開機完成,Node2 也開機完成
  5. 登入 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;

  1. 在 Node1 主機執行 service heartbeat stop 指令將 Heartbeat 服務關閉
  2. 確認 Node2 主機接手成為 Primary Node 且相關服務運作正常
  3. 將 Node1 主機關機 (此時在 Node2 主機看到的 DRBD 狀態為 Primary/Unknown)
  4. 在 Node2 主機上執行 service mysqld stop 指令將 MySQL 服務關閉
  5. MySQL 服務關閉完成後即可將 Node2 主機關機
  6. 之後開機時再遵照上述的?正確開機流程?步驟即可順利讓 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;

  1. 先將 Node1、Node2 開機啟動 drbd、heartbeat 服務關閉
  2. 於 Node1 主機將 heartbeat 服務停止,確認 Node2 主機接手 HA 服務後即可將 Node1 主機關機
  3. 於 Node2 主機將 mysqld 服務停止後即可關機
  4. 將 Node1、Node2 退出機櫃後搬移至新地點,上架前先安裝擴充的網卡此時因為開機沒有啟動 HA 服務所以沒有開機順序的問題
  5. 開機完成後設定 Node1、Node2 的網卡 Bonding 模式及修改 IP Address 並測試容錯機制是否運作
  6. 修改 drbd、heartbeat 相關設定檔內容 (IP Address)
  7. 將 Node1、Node2 開機啟動 drbd、heartbeat 服務啟用後先將 Node1 重新開機約 1 分鐘後再將 Node2 重新開機
  8. 開機完成後確定 HA 機制是否運作並測試 HA 機制
  9. 完成改設定及移機要求

[DRBD - Software Development for High Availability Clusters]

[The DRBD User's Guide]

[The Linux-HA User's Guide]

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

 'ha' ignored, since this host (node2.weithenn.org) is not mentioned with an 'on' keyword.

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

 open(/dev/hdb1) failed: No such file or directory
 Command 'drbdmeta 0 v08 /dev/hdb1 internal create-md' terminated with exit code 20
 drbdadm create-md ha: exited with code 20

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

 #fdisk /dev/hdb                                                      //準備為 hdb 建立分割區
 The number of cylinders for this disk is set to 20805.
 There is nothing wrong with that, but this is larger than 1024,
 and could in certain setups cause problems with:
 1) software that runs at boot time (e.g., old versions of LILO)
 2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
 Command (m for help): n                                              //鍵入 n 表示要建立分割區
 Command action
    e   extended
    p   primary partition (1-4)
 p                                                                    //鍵入 p 表示建立主要分割區
 Partition number (1-4): 1                                            //鍵入 1 為此主要分割區代號
 First cylinder (1-20805, default 1):                                 //開始磁柱值,按下 enter 即可
 Using default value 1
 Last cylinder or +size or +sizeM or +sizeK (1-20805, default 20805): //結束磁柱值,按下 enter 即可
 Using default value 20805
 Command (m for help): w                                              //鍵入 w 表示確定執行剛才設定
 The partition table has been altered!
 Calling ioctl() to re-read partition table.
 Syncing disks.
 [root@node1 yum.repos.d]# partprobe                                  //使剛才的 partition table 變更生效

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

 Device size would be truncated, which
 would corrupt data and result in
 'access beyond end of device' errors.
 You need to either
    * use external meta data (recommended)
    * shrink that filesystem first
    * zero out the device (destroy the filesystem)
 Operation refused.
 Command 'drbdmeta 0 v08 /dev/hdb1 internal create-md' terminated with exit code 40
 drbdadm create-md ha: exited with code 40

Ans:

使用 dd 指令將一些資料塞到 /dev/hdb 後再執行 drbdadm create-md ha 指令即可順利執行

Use dd command to stuff some data to /dev/ hdb before executing drbdamcdate-md ha

 #dd if=/dev/zero of=/dev/hdb1 bs=1M count=100

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

 #service drbd status
 drbd driver loaded OK; device status:
 version: 8.3.8 (api:88/proto:86-94)
 GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by mockbuild@builder10.centos.org, 2010-06-04 08:04:16
 m:res  cs            ro                 ds                     p  mounted  fstype
 0:ha   WFConnection  Secondary/Unknown  Inconsistent/DUnknown  C

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.

美化布局示例

欧易(OKX)最新版本

【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   全球官网 大陆官网

币安(Binance)最新版本

币安交易所app【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

火币HTX最新版本

火币老牌交易所【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址
文字格式和图片示例

注册有任何问题请添加 微信:MVIP619 拉你进入群

弹窗与图片大小一致 文章转载注明

分享:

扫一扫在手机阅读、分享本文

发表评论
平台列表
美化布局示例

欧易(OKX)

  全球官网 大陆官网

币安(Binance)

  官网

火币(HTX)

  官网

Gate.io

  官网

Bitget

  官网

deepcoin

  官网
热门文章
  • 0.00003374个比特币等于多少人民币/美金

    0.00003374个比特币等于多少人民币/美金
    0.00003374比特币等于多少人民币?根据比特币对人民币的最新汇率,0.00003374比特币等于2.2826 1222美元/16.5261124728人民币。比特币(BTC)美元(USDT)人民币(CNY)0.00003374克洛克-0/22216.5261124728比特币对人民币的最新汇率为:489807.72 CNY(1比特币=489807.72人民币)(1美元=7.24人民币)(0.00003374USDT=0.0002442776 CNY)。汇率更新于2024...
  • 134 USD toBTC Calculator -

    134                            USD                        toBTC                        Calculator -
    For the week (7 days) Date Day 134 USD to BTC Changes Changes % June...
  • 0.00006694个比特币等于多少人民币/美金

    0.00006694个比特币等于多少人民币/美金
    0.00006694比特币等于多少人民币?根据比特币对人民币的最新汇率,0.00006694比特币等于4.53424784美元/32.5436 16人民币。比特币(BTC)美元(USDT)人民币(CNY)0.000066944.53424784【比特币密码】32.82795436 16比特币对人民币的最新汇率为:490408.64 CNY(1比特币=490408.64人民币)(1美元=7.24人民币)(0.00006694USDT=0.0004846456 CNY)汇率更新时...
  • 12年怎么购买比特币?比特币投资,轻松掌控

    12年怎么购买比特币?比特币投资,轻松掌控
    12年怎么购买比特币?买卖比特币可以通过以下交易所进行购买,分别是:欧易官网平台、ZG交易所、艾戴克斯交易所、C2CX交易软件、BaseFEX交易APP、波网交易平台、安银交易所、BitMart交易软件、紫牛币交所交易APP和澳网(AOMEX)交易平台等等十大平台下载,高效安全的数字货币交易平台。How do you buy bitcoins in 12 years? Bitcoins can be purchased through ten major platforms...
  • 0.00015693个比特币等于多少人民币/美金

    0.00015693个比特币等于多少人民币/美金
    0.000 15693比特币等于多少人民币?根据比特币对人民币的最新汇率,0.000 15693比特币等于10.6 1678529美元/76.86554996人民币。比特币(BTC)【比特币价格翻倍】美元(USDT)人民币(CNY)0.000/克洛克-0/5693【数字货币矿机】10.6 167852976.8655254996比特币对人民币的最新汇率为:489,807.72 CNY(1比特币= 489,807.72人民币)(1美元=7.24人民币)(0.00015693 U...
标签列表