//硬件准备:
1.两个机器,相同系统
2.网卡ip为:aming 192.168.11.24
aming1 192.168.11.23
//实验准备:
1. hostname : aming , aming1
2. 关闭防火墙 : iptables -F
setenforce 0
3. 更改hosts : vi /etc/hosts
192.168.11.24 aming
192.168.11.23 aming1
4. 安装epel扩展源: rpm -ivh 'http://www.lishiming.net/data/attachment/forum/epel-release-6-8_32.noarch.rpm'
5. 主从都安装heartbeat / libnet / nginx
yum install -y heartbeat* libnet nginx
//实验时需要的
一、 两个机器的网卡需要相同 ,不管是eth0还是eth1。但如果是克隆出来的虚拟机,则网卡一个为eth0,一个为eth1.所以需要设置
在eth1的虚拟机中做下列的步骤
1、在/etc/grub.conf 里增加biosdevname=0的启动参数,形如
kernel /vmlinuz-2.6.32-131.21.1.el6.i686 ro root=/dev/mapper/vg_test-lv_root rd_LVM_LV=vg_test/lv_root rd_LVM_LV=vg_test/lv_swap rd_NO_LUKS rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latar
--将此行复制在上行的下方
cyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto rhgb quiet biosdevname=0
2、删除udev的配置文件rm -f /etc/udev/rules.d/70-persistent-net.rules
3、把网卡配置文件改名
mv ifcfg-em1 ifcfg-eth0
4、把网卡配置文件内容修正,把em1的全部改成eth0
可以使用 perl -p -i -e 's/em1/eth0/g' ifcfg-eth0 或
sed -i 's/em1/eth0/g' ifcfg-eth0 进行替换。
5、重启系统即可
二、两个机器之间 拷贝文件 scp
安装:
--> yum install openssh-clients
主 和从都需要安装
//主上配置
[root@localhost ~]# cd /usr/share/doc/heartbeat-3.0.4/
[root@localhost /heartbeat-3.0.4]# cp authkeys ha.cf haresources /etc/ha.d/
[root@localhost /heartbeat-3.0.4]# cd /etc/ha.d
[root@localhost ha.d]# vi authkeys
//此配置文件 是主从进行通信的 要时时检测是否有当掉的迹象
//文件最尾处
将
#auth 1 //1 ,2 ,3表示通信的加密程度
#1 crc {最简单}
#2 sha1 HI! {最复杂}
#3 md5 Hello! {第二简单}
改为
auth 3 //选择中间值
#1 crc
#2 sha1 HI!
3 md5 Hello!
[root@localhost ha.d]# chmod 600 authkeys
[root@localhost ha.d]# vi haresources
//这里如果做了nfs共享存储 也可以配置在这里
//加入
aming 192.168.11.10/24/eth1:0 nginx
/*第一段 master的主机名*/
/*第二段 流动ip,实验时用同网段的*/
/*eth1:0 这个根据主机的网卡设置,主机为eth0则设置为eth0:n,n自定义*/
/*nginx 为哪一个提供服务 意义为如果主的nginx服务down掉以后,从会启动继续提供服务*/
<--流动ip--->
主和从都有自己的 ip ,但当主宕机时 ,主的ip不能跑到从去
即vip ,主运行时,流动ip在主,从运行时,则流动ip在从。
<-->
[root@localhost ~]# vi ha.cf
//改为
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 30
warntime 10
initdead 60
udpport 694
ucast eth1 192.168.11.23
auto_failback on
node aming
node aming1
ping 192.168.11.1
respawn hacluster /usr/lib/heartbeat/ipfail
//将主上配置文件拷贝到从上
[root@localhost ~]# cd /etc/ha.d/
[root@localhost ha.d]# scp authkeys ha.cf haresources aming1:/etc/ha.d/
//在从上编辑
[root@localhost ~]# vi /etc/ha.d/ha.cf
//只需要改一个地方,将此处的slave的ip改为master的ip
ucast eth1 192.168.11.23 改为 ucast eth1 192.168.11.24
//启动 heartbeat
//先主,后从
[root@localhost ~]# service heartbeat start
//检查测试
ifconfig 看是否有 eth1:0 / ip add 命令看是否多了 eth1:0
ps aux |grep nginx 看是否有nginx进程
//测试
1.主上对nginx的页面进行设置
echo "1111master" > /usr/share/nginx/html/index.html
去浏览器访问 http://192.168.11.10{流动ip}/ 页面会显示 1111master
从上也设置 echo "2222slave" > /usr/share/nginx/html/index.html
设置规则 iptables -A INPUT -p icmp -j DROP
查看heartbeat日志 tail /var/log/ha-log tail -f /var/log/ha-log
看到master停掉 ,转给slave运行
再打开浏览器 页面变成了 2222slave
则说明设置成功
2.主上故意禁ping
iptables -I INPUT -p icmp -j DROP
--主上的当掉了,从上还可以用