本文共 3021 字,大约阅读时间需要 10 分钟。
在开始配置之前,请确保以下环境条件已满足:
安装Heartbeat和相关工具包:
yum -y install libnet* heartbeat* ipvsadm
将Heartbeat的默认配置文件复制到/etc/ha.d/目录:
cp /usr/share/doc/heartbeat-2.1.3/ha.cf /etc/ha.d/cp /usr/share/doc/heartbeat-2.1.3/authkeys /etc/ha.d/cp /usr/share/doc/heartbeat-ldirectord-2.1.3/ldirectord.cf /etc/ha.d/
编辑/etc/ha.d/ha.cf文件:
vim /etc/ha.d/ha.cf
添加或修改以下参数:
logfacility local0keepalive 2deadtime 30warntime 10udpport 694ucast eth0 192.168.50.8auto_failback onnode lvs.abc.comnode lvs1.abc.comping 192.168.50.8ping_group group1 192.168.50.8 192.168.50.9ping_group group1 192.168.60.8 192.168.60.9respawn hacluster /usr/lib/heartbeat/ipfailapiauth ipfail gid=haclient uid=hacluster
编辑/etc/ha.d/authkeys文件:
vim /etc/ha.d/authkeys
确保文件中的验证信息与主从节点一致,并设置文件权限为600:
chmod 600 /etc/ha.d/authkeys
编辑/etc/ha.d/ldirectord.cf文件:
vim /etc/ha.d/ldirectord.cf
添加或修改以下内容:
checktimeout 3checkinterval 1autoreload yeslogfile "/var/log/ldirectord.log"fallback 127.0.0.1:80service httpscheduler wrrprotocol tcpchecktype negotiatecheckport 80request test.htmlreceive "Test Page"
创建/etc/ha.d/lvs文件:
vim /etc/ha.d/lvs
添加以下内容:
#!/bin/bash# description: start LVS of DirectorServer# Written by: NetSeek http://www.linuxtone.orgGW=192.168.50.80WEB_VIP=192.168.50.80WEB_RIP1=192.168.50.5WEB_RIP2=192.168.50.6WEB_RIP3=1.1.1.18WEB_RIP4=1.1.1.19case "$1" instart)echo "start LVS of DirectorServer"/sbin/ifconfig eth0:0 $WEB_VIP broadcast $WEB_VIP netmask 255.255.255.255 up/sbin/route add -host $WEB_VIP dev eth0:0/sbin/ipvsadm -A -t $WEB_VIP:80 -s wrr -p 3/sbin/ipvsadm -a -t $WEB_VIP:80 -r $WEB_RIP1:80 -g -w 1/sbin/ipvsadm -a -t $WEB_VIP:80 -r $WEB_RIP2:80 -g -w 2/sbin/ipvsadm -a -t $WEB_VIP:80 -r $WEB_RIP3:80 -g -w 1/sbin/ipvsadm -a -t $WEB_VIP:80 -r $WEB_RIP4:80 -g -w 1touch /var/lock/subsys/ipvsadm > /dev/null 2>&1 &/sbin/arping -I eth0 -c 5 -s $WEB_VIP $GW > /dev/null 2>&1 &echo "ipvsadm started";;stop)echo "stop LVS of DirectorServer"/sbin/ipvsadm -C/sbin/ipvsadm -Z/sbin/ifconfig eth0:0 down/sbin/route del $WEB_VIP > /dev/null 2>&1 &rm -rf /var/lock/subsys/ipvsadm > /dev/null 2>&1 &/sbin/arping -I eth0 -c 5 -s $WEB_VIP $GW > /dev/null 2>&1 &echo "ipvsadm stopped";;status)if [ ! -e /var/lock/subsys/ipvsadm ]; thenecho "ipvsadm is stopped"exit 1fiipvsadm -lnecho "..........ipvsadm is OK."fi;;*)echo "Usage: $0 {start|stop|status}"exit 1esacexit 0 设置Heartbeat和ipvsadm的自启动:
chkconfig heartbeat onchkconfig ipvsadm on
从节点的配置文件可以通过scp从主节点复制:
scp root@192.168.50.9:/etc/ha.d/* /etc/ha.d/
在所有节点上执行以下命令:
systemctl start heartbeatsystemctl enable heartbeat
tail -F /var/log/ha-logtail -F /var/log/messages
ipvsadm -L -n -stats
通过浏览器访问:
http://192.168.50.5/test.htmlhttp://192.168.50.6/test.html在主节点或从节点发生故障时,Heartbeat会自动切换到其他节点提供服务。
转载地址:http://ptufk.baihongyu.com/