用ldirectord实现负载均衡器的高可用

实验说明:为防止负载均衡器出现单点故障,本实验利用heartbeat v2 ldirectord来实现director的故障转移和后端realserver的健康状态监测,当real server 出现故障时,自动将对应主机的权重设置为0,如果所有主机都不能工作,让director返回一个提示页面,实验采用CentOS 6.4 64位平台.


114428212.png


一、安装heartbeatheartbeat-guiipvsadmheartbeat-ldirectord

# yum -y installPyXML net-snmp-libs libnet

# rpm -ivhheartbeat-pils-2.1.4-12.el6.x86_64.rpm

# rpm -ivhheartbeat-stonith-2.1.4-12.el6.x86_64.rpm

# rpm -ivhheartbeat-2.1.4-12.el6.x86_64.rpm

# rpm -ivhheartbeat-gui-2.1.4-12.el6.x86_64.rpm

# rpm -ivh heartbeat-ldirectord-2.1.4-12.el6.x86_64.rpm

# yum -yinstall ipvsadm


配置主机名解析:

# vi/etc/hosts

node1.magedu.com172.16.1.1

node2.magedu.com172.16.1.2

同步时间

# ntpdate172.16.0.1

配置ssh互信,director上都要做

#ssh-keygen -t rsa

# ssh-copy-id -i/root/.ssh/id_rsa.pub root@node2

复制样例配置文件到工作目录

# cd /usr/share/doc/heartbeat-2.1.4/

# cpauthkeys ha.cf haresources /etc/ha.d

编辑authkeys文件:

# cd/etc/ha.d

# opensslrand -hex 8 >> authkeys

# vi authkeys

auth 2

2 sha1 RandomNumber

# chmod600 authkeys


编辑ha.cf:

# viha.cf

设置组播地址传递心跳信息

mcast eth0 225.0.0.250694 1 0

auto_failback on;自动恢复;

添加集群节点,如果只有2个节点,需要配置pingnode功能;

nodenode1.magedu.com

nodenode2.magedu.com

ping172.16.0.1


启用crm(资源管理器)

crm respawn


scp 配置文件node2

# scpha.cf authkeys haresources node2:/etc/ha.d

hacluster用户设置密码,用以登录hb_gui

passwd hacluster


# cp/usr/share/doc/heartbeat-ldirectord-2.1.4/ldirectord.cf /etc/ha.d/


编辑配置文件:

# vi /etc/ha.d/ldirectord.cf


# Global Directives

checktimeout=3

checkinterval=1

#fallback=127.0.0.1:80

autoreload=yes

logfile="/var/log/ldirectord.log"

#logfile="local0"

#emailalert="[email protected]"

#emailalertfreq=3600

#emailalertstatus=all

quiescent=yes


# Sample for an httpvirtual service

virtual=172.16.1.10:80

real=172.16.1.3:80 gate

real=172.16.1.4:80 gate

fallback=127.0.0.1:80 gate

service=http

request=".test.html"(向172.16.1.10请求此页面)

receive="Test Page"(页面中必须包含TestPage,大小写区分)

virtualhost=172.16.1.10

scheduler=rr

#persistent=600

#netmask=255.255.255.255

protocol=tcp

checktype=negotiate

checkport=80


启动heartbeatldirectord服务

#service heartbeat start

# serviceldirectord start


在服务器节点上安装httpd,(两台服务器分别配置相应的内容,另一台服务器配置省略。)

# yum -yinstall httpd

建立director请求的页面,必须包含“receive”中的内容

# vi/var/www/html/.test.html

Test Page

建立index.html测试

#vi/var/www/html/index.html

node3.magedu.com

#chkconfig mysqld on


设置VIP,并禁止eth0接口响应非所在接口IParp请求

#ifconfig lo:0 172.16.1.10/32

# echo 1 >/proc/sys/net/ipv4/conf/eth0/arp_ignore

# echo 2 >/proc/sys/net/ipv4/conf/eth0/arp_announce


添加资源(详见上篇博客)

114531305.png



114606147.png



测试:当两个webserverdown掉,资源管理器会将ipvsadm中的条目的权重调为0,启动director上的httpd服务,提供一个通知页面;

[root@node2 ~]# ipvsadm-L

IP Virtual Server version1.2.1 (size=4096)

Prot LocalAddress:PortScheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

TCP 172.16.1.10:http rr

-> 127.0.0.1:http Local 1 0 8

-> node3.magedu.com:http Route 0 0 0

-> node4.magedu.com:http Route 0 0 0

当其中一台服务器恢复时:

[root@node2 ~]# ipvsadm-L

IP Virtual Server version1.2.1 (size=4096)

Prot LocalAddress:PortScheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

TCP 172.16.1.10:http rr

-> node3.magedu.com:http Route 1 0 0

-> node4.magedu.com:http Route 0 0 0



你可能感兴趣的:(HA)