Download - Dbabc.net 利用heartbeat + drbd搭建my sql高可用环境
http://dbabc.net Email:[email protected]
利用 Heartbeat + DRBD 搭建 MySQL 高可用环境
http://dbabc.net 畅享生活、关注技术 生活有你更精彩
http://dbabc.net Email:[email protected]
目录
一、DRBD 原理与架构信息 ............................................................................................. 3
1、原理简述................................................................................................................. 3
2、架构图 .................................................................................................................... 4
3、主机信息: ............................................................................................................. 4
二、Heartbeat+DRBD+MySQL 安装 .................................................................................. 5
1、定义主机名 ............................................................................................................. 5
2、创建磁盘分区 ......................................................................................................... 5
3、安装 drbd、heartbeat ............................................................................................. 7
4、配置 DRBD .............................................................................................................. 8
5、加载 drbd 模块并建立 resource .......................................................................... 9
6、配置 heartbeat ...................................................................................................... 12
三、测试 Mysql HA ........................................................................................................ 14
1、手动切换 Node ..................................................................................................... 14
2、模拟 Master 节点宕机或者网络中断 .................................................................. 17
附:HA 开、关机流程 ................................................................................................... 17
1、正确开机步骤 ....................................................................................................... 17
2、正确关机步骤 ....................................................................................................... 17
http://dbabc.net Email:[email protected]
一、DRBD 原理与架构信息
1、原理简述
DRBD 是一种块设备,可以被用于高可用(HA) 之中.它类似于一个网络 RAID -1 功能。当你将
数据写入本地文件系统时,数据还将会被发送到网络中另一台主机上。以相同的形式记录在
一个文件系统中。本地(主节点)与远程主机(备节点)的数据可以保证实时同步。当本地系统
出现故障时,远程主机上还会保留有一份相同的数据,可以继续使用。
http://dbabc.net Email:[email protected]
2、架构图
VIP:10.20.50.250
Heartbeat
DRBD
/data /data
/dev/drbd0 /dev/drbd0
Mysql Master HA
AP1 AP2 AP3
mysql-n1 mysql-n2
Primary Secondary
Primary Secondary
3、主机信息:
Node1:
主机名: mysql-n1
操作系统: CentOS release 5.8 (Final)
内核版本: 2.6.18-308
DRBD 版本: drbd83-8.3.12-2 (yum 安装的版本)
heartbeat 版本: heartbeat-2.1.3-3(yum 安装的版本)
eth0 IP: 10.20.50.221
eth1 IP: 192.168.18.1
http://dbabc.net Email:[email protected]
Node2:
主机名: mysql-n2
操作系统: CentOS release 5.8 (Final)
内核版本: 2.6.18-308
DRBD 版本: drbd83-8.3.12-2 (yum 安装的版本)
heartbeat 版本: heartbeat-2.1.3-3(yum 安装的版本)
eth0 IP: 10.20.50.222
eth1 IP: 192.168.18.2
VIP: eth0:0:10.20.50.250
DRBD 资源目录: /data
二、Heartbeat+DRBD+MySQL 安装
1、定义主机名
Node1、Node2 都必须设定
# vi /etc/hosts
10.20.50.221 mysql-n1
10.20.50.222 mysql-n2
2、创建磁盘分区
Node1、Node2 都必须设定
为 sdb 硬盘建立分区,此硬盘即为两台主机要同步数据的硬盘,也就是我们存放 MySQL 数
据库的地方。
先查看磁盘情况:
[root@mysql-n1 ~]# fdisk -l
Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 5221 41833260 8e Linux LVM
Disk /dev/sdb: 17.1 GB, 17179869184 bytes
http://dbabc.net Email:[email protected]
255 heads, 63 sectors/track, 2088 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdb doesn't contain a valid partition table
#fdisk /dev/sdb
依次输入:n --> p -->1 回车-->回车-->w 完成
[root@mysql-n1 ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.
The number of cylinders for this disk is set to 2088.
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)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2088, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-2088, default 2088):
Using default value 2088
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
建立分区完成后使用命令 fdisk -l 确定 partition talbe 状态
在两台主机上执行:
[root@mysql-n1 ~]# fdisk -l
http://dbabc.net Email:[email protected]
Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 5221 41833260 8e Linux LVM
Disk /dev/sdb: 17.1 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 2088 16771828+ 83 Linux
3、安装 drbd、heartbeat
Node1、Node2 都必须设定
yum install -y drbd83 kmod-drbd83
yum install -y heartbeat heartbeat-ldirectord heartbeat-pils heartbeat-stonith
[root@mysql-n2 ~]# yum install -y drbd83 kmod-drbd83
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
* base: mirrors.btte.net
* extras: mirrors.btte.net
* updates: mirrors.btte.net
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package drbd83.x86_64 0:8.3.12-2.el5.centos set to be updated
---> Package kmod-drbd83.x86_64 0:8.3.12-1.el5.centos set to be installed
--> Finished Dependency Resolution
Dependencies Resolved
======================================================================
=====
Package Arch Version Repository Size
http://dbabc.net Email:[email protected]
======================================================================
=====
Installing:
drbd83 x86_64 8.3.12-2.el5.centos extras 236 k
kmod-drbd83 x86_64 8.3.12-1.el5.centos extras 136 k
Transaction Summary
======================================================================
=====
Install 2 Package(s)
Upgrade 0 Package(s)
Total download size: 371 k
Downloading Packages:
(1/2): kmod-drbd83-8.3.12-1.el5.centos.x86_64.rpm | 136 kB 00:00
(2/2): drbd83-8.3.12-2.el5.centos.x86_64.rpm | 236 kB 00:00
---------------------------------------------------------------------------
Total 1.3 MB/s | 371 kB 00:00
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : drbd83 1/2
Installing : kmod-drbd83 2/2
Installed:
drbd83.x86_64 0:8.3.12-2.el5.centos
kmod-drbd83.x86_64 0:8.3.12-1.el5.centos
Complete!
4、配置 DRBD
Node1、Node2 都必须设定
安装 drbd 之后,会在/etc 下面创建 drbd.conf 文件,里面只有以下两行文字:
# please have a a look at the example configuration file in
# /usr/share/doc/drbd83/drbd.conf
实际应该在/usr/share/doc/drbd83-8.3.12/drbd.conf
http://dbabc.net Email:[email protected]
编辑配置文件/etc/drbd.conf,
Master Node:
# vi /etc/drbd.conf
#
# please have a a look at the example configuration file in
# /usr/share/doc/drbd83/drbd.conf
#
include "drbd.d/global_common.conf";
include "drbd.d/*.res";
在/etc/drbd.d/下创建 ha.res
# vi /etc/drbd.d/ha.res
resource ha {
on mysql-n1 {
device /dev/drbd1;
disk /dev/sdb1;
address 192.168.18.1:7898;
meta-disk internal;
}
on mysql-n2 {
device /dev/drbd1;
disk /dev/sdb1;
address 192.168.18.2:7898;
meta-disk internal;
}
}
同时将/etc/drbd.conf 和/etc/drbd.d/ 下的配置信息 scp 到 Node2.
5、加载 drbd 模块并建立 resource
Node1、Node2 都必须设定
使用命令 modprobe 命令来加载 drbd 模块,加载完成后使用命令 drbdadm create-md 来建
立 drbd resource,由于我们在 drbd 配置文件中命令 resource 名称为 ha 所以命令便输入
ha。
#modprobe drbd //加载 drbd 模块
#lsmod|grep drbd //确认 drbd 模块是否加载
http://dbabc.net Email:[email protected]
[root@mysql-n1 ~]# lsmod|grep drbd
drbd 298760 0
#drbdadm create-md ha //建立 drbd resource
[root@mysql-n1 ~]# drbdadm create-md ha
Writing meta data...
initializing activity log
NOT initialized bitmap
New drbd meta data block successfully created.
#service drbd start //启动 drbd 服务
[root@mysql-n1 ~]# service drbd start
Starting DRBD resources: [ ].
#chkconfig drbd on //设定 drbd 开机时自动启动
启动完成后可以使用 service drbd status 命令来查看目前 drbd 的状态,
当 node1 启动 drbd 服务而 node2 尚未启动时会发现状态为 Secondary/Unknown,
当 node2 启动 drbd 服务后则状态为 Secondary/Secondary,而 ds 状态为 Inconsistent 表示
两台主机数据尚未同步。
Node1
#service drbd status
[root@mysql-n1 ~]# service drbd status
drbd driver loaded OK; device status:
version: 8.3.12 (api:88/proto:86-96)
GIT-hash: e2a8ef4656be026bbae540305fcb998a5991090f build by
[email protected], 2012-01-28 13:52:25
m:res cs ro ds p mounted
fstype
1:ha Connected Secondary/Secondary Inconsistent/Inconsistent C
Node2
[root@mysql-n2 ~]# service drbd status
drbd driver loaded OK; device status:
version: 8.3.12 (api:88/proto:86-96)
GIT-hash: e2a8ef4656be026bbae540305fcb998a5991090f build by
[email protected], 2012-01-28 13:52:25
m:res cs ro ds p mounted
http://dbabc.net Email:[email protected]
fstype
1:ha Connected Secondary/Secondary Inconsistent/Inconsistent C
初始化两台主机 sdb 数据
确 定 Node1、Node2 主机都可检测到对方 (Secondary/Secondary) 后,设定 Node1 为
Primary Node,并使两台主机开始同步 sdb 硬盘数据 (即 /dev/drbd1),此时查看 drbd 状
态可发现同步的百分比及进度。 (仅 Node1 主机执行)
设置 Node1 为主节点并进行格式化:
#drbdadm primary all
[root@mysql-n1 ~]# drbdadm primary all
1: State change failed: (-2) Need access to UpToDate data
Command 'drbdsetup 1 primary' terminated with exit code 17
//如果执行出错请执行下面的命令 (仅 Node1 节点执行)
# drbdadm -- -- overwrite-data-of-peer primary All
在 Node1 主机执行看到下列状态 (此台为 Primary Node)
[root@mysql-n1 ~]# drbdadm -- --overwrite-data-of-peer primary ha
[root@mysql-n1 ~]# service drbd status
drbd driver loaded OK; device status:
version: 8.3.12 (api:88/proto:86-96)
GIT-hash: e2a8ef4656be026bbae540305fcb998a5991090f build by
[email protected], 2012-01-28 13:52:25
m:res cs ro ds p mounted fstype
... sync'ed: 2.3% (16016/16376)M
1:ha SyncSource Primary/Secondary UpToDate/Inconsistent C
在 Node2 主机执行看到下列状态 (此台为 Secondary Node)
[root@mysql-n2 ~]# service drbd status
drbd driver loaded OK; device status:
version: 8.3.12 (api:88/proto:86-96)
GIT-hash: e2a8ef4656be026bbae540305fcb998a5991090f build by
[email protected], 2012-01-28 13:52:25
m:res cs ro ds p mounted fstype
... sync'ed: 16.0% (13768/16376)M
1:ha SyncTarget Secondary/Primary Inconsistent/UpToDate C
http://dbabc.net Email:[email protected]
当同步完成后两台台主机的 ds 状态都为 UpToDate 表示同步完成,两台主机拥有相同且最
新的数据。
[root@mysql-n1 ~]# service drbd status
drbd driver loaded OK; device status:
version: 8.3.12 (api:88/proto:86-96)
GIT-hash: e2a8ef4656be026bbae540305fcb998a5991090f build by
[email protected], 2012-01-28 13:52:25
m:res cs ro ds p mounted fstype
1:ha Connected Primary/Secondary UpToDate/UpToDate C
格式化 sdb
#mkfs.ext3 /dev/drbd1
挂载磁盘
#mount /dev/drbd1 /data
6、配置 heartbeat
1)配置 authkeys
Node1、Node2 都必须设定
# vi /etc/ha.d/authkeys
auth 1
1 sha1 940427d340eb94fd2104580599ff7aa6
# chmod 600 /etc/ha.d/authkeys
Node1 设置好之后,通过 scp 拷贝到 Node2 确保两个认证文件的一致性
2)配置 ha.cf
Node1、Node2 都必须设定,内容不同
Node1:/etc/ha.d/ha.cf 信息
# vi /etc/ha.d/ha.cf
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 6
warntime 12
initdead 120
udpport 694
#ucast eth0 10.20.50.222
http://dbabc.net Email:[email protected]
ucast eth1 192.168.18.2
auto_failback on
node mysql-n1
node mysql-n2
ping 10.20.50.254
hopfudge 1
auto_failback on
respawn root /usr/lib64/heartbeat/ipfail
apiauth ipfail gid=root uid=root
Node2 :/etc/ha.d/ha.cf 信息
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 6
warntime 12
initdead 120
udpport 694
#ucast eth0 10.20.50.222
ucast eth1 192.168.18.2
auto_failback on
node mysql-n1
node mysql-n2
ping 10.20.50.254
hopfudge 1
auto_failback on
respawn root /usr/lib64/heartbeat/ipfail
apiauth ipfail gid=root uid=root
3)配置 haresources
Node1、Node2 都必须设定
# vi /etc/ha.d/haresources
Mysql-n1 IPaddr::10.4.5.100/16/eth0:0 drbddisk::ha Filesystem::/dev/drbd1::/data::ext3 mysql
其中:
第一个字段是主机名,是 uname -a得到的 (Node1和 Node2的配置文件相同,都是主节点的 hostname)
1. 指定 Primary Node 的 主机名 此例为 mysql-n1
2. 指定 Cluster IP Address(VIP) 此例为 10.20.50.250
3. 指定 Cluster Resource Name 此例为 drbddisk::ha
4. 指 定 Cluster Device 、 Mount Point 、 File System Type 此 例 为
Filesystem::/dev/drbd1::/data::ext3
http://dbabc.net Email:[email protected]
5. 指定 Service 此例为 mysql 其中 mysql 要在/etc/ha.d/resource.d 目录下,如果不存在
则通过软连实现
ln -s /etc/init.d/mysql /etc/ha.d/resource.d/mysql
目前 Mysql还没有安装,等 Mysql安装完成之后创建软连接
4)安装 MySQL
编译安装 MySQL 只将数据库目录存放在 DRBD 目录,我们要镜像的是 MySQL 数据库目录
注意:不要设置 Mysql 开机自动启动
参照 Mysql5.5.16 安装文档。
5)设置自动启动服务
# chkconfig -- add heartbeat
# chkconfig heartbeat on
[root@mysql-n1 ~]# service heartbeat start
Starting High-Availability services:
2012/03/15_09:55:03 INFO: Resource is stopped
[ OK ]
[root@mysql-n1 ~]# service heartbeat status
heartbeat OK [pid 20865 et al] is running on mysql-n1 [mysql-n1]...
三、测试 Mysql HA
Node1 是 Master 节点
Node2 是 Secondary 节点
1、手动切换 Node
Node1 节点状态:
[root@mysql-n1 ~]# service heartbeat status
heartbeat OK [pid 21649 et al] is running on mysql-n1 [mysql-n1]...
[root@mysql-n1 ~]# service mysql status
MySQL running (22399)[ OK ]
[root@mysql-n1 ~]# cat /proc/drbd
version: 8.3.12 (api:88/proto:86-96)
http://dbabc.net Email:[email protected]
GIT-hash: e2a8ef4656be026bbae540305fcb998a5991090f build by
[email protected], 2012-01-28 13:52:25
1: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
ns:17247612 nr:8776 dw:485108 dr:16806575 al:206 bm:987 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b
oos:0
[root@mysql-n1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
33G 6.0G 26G 19% /
/dev/sda1 99M 20M 75M 21% /boot
tmpfs 2.0G 0 2.0G 0% /dev/shm
/dev/drbd1 16G 203M 15G 2% /data
Node2 节点状态:
[root@mysql-n2 ~]# service heartbeat status
heartbeat OK [pid 20161 et al] is running on mysql-n2 [mysql-n2]...
[root@mysql-n2 ~]# cat /proc/drbd
version: 8.3.12 (api:88/proto:86-96)
GIT-hash: e2a8ef4656be026bbae540305fcb998a5991090f build by
[email protected], 2012-01-28 13:52:25
1: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r-----
ns:1440 nr:1672 dw:2944 dr:18139 al:13 bm:5 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
[root@mysql-n2 ~]# service mysql status
MySQL is not running[FAILED]
[root@mysql-n2 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
33G 4.7G 27G 15% /
/dev/sda1 99M 20M 75M 21% /boot
tmpfs 2.0G 0 2.0G 0% /dev/shm
注: /data 未被挂载
Node1 节点关闭 heartbeat:
[root@mysql-n1 ~]# service heartbeat stop
Stopping High-Availability services:
[ OK ]
http://dbabc.net Email:[email protected]
[root@mysql-n1 ~]# service heartbeat status
heartbeat is stopped. No process
[root@mysql-n1 ~]# cat /proc/drbd
version: 8.3.12 (api:88/proto:86-96)
GIT-hash: e2a8ef4656be026bbae540305fcb998a5991090f build by
[email protected], 2012-01-28 13:52:25
1: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r-----
ns:17247796 nr:9048 dw:485564 dr:16806575 al:206 bm:987 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b
oos:0
[root@mysql-n1 ~]# service mysql status
MySQL is not running[FAILED]
可以看到 Node1 节点的 mysql 服务停止了,drbd 由 Primary 状态转变为 Secondary 的状态
Node2 节点的状态
[root@mysql-n2 ~]# service drbd status
drbd driver loaded OK; device status:
version: 8.3.12 (api:88/proto:86-96)
GIT-hash: e2a8ef4656be026bbae540305fcb998a5991090f build by
[email protected], 2012-01-28 13:52:25
m:res cs ro ds p mounted fstype
1:ha Connected Primary/Secondary UpToDate/UpToDate C /data ext3
[root@mysql-n2 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
33G 4.7G 27G 15% /
/dev/sda1 99M 20M 75M 21% /boot
tmpfs 2.0G 0 2.0G 0% /dev/shm
/dev/drbd1 16G 203M 15G 2% /data
可以看到 Node2 节点上的 drbd 由 Secondary 转变为 Primary 的状态了,并挂载了/data,
[root@mysql-n2 ~]# service mysql status
MySQL running (1350)[ OK ]
节点 Node2 Mysql 服务已经启动。
ping 10.20.50.250 一直是 pint 通的状态,没有中断。详细的信息可以查看各自的日志。
由于 ha.cf 文档中 auto_failback 为 on,所以当 Primary Node 的 Heartbeat 服务再次启动后
http://dbabc.net Email:[email protected]
会将主权抢回。
2、模拟 Master 节点宕机或者网络中断
模拟当 Master 宕机或者网路中断时是否会自动切换 Node
这里通过执行 ifdown eth0 命令去关闭 eth0 网卡来进行节点切换测试。
1. 在 Node1 节点上执行 ifdown eth0 命令来关闭网卡
2. Secondary 节点即 Node2 需要等待 6 秒即 ha.cf 里面设置的 deadtime 参数 deadtime 6
3. Primary Node 切换为 Secondary Node,而原 Secondary Node 变为 Primary Node.
附:HA 开、关机流程
1、正确开机步骤
当两台 Node 主机都关机而预设 Node1 为 Primary Node 的情况下,正确的开机步骤为:
1. 主节点 Node1 先开机
2. 过约 2 分钟后再 Node2 主机 开机
3. 当 Node1 主机开机流程运作到 DRBD 程序时会等待 60 秒(等待 Secondary Node 回应),
在数秒期间 Node2 主机开机流程也运作到 DRBD 程序时会去找 Node1 主机进行沟通
4. Node1 顺利开机完成,Node2 也开机完成
登入 Node1、Node2 主机查看 DRBD 状态及相关资讯(挂载/dev/drbd1 至/data、启动
mysql 服务、启动 VIP 10.20.50.250 及网卡 eth0:0)
2、正确关机步骤
当遇到不可抗力因素必须要两台主机都关机且 Node1 为 Primary Node 的情况下,正确的关
机流程为:
1. 在 Node1 主机执行 service heartbeat stop 命令将 Heartbeat 服务关闭
2. 确认 Node2 主机接手成为 Primary Node 且相关服务运作正常
3. 将 Node1 主机关机(此时在 Node2 主机看到的 DRBD 状态为 Primary/Unknown)
4. 在 Node2 主机上执行 service mysql stop 命令将 MySQL 服务关闭
5. MySQL 服务关闭完成后即可将 Node2 主机关机
6. 之后开机时再遵照上述的正确开机流程步骤即可顺利让 Cluster 再度运作
参考:
http://linux-ha.org
http://dbabc.net Email:[email protected]
http://www.drbd.org
http://www.weithenn.org/cgi-bin/wiki.pl?HA-DRBD_Heartbeat_%E5%BB%BA%E7%BD%AE_MyS
QL_%E9%AB%8%E5%8F%AF%E7%94%A8%E6%80%A7