首页 > 代码库 > heartbeat 配置两台web服务器高可用集群案例
heartbeat 配置两台web服务器高可用集群案例
通过heartbeat 配置两台web服务器高可用集群案例:
拓扑:
web1:192.168.0.101
hostname: web1.example.com
web2:192.168.0.102
hostname:web2.example.com
vip:192.168.0.254
nfs服务器:192.168.0.100(存放web页面)
注意:
1主机名要与uname -n 保持一致
2.web1与web2主机的date时间保持同步
3.web1与web2,ssh双机互信通信
4.关闭iptables 与selinux
环境配置:
1配置主机名
web1:
#vim /etc/sysconfig/network
HOSTNAME=web1.example.com
#hostname web1.example.com
web2:
#vim /etc/sysconfig/network
HOSTNAME=web2.example.com
#hostname web2.example.com
2.配置hosts文件,主机名解析
web1与web2:
#vim /etc/hosts
192.168.0.101 web1.example.com web1
192.168.0.102 web2.example.com web2
3.实现web1与web2 ssh双机互信通信
web1:
#ssh-keygen -t rsa -f "~/.ssh/id_rsa -P ""
#ssh-copy-id i .ssh/id_rsa.pub root@192.168.0.102
web2:
#ssh-keygen -t rsa -f "~/.ssh/id_rsa -P ""
#ssh-copy-id i .ssh/id_rsa.pub root@192.168.0.101
4.时间同步设置
web1 与web2 中设置
#hwclock -s
5.安装web服务器
web1
#yum -y install httpd
#chkconfig httpd off
#echo ‘web1‘ > /var/www/html/index.html (测试)
#service httpd restart
web2
#yum -y install httpd
#chkconfig httpd off
#echo ‘web2‘ > /var/www/html/index.html (测试)
#service httpd restart
6.配置nfs服务器(192.168.0.100)
#mkdir /webfile
#cd /webfile;echo "nfs" >index.html
#vim /etc/exports
/webfile 192.168.0.0/24(ro,sync)
#service nfs start
#showmount -e 192.168.0.100
二:安装heartbeat
1.安装 heartbeat 相关软件包,有依赖关系,推荐使用yum安装
heartbeat-2.1.4-11.el5.i386.rpm
heartbeat-pils-2.1.4-11.el5.i386.rpm
heartbeat-stonith-2.1.4-11.el5.i386.rpm
heartbeat-gui-2.1.4-11.el5.i386.rpm
额外依赖包:libnet-1.1.4-3.el5.i386.rpm perl-MailTools-1.77-1.el5.noarch.rpm
#wget http://dl.fedoraproject.org/pub/epel/5/i386/heartbeat-2.1.4-11.el5.i386.rpm
#wget http://dl.fedoraproject.org/pub/epel/5/i386/heartbeat-gui-2.1.4-11.el5.i386.rpm
#wget http://dl.fedoraproject.org/pub/epel/5/i386/heartbeat-stonith-2.1.4-11.el5.i386.rpm
#wget http://dl.fedoraproject.org/pub/epel/5/i386/heartbeat-pils-2.1.4-11.el5.i386.rpm
#wget http://dl.fedoraproject.org/pub/epel/5/i386/libnet-1.1.6-7.el5.i386.rpm
#wget http://dl.fedoraproject.org/pub/epel/5/i386/perl-MailTools-1.77-1.el5.noarch.rpm
# yum -y localinstall --nogpgcheck *.rpm
2.拷贝配置文件
#cp /usr/share/doc/heartbeat-2.1.4/{haresources,ha.cf authkeys} /etc/had.d/
#cd /etc/had.d/
#chmod 600 authkeys (权限必须为600,否则会出现问题)
3.编辑配置文件
(1)#vim /etc/ha.d/ha.cf
bcast eth0
node web1.example.com
node web2.example.com
(2)身份验证
#dd if=/dev/random count=1 bs=512 |md5sum
#vim authkeys
auth 3
3 md5 ec92b27792a008b420ec5adfe0a34ea0 //为刚才借助dd产生的随机md5加密值
(3)配置资源
#vim haresources
web1.example.com 192.168.0.254/24/eth0 httpd
//主节点为web1.example.com vip:192.168.0.254 高可用的服务为web三个资源作为一组
复制配置文件到web2中
scp /etc/ha.d/{haresources,authkeys,ha.cf} root@web2:/etc/ha.d
4.web1 和web2中启动heartbeat
#service heartbeat start
#ssh web2 ‘service heartbeat start’
5.测试heartbeat web高可用
web1作为主模式测试正常,测试web服务器的运行状态
#elinks -dump http://192.168.0.254
web1
现在将web1通过命令实现成为备份模式,来测试web2的工作情况:
web1上执行:
#/usr/share/heartbeat/hb_standby
2012/12/28_19:43:49 Going standby [all].
#elinks -dump http://192.168.0.254
web2
通过测试说明当web1作为主模式时,集群资源在web1服务上;
当web2作为主模式时,集群资源在web2服务上
6.重新配置资源实现web1与web2共享同一个web页面
web1
#vim /etc/ha.d/haresources
web1.example.com 192.168.0.254/24/eth0 Filesystems::192.168.0.100:/webfile::/var/www/html httpd
#scp /etc/ha.d/haresources root@web2:/etc/ha.d/
#service heartbeat restart
#ssh web2 ‘service heartbeat restart‘
7再重新进行第五步测试,查看结果
web1作为主模式测试正常,测试web服务器的运行状态
#elinks -dump http://192.168.0.254
nfs
现在将web1通过命令实现成为备份模式,来测试web2的工作情况:
web1上执行:
#/usr/share/heartbeat/hb_standby
2012/12/28_19:43:49 Going standby [all].
#elinks -dump http://192.168.0.254
nfs
通过测试说明当web1与web2其中一个节点故障,不影响web的访问,实现了高可用