环境:
主机(master):192.168.11.160
主机(slave):192.168.11.20
流动 ip:192.168.11.100
1. 关闭主从机器的防火墙,配置文件/etc/selinux/config,修改SELINUX=disabled,iptables -F
2. 编辑主从机器的hosts文件/etc/hosts,修改hostname,便于标记
192.168.11.160 master
192.168.11.20 slave
3. 主从机器上yum安装heartbeat和依赖包libnet
说明:默认yum没有heartbeat,需下载安装扩展源,链接如下:
命令 yum install -y heartbeat libnet
主从配置
1. master上拷贝heartbeat的配置文件
说明:authkey文件,主从通信验证作用
命令 cd /usr/share/doc/heartbeat-3.0.4/
命令 cp authkeys ha.cf haresources /etc/ha.d/
2. 编辑master文件/etc/ha.d/authkeys,并赋予600权限
说明:启用认证;加密级别为3(md5),加密级别,1<3<2
内容:
<code> </code><code>auth 3</code>
<code> </code><code>3 md5 Hello!</code>
授权:
<code> </code><code>chmod</code> <code>600 </code><code>/etc/ha</code><code>.d</code><code>/authkeys</code>
3. 编辑master上/etc/ha.d/haresources文件
说明:主的主机名;虚拟ip,即主从共用的对外ip,当主ip挂了,流动ip自动流动到从主
机上,并对外开放;主从服务对象为nginx,主宕机了,切换为从上
内容:
<code> </code><code>master 192.168.11.110</code><code>/24/eth0</code><code>:0 nginx</code>
一网卡配置多个ip:复制/etc/sysconfig/network-scripts/ifcfg-eth0,为ifcfg-eth0:\1,注释掉mac和uuid,重启添加一个新的ip即可
4. 编辑master上/etc/ha.d/vim ha.cf文件 ,清空配置,自定义编辑
<code> </code><code>debugfile </code><code>/var/log/ha-debug</code>
<code> </code><code>logfile </code><code>/var/log/ha-log</code>
<code> </code><code>logfacility local0</code>
<code> </code><code>keepalive 2</code>
<code> </code><code>deadtime 30</code>
<code> </code><code>warntime 10</code>
<code> </code><code>initdead 60</code>
<code> </code><code>udpport 694</code>
<code> </code><code>ucast eth0 192.168.11.20</code>
<code> </code><code>auto_failback on</code>
<code> </code><code>node master</code>
<code> </code><code>node slave</code>
<code> </code><code>ping</code> <code>192.168.11.1</code>
<code> </code><code>respawn hacluster </code><code>/usr/lib/heartbeat/ipfail</code>
解释:
debugfile /var/log/ha-debug # debug文件
logfile /var/log/ha-log #日志文件
logfacility local0 #日志级别
keepalive 2 #2秒探测一次
deadtime 30 #30秒探测不到,认为死掉
warntime 10 #10秒探测不到,告警至日志文件中
initdead 60 #为对方服务器重启预留60秒时间
udpport 694 #心跳线通信端口为694
ucast eth0 192.168.11.20 #配置对方的ip,也可以使用bcast广播形式,去搜索ip,这里直接指
#定ip(实验中心跳网卡和主从网卡共用,不怎么安全,实际线上,最好
#给心跳独立网卡或用串口通信)
auto_failback on #当主恢复,从自动下线
node master #两个节点
node slave #两个节点
ping 192.168.11.1 #仲裁ip,仲裁主从谁在线
respawn hacluster /usr/lib/heartbeat/ipfail #以hacluster身份去运行脚本后面的脚本,该脚本用户
#检测网络连通性,heartbeat自带的断网切换的工具-ipfail,
#64位系统位置/usr/lib64/heartbeat/ipfail
5. 将主上编辑好的三个配置文件拷贝至从上
命令 cd /etc/ha.d/
命令 scp authkeys haresources ha.cf slave:/etc/ha.d/
6. 修改slave上的配置文件/etc/ha.d/ha.cf
说明:修改为主的ip
命令 vim /etc/ha.d/ha.cf
内容:
<code> </code><code>ucast eth0 192.168.11.160</code>
7. master和slave上安装nginx
命令 yum install -y nginx
8. 依次启动master、slave上的heartbeat
说明:nginx 无需手动启动;先启动主上的heartbeat,再启动从上heartbeat,等一段时间,主
上虚拟ip自动开启,nginx也被拉起,从上nginx不会立即起来
命令 /etc/init.d/heartbeat start
ip addr; ifconfig ; ps aux |grep nginx
9. master、slave上修改nginx首页
说明:先浏览配置文件cat /etc/nginx/nginx.conf-->找到"include /etc/nginx/conf.d/*.conf;"- ->cd /etc/nginx/conf.d/ -->cat default.conf-->找到“root /usr/share/nginx/html”,重定向首页文件
主:echo "11111111111master" > /usr/share/nginx/html/index.html
从:echo "22222222222222slave" > /usr/share/nginx/html/index.html
10. 测试
a. master上器上插入一条iptbales防火墙规则,禁用ping
iptables -A INPUT -p icmp -j DROP
结果:主、从上查看日志tail -f /var/log/ha-log,发现主heartbeat挂掉;从heartbeat起来接管,并拉起nginx
b. master上器上去掉icmp规则,重新开启ping
iptbales -D INPUT -p icmp -j DROP
结果: 从上释放掉了流动ip,主自动启用,网页重新回到主机器上主机器上;停掉heartbeat, /etc/init.d/heartbeat stop,也会自动切换到从机器
本文转自 huangzp168 51CTO博客,原文链接:http://blog.51cto.com/huangzp/1885185,如需转载请自行联系原作者