DRBD && keepalived && nginx && heartbeat
- Category: 電腦相關
- Last Updated: Thursday, 30 July 2015 15:53
- Published: Wednesday, 08 July 2015 14:17
- Written by sam
環境是debian 7 *2
先建立相關資訊,讓對方可以互相找的到
root@drbd1:~# cat /etc/hosts 127.0.0.1 localhost 192.168.20.62 drbd1
編輯名稱
root@drbd1:~# cat /etc/hostname drbd1
安裝套件
apt-get install drbd8-utils
可以看到CONF用法
cp /usr/share/doc/drbd8-utils/examples/drbd.conf.example.gz ./
解開
gzip -d drbd.conf.example.gz
我是用虛擬機,另加新磁碟,並用指令切partition table
fdisk /dev/xvdb
啟動時,如果有報錯
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 1 v08 /dev/xvdb1 internal create-md' terminated with exit code 40 drbdadm create-md r0: exited with code 40
記得先使用DD寫入0
dd if=/dev/zero of=/dev/xvdb1 bs=1M
再次啟動
drbdadm create-md r0
New drbd meta data block successfully created.
root@drbd1:~# service drbd start
先看一下狀態(未指定Primary,兩臺都是Secondary)
root@drbd1:~# cat /proc/drbd version: 8.3.11 (api:88/proto:86-96) srcversion: F937DCB2E5D83C6CCE4A6C9 1: cs:SyncSource ro:Secondary/Secondary ds:UpToDate/Inconsistent C r----- ns:157372 nr:0 dw:0 dr:164736 al:0 bm:9 lo:7 pe:56 ua:64 ap:0 ep:1 wo:f oos:10334108 [>....................] sync'ed: 1.5% (10088/10236)Mfinish: 0:04:34 speed: 37,568 (37,568) K/sec
在drbd1指為主要
root@drbd1:~# sudo drbdadm -- --overwrite-data-of-peer primary all
或將特定資源轉入 sudo drbdadm -- --overwrite-data-of-peer primary r0
再看一次已變更為主要
root@drbd1:~# cat /proc/drbd version: 8.3.11 (api:88/proto:86-96) srcversion: F937DCB2E5D83C6CCE4A6C9 1: cs:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r----- ns:10074912 nr:0 dw:0 dr:10083224 al:0 bm:614 lo:5 pe:72 ua:64 ap:0 ep:1 wo:f oos:418588 [==================>.] sync'ed: 96.1% (408/10236)Mfinish: 0:00:11 speed: 36,656 (35,568) K/sec
使用指令察看
drbdadm role r0
mkfs && mount
mkfs.ext4 /dev/drbd1 mkdir /sync mount /dev/drbd1 /sync
test
root@drbd1:/sync# cp /etc/drbd.conf ./
root@drbd1:/# umount /sync root@drbd1:/# drbdadm secondary r0 root@drbd1:/# cat /proc/drbd version: 8.3.11 (api:88/proto:86-96) srcversion: F937DCB2E5D83C6CCE4A6C9 1: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r----- ns:10782220 nr:24 dw:297864 dr:10486105 al:86 bm:640 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
到drbd2查看一下是否正常
root@drbd2:~# drbdadm primary r0 root@drbd2:~# mount /dev/drbd1 /sync root@drbd2:~# ls /sync drbd.conf lost+found
最後附上drbd.conf
root@drbd2:~# cat /etc/drbd.conf # You can find an example in /usr/share/doc/drbd.../drbd.conf.example #include "drbd.d/global_common.conf"; #include "drbd.d/*.res"; #global { usage-count no; } common { syncer { rate 1000M; } } resource r0 { protocol C; startup { wfc-timeout 15; degr-wfc-timeout 60; } net { cram-hmac-alg sha1; shared-secret "sam"; } on drbd1 { device /dev/drbd1; disk /dev/xvdb1; address 192.168.20.62:7788; meta-disk internal; } on drbd2 { device /dev/drbd1; disk /dev/xvdb1; address 192.168.20.51:7788; meta-disk internal; } }
###########################################
keepalived
apt-get install keepalived
編輯設定檔
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass $ sam } virtual_ipaddress { 192.168.20.200 } }
其中的priority 以高的(大的)為Master,小的為Slave
然後更新sysctl
echo 1 > /proc/sys/net/ipv4/ip_nonlocal_bind echo net.ipv4.ip_nonlocal_bind = 1 > /etc/sysctl.conf
套用
root@drbd2:/sync# sysctl -p net.ipv4.conf.all.arp_notify = 1 net.ipv4.ip_nonlocal_bind = 1
啟動一下
root@drbd2:/sync# /etc/init.d/keepalived start
也可以再用nginx來驗証
apt-get install nginx -y
root@drbd1:/#vim /usr/share/nginx/www/index.html
修改一下內容以顯示連到哪臺
root@drbd1:/# cat /usr/share/nginx/www/index.html <html> <head> <title>Welcome to nginx!</title> </head> <body bgcolor="white" text="black"> <center><h1>Welcome to nginx!DRBD1</h1></center> </body> </html>
啟動一下
root@drbd1:/# /etc/init.d/nginx start Starting nginx: nginx.
因為我的conf是DRBD2較大,預設就是連到它
先停掉DRBD2
root@drbd2:/sync# /etc/init.d/keepalived stop
正常來說就是換到DRBD1
這樣就是功能正常
###################################################################
Heartbeat
NAS掛點了…只行重來…這次使用DRBD加上Heartbeat
DRBD就和之前一樣裝法
Heartbeat
apt-get install heartbeat -y
vi /etc/heartbeat/ha.cf
root@xen-guest1:/etc/heartbeat# cat ha.cf logfile /var/log/ha.log keepalive 2 deadtime 10 warntime 5 initdead 20 udpport 694 auto_failback off ucast eth0 192.168.20.40 這行是貼對方IP,所以兩臺要不同 node xen-guest0 node xen-guest1
keys
vi /etc/heartbeat/authkeys
auth 1 1 md5 139dhckaox
以上表示使用 139XXX加上MD5來當驗證密碼
chmod 600 /etc/heartbeat/authkeys
改成權限600
cat /etc/heartbeat/haresources #兩臺相同,以下設定為以xen-guest0為主 xen-guest0 drbddisk::r0 Filesystem::/dev/drbd1::/sync::xfs::noatime,nodiratime IPaddr::192.168.20.191/255.255.255.0/eth0/192.168.20.1
啟動試試
root@xen-guest0:/etc/heartbeat# /etc/init.d/heartbeat start
Starting High-Availability services: Done.
看來正常了
記得最重要的就是/etc/hosts
192.168.20.40 xen-guest1 192.168.20.46 xen-guest0
要讓它們找的到對方
測試一下
原先的
root@xen-guest1:/etc/heartbeat# cat /proc/drbd version: 8.4.3 (api:1/proto:86-101) srcversion: 107E17F432EA25ED3AF8929 1: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r----- ns:2066 nr:0 dw:2066 dr:911 al:2 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0 root@xen-guest1:/etc/heartbeat# shutdown -h now 或使用指令切換 root@xen-guest0:/etc/heartbeat# /usr/share/heartbeat/hb_takeover
root@xen-guest0:/etc/heartbeat# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/xvda1 40636768 1321876 37227624 4% / none 4 0 4 0% /sys/fs/cgroup udev 2990920 4 2990916 1% /dev tmpfs 600420 324 600096 1% /run none 5120 0 5120 0% /run/lock none 3002100 0 3002100 0% /run/shm none 102400 0 102400 0% /run/user /dev/xvdb1 206292644 60684 195729872 1% /server /dev/drbd1 104802144 32928 104769216 1% /sync #自動切過來了 root@xen-guest0:/etc/heartbeat# cat /proc/drbd version: 8.4.3 (api:1/proto:86-101) srcversion: 107E17F432EA25ED3AF8929 1: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r----- #看到變成Primary ns:2048 nr:4119 dw:6167 dr:527 al:1 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
對方重開機時
root@xen-guest1:/etc/heartbeat# cat /proc/drbd version: 8.4.3 (api:1/proto:86-101) srcversion: 107E17F432EA25ED3AF8929 1: cs:WFConnection ro:Primary/Unknown ds:UpToDate/DUnknown C r----- ns:4098 nr:2050 dw:6149 dr:1890 al:2 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
開機完成後,自動回復Secondary
root@xen-guest1:/etc/heartbeat# cat /proc/drbd version: 8.4.3 (api:1/proto:86-101) srcversion: 107E17F432EA25ED3AF8929 1: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r----- ns:4098 nr:2050 dw:6149 dr:1890 al:2 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0