首页 > 代码库 > Heartbeat配置多播方式以及heartbeat安装脚本
Heartbeat配置多播方式以及heartbeat安装脚本
之前部署heartbeat都是使用单播的方式进行配置,但是网上也有传多播比较稳定的说法,这里先不去管那种方式更有效,先来实验一下多播的配置。
多播的配置只需要修改ha.cf中的几个参数,下面是多播的配置:
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility local1
keepalive 2
deadtime 30
warntime 10
initdead 60
udpport 694
mcast eth1 225.0.0.1 694 1 0
auto_failback on
node heartbeat01.contoso.com
node heartbeat02.contoso.com
respawn hacluster /usr/lib64/heartbeat/ipfail
作为比较,下面是之前单播的配置:
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility local1
keepalive 2
deadtime 30
warntime 10
initdead 60
ucast eth1 172.16.49.133
auto_failback on
node heartbeat01.contoso.com
node heartbeat02.contoso.com
ping 172.16.49.1
respawn hacluster /usr/lib64/heartbeat/ipfail
区别:
1)多播两个节点的ha.cf文件内容是一模一样的。
2)单播两个节点的ha.cf文件中只有ucast的地址不一样(对端IP地址)。
3)单播不能指定udpport,不然会报错,而多播要指定udpport。
4)如果环境中有多个heartbeat集群,则集群中多播的地址不能一模一样,容易出现问题;单播不受集群数量的限制,每个集群中的单个节点只需要指定对端的IP地址即可。
下面是一个安装heartbeat的shell脚本(仅供参考):
#!/bin/bash ############################################## #Description:This script is used to install #hearbeat on CentOS 6.x. #Usage: run the script, and it will ask you #to input some information about the heartbeat #nodes. #Author: Jerry Zhao QQ:1217406852 #Date: 2016-09-23 14:43:40 Friday ############################################## stop_iptables(){ /etc/init.d/iptables status >/dev/null 2>&1 if [ $? -eq 0 ];then /etc/init.d/iptables stop >/dev/null chkconfig iptables off >/dev/null echo "Stop iptables successful." else echo "Iptables firewall was stopped." fi selinux_status=`getenforce` if [ "$selinux_status" != "Disabled" ];then sed -i ‘/^SELINUX/s/enforcing/disabled/‘ /etc/selinux/config setenforce 0 2>/dev/null echo "Disable selinux successful." else echo "SELinux was disabled." fi } time_sync(){ crond_num=`crontab -l|grep ntpdate|wc -l` if [ $crond_num -ge 1 ];then echo "Already added crond job for time sync." else echo ‘0 * * * * /usr/sbin/ntpdate 210.72.145.44 64.147.116.229 time.nist.gov‘ >>/var/spool/cron/root grep ntpdate /var/spool/cron/root >/dev/null [ $? -ne 0 ] && echo "Add crond job for time sync failed." && exit 1 || echo "Add crond job for time sync successful." fi } set_hostname(){ sed -i ‘/^HOSTNAME/s/^/#/‘ /etc/sysconfig/network sed -i "/#HOSTNAME/aHOSTNAME=$NodeHostName1" /etc/sysconfig/network sed -i ‘/^#HOSTNAME/d‘ /etc/sysconfig/network hostname "$NodeHostName1" Uname=`uname -n` if [ "$NodeHostName1" == "$Uname" ];then echo "Change hostname successful." break; else echo "Change hostname failed." break; fi } change_hosts(){ cp /etc/hosts /etc/hosts.bak$(date +%F) echo -e "127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4\n::1 localhost localhost.localdomain localhost6 localhost6.localdomain6" >/etc/hosts echo -e "$localip $LocalHostName\n$ip $HostName" >>/etc/hosts Host_Num=`grep -v "localhost" /etc/hosts |wc -l` [ $Host_Num -lt 2 ] && echo "Change hosts failed, please add hosts manaully." && exit 1 || echo "Change hosts successful." } hbt_install(){ rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm 1>/dev/null 2>/dev/null rpm -qa epel-release >/dev/null [ $? -ne 0 ] && echo "Install epel failed." && exit 1 yum -y install heartbeat >/dev/null [ $? -ne 0 ] && echo "Install heartbeat failed." && exit 1 || echo "Install heartbeat successful." } hbt_config(){ cp /usr/share/doc/heartbeat-3.0.4/{ha.cf,haresources,authkeys} /etc/ha.d/ cd /etc/ha.d/ #config authkeys #Str_SHA=`openssl rand -hex 8` #会导致两节点上的sha1不同,手动操作可以用来生成随机字符串 Str_SHA=55c91e0592851a27 sed -i ‘/^#auth/aauth 2‘ authkeys >/dev/null sed -i "/^#2 sha1/a2 sha1 $Str_SHA" authkeys >/dev/null chmod 600 authkeys echo "Config authkeys successful." #config ha.cf sed -i ‘/^#debugfile/s/^#//‘ ha.cf sed -i ‘/^#logfile/s/^#//‘ ha.cf sed -i ‘/^logfacility/s/local0/local1/‘ ha.cf sed -i ‘/#keepalive/s/^#//‘ ha.cf sed -i ‘/#deadtime/s/^#//‘ ha.cf sed -i ‘/#warntime/s/^#//‘ ha.cf sed -i ‘/#initdead/ainitdead 60‘ ha.cf sed -i "/#mcast/amcast eth$InterFaceNumber 225.0.0.$d 694 1 0" ha.cf sed -i ‘/#udpport/s/^#//‘ ha.cf sed -i "/kathy$/anode$LocalHostName\nnode $HostName" ha.cf sed -i ‘/^#respawn hacluster/arespawn hacluster /usr/lib64/heartbeat/ipfail‘ ha.cf echo "Config ha.cf successful." #config haresources echo "$LocalHostName IPaddr::$VIP/24/eth$InterFaceNumber" >>haresources echo "Config haresources successful." /etc/init.d/heartbeat start >/dev/null [ $? -ne 0 ] && echo "Start heartbeat failed." && exit 1 || echo "Start heartbeat successful." } check_ip(){ read -p "Please enter your ip: " ip if [ -z $ip ];then #输入不能为空 echo "Enter not null." elif [[ $ip =~ ^[1-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[1-9][0-9]{0,2}$ ]];then #输入的不是数字或不是IP格式,则重新输入 #^$:从开始到结束是数字才满足条件,=~:一个操作符,表示左边是否满足右边(作为一个模式)正则表达式 a=`echo $ip |cut -d. -f1` b=`echo $ip |cut -d. -f2` c=`echo $ip |cut -d. -f3` d=`echo $ip |cut -d. -f4` if [ $a -le 255 -a $b -le 255 -a $c -le 255 -a $d -le 255 ];then #当满足输入条件时,截取IP四段数字进行整数比较,判断四段数字是否小于或等于255,同时满足条件,跳出所有循环继续,如果其中一个不满足,则重新输入 echo "The IP address of another heartbeat node is [$ip]." break; else echo "IP format error,Please enter the IP again." fi else echo "IP format error,Please enter the IP again." fi } check_local_ip(){ read -p "Please enter your ip: " localip if [ -z $localip ];then #输入不能为空 echo "Enter not null." return 1 elif [[ $localip =~ ^[1-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[1-9][0-9]{0,2}$ ]];then #输入的不是数字或不是IP格式,则重新输入 #^$:从开始到结束是数字才满足条件,=~:一个操作符,表示左边是否满足右边(作为一个模式)正则表达式 a=`echo $localip |cut -d. -f1` b=`echo $localip |cut -d. -f2` c=`echo $localip |cut -d. -f3` d=`echo $localip |cut -d. -f4` if [ $a -le 255 -a $b -le 255 -a $c -le 255 -a $d -le 255 ];then #当满足输入条件时,截取IP四段数字进行整数比较,判断四段数字是否小于或等于255,同时满足条件,跳出所有循环继续,如果其中一个不满足,则重新输入 Eth_Num=`ifconfig|grep eth|wc -l` for ((i=0;i<$Eth_Num;i++)) do LIP=`ifconfig eth$i|grep "inet addr"|awk -F" " ‘{print $2}‘|cut -d: -f2` if [ "$localip" == "$LIP" ];then echo "$localip is on eth$i." return 0 break; fi echo "The ip address [$localip] you input is not on eth$i." return 1 done else echo "IP format error,Please enter the IP again." return 1 fi else echo "IP format error,Please enter the IP again." return 1 fi } check_vip(){ read -p "Please enter your VIP: " VIP if [ -z $VIP ];then #输入不能为空 echo "Enter not null." elif [[ $VIP =~ ^[1-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[1-9][0-9]{0,2}$ ]];then #输入的不是数字或不是IP格式,则重新输入 #^$:从开始到结束是数字才满足条件,=~:一个操作符,表示左边是否满足右边(作为一个模式)正则表达式 a=`echo $VIP |cut -d. -f1` b=`echo $VIP |cut -d. -f2` c=`echo $VIP |cut -d. -f3` d=`echo $VIP |cut -d. -f4` if [ $a -le 255 -a $b -le 255 -a $c -le 255 -a $d -le 255 ];then #当满足输入条件时,截取IP四段数字进行整数比较,判断四段数字是否小于或等于255,同时满足条件,跳出所有循环继续,如果其中一个不满足,则重新输入 echo "The VIP address is [$VIP]." break; else echo "IP format error,Please enter the VIP again." fi else echo "IP format error,Please enter the VIP again." fi } check_ethnum(){ read -p "Please enter the interface number: " InterFaceNumber if [ -z $InterFaceNumber ];then echo "Interface number can‘t be null." elif [[ $InterFaceNumber =~ ^[0-9]$ ]];then if [ $InterFaceNumber -lt $Eth_Num ];then echo "VIP will be run on eth$InterFaceNumber." break; else echo "Can‘t find eth$InterFaceNumber." fi else echo "Invalid interface number." fi } check_hostname(){ read -p "Please enter your hostname : " HostName if [ "$HostName" != "" ];then echo "The hostname you typed is $HostName." break; else echo "Hostname can‘t be null." fi } change_local_hostname(){ read -p "Please input your hostname : " LocalHostName if [ "$LocalHostName" != "" ];then sed -i ‘/^HOSTNAME/s/^/#/‘ /etc/sysconfig/network sed -i "/#HOSTNAME/aHOSTNAME=$LocalHostName" /etc/sysconfig/network sed -i ‘/^#HOSTNAME/d‘ /etc/sysconfig/network hostname "$LocalHostName" Uname=`uname -n` if [ "$LocalHostName" == "$Uname" ];then echo "Change hostname successful." break; else echo "Change hostname failed." exit 1; fi fi } #检查用户输入的本地IP地址 while true;do echo -e "You need to input your local \033[1;32mIP address\033[0m on \033[1;32mthis server\033[0m." check_local_ip if [ $? -eq 0 ];then break; fi done #检查用户输入的另外一个节点的IP地址 while true;do echo -e "You need to input the \033[1;32mIP address\033[0m of \033[1;32manother heartbeat node\033[0m." check_ip done #检查用户输入的运行脚本的这台主机的主机名 while true;do echo -e "You need to input the \033[1;32mhostname\033[0m for \033[1;32mthis server\033[0m." change_local_hostname done #检查用户输入的另外一个节点的主机名 while true;do echo -e "You need to input the \033[1;32mhostname\033[0m of \033[1;32manother heartbeat node\033[0m." check_hostname done #检查用户输入的VIP地址 while true;do echo -e "You need to input the \033[1;32mVIP address\033[0m." check_vip done #检查绑定VIP的网卡 while true;do echo -e "You need determine \033[1;32mwhich interface\033[0m to \033[1;32mrun the VIP\033[0m." check_ethnum done stop_iptables time_sync change_hosts hbt_install hbt_config
本文出自 “IT小二郎” 博客,请务必保留此出处http://jerry12356.blog.51cto.com/4308715/1855926
Heartbeat配置多播方式以及heartbeat安装脚本