1. 这里简单介绍一下heartbeat和drbd。若是主服务器宕机,形成的损失是不可估量的。要保证主服务器不间断服务,就须要对服务器实现冗余。在众多的实现服务器冗余的解决方案中,heartbeat为咱们提供了廉价的、可伸缩的高可用集群方案。咱们经过heartbeat+drbd在Linux下建立一个高可用(HA)的集群服务器。node
2.
DRBD是一种块设备,能够被用于高可用(HA)之中。它相似于一个网络RAID-1功能。当你将数据写入本地文件系统时,数据还将会被发送到网络中另外一台主机上。以相同的形式记录在一个文件系统中。本地(主节点)与远程主机(备节点)的数据能够保证明时同步。当本地系统出现故障时,远程主机上还会保留有一份相同的数据,能够继续使用。在高可用(HA)中使用DRBD功能,能够代替使用一个共享磁盘阵。由于数据同时存在于本地主机和远程主机上。切换时,远程主机只要使用它上面的那份备份数据,就能够继续进行服务了。
3.
在主备两台虚拟机里新增2块硬盘,模拟raw device,只分区不要格式化
查看硬盘: fdisk –l
Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
对/dev/sdb进行分区: fdisk /dev/sdb
步骤: n----p----1----1---261-----w
再次查看硬盘: fdisk –l
Device Boot Start End Blocks Id System
/dev/sdb1 1 261 2096451 83 Linux
|
4.
因为Centos6.3的iso并无drbd的rpm包采用互联网上的资源下载安装elrep,能够直接yum install drbd
wget http://elrepo.org/elrepo-release-6-4.el6.elrepo.noarch.rpm
rpm -ivUh elrepo-release-6-4.el6.elrepo.noarch.rpm
vi /etc/yum.repos.d/elrepo.repo #把第8行改为enabled=0
|
安装kmod-drdb可能会遇到kernel版本不支持的问题,若有须要先升级kernel下载163的YUM源进行kernel升级
wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
mv CentOS6-Base-163.repo / /etc/yum.repos.d
yum --enablerepo=updates install kernel
|
kernel更新好之后就可使用yum安装drbd
yum --enablerepo=elrepo install drbd83-utils kmod-drbd83
安装完成后让内核加载drbd
modprobe drbd
使用modprobe -l |grep drbd 和 lsmod |grep drbd查看是否加载成功
[root@test1 ~]# modprobe -l |grep drbd
extra/drbd83/drbd.ko
[root@test1 ~]# lsmod |grep drbd
drbd 318209 0
|
5.
2台机器都修改主机名并设定hosts文件drbd和heartbeat都要依赖于主机名来通讯
vi /etc/hosts
192.168.159.100 test1
192.168.159.110 test2
|
6.
修改drbd配置文件
vi /etc/drbd.conf
global {
usage-count yes;
}
common {
protocol C; #定义当数据被写入块设备时候的一致性级别(数据同步协议),A、B、C三个级别,C为数据被确认写到本地磁盘和远程磁盘后返回,确认成功
syncer { rate 100M;} #设置两个节点间的同步速率
}
resource r0 {
on test1 { #节点名称必定要与hostname保持一致
device /dev/drbd1;
disk /dev/sdb1;
address 192.168.159.100:7789;
meta-disk internal;
}
on test2 {
device /dev/drbd1;
disk /dev/sdb1;
address 192.168.159.110:7789;
meta-disk internal;
}
}
|
建立resource metadata (须要在2台server上执行)
drbdadm create-md r0
在iptables里开启TCP 7789端口重启服务后,启动dbrd服务(须要在2台server上执行)
/etc/init.d/drbd start
观察drbd状态
[root@test1 ~]# cat /proc/drbd
version: 8.3.13 (api:88/proto:86-96)
GIT-hash: 83ca112086600faacab2f157bc5a9324f7bd7f77 build by dag@Build32R6, 2012-09-04 12:05:34
1: 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:b oos:2096348
"/proc/drbd"中显示了drbd当前的状态.第一行的ro表示两台主机的状态,都是"备机"状态.
ds是磁盘状态,都是"不一致"状态.这是因为,DRBD没法判断哪一方为主机,以哪一方的磁盘数据做为标准数据.因此,咱们须要初始化
|
8.
将test1设置为primary并初始化
初始化primary
drbdsetup /dev/drbd1 primary -o
观察drbd状态,正在同步drbd
[root@test1 ~]# watch -n1 'cat /proc/drbd'
1: cs:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r-----
ns:1320832 nr:0 dw:0 dr:1329688 al:0 bm:80 lo:1 pe:3 ua:64 ap:0 ep:1 wo:b oos:775772
[===========>........] sync'ed: 63.1% (775772/2096348)K
finish: 0:00:10 speed: 73,364 (73,364) K/sec
完成初始化,查看primary状态
[root@test1 ~]# cat /proc/drbd
1: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
ns:2096348 nr:0 dw:0 dr:2097012 al:0 bm:128 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
完成初始化,查看secondary状态
[root@test2 ~]# cat /proc/drbd
1: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r-----
ns:0 nr:2096348 dw:2096348 dr:0 al:0 bm:128 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
|
9.
如今能够把Primary上的DRBD设备挂载到一个目录上进行使用.备机的DRBD设备没法被挂载,由于它是用来接收主机数据的,由DRBD负责操做.
格式化成EXT3
mkfs.ext3 /dev/drbd1
挂载到系统上就可使用了
mkdir /drbd
mount /dev/drbd1 /drbd
|
10.
drbd主备切换测试,查看数据同步
在/drbd目录写入一个测试文件
dd if=/dev/zero of=drbdtest bs=4k count=10240
在原来的primary上卸载drbd,并设置为secondary
umount /drbd
drbdadm secondary r0
把原来的secondary设置为primary,并挂载drbd
drbdadm primary r0
mount /dev/drbd1 /drbd
查看刚才的文件是否存在,说明同步成功
[root@test2 drbd]# ll -h /drbd/drbdtest
-rw-r--r--. 1 root root 40M Nov 7 14:55 /drbd/drbdtest
|
11.
因为Centos6.3的iso并无heartbeat的rpm包,因此采用互联网上的资源下载安装epel,能够直接yum install heartbeat
wget ftp://mirror.switch.ch/pool/1/mirror/scientificlinux/6rolling/i386/os/Packages/epel-release-6-5.noarch.rpm
rpm -ivUh epel-release-6-5.noarch.rpm
vi /etc/yum.repos.d/epel.repo #把第6行改为enabled=0
|
使用yum安装heartbeat
yum --enablerepo=epel install heartbeat
|
12.
修改heartbeat配置文件(如下步骤须要在2台node上执行)
复制配置文件,资源文件,认证密钥文件
cp /usr/share/doc/heartbeat-3.0.4/ha.cf /etc/ha.d/
cp /usr/share/doc/heartbeat-3.0.4/haresources /etc/ha.d/
cp /usr/share/doc/heartbeat-3.0.4/authkeys /etc/ha.d/
vi /etc/ha.d/ha.cf
logfile /var/log/ha-log
logfacility local0
keepalive 1 #定义心跳频率1s
deadtime 10 #若是其余节点10S内没有回应,则确认其死亡
warntime 5 #确认一个节点链接不上5S以后将警告信息写入日志
initdead 60 #在其余节点死掉以后,系统启动前须要等待的时间,通常为deadtime的两倍
udpport 694
ucast eth0 192.168.169.110 #对端的IP,在备机上改成192.168.169.100
auto_failback off
node cc-system-manager1
node cc-system-manage
chmod 600 /etc/ha.d/authkeys
vi /etc/ha.d/authkeys
auth 1
1
Crc
2 vi /etc/ha.d/haresources
test1 IPaddr::192.168.159.250/24/eth0 drbddisk::r0 Filesystem::/dev/drbd1::/drbd::ext3 nginx
资源文件说明:
test1– the hosname that will be the primary node nginx–the service we’re going to watch over and take care of, in this case nginx(which wasn’t really what I was configuring, but it’s the easiest to show as an example)
更多heartbeat配置说明请参考
|
13.
启动heartbeat并作切换测试
先中止nginx并复制启动文件到resource目录(须要在2台node上执行)
/etc/init.d/nginx stop
先调整SElinux配置为Permissive,防止heartbeat没法启动
[root@test1 log]# getenforce
Enforcing
[root@test1 log]# setenforce 0
[root@test1 log]# getenforce
Permissive
启动heartbeat服务
/etc/init.d/heartbeat start
tail -f /var/log/ha-log
主机:
Nov 07 17:25:26 test1 heartbeat: [4610]: info: Status update for node test2: status active
harc(default)[5261]: 2012/11/07_17:25:26 info: Running /etc/ha.d//rc.d/status status
备机:
Nov 07 17:24:45 test2 heartbeat: [3453]: info: Link test1:eth1 up.
Nov 07 17:24:45 test2 heartbeat: [3453]: info: Status update for node test1: status active
harc(default)[4123]: 2012/11/07_17:24:45 info: Running /etc/ha.d//rc.d/status status
抓包查看UDP通讯是否正常
[root@test2 ~]# tcpdump -nni eth1 host 192.168.159.100
17:25:41.412651 IP 192.168.159.110.22152 > 192.168.159.100.694: UDP, length 175
17:25:41.412781 IP 192.168.159.110.22152 > 192.168.159.100.694: UDP, length 172
虚拟IP在主机上已经启用,nginx服务也被heartbeat正常启动
[root@test1 www.prefect.com]# ip add list
inet 192.168.159.250/24 brd 192.168.159.255 scope global secondary eth0:0
|
14.
进行主备机的HA切换测试
在主机上中止heartbeat服务
/etc/init.d/heartbeat stop
查看日志
tail -f /var/log/ha-log
主机:
ResourceManager(default)[5304]: 2012/11/07_17:31:01 info: Releasing resource group: test1 IPaddr::192.168.159.250/24/eth0 drbddisk::r0 Filesystem::/dev/drbd1::/drbd::ext3 nginx
ResourceManager(default)[5304]: 2012/11/07_17:31:01 info: Running /etc/ha.d/resource.d/nginx stop
ResourceManager(default)[5304]: 2012/11/07_17:31:01 info: Running /etc/ha.d/resource.d/Filesystem /dev/drbd1 /drbd ext3 stop
Filesystem(Filesystem_/dev/drbd1)[5369]: 2012/11/07_17:31:01 INFO: Running stop for /dev/drbd1 on /drbd
Filesystem(Filesystem_/dev/drbd1)[5369]: 2012/11/07_17:31:01 INFO: Trying to unmount /drbd
Filesystem(Filesystem_/dev/drbd1)[5369]: 2012/11/07_17:31:01 INFO: unmounted /drbd successfully
备机:
Nov 07 17:30:50 test2 heartbeat: [3453]: info: mach_down takeover complete.
Nov 07 17:30:59 test2 heartbeat: [3453]: WARN: node test1: is dead
Nov 07 17:30:59 test2 heartbeat: [3453]: info: Dead node test1 gave up resources.
Nov 07 17:30:59 test2 heartbeat: [3453]: info: Link test1:eth1 dead.
从日志上观察备机切换正常
在备机上查看IP, drbd挂载,nginx服务
[root@test2 ~]# ip add list
inet 192.168.159.250/24 brd 192.168.159.255 scope global secondary eth1:0
[root@test2 ~]# df -h
/dev/drbd1 2.0G 76M 1.8G 4% /drbd
[root@test2 ~]# netstat -tunlp
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4635/nginx
以上说明切换正常,heartbeat+drbd能够在备机正常使用
|
15.
主机heartbeat恢复,手动进行主备切换
在主机上再次开启heartbeat服务,主备不会切换
/etc/init.d/heartbeat start
在备机上中止heartbeat服务,主备会再次切换,服务和数据依然能够正常使用,数据也经过drbd保持一致
/etc/init.d/heartbeat stop |