搭建heartbeat+lvs+Ldirectord

更新时间:2024-04-13 13:43:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

搭建heartbeat 环境

Xuegod62 nfs是服务器,实现网站目录共享

Xuegod63 heartbeat主,实现自动挂载nfs共享存储,启动httpd服务 Xuegod64 heartbeat从, 实现自动挂载nfs共享存储,启动httpd服务

服务资源 服务器名 Xuegod63 Xuegod64 Xuegod62

搭建nfs服务器

[root@xuegod62 ~]# hostname xuegod62.cn

[root@xuegod62 ~]# rpm -qf /etc/init.d/nfs nfs-utils-1.2.3-15.el6.x86_64 [root@xuegod62 ~]# mkdir /www

[root@xuegod62 ~]# echo \

IP地址 192.168.1.63 192.168.1.64 192.168.1.62 VIP 192.168.1.200 192.168.1.200 [root@xuegod62 ~]# vim /etc/exports [root@xuegod62 /]# cat /etc/exports /www 192.168.1.0/24(rw)

配置xuegod63服务器 查看xuegod62共享文件

[root@xuegod63 /]# showmount -e 192.168.1.62 Export list for 192.168.1.62: /www 192.168.1.0/24

挂载xuegod62共享网页并测试

[root@xuegod63 /]# mount 192.168.1.62:/www /var/www/html/ [root@xuegod63 /]# yun install elinks –y [root@xuegod63 /]# service httpd start [root@xuegod63 /]# elinks --dump 192.168.1.63 heartbeat http ha

停止httpd服务,卸载/var/www/html目录,让heartbeat启动自动挂载 [root@xuegod63 /]# umount /var/www/html/ [root@xuegod63 /]# service httpd stop Stopping [ OK ]

httpd:

Xuegod63上安装heartbeat 配置yum源

[root@xuegod63 yum.repos.d]# vim rhel-source.repo [root@xuegod63 yum.repos.d]# cat rhel-source.repo [rhel-source]

name=Red Hat Enterprise Linux $releasever - $basearch - Source baseurl=file:///mnt/ enabled=1 gpgcheck=0

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release [rhel-ha] name=Red ha

baseurl=file:///mnt/HighAvailability enabled=1 gpgcheck=0

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

centos 6.2的源里 cluster-glue、resource-agents、pacemaker 都有,就是没有heartbeat。 幸好heartbeat的source里提供了.spec 可以用rpmbuild来生成rpm包。

首先把下载下来的heartbeat源码包解压一仹,然后再把源码包(.tar.gz)复制到 /root/rpmbuild/SOURCES/heartbeat.tar.bz2 上传Heartbeat-3-0-7e3a82377fa8.tar.bz2到xuegod63上 [root@xuegod63 ~]# tar -jxvf Heartbeat-3-0-958e11be8686.tar.bz2 [root@xuegod63 ~]# cd Heartbeat-3-0-958e11be8686 [root@xuegod63

Heartbeat-3-0-958e11be8686]#

rpmbuild

-ba

heartbeat-fedora.spec #虽然报了错,但是帮你生成了目录:/root/rpmbuild/SOURCES/ 。如你系统本身就有此目录,可以直接拷贝这个压缩包到这个目录

error: File /root/rpmbuild/SOURCES/heartbeat.tar.bz2: No such file or directory [root@xuegod63

Heartbeat-3-0-958e11be8686]#

tar

-jxvf -C

/root/Heartbeat-3-0-958e11be8686.tar.bz2 /root/rpmbuild/SOURCES/ [root@xuegod63

Heartbeat-3-0-958e11be8686]#

cd

/root/rpmbuild/SOURCES/ [root@xuegod63 heartbeat

[root@xuegod63 SOURCES]# tar -jcvf heartbeat.tar.bz2 heartbeat #生成heartbeat.tar.bz2软件包,如果直接使用源码包,生成rpm包时,会报错。 如:

[root@xuegod63 SOURCES]# cd /root/rpmbuild/SOURCES/heartbeat/

SOURCES]#

mv

Heartbeat-3-0-958e11be8686

[root@xuegod63 heartbeat]# rpmbuild -ba heartbeat-fedora.spec error: File /root/rpmbuild/SOURCES/heartbeat.tar.bz2: No such file or directory # 解决依赖

[root@xuegod63 SOURCES]# yum install ncurses-devel openssl-devel gettext bison flex mailx cluster-glue-libs-devel docbook-dtds docbook-style-xsl libtool-ltdl-devel libuuid-devel -y

# 开始生成。 (-bb 只编译二进制rpm包 -bs 只编译源码srpm包 -ba 同时编译二进制和源码rpm包) [root@xuegod63 SOURCES]# pwd /root/rpmbuild/SOURCES

[root@xuegod63 SOURCES]# cd heartbeat

[root@xuegod63 heartbeat]# rpmbuild -ba heartbeat-fedora.spec [root@xuegod63 heartbeat]# ls /root/rpmbuild/RPMS/x86_64/ #查看编译好的rpm包

heartbeat-3.0.6-1.el6.x86_64.rpm

heartbeat-debuginfo-3.0.6-1.el6.x86_64.rpm heartbeat-devel-3.0.6-1.el6.x86_64.rpm heartbeat-libs-3.0.6-1.el6.x86_64.rpm

[root@xuegod63 heartbeat]# ls /root/rpmbuild/SRPMS/ #查看编译好的源码rpm包

heartbeat-3.0.6-1.el6.src.rpm

在xuegod63和xuegod64上都安装:heartbeat [root@xuegod63 ~]# cd /root/rpmbuild/RPMS/x86_64/

[root@xuegod63 x86_64]# yum install -y cluster-glue resource-agents [root@xuegod63

x86_64]#

rpm

-ivh

heartbeat-libs-3.0.6-1.el6.x86_64.rpm

[root@xuegod63 x86_64]# rpm -ivh heartbeat-3.0.6-1.el6.x86_64.rpm 把软件包和yum配置文件拷贝到xuegod64上: [root@xuegod63 ~]#cd /root/rpmbuild/RPMS/x86_64/ [root@xuegod63 ~]# scp -r ./* 192.168.1.64:/root/ [root@xuegod63

~]#

scp

/etc/yum.repos.d/rhel-source.repo

192.168.1.64:/etc/yum.repos.d/ xuegod64配置:heartbeat

[root@xuegod64 ~]# yum install ncurses-devel openssl-devel gettext bison flex mailx cluster-glue-libs-devel docbook-dtds docbook-style-xsl -y

[root@xuegod64 ~]# rpm -ivh heartbeat-libs-3.0.5-1.el6.x86_64.rpm [root@xuegod64 ~]# yum install -y cluster-glue resource-agents [root@xuegod64 ~]# rpm -ivh heartbeat-3.0.5-1.el6.x86_64.rpm 说明:

如果你的机器事先装过RHCM的软件来实现HA集群了,再装heartbesat时,会出现软件包冲突的情冴,因为同样的

HA软件,底层所用的程序都一样。需要手劢删除如下包,来解决冲

突问题:

# yum -y remove openais corosync cman rgmanager # yum -y remove openais corosync heartbeat 查看生成的用户和组:

[root@xuegod63 ha.d]# grep haclient /etc/group haclient:x:489:

[root@xuegod63 ha.d]# id hacluster

uid=495(hacluster) gid=489(haclient) groups=489(haclient) 配置heartbeat :xuegod63

在xuegod63上配置好3个配置文件,然后拷给xuegod64即可。 生成配置文件:

[root@xuegod63 x86_64]# cp /usr/share/doc/heartbeat-3.0.6/ha.cf /etc/ha.d/ #主配置文件

[root@xuegod63 ~]# cp /usr/share/doc/heartbeat-3.0.6/authkeys /etc/ha.d/ #主备节点间通信时所使用的验证文件,保证安全性 [root@xuegod63 ~]# cp /usr/share/doc/heartbeat-3.0.6/haresources /etc/ha.d/ #浮动资源的配置文件 开始配置:

A.设置主备节点间通信时所使用的验证文件,保证安全性。主备节点配置要求一致。

authkeys文件决定了认证密钥。共有三种认证方式:crc,md5,和

sha1。 如果Heartbeat运行亍安全网络上,如本例中的交叉线,可以使用crc,从资源的角度来看,这是代价最低的方法。如果网络并安全,但也希望降低CPU使用,则使用md5。最后,如果想得到最好的认证,而不考虑CPU使用情冴,则使用sha1,它在三者中最难破解。

[root@xuegod63 ~]# vim /etc/ha.d/authkeys auth 3 #1 crc #2 sha1 HI! 3 md5 mkkey

[root@xuegod63 ~]# chmod 600 /etc/ha.d/authkeys #此文件权限必须是600,否则启动不成功

B.配置浮动资源,注意添加的顺序,::表示分隔符 [root@xuegod63 ~]#vim /etc/ha.d/haresources 改:

44 #node-name resource1 resource2 ... resourceN 为: xuegod63.cn

IPaddr::192.168.1.200/24/eth0

Filesystem::192.168.1.62:/wwwdir::/var/www/html::nfs httpd 注:node-name填的是主服务器的主机名。 xuegod64上不需要修改。这样资源默认会加一这个主机上。当xuegod63坏了,xuegod64会再接管。

测试: 加载VIP 192.168.1.200 [root@xuegod63

~]#

/etc/ha.d/resource.d/IPaddr

192.168.1.200/24/eth0 start

IPaddr[7142]: INFO: Using calculated netmask for 192.168.1.200: 255.255.255.0

IPaddr[7142]: DEBUG: Using calculated broadcast for 192.168.1.200: 192.168.1.255

IPaddr[7142]: INFO: eval ifconfig eth0:0 192.168.1.200 netmask 255.255.255.0 broadcast 192.168.1.255

IPaddr[7142]: DEBUG: Sending Gratuitous Arp for 192.168.1.200 on eth0:0 [eth0]

ARPING 192.168.1.200 from 192.168.1.200 eth0 IPaddr[7116]: INFO: Success INFO: Success 查看 VIP:

[root@xuegod63 x86_64]# ifconfig

#可以看到 eth0:0 的IP为:192.168.1.200 测试搭建NFS存储 [root@xuegod63

~]#

/etc/ha.d/resource.d/Filesystem

192.168.1.62:/wwwdir /var/www/html/ nfs start

Filesystem[23575]: INFO: Running start for 192.168.1.62:/wwwdir on /var/www/html

Filesystem[23567]: INFO: Success INFO: Success

[root@xuegod63 ~]# ls /var/www/html/index.html /var/www/html/index.html [root@xuegod63 ~]# mount 。。。

192.168.1.62:/wwwdir

on

/var/www/html

type

nfs

(rw,vers=4,addr=192.168.1.62,clientaddr=192.168.1.63) 启动服务:

[root@xuegod63 ~]# /etc/init.d/httpd restart C.配置主配置文件/etc/ha.d/ha.cf

[root@xuegod63 ~]# grep ^[^#] /etc/ha.d/ha.cf #默认只开启了以下两行内容 logfacility local0 auto_failback on

#auto_failback off 如果改为这个,主结点恢复正常后,资源自动转给主结点。 建议设为auto_failback off ,等主节点恢复正常后,在业务不繁忙时,切换回来。防止主节点恢复正常时,回切时,再次影起网络中断。

[root@node1 ha.d]# vim /etc/ha.d/ha.cf #将配置文件中,以下内存前面的#号去掉

24 debugfile /var/log/ha-debug

29 logfile /var/log/ha-log

48 keepalive 2 #设定heartbeat之间的时间间隔为2秒 56 deadtime 30 #在30秒后宣布节点死亡。

61 warntime 10 #在日志中发出“late heartbeat“警告之前等待的时间,单位为秒。

71 initdead 120 #在某些配置下,重启后网络需要一些时间才能正常工作。这个单独的”deadtime”选项可以处理这种情况。它的取值至少应该为通常deadtime的两倍。

注:在某些系统上,系统启劢戒重启乀后需要经过一段时间网络才能正常工作,该选项用亍解决这种情冴产生的时间间隔。取值至少为deadtime的两倍。)

76 udpport 694 #使用端口694进行bcast和ucast通信。这是默认的,并且在IANA官方注册的端口号。

121 ucast eth0 192.168.1.64 #表示从本机的eth0接口发心跳消息给对方节点,写另一端的IP地址 。 这是单播地址。 xuegod64上改为192.168.1.62 。心跳网卡 ,如果你有两个网卡,可以写成eth1 注:配置文件中91 #bcast eth0 #表示在eth0接口上使用广播heartbeat(将eth1替换为eth0,eth2,或者您使用的任何接口)。 157 auto_failback on #当auto_failback设置为on时,一旦主节点重新恢复联机,将从从节点取回所有资源。若该选项设置为off,主节点便不能重新获得资源。

211 node xuegod63.cn #该选项是必须配置的。集群中机器的主机

名,与“uname –n”的输出相同。 212 node xuegod64.cn 这两行内容大约在此位置: 改:223 #ping 10.10.10.254 为:223 ping 192.168.1.1

改:256 #respawn hacluster /usr/lib/heartbeat/ipfail 为:256 respawn hacluster /usr/libexec/heartbeat/ipfail 改:262 #apiauth ipfail gid=haclient uid=hacluster 为: apiauth ipfail gid=haclient uid=hacluster 到此修改结束,保存,退出。 [root@xuegod63

ha.d]#

scp

ha.cf

haresources

authkeys

192.168.1.64:/etc/ha.d/ xuegod64上配置heartbeat

[root@xuegod64 ~]# chmod 600 /etc/ha.d/authkeys #此文件必须是600否则heartbeat启动不成功 #修改单播地址

[root@xuegod64 ~]# vim /etc/ha.d/ha.cf 改: ucast eth0 192.168.1.64 为: ucast eth0 192.168.1.63 两机器启劢heartbeat服务:

[root@xuegod63 ~]# /etc/init.d/heartbeat restart [root@xuegod64 ~]# /etc/init.d/heartbeat restart

Stopping High-Availability services: [ OK ]

Waiting to allow resource takeover to complete: [ OK ]

#等待资源接管完成。 takeover接管。 当启动停留在这个界面时,xuegod64会接管所有浮动资源。等下面启劢成功时,xuegod64会释放资源,浮于资源,再次加载。

Starting High-Availability services: IPaddr[27857]: INFO: Resource is stopped [ OK ] 查看端口号:

[root@xuegod63 ~]# netstat -antup | grep 694 udp 0 0 0.0.0.0:694 0.0.0.0:* 6895/heartbeat: wri [root@xuegod64 ~]# /etc/init.d/heartbeat restart Stopping High-Availability services: [ OK ]

Waiting to allow resource takeover to complete: [ OK ]

Starting High-Availability services: IPaddr[5811]: INFO: Resource is stopped [ OK ] 查看端口号:

[root@xuegod64 ~]# netstat -antup | grep 694 udp 0 0 0.0.0.0:694 0.0.0.0:* 6895/heartbeat: wri 在xuegod63查看集群资源: [root@xuegod63 ~]# ifconfig

。。。

eth0:0 Link encap:Ethernet HWaddr 00:0C:29:12:EC:1E

inet addr:192.168.1.200 Bcast:192.168.1.255 Mask:255.255.255.0 [root@xuegod63 ~]# df -h 。。。

/dev/sr0 3.4G 3.4G 0 100% /mnt

192.168.1.62:/wwwdir 9.7G 3.4G 5.8G 37% /var/www/html [root@xuegod63 ~]# /etc/init.d/httpd status httpd (pid 23641) is running...

xuegod64上查看,没有仸务浮于资源: [root@xuegod64 ~]# ifconfig [root@xuegod64 ~]# df -h

[root@xuegod63 ~]# /etc/init.d/httpd status httpd is stopped 测试:

两台主机都开启时,所有请求到转到了xuegod63上。访问:http://192.168.1.200/ 正常

当掉xuegod63,关闭网卡,等30秒钟,所有请求都转到xuegod64上了

[root@xuegod63 ha.d]# ifdown eth0 [root@xuegod64 ~]# ifconfig ...

eth0:0 Link encap:Ethernet HWaddr 00:0C:29:48:80:95

inet addr:192.168.1.200 Bcast:192.168.1.255 Mask:255.255.255.0 [root@xuegod64 ~]# df -h ...

192.168.1.62:/wwwdir 9.7G 3.4G 5.8G 37% /var/www/html [root@xuegod64 ~]# service httpd status httpd (pid 6375) is running... xuegod63上把eth0网卡再次开启: [root@xuegod63 ~]# ifup eth0 等几秒后,查看: [root@xuegod63 ~]# df -h ...

192.168.1.62:/wwwdir 9.7G 3.4G 5.8G 37% /var/www/html #已经加载了httpd资源

[root@xuegod63 ~]# service httpd status httpd (pid 27097) is running... [root@xuegod63 ~]# ifconfig 。。。

eth0:0 Link encap:Ethernet HWaddr 00:0C:29:12:EC:1E

inet addr:192.168.1.200 Bcast:192.168.1.255 Mask:255.255.255.0 资源已经回切过来。

在xuegod64上查看释放资料:

[root@xuegod64 ~]# ifconfig #查看丌到eth0:0 192.168.1.200 这个IP地址

[root@xuegod64 ~]# df -h

Filesystem Size Used Avail Use% Mounted on /dev/sda2 9.7G 3.7G 5.5G 41% / tmpfs 569M 0 569M 0% /dev/shm /dev/sda1 194M 28M 157M 15% /boot /dev/sr0 3.4G 3.4G 0 100% /mnt [root@xuegod64 ~]# service httpd status httpd is stopped

到此heartbeat环境搭建完成

heartbeat+ldirectord+lvsDR实现集群高

注意:需在前面环境基础上搭建该环境

xuegod63 192.168.1.63 主LVS分发器 VIP :192.168.1.200 xuegod64 192.168.1.64 从LVS分发器 VIP :192.168.1.200 xuegod62 192.168.1.62 real server web服务器 xuegod61 192.168.1.61 real server web服务器 配置4台服务器hosts

[root@xuegod63 ~]# vim /etc/hosts #写如以下内容 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.1.63 xuegod63.cn 192.168.1.64 xuegod64.cn 192.168.1.62 xuegod62.cn 192.168.1.61 xuegod61.cn 复制hosts文件到其它服务器上:

[root@xuegod63 ~]# scp /etc/hosts 192.168.1.64:/etc/hosts [root@xuegod63 ~]# scp /etc/hosts 192.168.1.62:/etc/hosts [root@xuegod63 ~]# scp /etc/hosts 192.168.1.61:/etc/hosts 4 配置heartbeat的切换操作。 配置浮动资源

由亍本实验里LVS没有共享存储,所以这里不需要设置共享

存储的切换,

[root@xuegod63 ~]# vim /etc/ha.d/haresources 改: 45 #xuegod63.cn IPaddr::192.168.1.200/24/eth0 Filesystem::192.168.1.62:/w wwdir::/var/www/html::nfs httpd

为:45 xuegod63.cn IPaddr::192.168.1.200/24/eth0 ipvsadm

[root@xuegod63

~]#

scp

/etc/ha.d/haresources

192.168.1.64:/etc/ha.d/

配置xuegod63上的LVS [root@xuegod63

ha.d]#

rpm

-ivh

/mnt/Packages/ipvsadm-1.25-9.el6.x86_64.rpm [root@xuegod63 ~]# ipvsadm -A -t 192.168.1.200:80 -s rr [root@xuegod63 ~]# ipvsadm -a -t 192.168.1.200:80 -r 192.168.1.62 -g (DR模式)

[root@xuegod63 ~]# ipvsadm -a -t 192.168.1.200:80 -r 192.168.1.61 -g

[root@xuegod63 ~]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags ->

RemoteAddress:Port

Forward

Weight

ActiveConn

InActConn

TCP 192.168.1.200:80 rr -> 192.168.1.61:80 Route 1 0 0 -> 192.168.1.62:80 Route 1 0 0

[root@xuegod63 ha.d]# /etc/init.d/ipvsadm save

ipvsadm: Saving IPVS table to /etc/sysconfig/ipvsadm: [ OK ] [root@xuegod63 ha.d]# chkconfig ipvsadm off 启动heartbeat服务

[root@xuegod63 ha.d]# /etc/init.d/heartbeat restart 查看,浮动资源:

[root@xuegod63 ha.d]# ifconfig 。。。

eth0:0 Link encap:Ethernet HWaddr 00:0C:29:12:EC:1E inet

addr:192.168.1.200

Bcast:192.168.1.255

Mask:255.255.255.0

[root@xuegod63 ~]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags ->

RemoteAddress:Port

Forward

Weight

ActiveConn

InActConn

TCP 192.168.1.200:80 rr

-> 192.168.1.62: 80 Route 1 0 0

-> 192.168.1.61:80 Route 1 0 0

配置xuegod64上的LVS [root@xuegod64

ha.d]#

rpm

-ivh

/mnt/Packages/ipvsadm-1.25-9.el6.x86_64.rpm 传输LVS配置文件: [root@xuegod64

ha.d]#

scp

192.168.1.63:/etc/sysconfig/ipvsadm /etc/sysconfig/ [root@xuegod64

ha.d]#

/etc/init.d/ipvsadm

restart

[root@xuegod64 ha.d]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags ->

RemoteAddress:Port

Forward

Weight

ActiveConn

InActConn

TCP 192.168.1.200:80 rr

-> 192.168.1.62:80 Route 1 0 0 -> 192.168.1.61:80 Route 1 0 0 关闭ipvsadm服务,后期此服务启动由heartbeat执行: [root@xuegod64 ha.d]# service ipvsadm stop [root@xuegod64 ha.d]# chkconfig ipvsadm off 重启服务:

[root@xuegod64 ha.d]# service heartbeat restart

测试:查看资源回切

关闭,拔掉主服务器xuegod63的网卡, #表示心跳停止 [root@xuegod64 ha.d]# tail -f /var/log/ha-log #xuegod63关闭网卡之前,在xuegod64动态查看日志 [root@xuegod63 ha.d]# ifdown eth0 xuegod64上过30秒查看: [root@xuegod64 ha.d]# ifconfig

eth0:0 Link encap:Ethernet HWaddr 00:0C:29:48:80:95 inet

addr:192.168.1.200

Bcast:192.168.1.255

Mask:255.255.255.0

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 [root@xuegod64 ha.d]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags ->

RemoteAddress:Port

Forward

Weight

ActiveConn

InActConn

TCP 192.168.1.200:80 rr

-> 192.168.1.62:80 Route 1 0 0 -> 192.168.1.61:80 Route 1 0 0 当xuegod63ok了,测试资源回切到xuegod63上。 [root@xuegod63 ~]# ifup eth0 [root@xuegod63 ~]# ifconfig

...

eth0:0 Link encap:Ethernet HWaddr 00:0C:29:12:EC:1E inet

addr:192.168.1.200

Bcast:192.168.1.255

Mask:255.255.255.0

[root@xuegod63 ~]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags ->

RemoteAddress:Port

Forward

Weight

ActiveConn

InActConn

TCP 192.168.1.200:80 rr

-> 192.168.1.62:80 Route 1 0 0 -> 192.168.1.61:80 Route 1 0 0 [root@xuegod64 ha.d]# ifconfig #看不到到 VIP [root@xuegod64 ha.d]# ipvsadm -L -n #看不规则 说明成功。

配置:LVS RealServer xuegod62,xuegod61 RealServer: xuegod62: ifconfig eth0 192.168.1.62/24 2, 回环接口

[root@xuegod62 ~]# ifconfig lo:1 192.168.1.200 netmask 255.255.255.255

生成回环口配置文件: cd /etc/sysconfig/network-scripts

[root@xuegod62 network-scripts]# cp ifcfg-lo ifcfg-lo:1 [root@xuegod62 network-scripts]# vim ifcfg-lo:1 #写入以下内容

DEVICE=lo:1

IPADDR=192.168.1.200 NETMASK=255.255.255.255 ONBOOT=yes

[root@xuegod62 network-scripts]# ifconfig lo:1 Link encap:Local Loopback

inet addr:192.168.1.200 Mask:255.255.255.255 3, 关闭ARP转发。 [root@xuegod62

~]#

echo

1

/proc/sys/net/ipv4/conf/eth0/arp_ignore [root@xuegod62

~]#

echo

2

/proc/sys/net/ipv4/conf/eth0/arp_announce 永久关闭ARP转发

[root@xuegod62 ~]# vim /etc/sysctl.conf #最后添加 net.ipv4.conf.eth0.arp_ignore = 1 net.ipv4.conf.eth0.arp_announce = 2 [root@xuegod62 ~]# sysctl -p

>

>

5, 启动80端口

[root@xuegod62 network-scripts]# yum install httpd -y [root@xuegod62 network-scripts]# echo 192.168.1.62 > /var/www/html/index.html

[root@xuegod62 network-scripts]# service httpd restart

配置real server xuegod61 配置IP: 192.168.1.61 ifconfig eth0 192.168.1.61/24 2, 回环接口

[root@xuegod61 ~]# ifconfig lo:1 192.168.1.200 netmask 255.255.255.255 生成回环口配置文件: [root@xuegod61

network-scripts]#

cd

/etc/sysconfig/network-scripts

[root@xuegod61 network-scripts]# cp ifcfg-lo ifcfg-lo:1 [root@xuegod61 network-scripts]# vim ifcfg-lo:1 #写入以下内容

DEVICE=lo:1

IPADDR=192.168.1.200 NETMASK=255.255.255.255 ONBOOT=yes

[root@xuegod62 network-scripts]# ifconfig lo:1 Link encap:Local Loopback

inet addr:192.168.1.200 Mask:255.255.255.255 3, 关闭ARP转发 [root@xuegod61

~]#

echo

1

>

/proc/sys/net/ipv4/conf/eth0/arp_ignore [root@xuegod61

~]#

echo

2

>

/proc/sys/net/ipv4/conf/eth0/arp_announce 永丽关闭ARP转发

[root@xuegod61 ~]# vim /etc/sysctl.conf #最后添加 net.ipv4.conf.eth0.arp_ignore = 1 net.ipv4.conf.eth0.arp_announce = 2 [root@xuegod61 ~]# sysctl -p 5, 启动80端口

[root@xuegod61 ~]# yum install httpd -y

[root@xuegod61 network-scripts]# echo 192.168.1.61 > /var/www/html/index.html

[root@xuegod61 network-scripts]# service httpd restart 当xuegod63运行时在xuegod64上测试访问xuegod61和xuegod62的web服务器:

[root@xuegod64 ~]# yum install elinks -y [root@xuegod64 ~]# elinks -dump 192.168.1.200

192.168.1.61

[root@xuegod64 ~]# elinks -dump 192.168.1.200 192.168.1.62

LVS+Heartbeat+Ldirectord高可用集群 注意:此环境是在上两个环境基础上搭建

2 安装:ldirectord。 xuegod63和xuegod64都需要配置ldirectord

下载:ldirectord 官网:

http://horms.net/projects/ldirectord/ download.shtml

注:ldirectord已经被集成在 linux-HA中 resource-agents软件包中,所以直接下载resource-agents-3.9.6.tar.gz http://www.linux-ha.org/wiki/Downloads

[root@xuegod63 ~]# tar -zxvf resource-agents-3.9.6.tar.gz [root@xuegod63 ~]# cd resource-agents-3.9.6 [root@xuegod63 resource-agents-3.9.6]# ls

[root@xuegod63 resource-agents-3.9.6]# ./autogen.sh [root@xuegod63 resource-agents-3.9.6]# ./configure

[root@xuegod63 resource-agents-3.9.6]#make [root@xuegod63 resource-agents-3.9.6]#make install 报错: ln:

creating

symbolic

link

`/usr/lib/ocf/resource.d/heartbeat/.ocf-binaries': File exists gmake[3]: *** [install-exec-local] Error 1 #说这个文件已经存在

gmake[3]: Leaving directory `/root/resource-agents-3.9.6' gmake[2]: *** [install-am] Error 2

gmake[2]: Leaving directory `/root/resource-agents-3.9.6' gmake[1]: *** [install-recursive] Error 1

gmake[1]: Leaving directory `/root/resource-agents-3.9.6' make: *** [install] Error 2 出错原因: [root@xuegod63

ha.d]#

rpm

-qf

/usr/lib/ocf/resource.d/heartbeat/.ocf-binaries resource-agents-3.9.2-7.el6.x86_64 [root@xuegod63

ha.d]#

ls

/mnt/Packages/resource-agents-3.9.2-7.el6.x86_64.rpm /mnt/Packages/resource-agents-3.9.2-7.el6.x86_64.rpm 这是因为之前咱们安装了resource-agents软件包。 暂时不解决:

查看是否安装成功

[root@xuegod63 ~]# ls /etc/init.d/ldirectord /etc/init.d/ldirectord

[root@xuegod63 ~]# service ldirectord status

Can't locate Socket6.pm in @INC (@INC contains: /usr/local/lib64/perl5

/usr/local/share/perl5

/usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/sbin/ldirectord line 847.

BEGIN failed--compilation aborted at /usr/sbin/ldirectord line 847.

需要安装一些perl的包 [root@xuegod63 perl-IO-Socket-INET6

~]#yum

install

perl-Socket6 perl-Pod-Escapes

perl-TimeDate

perl-Pod-Simple perl-Test-Pod perl-MailTools perl-libwww-perl -y

[root@xuegod63 ~]# service ldirectord status Config file ldirectord.cf not found 查看配置文件位置:

[root@xuegod63 ~]# vim /etc/init.d/ldirectord

复制配置文件: [root@xuegod63

resource-agents-3.9.6]#

cp

/root/resource-agents-3.9.6/ldirectord/ldirectord.cf /etc/ha.d/

[root@xuegod63 resource-agents-3.9.6]# service ldirectord status

ldirectord is stopped for /etc/ha.d/ldirectord.cf

[root@xuegod63 resource-agents-3.9.6]# service ldirectord start

Starting ldirectord... success

[root@xuegod63 resource-agents-3.9.6]# service ldirectord status

ldirectord for /etc/ha.d/ldirectord.cf is running with pid: 37344

配置ldirectord 监控realserver功能

[root@xuegod63 ha.d]# cd /etc/ha.d/ [root@ xuegod63 ha.d]# vim ldirectord.cf 查看:

[root@xuegod63 ha.d]# grep ^[^#] ldirectord.cf checktimeout=3 checkinterval=1 autoreload=yes quiescent=yes

virtual=192.168.1.200:80 real=192.168.1.62:80 gate real=192.168.1.61:80 gate fallback=127.0.0.1:80 gate service=http scheduler=rr #persistent=600

#netmask=255.255.255.255 protocol=tcp checktype=negotiate checkport=80

request=\ receive=\

3 检验上面设置是否生效

[root@xuegod63 ha.d]# /etc/init.d/ldirectord restart Restarting ldirectord... success [root@xuegod63 ha.d]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags ->

RemoteAddress:Port

Forward

Weight

ActiveConn

InActConn

TCP 192.168.1.200:80 rr -> 127.0.0.1:80 Local 1 0 0 -> 192.168.1.61:80 Route 0 0 0 -> 192.168.1.62:80 Route 0 0 0

注:如果哪台real server 的weight权限为0而丌是1,就说明此realserver坏了 。从图可以看到我们的realserver 都坏了。 因为我们没有创建测试页面。 创建测试页: [root@xuegod62

~]#

echo

\

>

/var/www/html/test-ipvsadm.html [root@xuegod61

~]#

echo

\

>

/var/www/html/test-ipvsadm.html

[root@xuegod63 ha.d]# /etc/init.d/ldirectord restart #重启ldirectord

Restarting ldirectord... success

[root@xuegod63 ha.d]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags ->

RemoteAddress:Port

Forward

Weight

ActiveConn

InActConn

TCP 192.168.1.200:80 rr -> 192.168.1.61:80 Route 1 0 0 -> 192.168.1.62:80 Route 1 0 0 再次测试:

[root@xuegod64 ~]# elinks --dump 192.168.1.200 192.168.1.62

[root@xuegod64 ~]# elinks --dump 192.168.1.200 192.168.1.61 已经正常。

[root@xuegod63 ha.d]# service ldirectord stop #关闭服务 Stopping ldirectord... success [root@xuegod63 ha.d]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags ->

RemoteAddress:Port

Forward

Weight

ActiveConn

InActConn

配置让ldirectord服务在心跳服务启动后自动启动

[root@xuegod63 ha.d]# vim /etc/ha.d/haresources

改:45 xuegod63.cn IPaddr::192.168.1.200/24/eth0 ipvsadm 为:45 xuegod63.cn IPaddr::192.168.1.200/24/eth0 ipvsadm ldirectord

[root@stu1 ~]# service heartbeat start xuegod64安装ldirectord: [root@xuegod63

~]#

scp

resource-agents-3.9.6.tar.gz

192.168.1.64:/root/

[root@xuegod64 ~]# tar -zxvf resource-agents-3.9.6.tar.gz [root@xuegod64 ~]# cd resource-agents-3.9.6 [root@xuegod64 resource-agents-3.9.6]# ls

[root@xuegod64 resource-agents-3.9.6]# ./autogen.sh #执行完这个命令后,生成make命令

[root@xuegod64 resource-agents-3.9.6]# ./configure [root@xuegod64 resource-agents-3.9.6]#make [root@xuegod64 resource-agents-3.9.6]#make install

查看是否安装成功ldirectord启动脚本和命令: [root@xuegod64 ~]# ls /etc/init.d/ldirectord /etc/init.d/ldirectord

[root@xuegod63 ha.d]# which ldirectord /usr/sbin/ldirectord

需要安装一些perl的包 [root@xuegod64 perl-IO-Socket-INET6

~]#yum

install

perl-Socket6 perl-Pod-Escapes

perl-TimeDate

perl-Pod-Simple perl-Test-Pod perl-MailTools perl-libwww-perl -y

[root@xuegod64 ~]# service ldirectord status Config file ldirectord.cf not found 复制配置文件: [root@xuegod63

ha.d]#

scp

/etc/ha.d/ldirectord.cf

192.168.1.64:/etc/ha.d/

[root@xuegod64 resource-agents-3.9.6]# service ldirectord start

Starting ldirectord... success

[root@xuegod64 resource-agents-3.9.6]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags ->

RemoteAddress:Port

Forward

Weight

ActiveConn

InActConn

TCP 192.168.1.200:80 rr -> 192.168.1.61:80 Route 1 0 0 -> 192.168.1.62:80 Route 1 0 0

[root@xuegod64 resource-agents-3.9.6]# service ldirectord

stop

Stopping ldirectord... success

然后查看log看ldirectord的工作状冴

[root@master mysqlinstall]# tail -f /var/log/ldirectord.log [root@xuegod63

~]#

scp

/etc/ha.d/haresources

192.168.1.64:/etc/ha.d/

[root@xuegod64 ~]# service heartbeat restart xuegod63断网,测试:

[root@xuegod63 ha.d]# ifdown eth0 在xuegod64上查看资源: [root@xuegod64 ~]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags ->

RemoteAddress:Port

Forward

Weight

ActiveConn

InActConn

TCP 192.168.1.200:80 rr -> 192.168.1.61:80 Route 1 0 6 -> 192.168.1.62:80 Route 1 0 6

本文来源:https://www.bwwdw.com/article/tbsp.html

Top