ldirectord简介:为了从主Director将LVS负载均衡资源故障转移到备用Director,并从集群中自动移除节点,我们需要使用ldirectord程序,这个程序在启动时动建立IPVS表,然后监视集群节点的健康情况,在发现失效节点时将其自动从IPVS表中移除。

ldirectord监视集群(真实服务器):ldirectord守护进程通过向每台真实服务器真实IP(RIP)上的集群资源发送访问请求来实现对真实服务器的监控,这对所有类型的LVS集群都是成立的:LVS-DR,LVS-NAT和LVS-TUN。正常情况下,为每个Director上的VIP地址运行一个ldirectord守护进程,当真实服务器不响应运行在Director上的ldirectord守护进程时,ldirectord守护进程运行适当的ipvsadm命令将VIP地址从IPVS表中移除。(以后,当真实服务器回到在线状时,ldirectord使用适当的ipvsadm命令将真实服务器重新添加到IPVS表中)

案例:在前端架设两台director主机,互为备份,实现ipvs群集的高可用性。后端架设两台web服务器实现负载均衡LB,在realserver上使用DR模式

ip地址规划:

VIP:192.168.145.101

director1:eth0:192.168.145.99

               eth1:192.168.10.1 (心跳线)

director2:eth0:192.168.145.100

               eth1:192.168.10.2(心跳线)

realserver1:eth0:192.168.145.200

realserver2:eth0:192.168.145.201

拓扑方案:

IPvs的高可用性+LVS的DR模式+ldirectord

配置director1部分:

一:修改director1的主机名:

[[email protected] ~]# vim /etc/sysconfig/network

IPvs的高可用性+LVS的DR模式+ldirectord 

二:给director1配置静态ip地址

IPvs的高可用性+LVS的DR模式+ldirectord

IPvs的高可用性+LVS的DR模式+ldirectord 

三:编译本地yum:

[[email protected] ~]# vim /etc/yum.repos.d/rhel-debuginfo.repo

IPvs的高可用性+LVS的DR模式+ldirectord

四:安装相关关键包:

[[email protected] ~]# mkdir /mnt/cdrom

[[email protected] ~]# mount /dev/cdrom /mnt/cdrom/       #挂载光盘,在使用

yum localinstall中需要用到光盘上的软件包

[[email protected] ~]# yum localinstall -y heartbeat-2.1.4-9.el5.i386.rpm heartbeat-pils-2.1.4-10.el5.i386.rpm heartbeat-stonith-2.1.4-

10.el5.i386.rpm  libnet-1.1.4-3.el5.i386.rpm perl-MailTools-1.77-1.el5.noarch.rpm  --nogpgcheck   #使用localinstall可以安装外部软件包以及调用系统光盘软件包,—nogpgcheck取消签名测

五:安装ipvs的管理工具ipvsadm:

[[email protected] ~]# yum install ipvsadm -y

[[email protected] ~]# ipvsadm -A -t 192.168.145.101:80 -s rr #定义群集,rr表示

采用轮询调度,监听httpd服务的80端口

[[email protected] ~]# ipvsadm -a -t 192.168.145.101:80 -r 192.168.145.200 –g

#当监听到有访问80端口的,转换到真实的ip地址上,-g表示DR模型

[[email protected] ~]# ipvsadm -a -t 192.168.145.101:80 -r 192.168.145.201 –g

#当监听到有访问80端口的,转换到真实的ip地址上,-g表示DR模型

[[email protected] ~]# service ipvsadm save #保存配置的规则

[[email protected] ~]# service ipvsadm stop #将ipvsadm停止

六:编辑配置心跳参数,群集资源等文件

[[email protected] ~]# cd /usr/share/doc/heartbeat-2.1.4/     

[[email protected] heartbeat-2.1.4]# cp ha.cf /etc/ha.d/#将ha.cf调整心跳探测参数

[[email protected] heartbeat-2.1.4]# cp haresources /etc/ha.d/      #haresources

文件定义群集资源的资源管理器

[[email protected] heartbeat-2.1.4]# cp authkeys /etc/ha.d/       #authkeys实现

心跳线双方的身份验证

[[email protected] heartbeat-2.1.4]# cp /etc/init.d/ipvsadm /etc/ha.d/resource.d/    #将ipvsadm的控制脚本拷贝到resource.d目录下

[[email protected] heartbeat-2.1.4]# cd /etc/ha.d/

[[email protected] ha.d]# vim ha.cf        #编辑定义心跳探测参数的文件

IPvs的高可用性+LVS的DR模式+ldirectord

IPvs的高可用性+LVS的DR模式+ldirectord

[[email protected] ha.d]# dd if=/dev/random bs=512 count=1 |openssl md5      #利用随机文件random产生一段字符,一块512字节,数量是1,通过md5加密后输出

IPvs的高可用性+LVS的DR模式+ldirectord

[[email protected] ha.d]# vim authkeys        #编辑心跳双方的身份验证文件

IPvs的高可用性+LVS的DR模式+ldirectord

IPvs的高可用性+LVS的DR模式+ldirectord

[[email protected] ha.d]# chmod 600 authkeys        #根据文件内提示将authkeys的

权限改为600

[[email protected] ha.d]# vim haresources       #定义群集的抢夺资源

IPvs的高可用性+LVS的DR模式+ldirectord

[[email protected] ha.d]# service heartbeat start      #启动heartbeat服务

[[email protected] ha.d]# chkconfig heartbeat on      #将该服务设置为开机自动启动

[[email protected] ha.d]# ifconfig      #使用该命令查看ip地址情况

IPvs的高可用性+LVS的DR模式+ldirectord

配置director2部分:

一:修改director2的主机名:

[[email protected] ~]# vim /etc/sysconfig/network

IPvs的高可用性+LVS的DR模式+ldirectord

二:给director2配置静态ip地址:

IPvs的高可用性+LVS的DR模式+ldirectord

IPvs的高可用性+LVS的DR模式+ldirectord

三:编辑本地yum:

[[email protected] ~]# vim /etc/yum.repos.d/rhel-debuginfo.repo

IPvs的高可用性+LVS的DR模式+ldirectord

四:安装相关软件包:

[[email protected] ~]# mkdir /mnt/cdrom

[[email protected] ~]# mount /dev/cdrom /mnt/cdrom/     #挂载光盘,在下一步中需要使用自带的光盘软件,解决依赖性问题

[[email protected] ~]# yum localinstall heartbeat-2.1.4-9.el5.i386.rpm heartbeat-pils-2.1.4-10.el5.i386.rpm heartbeat-stonith-2.1.4-10.el5.i386.rpm

libnet-1.1.4-3.el5.i386.rpm perl-MailTools-1.77-1.el5.noarch.rpm -y –nogpgcheck       #使用localinstall可以安装外部软件包以及调用系统光盘软件

包,—nogpgcheck取消签名检测

五:安装ipvs的管理工具ipvsadm:

[[email protected] ~]# yum install ipvsadm -y

[[email protected] ~]# ipvsadm -A -t 192.168.145.101:80 -s rr #定义群集,rr表示采用轮询调度,监听httpd服务的80端口

[[email protected] ~]# ipvsadm -a -t 192.168.145.101:80 -r 192.168.145.200 –g #当监听到有访问80端口的,转换到真实的ip地址上,-g表示DR模型

[[email protected] ~]# ipvsadm -a -t 192.168.145.101:80 -r 192.168.145.201 –g #当监听到有访问80端口的,转换到真实的ip地址上,-g表示DR模型

[[email protected] ~]# service ipvsadm save #保存配置的规则

[[email protected] ~]# service ipvsadm stop #将ipvsadm停止

六:编辑配置心跳参数,群集资源等文件

[[email protected] ~]# cd /usr/share/doc/heartbeat-2.1.4/

[[email protected] heartbeat-2.1.4]# cp ha.cf /etc/ha.d/       #将ha.cf调整心跳探测参数

[[email protected] heartbeat-2.1.4]# cp haresources /etc/ha.d/       #haresources文件定义群集资源的资源管理器

[[email protected] heartbeat-2.1.4]# cp authkeys /etc/ha.d/         #authkeys实现心跳线双方的身份验证

[[email protected] heartbeat-2.1.4]# cp  /etc/init.d/ipvsadm   /etc/ha.d/resource.d/     #将ipvsadm的控制脚本拷贝到resource.d目录下

[[email protected] heartbeat-2.1.4]# cd  /etc/ha.d/

[[email protected] ha.d]# vim ha.cf       #编辑心跳探测的配置文件

IPvs的高可用性+LVS的DR模式+ldirectord

IPvs的高可用性+LVS的DR模式+ldirectord

[[email protected] ha.d]# vim authkeys        #心跳双方的身份验证文件

IPvs的高可用性+LVS的DR模式+ldirectord

IPvs的高可用性+LVS的DR模式+ldirectord

[[email protected] ha.d]# chmod 600 authkeys #根据文件内提示将authkeys的权限改为600

[[email protected] ha.d]# vim haresources       #定义群集的抢夺资源

IPvs的高可用性+LVS的DR模式+ldirectord

[[email protected] ha.d]# service heartbeat start

[[email protected] ha.d]# chkconfig heartbeat on       #将该服务设置为开机自动启动

[[email protected] ha.d]# ifconfig       #使用该命令并没有查看到抢占的VIP

配置realserver 1:

一:配置内核参数:

IPvs的高可用性+LVS的DR模式+ldirectord

[[email protected] ~]# echo "net.ipv4.conf.eth0.arp_announce = 2">>/etc/sysctl.conf

[[email protected] ~]# echo "net.ipv4.conf.all.arp_announce = 2">>/etc/sysctl.conf

[[email protected] ~]# echo "net.ipv4.conf.eth0.arp_ignore = 1">>/etc/sysctl.conf

[[email protected] ~]# echo "net.ipv4.conf.all.arp_ignore = 1">>/etc/sysctl.conf

[[email protected] ~]# sysctl –p       #使新编辑的内核文件生效

IPvs的高可用性+LVS的DR模式+ldirectord

IPvs的高可用性+LVS的DR模式+ldirectord

IPvs的高可用性+LVS的DR模式+ldirectord

[[email protected] ~]# service network restart     #重启网络服务

[[email protected] ~]# route add -host 192.168.145.101 dev lo:0      #添加一条路由,保证数据包返回时使用VIP地址192.168.145.101,出口是lo:0

二:安装web服务器:

[[email protected] ~]# mkdir /mnt/cdrom

[[email protected] ~]# mount /dev/cdrom /mnt/cdrom/

[[email protected] ~]# cd /mnt/cdrom/Server/

[[email protected] Server]# rpm -ivh httpd-2.2.3-31.el5.i386.rpm     #安装httpd

[[email protected] Server]# service httpd start

[[email protected] Server]# chkconfig httpd on

[[email protected] Server]# echo "realserver1 web1">/var/www/html/index.html      #编辑网页文件index.html

配置realserver 2:

一:配置内核参数:

IPvs的高可用性+LVS的DR模式+ldirectord

[[email protected] ~]# echo "net.ipv4.conf.eth0.arp_announce = 2">>/etc/sysctl.conf

[[email protected] ~]# echo "net.ipv4.conf.all.arp_announce = 2">>/etc/sysctl.conf 

[[email protected] ~]# echo "net.ipv4.conf.eth0.arp_ignore = 1">>/etc/sysctl.conf

[[email protected] ~]# echo "net.ipv4.conf.all.arp_ignore = 1">>/etc/sysctl.conf

[[email protected] ~]# sysctl –p      #使刚配置的内核参数生效

IPvs的高可用性+LVS的DR模式+ldirectord

IPvs的高可用性+LVS的DR模式+ldirectord

IPvs的高可用性+LVS的DR模式+ldirectord

[[email protected] ~]# service network restart

[[email protected] ~]# route add -host 192.168.145.101 dev lo:0     #添加一条路由,使返回的数据包从VIP—192.168.145.101出去,出口是lo:0

二:安装web服务器

[[email protected] ~]# mkdir /mnt/cdrom

[[email protected] ~]# mount /dev/cdrom /mnt/cdrom/

[[email protected] ~]# cd /mnt/cdrom/Server/

[[email protected] Server]# rpm -ivh httpd-2.2.3-31.el5.i386.rpm     #安装httpd服务器

[[email protected] Server]# service httpd start      #启动httpd

[[email protected] Server]# chkconfig httpd on      #开机自动启动

[[email protected] Server]# echo "realserver2 web2">/var/www/html/index.html       #编辑网页文件

测试:

IPvs的高可用性+LVS的DR模式+ldirectord

在director1上查看:

[[email protected] ha.d]# ipvsadm –ln

IPvs的高可用性+LVS的DR模式+ldirectord

在director2上查看:

IPvs的高可用性+LVS的DR模式+ldirectord

[[email protected] ha.d]# cd /usr/lib/heartbeat/     #在node1.a.com上切换目录

[[email protected] heartbeat]# ./hb_standby       #将node1.a.com设置为备份

在director1上查看:

IPvs的高可用性+LVS的DR模式+ldirectord

在director2上查看:

IPvs的高可用性+LVS的DR模式+ldirectord

[[email protected] heartbeat]# ./hb_takeover       #将node1.a.com设置为主节点

在director1上查看:

IPvs的高可用性+LVS的DR模式+ldirectord

在director2上查看:

IPvs的高可用性+LVS的DR模式+ldirectord

在上个案例中实现的手工方式建立ipvs表,也可以使用ldirectord自动创建ipvs表,当节点失效时,自动清除ipvs表。在上个案例中,如果要实现ldirectord,可以作如下修改:

节点一node1.a.com

[[email protected] ~]# yum localinstall heartbeat-ldirectord-2.1.4-9.el5.i386.rpm -y --nogpgcheck   #安装ldirectord

[[email protected] ~]# rpm -ql heartbeat-ldirectord    #查看安装路径

IPvs的高可用性+LVS的DR模式+ldirectord

[[email protected] ~]# cp /usr/share/doc/heartbeat-ldirectord-2.1.4/ldirectord.cf /etc/ha.d/

[[email protected] ~]# cd /etc/ha.d/

[[email protected] ha.d]# vim ldirectord.cf       #编辑资源

IPvs的高可用性+LVS的DR模式+ldirectord

IPvs的高可用性+LVS的DR模式+ldirectord

IPvs的高可用性+LVS的DR模式+ldirectord

[[email protected] ha.d]# vim haresources

IPvs的高可用性+LVS的DR模式+ldirectord

节点二:node.a.com

[[email protected] ~]# yum localinstall heartbeat-ldirectord-2.1.4-9.el5.i386.rpm  -y --nogpgcheck

[[email protected] ~]# cp /usr/share/doc/heartbeat-ldirectord-2.1.4/ldirectord.cf  /etc/ha.d/

[[email protected] ~]# cd /etc/ha.d/

[[email protected] ha.d]# vim haresources     

IPvs的高可用性+LVS的DR模式+ldirectord

[[email protected] ha.d]# vim ldirectord.cf      #编辑资源

IPvs的高可用性+LVS的DR模式+ldirectord

IPvs的高可用性+LVS的DR模式+ldirectord