1. 程式人生 > >heartbeat單獨提供高可用服務

heartbeat單獨提供高可用服務

而不是 boolean member net 設置ip 失效 文件 plugin receive

本文目錄:
1.簡介
2.安裝heartbeat
 2.1 編譯安裝Heartbeat
3.heartbeat相關配置文件
 3.1 配置文件ha.cf
 3.2 配置文件authkeys
 3.3 配置文件haresources
4.示例:heartbeat為httpd提供高可用服務

1.簡介

heartbeat是人所眾知高可用軟件。但是在以前,heartbeat是Linux-ha項目裏一大堆提供高可用組件的集合體:

  • Messaging Layer(消息傳遞層)
  • local resource manager(LRM,本地資源管理,cluster glue的一個功能)
  • stonith(爆頭,cluster glue的一個功能)
  • Resource Agent(RA,資源代理)、
  • cluster resource manager(CRM,集群資源管理器,現在獨立出去的pacemaker)。

現在,由於linux-ha將很多這些組件都分離為一個個單獨的軟件,heartbeat已經只代表消息層(取代它的是corosync)。而且linux-ha項目團隊已經不再維護heartbeat,目前能從官方獲取到的最新版本是Heartbeat 3.0.6,在epel中可獲取到"Heartbeat 3.0.4"版本的rpm包。

雖然Heartbeat只代表高可用集群的消息傳遞層,但它結合cluster glue和resource agent也可以提供高可用服務,這正是本文的內容。相比於corosync+pacemaker,heartbeat要輕量級的多,配置起來也簡單許多。相應的,它的功能和完整性要簡陋的多,它只支持兩個heartbeat節點(結合pacemaker可多於兩節點),且加載資源的速度比較慢

當然,既然heartbeat是消息傳遞層,它也能配合pacemaker,但是不建議如此。而且這樣搭配時,各個組件的版本要求很嚴格。

2.安裝heartbeat

如果使用yum安裝,可以配置epel源。註意,在centos7的epel上,已經沒有heartbeat了。

release=`grep -o "[0-9]" /etc/redhat-release | head -1`
cat <<eof>/etc/yum.repos.d/epel.repo
[epel]
name=epelrepo
baseurl=https://mirrors.aliyun.com/epel/${release
}Server/\$basearch gpgcheck=0 enable=1 eof yum -y install heartbeat

可以看到,安裝heartbeat的時候,同時還會安裝cluster-glue和resource-agent。

技術分享圖片

然後提供配置文件。

cp -a /usr/share/doc/heartbeat-3.0.4/{ha.cf,haresources,authkeys} /etc/ha.d/
chmod 600 /etc/ha.d/authkeys

2.1 編譯安裝heartbeat

不建議編譯安裝heartbeat,heartbeat這個程序本身附帶的很多腳本在路徑指定上很混亂,安裝後很可能需要到處修改腳本中關於路徑的變量。

如果要編譯安裝heartbeat,則必須先安裝cluster-glue和resource-agent,建議這3個程序安裝在同一路徑下。

(1).安裝依賴包。

yum -y install gcc gcc-c++ autoconf automake libtool glib2-devel libxml2-devel bzip2 bzip2-devel e2fsprogs-devel libxslt-devel libtool-ltdl-devel asciidoc

創建好所需組和所有者,在編譯cluster-glue會設置一些文件的屬組和屬主分別為haclient、hacluster,因此名稱必須為這兩個。

groupadd -g 65 haclient
useradd -g 65 -u 17 hacluster

(2).編譯cluster-glue。

tar xf Cluster\ Glue\ 1.0.12.bz2
cd Reusable-Cluster-Components-glue--0a7add1d9996/
./autogen.sh
./configure --prefix=/usr/local/heartbeat LIBS=/lib64/libuuid.so.1 
make
make install

如果make時有如下錯誤:

./.libs/libplumb.so: undefined reference to `uuid_parse‘
./.libs/libplumb.so: undefined reference to `uuid_generate‘
./.libs/libplumb.so: undefined reference to `uuid_copy‘
./.libs/libplumb.so: undefined reference to `uuid_is_null‘
./.libs/libplumb.so: undefined reference to `uuid_unparse‘
./.libs/libplumb.so: undefined reference to `uuid_clear‘
./.libs/libplumb.so: undefined reference to `uuid_compare‘
collect2: ld returned 1 exit status
gmake[2]: *** [ipctest] Error 1
gmake[2]: Leaving directory `/root/Reusable-Cluster-Components-glue--0a7add1d9996/lib/clplumbing‘
gmake[1]: *** [all-recursive] Error 1
gmake[1]: Leaving directory `/root/Reusable-Cluster-Components-glue--0a7add1d9996/lib‘
make: *** [all-recursive] Error 1

解決方法:在configure中使用LIBS指向正確的庫文件,64位使用/lib64,32位使用/lib

./configure LIBS=/lib64/libuuid.so.1

如果是如下錯誤:

gmake[2]: a2x: Command not found

解決方法:yum install asciidoc

可以看下cluster-glue提供了哪些命令。

[root@xuexi ~]# ls /usr/local/heartbeat/sbin/
cibsecret   ha_logger   hb_report   lrmadmin    meatclient  stonith

從此不難看出,cluster-glue是整個ha的核心組件,除了crm、messageing layer、resource agent,所有的功能包括最基本的功能都是它提供的。例如,日誌記錄,stonith,lrm等。

(3).編譯resource-agent

tar xf  resource-agents-3.9.6.tar.gz 
cd resource-agents-3.9.6
./autogen.sh 
./configure --prefix=/usr/local/heartbeat
make
make install

(4).編譯heartbeat。

tar xf heartbeat\ 3.0.6.bz2
cd Heartbeat-3-0-958e11be8686/
./bootstrap
export CFLAGS="$CFLAGS -I/usr/local/heartbeat/include -L/usr/local/heartbeat/lib"
./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient LIBS=/lib64/libuuid.so.1
make
make install

如果出現如下錯誤:

【configure時錯誤:】
configure: error: in `/root/Heartbeat-3-0-958e11be8686‘:
configure: error: Core development headers were not found

解決方法:
export CFLAGS="$CFLAGS -I/usr/local/heartbeat/include -L/usr/local/heartbeat/lib"

【make時錯誤:】
/usr/local/heartbeat/include/heartbeat/glue_config.h:105:1: error: "HA_HBCONF_DIR" redefined
In file included from ../include/lha_internal.h:38,
                 from strlcpy.c:1:
../include/config.h:390:1: error: this is the location of the previous definition
gmake[1]: *** [strlcpy.lo] Error 1
gmake[1]: Leaving directory `/root/Heartbeat-3-0-958e11be8686/replace‘
make: *** [all-recursive] Error 1

解決方法1:
刪除/usr/local/heartbeat/include/heartbeat/glue_config.h 中的第105#define HA_HBCONF_DIR "/etc/ha.d/"
sed -i ‘105d‘ /usr/local/heartbeat/include/heartbeat/glue_config.h

解決方法2:configure上加上忽略錯誤選項
./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient LIBS=/lib64/libuuid.so.1 --enable-fatal-warnings=no

(5).編譯後配置。

mkdir -p /usr/local/heartbeat/usr/lib/ocf/lib/heartbeat
cp -a /usr/lib/ocf/lib/heartbeat/ocf-* /usr/local/heartbeat/usr/lib/ocf/lib/heartbeat/
ln -s /usr/local/heartbeat/lib64/heartbeat/plugins/RAExec/* /usr/local/heartbeat/lib/heartbeat/plugins/RAExec/
ln -s /usr/local/heartbeat/lib64/heartbeat/plugins/* /usr/local/heartbeat/lib/heartbeat/plugins/
ln -s /usr/local/heartbeat/share/heartbeat /usr/share/heartbeat

提供配置文件:

cd /usr/local/heartbeat
cp -a share/doc/heartbeat/{ha.cf,haresources,authkeys} etc/ha.d/
chmod 600 etc/ha.d/authkeys

加入服務器啟動列表:

chkconfig --add heartbeat
chkconfig --level 2345 heartbeat on

設置環境變量PATH:

echo ‘export PATH=/usr/local/heartbeat/sbin:/usr/local/heartbeat/bin:$PATH >/etc/profile.d/ha.sh
chmod +x /etc/profile.d/ha.sh
source /etc/profile.d/ha.sh

設置man PATH:

echo ‘MANPATH /usr/local/heartbeat/share/man‘ >>/etc/man.config

3.heartbeat相關配置文件

heartbeat配置文件有3個:

  1. 密鑰文件authkeys,用在messaging layer下各節點之間的認證,防止外界主機隨意加入節點(600權限);
  2. heartbeat核心配置文件,ha.cf;
  3. 資源管理配置文件:haresources;

它們的生效位置在/etc/ha.d/目錄下,但是初始時在此目錄下並沒有這3個文件,它們的樣例配置文件在/usr/share/docs/heartbeat-$$version/目錄下,可以將它們復制到/etc/ha.d目錄下。

#以下是yum安裝,非編譯安裝的操作
cp /usr/share/doc/heartbeat-3.0.4/{authkeys,ha.cf,haresources} /etc/ha.d/

3.1 配置文件ha.cf

ha.cf的部分內容如下。該文件看起來很多,但如果不結合pacemaker,其實要修改的就幾項,包括node和bcast/mcast以及auto_failback,有時還配置下ping和log。註意該文件從上往下讀取,指令的配置位置很重要,因此一般不要修改它們的出現順序。

#   如果logfile/debugfile/logfacility都沒有設置,則等價於設置了"use_logd yes"
#   且use_logd設置為yes後,logfile/debugfile/logfacility的設置都失效
#
#       Note on logging:
#       If all of debugfile, logfile and logfacility are not defined, 
#       logging is the same as use_logd yes. In other case, they are
#       respectively effective. if detering the logging to syslog,
#       logfacility must be "none".
#
#       File to write debug messages to
#debugfile /var/log/ha-debug
#
#
#       File to write other messages to
#
#logfile        /var/log/ha-log
#
#
#       Facility to use for syslog()/logger 
#
logfacility     local0
#
#
#       A note on specifying "how long" times below...
#
#       The default time unit is seconds
#               10 means ten seconds
#
#       You can also specify them in milliseconds
#               1500ms means 1.5 seconds
#
#
#       keepalive: how long between heartbeats?
#  發送心跳信息的時間間隔,默認每兩秒發送一次心跳信息
#keepalive 2
#
#       deadtime: how long-to-declare-host-dead?
#
#               If you set this too low you will get the problematic
#               split-brain (or cluster partition) problem.
#               See the FAQ for how to use warntime to tune deadtime.
#  指定若備節點在30秒內未收到主節點心跳信號,則判定主節點死亡,並接管主服務器資源
#deadtime 30
#
#       warntime: how long before issuing "late heartbeat" warning?
#       See the FAQ for how to use warntime to tune deadtime.
#  指定心跳延遲的時間為10秒,10秒內備節點不能接收主節點心跳信號,即往日誌寫入警告日誌,但不會切換服務
#warntime 10
#
#
#       Very first dead time (initdead)
#
#       On some machines/OSes, etc. the network takes a while to come up
#       and start working right after you‘ve been rebooted.  As a result
#       we have a separate dead time for when things first come up.
#       It should be at least twice the normal dead time.
#  定義第一次死亡判定時間,即第一個heartbeat啟動後等待第二個heartbeat啟動,
#  第二個啟動後才會啟動高可用服務、啟動VIP等。若在此時間內第二個節點未啟動則
#  判定其dead,然後才啟動高可用服務和VIP,這是雙方為形成高可用群集的等待時間。
#  此時間至少要是deadtime的兩倍
#initdead 120
#
#
#       What UDP port to use for bcast/ucast communication?
#  心跳信息端口
#udpport        694
#
#       Baud rate for serial ports...
#  支持兩種方式發送心跳信息,一是以太網(廣播組播單播),一是串行線,在heartbeat3中,baud已經廢棄
#baud   19200
#
#       serial  serialportname ...
#serial /dev/ttyS0      # Linux
#serial /dev/cuaa0      # FreeBSD
#serial /dev/cuad0      # FreeBSD 6.x
#serial /dev/cua/a      # Solaris
#
#
#       What interfaces to broadcast heartbeats over?
#
#bcast  eth0            # Linux
#bcast  eth1 eth2       # Linux
#bcast  le0             # Solaris
#bcast  le1 le2         # Solaris
#
#       Set up a multicast heartbeat medium
#       mcast [dev] [mcast group] [port] [ttl] [loop]
#
#       [dev]           device to send/rcv heartbeats on
#       [mcast group]   multicast group to join (class D multicast address
#                       224.0.0.0 - 239.255.255.255)
#       [port]          udp port to sendto/rcvfrom (set this value to the
#                       same value as "udpport" above)
#       [ttl]           the ttl value for outbound heartbeats.  this effects
#                       how far the multicast packet will propagate.  (0-255)
#                       Must be greater than zero.
#       [loop]          toggles loopback for outbound multicast heartbeats.
#                       if enabled, an outbound packet will be looped back and
#                       received by the interface it was sent on. (0 or 1)
#                       Set this value to zero.
#
#
#mcast eth0 225.0.0.1 694 1 0
#
#       Set up a unicast / udp heartbeat medium
#       ucast [dev] [peer-ip-addr]
#
#       [dev]           device to send/rcv heartbeats on
#       [peer-ip-addr]  IP address of peer to send packets to
#
#  單播心跳,需指定對方心跳接口地址
#ucast eth0 192.168.1.2
#
#
#       About boolean values...
#
#       Any of the following case-insensitive values will work for true:
#               true, on, yes, y, 1
#       Any of the following case-insensitive values will work for false:
#               false, off, no, n, 0
#
#
#
#       auto_failback:  determines whether a resource will
#       automatically fail back to its "primary" node, or remain
#       on whatever node is serving it until that node fails, or
#       an administrator intervenes.
#
#       The possible values for auto_failback are:
#               on      - enable automatic failbacks
#               off     - disable automatic failbacks
#               legacy  - enable automatic failbacks in systems
#                       where all nodes do not yet support
#                       the auto_failback option.
#
#       auto_failback "on" and "off" are backwards compatible with the old
#               "nice_failback on" setting.
#
#       See the FAQ for information on how to convert
#               from "legacy" to "on" without a flash cut.
#               (i.e., using a "rolling upgrade" process)
#
#       The default value for auto_failback is "legacy", which
#       will issue a warning at startup.  So, make sure you put
#       an auto_failback directive in your ha.cf file.
#       (note: auto_failback can be any boolean or "legacy")
#  主節點恢復重新上線後,是否自動接管服務
auto_failback on
#
#  以下是fence設備相關
#       Basic STONITH support
#       Using this directive assumes that there is one stonith 
#       device in the cluster.  Parameters to this device are 
#       read from a configuration file. The format of this line is:
#
#         stonith <stonith_type> <configfile>
#
#       NOTE: it is up to you to maintain this file on each node in the
#       cluster!
#
#stonith baytech /etc/ha.d/conf/stonith.baytech
#
#       STONITH support
#       You can configure multiple stonith devices using this directive.
#       The format of the line is:
#         stonith_host <hostfrom> <stonith_type> <params...>
#         <hostfrom> is the machine the stonith device is attached
#              to or * to mean it is accessible from any host. 
#         <stonith_type> is the type of stonith device (a list of
#              supported drives is in /usr/lib/stonith.)
#         <params...> are driver specific parameters.  To see the
#              format for a particular device, run:
#           stonith -l -t <stonith_type> 
#
#
#       Note that if you put your stonith device access information in
#       here, and you make this file publically readable, you‘re asking
#       for a denial of service attack ;-)
#
#       To get a list of supported stonith devices, run
#               stonith -L
#       For detailed information on which stonith devices are supported
#       and their detailed configuration options, run this command:
#               stonith -h
# 
#stonith_host *     baytech 10.0.0.3 mylogin mysecretpassword
#stonith_host ken3  rps10 /dev/ttyS1 kathy 0 
#stonith_host kathy rps10 /dev/ttyS1 ken3 0 
#
#
#  看門狗是一個計時器。如果自身60秒不心跳了,則本節點會重啟
#       Watchdog is the watchdog timer.  If our own heart doesn‘t beat for
#       a minute, then our machine will reboot.
#       NOTE: If you are using the software watchdog, you very likely
#       wish to load the module with the parameter "nowayout=0" or
#       compile it without CONFIG_WATCHDOG_NOWAYOUT set. Otherwise even
#       an orderly shutdown of heartbeat will trigger a reboot, which is
#       very likely NOT what you want.
#
#watchdog /dev/watchdog   #看門狗fence設備,Linux自帶的軟watchdog
#       
#       Tell what machines are in the cluster
#       node    nodename ...    -- must match uname -n
#  必須配置的node,必須和uname -n的結果一致
#node   ken3
#node   kathy
#
#       Less common options...
#
#       Treats 10.10.10.254 as a psuedo-cluster-member
#       Used together with ipfail below...
#       note: don‘t use a cluster node as ping node
#
#  通過ping參考ip檢測本節點對外的網絡連通性,需要配合ipfail進程。當ping不通時將down掉本節點
#  ping_group是通過ping一組ip來檢查ip的連通性,防止因對方節點故障而誤以為自己壞了。
#  只有當組中所有節點都ping不通才認為自己壞了。和ping只能使用二選一
#  不要使用集群節點作為ping的參考ip,一般ping的對象都是網關
#ping 10.10.10.254
#ping_group group1 172.16.103.254 172.16.103.212

#  隨heartbeat啟動、停止而啟動停止的進程,它是pacemaker在heartbeat中的實現
#respawn hacluster   /usr/local/lib/heartbeat/ipfail

#  指定哪個用戶、組可以連通使用某進程,此處為ipfail
#apiauth ipfail gid=haclient uid=hacluster

綜上,必須要配置的就是三項:node、bcast/mcast、auto_failback。它們在文件中的位置順序是先bcast/mcast/ucast,再auto_failback,最後才是node。偶爾還需配置ping來檢測自身網絡。

例如,以下是兩個heartbeat節點,分別兩個網卡,使用廣播方式從eth0發送心跳信息的配置。

logfile /var/log/ha-log
logfacility local0
bcast eth0
#mcast eth1 225.0.0.193 694 1 0
auto_failback yes
node node1.longshuai.com
node node2.longshuai.com
ping 192.168.100.1
respawn hacluster   /usr/local/lib/heartbeat/ipfail
apiauth ipfail gid=haclient uid=hacluster

因為使用的是廣播,兩臺服務器上的ha.cf文件是完全一樣的。如果是通過多播或單播的方式發送心跳信息,則兩臺服務器的ha.cf在mcast/ucast指令配置參數上是不一樣的。

另外需註意,heartbeat主節點重啟heartbeat或重啟系統,當前正運行的heartbeat會發出通告給備節點,使其快速接管資源,而不是按照配置文件中定義的deadtime來獲取資源的。同理,當再次啟動的時候,如果設置了failback,會發送通告迅速收回資源。

3.2 配置文件authkeys

chmod 600 authkeys

以下為authkeys的內容。兩個節點上該文件的內容一致。

#auth 1
#1 crc
#2 sha1 HI!
#3 md5 Hello!
auth 3
3 md5 6hy6Y6NCdVInax1PlGlvFyIMm/k

使用的是md5格式,使用sha1更安全。後面的是一段隨機數,這裏用隨機數來做md5加密。隨機數的生成方式有很多種。如:

openssl rand -base64 20

3.3 配置文件haresources

在此配置文件內配置資源代理(resource agent)參數。heartbeat自身支持兩種風格的RA:一種是LSB類型的程序,它的路徑在/etc/init.d/下;一種是heartbeat自帶的haresource,它的路徑在ha.d/resource.d/目錄下。

以下是heartbeat自帶的RA。

[root@xuexi ~]# ls /etc/ha.d/resource.d/
apache  db2  Filesystem  ICP  IPaddr  IPsrcaddr  LinuxSCSI  MailTo  portblock  SendArp  WAS  Xinetd
AudibleAlarm  Delay  hto-mapfuncs  ids  IPaddr2  IPv6addr  LVM  OCF  Raid1  ServeRAID  WinPopup

需要記住其中幾個:

  • apache:管理httpd,需指定httpd配置文件作為該RA的參數。
  • IPaddr和IPaddr2:設置IP別名(註意,是別名),IPaddr2是IPaddr的升級版,但兩者可以通用。
  • Filesystem:掛載文件系統。

自帶的RA類似於LSB,只是它能接受參數(如start/stop/status),而LSB不能,LSB的start、stop和status參數由heartbeat自行發送。

它們都必須能接受start/stop/status參數,且必須具有冪等性。例如running狀態的再次start,返回狀態碼為0,還是繼續running,stop狀態的再次stop不會報錯,且狀態碼為0。除了對stop狀態的資源進行status時返回狀態碼3,其他任意參數的狀態碼都必須為0。

如果結合pacemaker,則還支持ocf風格的程序來管理資源。

通過以下5行來說明該配置文件的配置方法。

#node-name resource1 resource2 ... resourceN
#IPaddr::135.9.8.7/24/eth0
#just.linux-ha.org 135.9.216.110 httpd
#node1  10.0.0.170 Filesystem::/dev/sda1::/data1::ext2
#node1  10.0.0.170 apache::/etc/apache/httpd.conf
  1. 第一行是配置語法說明,首先指定節點名,節點名必須和uname -n一致。後面指定在此節點上運行的資源,多個資源使用空格隔開。
  2. 第二行中的"IPaddr"是資源代理程序,該程序在/etc/ha.d/resource.d/目錄下,如果該目錄下找不到就會去找/etc/init.d/目錄下的程序。IPaddr後面的雙冒號"::"是參數分隔符,多個參數之間使用雙冒號分割,參數是傳遞給資源代理程序的。這一行說明的是設置在某節點上設置ip為135.9.8.7,掩碼為24位,配置在eth0的別名上。它實現的是resource.d/IPaddr 135.9.8.7/24/eth0 start|stop|status
  3. 第三行說明在節點just.linux-ha.org上啟用ip 135.9.216.110(IPaddr程序可省略)和httpd服務。
  4. 第四行說明節點node1上啟用IP 10.0.0.170,成功後運行資源代理程序Filesystem,向其傳入運行3個參數"/dev/sda1"、"/data1"、"ext2"。

如果不知道某個RA接什麽樣的參數實現怎樣的功能,可以去查看ha.d/resource.d/下對應RA的程序用法(一般都是shell腳本,前幾行就會寫Usage)。例如,IPaddr2的前幾行:

[root@xuexi ~]# vim /etc/ha.d/resource.d/IPaddr2
#!/bin/sh
#
#
# Description:  wrapper of OCF RA IPaddr2, based on original heartbeat RA.
#               See OCF RA IPaddr2 for more information.
#
# Author:       Xun Sun <[email protected]>
# Support:      [email protected]
# License:      GNU General Public License (GPL)
# Copyright:    (C) 2005 International Business Machines
#
#       This script manages IP alias IP addresses
#
#       It can add an IP alias, or remove one.
#
#       usage: $0 ip-address[/netmaskbits[/interface[:label][/broadcast]]] \
#           {start|stop|status|monitor}
#
#       The "start" arg adds an IP alias.
#
#       Surprisingly, the "stop" arg removes one.       :-)

例如,以下是只管理兩個節點VIP資源的haresources文件內容。兩個服務器上的內容相同時表示:這是一個主主模型,節點1初始時只設置20.16這個IP,節點2初始時只設置20.14這個IP,當某節點故障後,另一節點接管其上VIP。

node1.longshuai.com IPaddr2::192.168.20.16/24/eth0
node2.longshuai.com IPaddr2::192.168.20.14/24/eth0

註意:

  1. heartbeat只支持兩個節點,沒有主從之分,只能根據haresources中的節點名稱來決定是否設置某資源
  2. VIP這種配置在別名接口上的地址,必須要和它所在接口同網段,否則它沒有對應的路由。即使它通過默認路由出去了,如果它的下一跳是Linux主機,由於Linux主機默認設置了rp_filter=1的源地址嚴格檢查,會直接丟棄這樣的數據包。如果真這樣,將其設置為2(也可以設置為0,但不推薦)。
  3. 如果采用廣播發送心跳信息,建議心跳接口地址不要和VIP所在接口主地址同網段,否則心跳信息會被各節點的對外通信接口接收,從而影響性能。

4.示例:heartbeat為httpd提供高可用

這是一個沒有實際意義的示例,只是為了演示heartbeat提供高可用時需要配置哪些必要的東西,以及如何提供VIP漂移(即IP資源高可用)。

環境如下:

技術分享圖片

需要說明的是,httpd有兩種管理方式:

  1. heartbeat只管理vip的漂移,不管理httpd的啟動。這種情況要求httpd要事先在兩節點啟動好。
  2. heartbeat同時管理VIP和httpd,這時httpd不能事先啟動,也不能設置開機自啟動。

本文測試采用第二種方案。

(1).配置節點主機名。

# node1上執行
hostname node1.longshuai.com
sed -i "/HOSTNAME/Is/=.*$/=node1\.longshuai\.com/" /etc/sysconfig/network

# node2上執行
hostname node2.longshuai.com
sed -i "/HOSTNAME/Is/=.*$/=node2\.longshuai\.com/" /etc/sysconfig/network

(2).配置主機名解析。

# 兩節點都執行
cat >>/etc/hosts<<eof
192.168.100.59 node1.longshuai.com node1 
192.168.100.44 node2.longshuai.com node2 
eof

(3).為心跳線接口配置主機路由。

# 在node上執行:
route add -host 192.168.10.20 dev eth1
route -n

# 在node2上執行:
route add -host 192.168.10.10 dev eth1
route -n

(4).將兩節點進行時間同步。

# 兩節點都執行
ntpdate ntp1.aliyun.com

(5).兩節點安裝httpd,並設置不同頁面內容以方便測試。

# node1上執行:
yum -y install httpd
echo "response from node1" >/var/www/html/index.html

# node2上執行:
yum -y install httpd
echo "response from node2" >/var/www/html/index.html

(6).提供配置文件ha.cf。由於此處采用廣播方式,兩節點ha.cf內容完全一致。

cp /etc/ha.d/ha.cf /etc/ha.d/ha.cf.bak
cat <<eof>/etc/ha.d/ha.cf
logfile        /var/log/ha-log
logfacility     local0
keepalive 2
deadtime 30
warntime 10
initdead 120
udpport        694
bcast  eth1
auto_failback on
node   node1.longshuai.com
node   node2.longshuai.com
ping 192.168.100.1    # 虛擬機網關
respawn hacluster   /usr/local/lib/heartbeat/ipfail
apiauth ipfail gid=haclient uid=hacluster
eof

(7).提供配置文件authkeys。兩節點authkeys內容完全一致。

# 在node1上執行:
chmod 600 /etc/ha.d/authkeys
echo -e "auth 3\n3 md5 `openssl rand -base64 20`" >>/etc/ha.d/authkeys
scp /etc/ha.d/authkeys node2:/etc/ha.d/

(8).提供配置文件haresources,兩節點內容一致。

# 在node1上執行:
cp /etc/ha.d/haresources /etc/ha.d/haresources.bak
echo "node1.longshuai.com IPaddr2::192.168.100.33/24/eth0 httpd" >/etc/ha.d/haresources
scp /etc/ha.d/haresources node2:/etc/ha.d/

最後,啟動兩端heartbeat節點,並通過訪問VIP的web頁面進行測試。可以查看日誌/var/log/ha-log,看看heartbeat節點之間是如何等待對方並工作的。

回到Linux系列文章大綱:http://www.cnblogs.com/f-ck-need-u/p/7048359.html
回到網站架構系列文章大綱:http://www.cnblogs.com/f-ck-need-u/p/7576137.html
回到數據庫系列文章大綱:http://www.cnblogs.com/f-ck-need-u/p/7586194.html
轉載請註明出處:http://www.cnblogs.com/f-ck-need-u/p/8587882.html

註:若您覺得這篇文章還不錯請點擊右下角推薦,您的支持能激發作者更大的寫作熱情,非常感謝!

heartbeat單獨提供高可用服務