You are on page 1of 7

Bi thc hnh

Ci t v cu hnh HA Cluster vi Heartbeat


cho dch v Web
Trong bi thc hnh ny, bn cn s dng 02 my o Linux v my tht Windows kt ni theo
m hnh bn di

node1

client

node2

172.16.0.1
172.16.0.2
HOST ONLY
eth1
eth1
192.168.1.10
eth0

eth0
192.168.1.1

192.168.1.2

192.168.1.100

NAT NETWORK (192.168.1.0/24)


Trong bi thc hnh ny, bn s cu hnh mi trng cluster sn sng cao cho dch v web
trn cp my ch Linux node1 v node2. Thng thng, dch v web ny s chy trn node1.
Trong trng hp node1 xy ra s c, dch v web s c t ng chuyn sang chy trn
node2 khng lm nh hng n vic truy cp ca ngi dng.
Ci t v cu hnh mi trng cluster sn sng cao vi cp my ch Linux node1 v
node2 s dng b phn mm heartbeat.
Cu hnh dch v web trn cc my ch node1 v node2. Dch v web ny s c
cu hnh hot ng trn Virtual IP 192.168.1.10
Tch hp dch v web vo trong mi trng sn sng cao qun l bi heartbeat
Kim tra m bo khi bt k node no trong cc node b s c, dch v web vn
hot ng bnh thng.

Linux Basic Course

I.Ci t v khi to dch v heartbeat trn cc node


Bc 1. [Trn my ch node1 v node2] Cu hnh hostname v a ch tnh cho cc interface theo m
hnh trn, stop dch v iptables
[root@node1]# hostname node1
[root@node1]# vi /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=node1
[root@node1]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
HWADDR=00:0C:29:E6:08:F0
ONBOOT=yes
IPADDR=192.168.1.1
NETMASK=255.255.255.0
[root@node1]# vi /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
BOOTPROTO=static
HWADDR=00:0C:29:E6:08:F1
ONBOOT=yes
IPADDR=172.16.0.1
NETMASK=255.255.255.0
[root@node1]#
[root@node1]#
node1
[root@node1]#
[root@node1]#

service network restart


uname -n
exit
service iptables stop

[root@node2]# hostname node2


[root@node2]# vi /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=node2
[root@node2]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
HWADDR=00:0C:29:E6:08:F2
ONBOOT=yes
IPADDR=192.168.1.2
NETMASK=255.255.255.0
[root@node2]# vi /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
BOOTPROTO=static
HWADDR=00:0C:29:E6:08:F3
ONBOOT=yes
IPADDR=172.16.0.2
NETMASK=255.255.255.0
[root@node2]# service network restart
[root@node2]# uname -n

Linux Basic Course

node2
[root@node2]# exit
[root@node2]# service iptables stop

Bc 2. [Trn my ch node1] Cu hnh li file /etc/hosts trn cc my ch c th kt ni vi nhau


thng qua hostname. Copy file ny sang node2)
[root@node1]# vi /etc/hosts
127.0.0.1
localhost
# Public node name
192.168.1.1 node1
192.168.1.2 node2
# Private hear-beat name
172.16.0.1
node1-hb
172.16.0.2
node2-hb
# Web server name
192.168.1.100 webserver
[root@node1]# scp /etc/hosts root@node2:/etc/
The authenticity of host node2 (192.168.1.2)' can't be established.
RSA key fingerprint is 7f:7e:93:4c:f9:10:bc:76:02:d4:b9:4e:47:55:d8:48.
Are you sure you want to continue connecting (yes/no)? yes
root@node2's password:
hosts
100% 783
7.7KB/s
00:00

Bc 3. [Trn my ch node1 v node2] Kim tra m bo 2 node c th ping qua li ln nhau


thng qua hostname
[root@node1]# ping node2
PING node2 (192.168.1.2) 56(84) bytes of data.
64 bytes from node2 (192.168.1.2): icmp_seq=1 ttl=64 time=0.128 ms
64 bytes from node2 (192.168.1.2): icmp_seq=2 ttl=64 time=0.127 ms
[root@node1]# ping node2-hb
PING node2-hb (172.16.0.2) 56(84) bytes of data.
64 bytes from node2-hb (172.16.0.2): icmp_seq=1 ttl=64 time=0.230 ms
64 bytes from node2-hb (172.16.0.2): icmp_seq=2 ttl=64 time=0.225 ms
[root@node2]# ping node1
PING node1 (192.168.1.1) 56(84) bytes of data.
64 bytes from node1 (192.168.1.1): icmp_seq=1 ttl=64 time=0.208 ms
64 bytes from node1 (192.168.1.1): icmp_seq=2 ttl=64 time=0.270 ms
[root@node1]# ping node1-hb
PING node1-hb (172.16.0.1) 56(84) bytes of data.
64 bytes from node1-hb (172.16.0.1): icmp_seq=1 ttl=64 time=0.301 ms
64 bytes from node1-hb (172.16.0.1): icmp_seq=2 ttl=64 time=0.150 ms

Bc 4.[Trn my ch node1 v node2] Tm kim v download package heartbeat v cc package h


tr t rpmfind.net v tin hnh ci t cc package ny. Cc package cn download gm:
heartbeat-2.1.4-11
o Bn 32/64 bit: ftp://rpmfind.net/linux/epel/5/i386/heartbeat-2.1.4-11.el5.i386.rpm
heartbeat-pils-2.1.4-11
o Bn 32bit: ftp://rpmfind.net/linux/epel/5/i386/heartbeat-pils-2.1.4-11.el5.i386.rpm
o Bn 64bit: ftp://rpmfind.net/linux/epel/5/x86_64/heartbeat-pils-2.1.4-11.el5.x86_64.rpm

Linux Basic Course

heartbeat-stonith-2.1.4-11
o Bn 32bit: ftp://rpmfind.net/linux/epel/5/i386/heartbeat-stonith-2.1.4-11.el5.i386.rpm
o Bn 64bit: ftp://rpmfind.net/linux/epel/5/x86_64/heartbeat-stonith-2.1.4-11.el5.x86_64.rpm
libnet-1.1.5-1
o Bn 32/64 bit: ftp://rpmfind.net/linux/epel/5/i386/libnet-1.1.5-1.el5.i386.rpm
[root@node1]# cd /tmp
[root@node1]# wget ftp://rpmfind.net/linux/epel/5/i386/heartbeat-2.1.4-11.el5.i386.rpm
[root@node1]# wget ftp://rpmfind.net/linux/epel/5/i386/heartbeat-pils-2.1.4-11.el5.i386.rpm
[root@node1]# wget ftp://rpmfind.net/linux/epel/5/i386/heartbeat-stonith-2.1.4-11.el5.i386.rpm
[root@node1]# wget ftp://rpmfind.net/linux/epel/5/i386/libnet-1.1.5-1.el5.i386.rpm
[root@node1]# yum install openhpi-libs
[root@node1]# rpm -ivh heartbeat-pils-2.1.4-11.el5.i386.rpm
[root@node1]# rpm -ivh heartbeat-stonith-2.1.4-11.el5.i386.rpm
[root@node1]# rpm -ivh libnet-1.1.5-1.el5.i386.rpm
[root@node1]# rpm -ivh heartbeat-2.1.4-11.el5.i386.rpm
[root@node2]#
[root@node2]#
[root@node2]#
[root@node2]#
[root@node2]#
[root@node2]#
[root@node2]#
[root@node2]#
[root@node2]#
[root@node2]#

cd /tmp
wget ftp://rpmfind.net/linux/epel/5/i386/heartbeat-2.1.4-11.el5.i386.rpm
wget ftp://rpmfind.net/linux/epel/5/i386/heartbeat-pils-2.1.4-11.el5.i386.rpm
wget ftp://rpmfind.net/linux/epel/5/i386/heartbeat-stonith-2.1.4-11.el5.i386.rpm
wget ftp://rpmfind.net/linux/epel/5/i386/libnet-1.1.5-1.el5.i386.rpm
yum install openhpi-libs
rpm -ivh heartbeat-pils-2.1.4-11.el5.i386.rpm
rpm -ivh heartbeat-stonith-2.1.4-11.el5.i386.rpm
rpm -ivh libnet-1.1.5-1.el5.i386.rpm
rpm -ivh heartbeat-2.1.4-11.el5.i386.rpm

Bc 5. [Trn my ch node1] Cu hnh cho dch v heartbeat trn node1. heartbeat hot ng
cn to 03 file cu hnh trong th mc /etc/ha.d/ l authkeys (cha thng tin v vic xc thc gia cc
node), ha.cf (cu hnh cho dch v heartbeat) v haresources m t cc dch v s s dng vi

heartbeat. File authkeys cn phi c t mode l 600. File hearesource tm thi cha cn
c thng tin g (to bng lnh touch)
[root@node1]# vi /etc/ha.d/authkeys
auth 1
#Chn c ch xc thc s 1 c m t bn di
1 sha1 ipmac-cluster #C ch 1: Xc thc s dng SHA1 vi passkey l ipmac-cluster
[root@node1]# chmod 600 /etc/ha.d/authkeys
[root@node1]# vi /etc/ha.d/ha.cf
debugfile /var/log/ha-debug
#Ghi cc thng tin debug vo /var/log/ha-debug
logfile /var/log/ha-log
#Ghi log hot ng ca cluster vo /var/log/ha-log
logfacility local0
#M t loi facility khi s dng cng vi syslog
keepalive 3
#Tn sut gi heartbeat gia cc node (3 giy mt ln)
deadtime 30
#Thi gian ch xc nhn mt host cht (30 giy)
initdead 120
#Thi gian xc nhn mt host cht khi mi khi ng
udpport 694
#Port s dng gi heartbeat
bcast eth1
#Interface s s dng gi heartbeat
auto_failback on
#C t ng failback dch v hay khng
node node1
#Lit k cc node c trong mi trng cluster
node node2
[root@node1]# touch /etc/ha.d/haresources

Bc 6. [Trn my ch node1] Copy ton b th mc cha cc file cu hnh t node1 sang node2

Linux Basic Course

[root@node1]# scp -r /etc/ha.d/ root@node2:/etc/


root@node2's password:
shellfuncs
100% 7862
README.config
100% 692

7.7KB/s
0.7KB/s

00:00
00:00

Bc 7. [Trn my ch node1 v node2] Khi ng dch v heartbeat trn c hai node


[root@node1]# chkconfig heartbeat on
[root@node1]# service heartbeat start
Starting High-Availability services:

OK

[root@node2]# chkconfig heartbeat on


[root@node2]# service heartbeat start
Starting High-Availability services:

OK

Bc 8. [Trn my ch node1 v node2] Kim tra m bo hai node nhn thy nhau v cluster ang
hot ng bnh thng. Stop cc dch v sau khi hon tt kim tra
[root@node1]# cl_status
node1
node2
[root@node1]# cl_status
Heartbeat is running on
[root@node1]# cl_status
active
[root@node1]# cl_status
active

listnodes

hbstatus
this machine.
nodestatus node1
nodestatus node2

[root@node2]# cl_status listnodes


node1
node2
[root@node2]# cl_status hbstatus
Heartbeat is running on this machine.
[root@node1]# service heartbeat stop
[root@node2]# service heartbeat stop

Linux Basic Course

II. Cu hnh v th nghim HA cho dch v web


Bc 9. [Trn my ch node1 v node2] Ci t dch v Apache webserver trn cc node (nu cha
ci t). Cu hnh cho dch v web (Apache httpd) lng nghe trn a ch IP 192.168.1.10. Dng v
cu hnh dch v httpd khng t ng khi ng (heartbeat s qun l vic khi ng cc dch v
ny). To cc trang ch ca website (trong /var/www/html)
[root@node1]# vi /etc/httpd/conf/httpd.conf

Listen 192.168.1.10:80
#Dng 134. Nhn Esc v g 134G chuyn n dng ny

DocumentRoot "/var/www/html" #Dng 281. Nhn Esc v g 259G chuyn n dng ny

[root@node1]# service httpd stop


[root@node1]# chkconfig httpd off
[root@node1]# vi /var/www/html/index.htm
Chao mung den website cua IPMac Lab. Dich vu web hien dang chay tren <b>NODE1</b>
[root@node2]# vi /etc/httpd/conf/httpd.conf

Listen 192.168.1.10:80
#Dng 134. Nhn Esc v g 134G chuyn n dng ny

DocumentRoot "/var/www/html" #Dng 281. Nhn Esc v g 259G chuyn n dng ny

[root@node2]# service httpd stop


[root@node2]# chkconfig httpd off
[root@node2]# vi /var/www/html/index.html
Chao mung den website cua IPMac Lab. Dich vu web hien dang chay tren <b>NODE2</b>

Bc 10. [Trn my ch node1] Cu hnh li file /etc/ha.d/haresources a vo cc thng tin m t


dch v httpd. Sau khi thc hin xong trn node1, copy file ny sang node2.
[root@node1]# vi /etc/ha.d/haresources
node1 IPaddr::192.168.1.10
httpd
[root@node1]# scp /etc/ha.d/haresources root@node2:/etc/ha.d

Bc 11. [Trn my ch node1 v node2] Khi ng li dch v heartbeat trn c 02 node. Ch cho
dch v trn c 02 node khi ng hon tt. Kim tra thy lc ny trn node1, Virtual IP vi a ch
192.168.1.10 c to ra trn eth0 v dch v httpd c khi ng.
[root@node1]# service heartbeat start
#Start dch v trn node2 trc khi thc hin cc cu lnh tip theo bn di.
[root@node1]# tail -f /var/log/ha-log

IPaddr[5779]:
2011/04/10_14:49:09 INFO: eval ifconfig eth0:0 192.168.1.10
netmask 255.255.255.0 broadcast 192.168.100.255
IPaddr[5762]:
2011/04/10_14:49:09 INFO: Success
ResourceManager[5676]: 2011/04/10_14:49:10 info: Running /etc/init.d/httpd start
[root@node1]# ifconfig | grep eth0
eth0
Link encap:Ethernet HWaddr 00:0C:29:E6:08:F0
eth0:0
Link encap:Ethernet HWaddr 00:0C:29:E6:08:F0
[root@node1]# ifconfig eth0:0
eth0:0
Link encap:Ethernet HWaddr 00:0C:29:E6:08:F0
inet addr:192.168.100.10 Bcast:192.168.100.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

Linux Basic Course

Interrupt:67 Base address:0x2024


[root@node1]# service httpd status
httpd (pid 7070) is running...
[root@node2]# service heartbeat start
[root@node2]# ifconfig | grep eth0
eth0
Link encap:Ethernet HWaddr 00:0C:29:E6:08:F8
[root@node2]# service httpd status
httpd is stopped

Bc 12. [T my ch client] S dng Web Browser truy cp vo website ti a ch 192.168.1.10


thy dch v web hin ang hot ng trn node1

Bc 13. [Trn my ch node1 v node2 v client] Stop dch v heartbeat trn node1 (hoc
shutdown node1). Kim tra log trn node2 xem qu trnh failover din ra. Trn client thc hin
refresh trang web thy lc ny dch v web ang hot ng trn node2
[root@node1]# service heartbeat stop #hoc shutdown now -h
[root@node2]# tail -f /var/log/ha-log

IPaddr[6487]:
2011/04/10_14:53:54 INFO: eval ifconfig eth0:0 192.168.1.10
netmask 255.255.255.0 broadcast 192.168.100.255
IPaddr[6470]:
2011/04/10_14:53:54 INFO: Success
ResourceManager[6386]: 2011/04/10_14:53:54 info: Running /etc/init.d/httpd

start

[root@node2]# service httpd status


httpd (pid 7222) is running...

Bc 14. [Trn my ch node1 v client] Start li dch v heartbeat trn node1 (hoc khi ng li
node1 nu bc 13 thc hin vic shutdown). Ch cho qu trnh khi ng li hon tt. Trn client
thc hin refressh li trang web thy lc ny dch v web tr li hot ng trn node1 do chnh
sch auto_failback trong file cu hnh cluster (/etc/ha.d/ha.cf) ang l on.

Linux Basic Course