设为首页 收藏本站
查看: 2313|回复: 0

[经验分享] 利用heartbeat的ldirectord实现ipvs的高可用集群构建

[复制链接]

尚未签到

发表于 2015-11-21 10:47:29 | 显示全部楼层 |阅读模式
DSC0000.jpg
  

网络规划:


两台LVS server:(两台LVS也可以为用户提供错误页面)


         node1172.16.31.10


         node2172.16.31.11


VIP172.16.31.180


ipvs规则内包含2Real Server:(后面的RS指的就是后端的web服务器)


         rs1172.16.31.13


         rs2172.16.31.14


我们还需要错误页面提供者:我们选择LVS作为sorry server,所有的real server不可用时就指向这个sorry
server


.配置HA集群前提


1、节点之间时间必须同步;


建议使用ntp协议进行;


2、节点之间必须要通过主机名互相通信;


建议使用hosts文件;


通信中使用的名字必须与其节点为上“uname -n”命令展示出的名字保持一致;


3、如果是2个节点,需要仲裁设备;


4、节点之间彼此root用户能基于ssh密钥方式进行通信;


HA节点之间传递公钥:node1node2配置,实现ssh无密钥通信


[iyunv@node1 ~]# ssh-keygen -t  rsa  -P""
[iyunv@node1 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@node2
[iyunv@node2 ~]# ssh-keygen -t rsa -P""
[iyunv@node2 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@node1[size=1em]四个节点的
[size=1em]hosts
[size=1em]文件相同:

[size=1em]
[iyunv@node1 ~]# cat /etc/hosts
127.0.0.1  localhost localhost.localdomain localhost4 localhost4.localdomain4
::1        localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.0.1 server.magelinux.com server
172.16.31.10 node1.stu31.com node1
172.16.31.11 node2.stu31.com node2
172.16.31.13 rs1.stu31.com   rs1
172.16.31.14 rs2.stu31.com   rs2[size=1em]HA
[size=1em]节点间实现无密钥通信:测试时间一致

[iyunv@node1 ~]# date;ssh node2 'date'
Sun Jan 4 17:57:51 CST 2015
Sun Jan 4 17:57:50 CST 2015.安装heartbeat

1.LVS高可用集群安装heartbeat


获取heartbeat程序包组:


[iyunv@node1 heartbeat2]# ls
heartbeat-2.1.4-12.el6.x86_64.rpm           
heartbeat-gui-2.1.4-12.el6.x86_64.rpm        
heartbeat-stonith-2.1.4-12.el6.x86_64.rpm
heartbeat-ldirectord-2.1.4-12.el6.x86_64.rpm
heartbeat-pils-2.1.4-12.el6.x86_64.rpm安装程序包组:


node1node2都需要安装;


前提条件需要安装如下依赖包:



[iyunv@node1 heartbeat2]# yum install -ynet-snmp-libs libnet PyXML3.安装heartbeat套件程序:


[iyunv@node1 heartbeat2]# rpm -ivh heartbeat-2.1.4-12.el6.x86_64.rpm heartbeat-stonith-2.1.4-12.el6.x86_64.rpm heartbeat-pils-2.1.4-12.el6.x86_64.rpm heartbeat-gui-
2.1.4-12.el6.x86_64.rpm heartbeat-ldirectord-2.1.4-12.el6.x86_64.rpm
error: Failed dependencies:
ipvsadm is needed by heartbeat-ldirectord-2.1.4-12.el6.x86_64
perl(Mail::Send) is needed by heartbeat-ldirectord-2.1.4-12.el6.x86_64[size=1em]有依赖关系,解决依赖关系:[size=1em]node1[size=1em]和[size=1em]node2[size=1em]都安装
[size=1em]# yum -y install  ipvsadm  perl-MailTools  perl-TimeDate[size=1em]再次安装:
[size=1em][size=1em]# rpm -ivh heartbeat-2.1.4-12.el6.x86_64.rpm heartbeat-stonith-2.1.4-12.el6.x86_64.rpm heartbeat-pils-2.1.4-12.el6.x86_64.rpm heartbeat-gui-2.1.4-12.el6.x86_64.rpm
heartbeat-ldirectord-2.1.4-12.el6.x86_64.rpm
Preparing...                ###########################################[100%]
1:heartbeat-pils        ########################################### [ 20%]
2:heartbeat-stonith     ########################################### [ 40%]
3:heartbeat             ########################################### [ 60%]
4:heartbeat-gui         ########################################### [ 80%]
5:heartbeat-ldirectord  ########################################### [100%][size=1em]安装完成!

查看ldirectord生成的文件:


[iyunv@node1 heartbeat2]# rpm -ql heartbeat-ldirectord
/etc/ha.d/resource.d/ldirectord
/etc/init.d/ldirectord
/etc/logrotate.d/ldirectord
/usr/sbin/ldirectord
/usr/share/doc/heartbeat-ldirectord-2.1.4
/usr/share/doc/heartbeat-ldirectord-2.1.4/COPYING
/usr/share/doc/heartbeat-ldirectord-2.1.4/README
/usr/share/doc/heartbeat-ldirectord-2.1.4/ldirectord.cf
/usr/share/man/man8/ldirectord.8.gz[size=1em]存在模版配置文件。

ldirectord会自动检查后端Real Server的健康状态,实现按需添加或删除RS,还能定义sorry server;无需ipvsadmLVS
Server上定义,而是通过编辑ldirectord的配置文件来生成ipvs规则,因此,定义集群服务,添加RS都在配置文件中指定,而无须手动指向ipvsadm命令。


不用启动ipvs.


.heartbeat配置:


1.系统日志记录heartbeat日志


[iyunv@node1 ha.d]# vim /etc/rsyslog.conf
#添加如下行:
local0.*                                               /var/log/heartbeat.log
拷贝一份到node2:
[iyunv@node1 ha.d]# scp /etc/rsyslog.confnode2:/etc/rsyslog.conf[size=1em]2.[size=1em]拷贝配置文件模版到[size=1em]/etc/ha.d[size=1em]目录
[iyunv@node1 ha.d]# cd /usr/share/doc/heartbeat-2.1.4/
[iyunv@node1 heartbeat-2.1.4]# cp authkeysha.cf /etc/ha.d/主配置文件:
[iyunv@node1 ha.d]# grep -v ^#  /etc/ha.d/ha.cf
logfacility     local0
mcast eth0 225.231.123.31 694 1 0
auto_failback on
node   node1.stu31.com
node   node2.stu31.com
ping 172.16.0.1
crm on认证文件:
[iyunv@node1 ha.d]# vim authkeys
auth 2
2    sha1   password权限必须是600或者400:

[iyunv@node1 ha.d]# chmod 600 authkeys


将主配置文件和认证文件拷贝到node2


[iyunv@node1 ha.d]# scp authkeys ha.cf node2:/etc/ha.d/
authkeys                                      100%  675    0.7KB/s   00:00   
ha.cf                                        100%   10KB  10.4KB/s  00:00   
[iyunv@node1 ha.d]#[size=1em]
.配置RS
Server:即WEB服务器


1.提供了一个脚本:


[iyunv@rs1 ~]# cat rs.sh
#!/bin/bash
vip=172.16.31.180
interface="lo:0"
case $1 in
start)
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
ifconfig $interface $vip broadcast $vip netmask 255.255.255.255  up
route add -host $vip dev $interface
;;
stop)
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
ifconfig $interface  down
;;
status)
if ficonfig lo:0 |grep $vip &>/dev/null; then
echo "ipvs isrunning."
else
echo "ipvs isstopped."
fi
;;
*)
echo "Usage 'basename $0 start|stop|status"
exit 1
;;
esac[size=1em]在两台[size=1em]RS [size=1em]上执行启动。

保证RS能提供WEB服务:


[iyunv@rs1 ~]# echo "rs1.stu31.com" > /var/www/html/index.html
[iyunv@rs2 ~]# echo "rs2.stu31.com" > /var/www/html/index.html[size=1em]启动[size=1em]httpd[size=1em]服务测试:
[iyunv@rs1 ~]# service httpd start
Starting httpd:                                           [  OK  ]
[iyunv@rs1 ~]# curl http://172.16.31.13
rs1.stu31.com
[iyunv@rs2 ~]# service httpd start
Starting httpd:                                           [  OK  ]
[iyunv@rs2 ~]# curl http://172.16.31.14
rs2.stu31.com[size=1em]测试完成后就将[size=1em]httpd[size=1em]服务停止,关闭自启动:
[size=1em]# service httpd stop                                    
# chkconfig httpd off[size=1em]RS[size=1em]就设置好了。

.下面开始配置LVS的高可用集群:


1.手动配置好lvs-dr负载均衡集群:


[iyunv@node1 ~]# ifconfig eth0:0 172.16.31.180 broadcast 172.16.31.180 netmask 255.255.255.255 up
[iyunv@node1 ~]# route add -host 172.16.31.180 dev eth0:0
[iyunv@node1 ~]# ipvsadm -C
[iyunv@node1 ~]# ipvsadm -A -t 172.16.31.180:80 -s rr
[iyunv@node1 ~]# ipvsadm -a -t 172.16.31.180:80 -r 172.16.31.13 -g
[iyunv@node1 ~]# ipvsadm -a -t 172.16.31.180:80 -r 172.16.31.14 -g
[iyunv@node1 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port          Forward Weight ActiveConn InActConn
TCP 172.16.31.180:80 rr
-> 172.16.31.13:80             Route   1      0         0        
-> 172.16.31.14:80             Route   1      0         0[size=1em]2.[size=1em]访问测试:
[iyunv@nfs ~]# curl http://172.16.31.180
rs2.stu31.com
[iyunv@nfs ~]# curl http://172.16.31.180
rs1.stu31.com[size=1em]负载均衡实现了:
[iyunv@node1 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port          Forward Weight ActiveConn InActConn
TCP 172.16.31.180:80 rr
-> 172.16.31.13:80             Route   1      0         1        
-> 172.16.31.14:80             Route   1      0         1[size=1em]在节点上清除配置的规则:
[iyunv@node1 ~]# ipvsadm -C
[iyunv@node1 ~]# route del -host 172.16.31.180
[iyunv@node1 ~]# ifconfig eth0:0 down[size=1em]第二个[size=1em]LVS Server[size=1em]也同样测试一遍,测试成功后清除配置的规则。

3.LVS上设置错误页面:配置LVSsorry server,为用户提供错误提示页


[iyunv@node1 ~]# echo "sorry page fromlvs1" > /var/www/html/index.html
[iyunv@node2 ~]# echo "sorry page fromlvs2" > /var/www/html/index.html[size=1em]启动[size=1em]web[size=1em]服务测试错误页面正常:
[size=1em][iyunv@node1 ha.d]# service httpd start
Starting httpd:                                           [  OK  ]
[iyunv@node1 ha.d]# curl http://172.16.31.10
sorry page from lvs1
[iyunv@node2 ~]# service httpd start
Starting httpd:                                           [  OK  ]
[iyunv@node2 ~]# curl http://172.16.31.11
sorry page from lvs2[size=1em]4.[size=1em]将[size=1em]LVS[size=1em]的两个节点配置为[size=1em]HA[size=1em]集群:

拷贝ldirectord软件安装后的配置文件模版:


[iyunv@node1 ~]# cd  /usr/share/doc/heartbeat-ldirectord-2.1.4/
[iyunv@node1 heartbeat-ldirectord-2.1.4]# ls
COPYING ldirectord.cf  README
[iyunv@node1 heartbeat-ldirectord-2.1.4]# cp  ldirectord.cf /etc/ha.d[iyunv@node1 ha.d]# grep -v ^#  /etc/ha.d/ldirectord.cf
#检测超时
checktimeout=3
#检测间隔
checkinterval=1
#重新载入客户机
autoreload=yes
#real server 宕机后从lvs列表中删除,恢复后自动添加进列表
quiescent=yes
#监听VIP地址80端口
virtual=172.16.31.180:80
#真正服务器的IP地址和端口,路由模式
real=172.16.31.13:80 gate
real=172.16.31.14:80 gate
#如果RS节点都宕机,则回切到本地环回口地址
fallback=127.0.0.1:80 gate
#服务是http
service=http
#保存在RS的web根目录并且可以访问,通过它来判断RS是否存活
request=".health.html"
#网页内容
receive="OK"
#调度算法
scheduler=rr
#persistent=600
#netmask=255.255.255.255
#检测协议
protocol=tcp
#检测类型
checktype=negotiate
#检测端口
checkport=80


我们需要指定这个健康检查页面,在后端RS server上配置:


[iyunv@rs1 ~]# echo "OK" > /var/www/html/.health.html
[iyunv@rs2 ~]# echo "OK" > /var/www/html/.health.html[size=1em]复制一份[size=1em]ldirectord.cf[size=1em]配置文件到[size=1em]node2[size=1em]:
[iyunv@node1 ha.d]# scp ldirectord.cf node2:/etc/ha.d/
ldirectord.cf                                 100% 7553     7.4KB/s  00:00[size=1em]启动[size=1em]heartbeat[size=1em]服务:
[iyunv@node1 ha.d]# service heartbeat start;ssh node2 'service heartbeat start'
Starting High-Availability services:
Done.
Starting High-Availability services:
Done.[size=1em]查看监听端口:
[size=1em][iyunv@node1 ha.d]# ss -tunl |grep 5560
tcp   LISTEN     0      10                     *:5560                  *:*.资源配置

需要对heartbeat图形界面登录用户授权加密:node2也需要设置密码。


[iyunv@node1 ~]# echo oracle |passwd --stdin  hacluster
Changing password for user hacluster.
passwd: all authentication tokens updatedsuccessfully.[size=1em]进入图形化配置端配置资源:

输入如下指令:需要桌面支持。


#hb_gui &


DSC0001.jpg


配置集群资源组:新建资源组


DSC0002.jpg


DSC0003.jpg


新建资源,VIP的定义:


DSC0004.jpg


添加新资源:


DSC0005.jpg


DSC0006.jpg


新建一个ipvs规则的资源:


DSC0007.jpg


注意ldirecotord.cf这个配置文件在哪个目录:


DSC0008.jpg


添加完成后就add


DSC0009.jpg


定义完成后就启动资源组ipvs


DSC00010.jpg


现在资源启动于node2


DSC00011.jpg


.测试集群


我们去客户端请求两次:


[iyunv@nfs ~]# curl http://172.16.31.180
rs2.stu31.com
[iyunv@nfs ~]# curl http://172.16.31.180
rs1.stu31.com[size=1em]到[size=1em]node2[size=1em]上查看负载均衡状态信息:
[iyunv@node2 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port          Forward Weight ActiveConn InActConn
TCP 172.16.31.180:80 rr
-> 172.16.31.13:80             Route   1      0         1         
-> 172.16.31.14:80             Route   1      0         1[size=1em]我们将后端两台[size=1em]RS[size=1em]服务器的[size=1em]web[size=1em]健康监测页面改名后查看效果:
[iyunv@rs1 ~]# cd /var/www/html/
[iyunv@rs1 html]# mv .health.html a.html
[iyunv@rs2 ~]# cd /var/www/html/
[iyunv@rs2 html]# mv .health.html a.html[size=1em]使用客户端访问:
[iyunv@nfs ~]# curl http://172.16.31.180
sorry page from lvs2集群判断后端的RS都宕机了,就决定启用错误页面的web服务器,返回错误信息给用户。

在节点2上查看负载均衡状态信息:


[iyunv@node2 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port          Forward Weight ActiveConn InActConn
TCP 172.16.31.180:80 rr
-> 127.0.0.1:80                Local   1      0         1        
-> 172.16.31.13:80             Route   0      0         0        
-> 172.16.31.14:80             Route   0      0         0[size=1em]将错误的
[size=1em]RS
[size=1em]服务器权重设置为[size=1em]0[size=1em],将本地[size=1em]httpd[size=1em]服务的权重设为[size=1em]1.

我们再复原:


[iyunv@rs1 html]# mv a.html .health.html
[iyunv@rs2 html]# mv a.html .health.html[size=1em]访问测试:
[iyunv@nfs ~]# curl http://172.16.31.180
rs2.stu31.com
[iyunv@nfs ~]# curl http://172.16.31.180
rs1.stu31.com[size=1em]查看负载均衡状态信息:
[size=1em][iyunv@node2 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port          Forward Weight ActiveConn InActConn
TCP 172.16.31.180:80 rr
-> 172.16.31.13:80             Route   1      0         1        
-> 172.16.31.14:80             Route   1      0         1测试成功。

进行节点切换测试:


将节点2设置为standby,备用节点。


DSC00012.jpg


点击yes后,主节点就到node1启动了:


DSC00013.jpg


进行访问测试:


[iyunv@nfs ~]# curl http://172.16.31.180
rs2.stu31.com
[iyunv@nfs ~]# curl http://172.16.31.180
rs1.stu31.com[size=1em]到节点[size=1em]1[size=1em]查看负载均衡状态:
[size=1em][iyunv@node1 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port          Forward Weight ActiveConn InActConn
TCP 172.16.31.180:80 rr
-> 172.16.31.13:80             Route   1      0         1        
-> 172.16.31.14:80             Route   1      0         1至此,使用heartbeat的ldirectord组件构建ipvs负载均衡集群的高可用性集群就搭建完毕,可以实现集群对后端RS的监控。

转自:http://sohudrgon.blog.iyunv.com/3088108/1599120

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-141764-1-1.html 上篇帖子: 如何实现Linux服务集群的高可用性(heartbeat) 下篇帖子: S3C2440 Linux驱动移植——LED heartbeat
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表