drdb+heartbeat+nfs實現Linux高可用羣集(HA)

      如果主服務器宕機,造成的損失是不可估量的。要保證主服務器不間斷服務,就需要對服務器實現冗餘。在衆多的實現服務器冗餘的解決方案 中,heartbeat爲我們提供了廉價的、可伸縮的高可用集羣方案。我們通過heartbeat+drbd在Linux下創建一個高可用(HA)的集羣 服務器。

DRBD是一種塊設備,可以被用於高可用(HA)之中。它類似於一個網絡RAID-1功能。當你將數據寫入本地文件系統時,數據還將會被髮送到網絡中另一臺主機上。以相同的形式記錄在一個文件系統中。本地(主節點)與遠程主機(備節點)的數據可以保證實時同步。當本地系統出現故障時,遠程主機上還會保留有一份相同的數據,可以繼續使用。在高可用(HA)中使用DRBD功能,可以代替使用一個共享盤陣。因爲數據同時存在於本地主機和遠程主機上。切換時,遠程主機只要使用它上面的那份備份數據,就可以繼續進行服務了。
服務器地址說明:drdb主服務器地址192.168.1.2 主機名master。drdb從屬服務器地址192.168.1.3, 主機名slave。 虛擬ip 192.168.1.10
 虛擬操作系統:redhat 企業版5.4。
1、主服務器配置:
1、固定ip地址:
[root@master ~]# setup
[root@master ~]# service network restart
 
2、修改hosts文件:
[root@manage ~]# echo "192.168.1.2 master ">>/etc/hosts
[root@manage ~]# echo "192.168.1.2 slave ">>/etc/hosts
 
3、編輯yum客戶端:
[root@master ~]# mkdir /mnt/cdrom
[root@master ~]# mount /dev/cdrom /mnt/cdrom/
[root@master ~]# vim /etc/yum.repos.d/rhel-debuginfo.repo
編輯的內容:
[rhel-server]
name=Red Hat Enterprise Linux server
baseurl=file:///mnt/cdrom/Server
enabled=1
gpgcheck=1
gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-redhat-release
[rhel-cluster]
name=Red Hat Enterprise Linux cluster
baseurl=file:///mnt/cdrom/Cluster
enabled=1
gpgcheck=1
gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-redhat-release
[rhel-clusterstorage]
name=Red Hat Enterprise Linux clusterstorage
baseurl=file:///mnt/cdrom/ClusterStorage
enabled=1
gpgcheck=1
gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-redhat-release
 
4、新建分區:
[root@master ~]# fdisk /dev/sda
 
The number of cylinders for this disk is set to 1958.
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   #增加新分區
Command action
   e   extended
   p   primary partition (1-4)
p   #增加主分區
Selected partition 4
First cylinder (328-1958, default 328):   #默認柱面,直接回車
Using default value 328
Last cylinder or +size or +sizeM or +sizeK (328-1958, default 1958): +1G  #大小爲1G
 
Command (m for help): w   #保存退出
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
 
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
[root@master ~]#partprobe   /dev/sda
查看分區:
[root@master ~]#cat /proc/partitions

 

 

5、安裝drdb:
[root@master ~]# yum localinstall drbd83-8.3.8-1.el5.centos.i386.rpm kmod-drbd83-8.3.8-1.el5.centos.i686.rpm –nogpgcheck -y
 
加載DRBD 模塊
[root@master ~]# modprobe drbd
查看模塊加載
 [root@master ~]# lsmod |grep drbd
drbd                  228528 0
編輯配置文件
[root@master ~]# vim /etc/drbd.conf
在底行模式下輸入“r /usr/share/doc/drbd83-8.3.8/drbd.conf”。
在從屬服務器執行相同的操作;
 
[root@master ~]# cd /etc/drbd.d/                  
[root@master drbd.d]# cp global_common.conf global_common.conf.bak
[root@master drbd.d]# vim global_common.conf
內容爲:
global {
        usage-count no;
        # minor-count dialog-refresh disable-ip-verification
}
 
common {
        protocol C;
 
 
        startup {
                 wfc-timeout 120;
                 degr-wfc-timeout 120;
        }
 
        disk {
                on-io-error detach;
                fencing resource-only;
        }
 
        net {
                cram-hmac-alg "sha1";
                shared-secret "mydrbdlab";
        }
 
        syncer {
                rate 100M;
        }
}
 
定義資源:
名字爲web.res
編輯
[root@master drbd.d]# vim web.res
resource web { 
      
        on master {                           
                device /dev/drbd0;           
                disk /dev/sda4;               
                address 192.168.1.2:7898;     
                meta-disk internal;           
        }
        on slave {
                device /dev/drbd0;
                disk /dev/sda4;
                address 192.168.1.3:7898;
                meta-disk internal;
        }
}
 
將此兩個文件拷貝到從屬服務器中(地址爲192.168.1.3)
[root@master drbd.d]# scp global_common.conf 192.168.1.3:/etc/drbd.d/
[root@master drbd.d]# scp web.res 192.168.1.3:/etc/drbd.d/
 
6、檢測配置文件
[root@master drbd.d]# drbdadm adjust web
drbdsetup 0 show:5: delay-probe-volume 0k => 0k out of range [4..1048576]k.
7、創建web 的資源
[root@master drbd.d]# drbdadm create-md web
Writing meta data...
initializing activity log
NOT initialized bitmap
New drbd meta data block successfully created.
啓動DRBD 服務
[root@master drbd.d]#service drbd start
7、將一些文件拷貝到slave(192.168.1.3)中:
 
先拷貝hosts文件:
[root@master ~]# scp /etc/hosts 192.168.1.3:/etc/
The authenticity of host '192.168.1.3 (192.168.1.3)' can't be established.
RSA key fingerprint is d4:f1:06:3b:a0:81:fd:85:65:20:9e:a1:ee:46:a6:8b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.3' (RSA) to the list of known hosts.
[email protected]'s password:         #輸入slave的管理員密碼
 
拷貝yum客戶端:
[root@master ~]# scp /etc/yum.repos.d/rhel-debuginfo.repo 192.168.1.3:/etc/yum.repos.d/
[email protected]'s password:   #輸入slave管理員的密碼
 
拷貝drdb安裝包:
[root@master ~]# scp *.rpm 192.168.1.3:/root
[email protected]'s password:   #輸入slave管理員的密碼
 
2、從屬服務器配置:
 
1、新建分區:(注意新建分區大小與主服務器一樣)
 
[root@slave ~]# fdisk /dev/sda
 
The number of cylinders for this disk is set to 2610.
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   #增加新分區
Command action
   e   extended
   p   primary partition (1-4)
p    #增加主分區
Selected partition 4
First cylinder (1580-2610, default 1580):    #默認柱面,直接回車
Using default value 1580
Last cylinder or +size or +sizeM or +sizeK (1580-2610, default 2610): +1G   #大小爲1G
 
Command (m for help): W    #保存退出
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
 
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
[root@slave ~]# partprobe   /dev/sda
 
 
2、安裝drdb:
[root@slave ~]# mkdir /mnt/cdrom/
[root@slave ~]# mount /dev/cdrom /mnt/cdrom/
[root@slave ~]#yum localinstall drbd83-8.3.8-1.el5.centos.i386.rpm kmod-drbd83-8.3.8-1.el5.centos.i686.rpm –nogpgcheck -y
[root@slave ~]# cp /usr/share/doc/drbd83-8.3.8/drbd.conf /etc/
3、檢測配置文件
[root@slave drbd.d]# drbdadm adjust web
 
4、創建web 的資源
[root@slave drbd.d]# drbdadm create-md web
Writing meta data...
initializing activity log
NOT initialized bitmap
New drbd meta data block successfully created.
 
3、接下來在兩臺服務器上同時操作
 
1、在主服務器和從屬服務器上同時啓動drbd服務:
 主服務器:[root@master drbd.d]# service drbd start
 從屬服務器:[root@slave drbd.d]# service drbd start
 
2、查看兩臺服務器上的drbd狀態:
 
master
[root@master ~]# drbd-overview
 0:web Connected Secondary/Secondary Inconsistent/Inconsistent C r----
[root@master drbd.d]# cat /proc/drbd
version: 8.3.8 (api:88/proto:86-94)
GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by [email protected], 2010-06-04 08:04:16
 0: 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:987928
slave
[root@slave ~]# drbd-overview
 0:web Connected Secondary/Secondary Inconsistent/Inconsistent C r----
[root@slave drbd.d]# cat /proc/drbd
version: 8.3.8 (api:88/proto:86-94)
GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by [email protected], 2010-06-04 08:04:16
 0: 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:987928
 
可知兩臺服務器都爲Secondary狀態,證明還沒有同步。
 
創建文件夾
[root@master ~]# mkdir /data
[root@slave ~]# mkdir /data
 
3、在master上操作:
[root@master ~]# cd /etc/drbd.d/
[root@master drbd.d]# drbdadm -- --overwrite-data-of-peer primary web
[root@master drbd.d]# drbd-overview
 0:web SyncSource Primary/Secondary UpToDate/Inconsistent C r----
   [====>...............] sync'ed: 26.5% (732280/987928)K delay_probe: 25
 
格式化:
[root@master drbd.d]# mkfs -t ext3 -L drbdweb /dev/drbd0
掛載:
[root@master drbd.d]# mkdir /mnt/1
[root@master drbd.d]# mount /dev/drbd0 /mnt/1
[root@master drbd.d]# df -h
Filesystem            Size Used Avail Use% Mounted on
/dev/sda2             9.7G 2.6G 6.7G 28% /
/dev/sda1              99M   12M   83M 12% /boot
tmpfs                  97M     0   97M   0% /dev/shm
/dev/hdc              2.8G 2.8G     0 100% /mnt/cdrom
/dev/drbd0            950M   18M 885M   2% /mnt/1
 
 
再次查看兩臺服務器的狀態:
master
[root@master drbd.d]# service drbd status
drbd driver loaded OK; device status:
version: 8.3.8 (api:88/proto:86-94)
GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by [email protected], 2010-06-04 08:04:16
m:res cs         ro                 ds                 p mounted fstype
0:web Connected Primary/Secondary UpToDate/UpToDate C /mnt/1   ext3
 
slave
[root@slave drbd.d]# service drbd status
drbd driver loaded OK; device status:
version: 8.3.8 (api:88/proto:86-94)
GIT-hash: d78846e52224fd00562f7c225bcc25b2d422321d build by [email protected], 2010-06-04 08:04:16
m:res cs         ro                 ds                 p mounted fstype
0:web Connected Secondary/Primary UpToDate/UpToDate C
 
可知master爲主服務,salve爲從屬,此時已經同步。
 
4、NFS配置:
 
兩臺服務器都修改nfs 配置文件如下:
[root@master drbd.d]# vim /etc/exports
 
/data *(rw,sync,insecure,no_root_squash,no_wdelay)
 
兩臺服務器都啓動服務並設爲開機自啓動:
service portmap start && chkconfig portmap on
service nfs start && chkconfig nfs on
 
兩臺服務器都修改nfs 啓動腳本。將/etc/init.d/nfs 腳本中的stop 部分中的killproc
nfsd -2 修改爲 -9
 
5、Heartbeat配置
在兩臺服務器上都操作:
yum localinstall heartbeat-2.1.4-9.el5.i386.rpm heartbeat-pils-2.1.4-10.el5.i386.rpm heartbeat-stonith-2.1.4-10.el5.i386.rpm libnet-1.1.4-3.el5.i386.rpm perl-MailTools-1.77-1.el5.noarch.rpm  --nogpgcheck
 
拷貝配置文檔:
主服務器
[root@master ~]# cd /etc/ha.d/
[root@master ha.d]# cp /usr/share/doc/heartbeat-2.1.4/ha.cf ./
[root@master ha.d]# cp /usr/share/doc/heartbeat-2.1.4/haresources ./
[root@master ha.d]# cp /usr/share/doc/heartbeat-2.1.4/authkeys ./
 
[root@master ha.d]# vim ha.cf
需要打開或修改以下幾行:
24行  debugfile /var/log/ha-debug
29行  logfile /var/log/ha-log
34行  logfacility     local0
48行  keepalive 2
56行  deadtime 10
76行  udpport 694
121行  ucast eth0 192.168.1.3   #修改爲對方的地址。
220行  ping 192.168.1.1
157行  auto_failback off #設爲關閉狀態。
在212行下面添加以下兩行:
node    master
node    slave
 
在另一臺服務器上修改的ha.cf中只有121行不一樣。改爲對方的地址192.168.1.2
 
配置haresources,2臺機子相同:
echo "master IPaddr::192.168.1.10/24/eth0 drbddisk::web Filesystem::/dev/drbd0::/data::ext3 killnfsd" >> /etc/ha.d/haresources
 
authkeys 配置相同:
auth 1
1 crc
#2 sha1 HI!
#3 md5 Hello!
 
在/etc/ha.d/resource.d目錄下創建文件killnfsd並編輯(在兩臺服務器編輯一樣)
 
echo "killall -9 nfsd;/etc/init.d/nfs restart;exit 0" >> /etc/ha.d/resource.d/killnfsd
 
設置文檔權限:
chmod 600 /etc/ha.d/authkeys
chmod 755 /etc/ha.d/resource.d/killnfsd
 
開啓Heartbeat服務
master
[root@master ha.d]# service heartbeat start
 
slave
[root@slave ha.d]# service heartbeat start
 
 
6、測試
先查看在heartbeat服務運行的情況下drbd的狀態:
master
[root@master ~]# drbd-overview
 0:web Connected Primary/Secondary UpToDate/UpToDate C r---- /data ext3 950M 18M 885M 2%
slave
[root@slave ha.d]# drbd-overview
 0:web Connected Secondary/Primary UpToDate/UpToDate C r----
可知master爲主服務器,slave爲備份服務
 
把master上的heartbeat服務停止:
[root@master ~]# service heartbeat stop
 
再查看drbd的狀態:
master
[root@master ~]# drbd-overview
 0:web Connected Secondary/Primary UpToDate/UpToDate C r----
slave
[root@slave ha.d]# drbd-overview
 0:web Connected Primary/Secondary UpToDate/UpToDate C r---- /data ext3 950M 18M 885M 2%
可知master爲備份服務器,slave爲主服務
 
至此,slave 接管服務成功,實驗已實現所需的功能。
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章