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 |