设为首页 收藏本站
查看: 1267|回复: 0

[经验分享] Heartbeat配置多播方式以及heartbeat安装脚本

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-9-26 10:36:11 | 显示全部楼层 |阅读模式
之前部署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脚本(仅供参考):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/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/ ... ease-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



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-277654-1-1.html 上篇帖子: HA Cluster基础及heartbeat实现HA 下篇帖子: Heartbeat+Drbd实现
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表