Cài đặt heartbeat trên linux

Linux Basic Course
1
Bài thực hành
Cài đặt và cấu hình HA Cluster với Heartbeat
cho dịch vụ Web
Trong bài thực hành này, bạn cần sử dụng 02 máy ảo Linux và máy thật Windows kết nối theo
mô hình bên dưới
HOST ONLY
NAT NETWORK [192.168.1.0/24]
node1
node2
192.168.1.1 192.168.1.2 192.168.1.100
eth0
client
eth0
eth2eth2
172.16.0.1 172.16.0.2
192.168.1.10

Trong bài thực hành này, bạn sẽ cấu hình môi trường cluster sẵn sàng cao cho dịch vụ web
trên cặp máy chủ Linux node1 và node2. Thông thường, dịch vụ web này sẽ chạy trên node1.
Trong trường hợp node1 xảy ra sự cố, dịch vụ web sẽ được tự động chuyển sang chạy trên
node2 để không làm ảnh hưởng đến việc truy cập của người dùng.
 Cài đặt và cấu hình môi trường cluster sẵn sàng cao với cặp máy chủ Linux node1 và
node2 sử dụng bộ phần mềm heartbeat.
 Cấu hình dịch vụ web trên các máy chủ node1 và node2. Dịch vụ web này sẽ được
cấu hình hoạt động trên Virtual IP 192.168.1.10
 Tích hợp dịch vụ web vào trong môi trường sẵn sàng cao quản lý bởi heartbeat
 Kiểm tra để đảm bảo khi bất kỳ node nào trong các node bị sự cố, dịch vụ web vẫn
hoạt động bình thường.

Linux Basic Course
2
I.Cài đặt và khởi tạo dịch vụ heartbeat trên các node
Bước 1. [Trên máy chủ node1 và node2] Cấu hình hostname và địa chỉ tĩnh cho các interface theo mô
hình trên, stop dịch 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-eth2
DEVICE=eth2
BOOTPROTO=static
HWADDR=00:0C:29:E6:08:F1
ONBOOT=yes
IPADDR=172.16.0.1
NETMASK=255.255.255.0

[root@node1]# service network restart
[root@node1]# uname -n
node1
[root@node1]# exit
[root@node1]# 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-eth2
DEVICE=eth2
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
3
node2
[root@node2]# exit
[root@node2]# service iptables stop
Bước 2. [Trên máy chủ node1] Cấu hình lại file /etc/hosts trên các máy chủ để có thể kết nối với nhau
thông qua hostname. Copy file này 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
Bước 3. [Trên máy chủ node1 và node2] Kiểm tra đảm bảo 2 node đã có thể ping qua lại lẫn nhau
thông 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

Bước 4. [Trên máy chủ node1 và node2] Tìm kiếm và download package heartbeat và các package hỗ
trợ từ rpmfind.net và tiến hành cài đặt các package này. Các package cần download gồm:
 heartbeat-2.1.4-11
o Bản 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 Bản 32bit: ftp://rpmfind.net/linux/epel/5/i386/heartbeat-pils-2.1.4-11.el5.i386.rpm
o Bản 64bit: ftp://rpmfind.net/linux/epel/5/x86_64/heartbeat-pils-2.1.4-11.el5.x86_64.rpm

Linux Basic Course
4
 heartbeat-stonith-2.1.4-11
o Bản 32bit: ftp://rpmfind.net/linux/epel/5/i386/heartbeat-stonith-2.1.4-11.el5.i386.rpm
o Bản 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 Bản 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]# cd /tmp
[root@node2]# wget ftp://rpmfind.net/linux/epel/5/i386/heartbeat-2.1.4-11.el5.i386.rpm
[root@node2]# wget ftp://rpmfind.net/linux/epel/5/i386/heartbeat-pils-2.1.4-11.el5.i386.rpm
[root@node2]# wget ftp://rpmfind.net/linux/epel/5/i386/heartbeat-stonith-2.1.4-11.el5.i386.rpm
[root@node2]# wget ftp://rpmfind.net/linux/epel/5/i386/libnet-1.1.5-1.el5.i386.rpm
[root@node2]# yum install openhpi-libs
[root@node2]# rpm -ivh heartbeat-pils-2.1.4-11.el5.i386.rpm
[root@node2]# rpm -ivh heartbeat-stonith-2.1.4-11.el5.i386.rpm
[root@node2]# rpm -ivh libnet-1.1.5-1.el5.i386.rpm
[root@node2]# rpm -ivh heartbeat-2.1.4-11.el5.i386.rpm
Bước 5. [Trên máy chủ node1] Cấu hình cho dịch vụ heartbeat trên node1. Để heartbeat hoạt động
cần tạo 03 file cấu hình trong thư mục /etc/ha.d/ là authkeys [chứa thông tin về việc xác thực giữa các
node], ha.cf [cấu hình cho dịch vụ heartbeat] và haresources mô tả các dịch vụ sẽ sử dụng với
heartbeat. File authkeys cần phải được đặt mode là 600. File hearesource tạm thời chưa cần
có thông tin gì [tạo bằng lệnh touch]
[root@node1]# vi /etc/ha.d/authkeys
auth 1 #Chọn cơ chế xác thực số 1 được mô tả bên dưới

1 sha1 ipmac-cluster #Cơ chế 1: Xác thực sử dụng SHA1 với 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 các thông tin debug vào /var/log/ha-debug
logfile /var/log/ha-log #Ghi log hoạt động của cluster vào /var/log/ha-log
logfacility local0 #Mô tả loại facility khi sử dụng cùng với syslog
keepalive 3 #Tần suất gửi heartbeat giữa các node [3 giây một lần]
deadtime 30 #Thời gian chờ để xác nhận một host đã chết [30 giây]
initdead 120 #Thời gian xác nhận một host đã chết khi mới khởi động
udpport 694 #Port sử dụng để gửi heartbeat
bcast eth2 #Interface sẽ sử dụng để gửi heartbeat
auto_failback on #Có tự động failback dịch vụ hay không
node node1 #Liệt kê các node có trong môi trường cluster
node node2

[root@node1]# touch /etc/ha.d/haresources
Bước 6. [Trên máy chủ node1] Copy toàn bộ thư mục chứa các file cấu hình từ node1 sang node2

Linux Basic Course
5
[root@node1]# scp -r /etc/ha.d/ root@node2:/etc/
root@node2's password:
shellfuncs 100% 7862 7.7KB/s 00:00
README.config 100% 692 0.7KB/s 00:00

Bước 7. [Trên máy chủ node1 và node2] Khởi động dịch vụ heartbeat trên 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 ]

Bước 8. [Trên máy chủ node1 và node2] Kiểm tra đảm bảo hai node đã nhìn thấy nhau và cluster đang
hoạt động bình thường. Stop các dịch vụ sau khi hoàn tất kiểm tra
[root@node1]# cl_status listnodes
node1
node2
[root@node1]# cl_status hbstatus
Heartbeat is running on this machine.
[root@node1]# cl_status nodestatus node1
active
[root@node1]# cl_status nodestatus node2
active

[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
6
II. Cấu hình và thử nghiệm HA cho dịch vụ web
Bước 9. [Trên máy chủ node1 và node2] Cài đặt dịch vụ Apache webserver trên các node [nếu chưa

cài đặt]. Cấu hình cho dịch vụ web [Apache httpd] để lắng nghe trên địa chỉ IP 192.168.1.10. Dừng và
cấu hình để dịch vụ httpd không tự động khởi động [heartbeat sẽ quản lý việc khởi động các dịch vụ
này]. Tạo các trang chủ của website [trong /var/www/html]
[root@node1]# vi /etc/httpd/conf/httpd.conf

Listen 192.168.1.10:80 #Dòng 134. Nhấn Esc và gõ 134G để chuyển đến dòng này

DocumentRoot "/var/www/html" #Dòng 281. Nhấn Esc và gõ 259G để chuyển đến dòng này

[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 NODE1

[root@node2]# vi /etc/httpd/conf/httpd.conf

Listen 192.168.1.10:80 #Dòng 134. Nhấn Esc và gõ 134G để chuyển đến dòng này

DocumentRoot "/var/www/html" #Dòng 281. Nhấn Esc và gõ 259G để chuyển đến dòng này

[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 NODE2
Bước 10. [Trên máy chủ node1] Cấu hình lại file /etc/ha.d/haresources để đưa vào các thông tin mô tả
dịch vụ httpd. Sau khi thực hiện xong trên node1, copy file này 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
Bước 11. [Trên máy chủ node1 và node2] Khởi động lại dịch vụ heartbeat trên cả 02 node. Chờ cho

dịch vụ trên cả 02 node khởi động hoàn tất. Kiểm tra để thấy lúc này trên node1, Virtual IP với địa chỉ
192.168.1.10 đã được tạo ra trên eth0 và dịch vụ httpd đã được khởi động.
[root@node1]# service heartbeat start
#Start dịch vụ trên node2 trước khi thực hiện các câu lệnh tiếp theo bên dưới.
[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
7
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
Bước 12. [Từ máy chủ client] Sử dụng Web Browser truy cập vào website tại địa chỉ 192.168.1.10 để
thấy dịch vụ web hiện đang hoạt động trên node1

Bước 13. [Trên máy chủ node1 và node2 và client] Stop dịch vụ heartbeat trên node1 [hoặc
shutdown node1]. Kiểm tra log trên node2 để xem quá trình failover diễn ra. Trên client thực hiện
refresh trang web để thấy lúc này dịch vụ web đang hoạt động trên node2
[root@node1]# service heartbeat stop #hoặc 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

Bước 14. [Trên máy chủ node1 và client] Start lại dịch vụ heartbeat trên node1 [hoặc khởi động lại
node1 nếu ở bước 13 thực hiện việc shutdown]. Chờ cho quá trình khởi động lại hoàn tất. Trên client
thực hiện refressh lại trang web để thấy lúc này dịch vụ web đã trở lại hoạt động trên node1 do chính
sách auto_failback trong file cấu hình cluster [/etc/ha.d/ha.cf] đang để là on.

Chủ Đề