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

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

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-1-5 08:46:27 | 显示全部楼层 |阅读模式
wKioL1SpPqTyx0lLAAGbPd8nXDo770.jpg
网络规划:
两台LVS server:(两台LVS也可以为用户提供错误页面)
         node1:172.16.31.10
         node2:172.16.31.11
VIP:172.16.31.180

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

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


一.配置HA集群前提:
1、节点之间时间必须同步;
建议使用ntp协议进行;
2、节点之间必须要通过主机名互相通信;
建议使用hosts文件;
通信中使用的名字必须与其节点为上“uname -n”命令展示出的名字保持一致;
3、如果是2个节点,需要仲裁设备;
4、节点之间彼此root用户能基于ssh密钥方式进行通信;

HA节点之间传递公钥:node1和node2配置,实现ssh无密钥通信
1
2
3
4
[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




四个节点的hosts文件相同:
1
2
3
4
5
6
7
8
[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




HA节点间实现无密钥通信:测试时间一致
1
2
3
[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程序包组:
1
2
3
4
5
6
[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




安装程序包组:
node1和node2都需要安装;
前提条件需要安装如下依赖包:
1
[iyunv@node1 heartbeat2]# yum install -ynet-snmp-libs libnet PyXML




3.安装heartbeat套件程序:
1
2
3
4
5
[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




有依赖关系,解决依赖关系:node1和node2都安装
1
# yum -y install  ipvsadm  perl-MailTools  perl-TimeDate




再次安装:
1
2
3
4
5
6
7
8
# 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%]




安装完成!

查看ldirectord生成的文件:
1
2
3
4
5
6
7
8
9
10
[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




存在模版配置文件。

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

三.heartbeat配置:
1.系统日志记录heartbeat日志
1
2
3
4
5
[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




2.拷贝配置文件模版到/etc/ha.d目录
1
2
[iyunv@node1 ha.d]# cd /usr/share/doc/heartbeat-2.1.4/
[iyunv@node1 heartbeat-2.1.4]# cp authkeysha.cf /etc/ha.d/



主配置文件:
1
2
3
4
5
6
7
8
[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




认证文件:
1
2
3
[iyunv@node1 ha.d]# vim authkeys
auth 2
2    sha1   password



权限必须是600或者400:
[iyunv@node1 ha.d]# chmod 600 authkeys

将主配置文件和认证文件拷贝到node2
1
2
3
4
[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]#





四.配置RS Server:即WEB服务器
1.提供了一个脚本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[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




在两台RS 上执行启动。

保证RS能提供WEB服务:
1
2
[iyunv@rs1 ~]# echo "rs1.stu31.com" > /var/www/html/index.html
[iyunv@rs2 ~]# echo "rs2.stu31.com" > /var/www/html/index.html




启动httpd服务测试:
1
2
3
4
5
6
7
8
[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




测试完成后就将httpd服务停止,关闭自启动:
1
2
# service httpd stop                                    
# chkconfig httpd off




RS就设置好了。


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

1.手动配置好lvs-dr负载均衡集群:
1
2
3
4
5
6
7
8
9
10
11
12
13
[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





2.访问测试:
1
2
3
4
[iyunv@nfs ~]# curl http://172.16.31.180
rs2.stu31.com
[iyunv@nfs ~]# curl http://172.16.31.180
rs1.stu31.com




负载均衡实现了:
1
2
3
4
5
6
7
[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




在节点上清除配置的规则:

1
2
3
[iyunv@node1 ~]# ipvsadm -C
[iyunv@node1 ~]# route del -host 172.16.31.180
[iyunv@node1 ~]# ifconfig eth0:0 down




第二个LVS Server也同样测试一遍,测试成功后清除配置的规则。

3.在LVS上设置错误页面:配置LVS为sorry server,为用户提供错误提示页
1
2
[iyunv@node1 ~]# echo "sorry page fromlvs1" > /var/www/html/index.html
[iyunv@node2 ~]# echo "sorry page fromlvs2" > /var/www/html/index.html



启动web服务测试错误页面正常:
1
2
3
4
5
6
7
8
[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





4.将LVS的两个节点配置为HA集群:
拷贝ldirectord软件安装后的配置文件模版:
1
2
3
4
[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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[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上配置:
1
2
[iyunv@rs1 ~]# echo "OK" > /var/www/html/.health.html
[iyunv@rs2 ~]# echo "OK" > /var/www/html/.health.html




复制一份ldirectord.cf配置文件到node2:
1
2
[iyunv@node1 ha.d]# scp ldirectord.cf node2:/etc/ha.d/
ldirectord.cf                                 100% 7553     7.4KB/s  00:00





启动heartbeat服务:
1
2
3
4
5
6
[iyunv@node1 ha.d]# service heartbeat start;ssh node2 'service heartbeat start'
Starting High-Availability services:
Done.
  
Starting High-Availability services:
Done.




查看监听端口:
1
2
[iyunv@node1 ha.d]# ss -tunl |grep 5560
tcp   LISTEN     0      10                     *:5560                  *:*





六.资源配置
需要对heartbeat图形界面登录用户授权加密:node2也需要设置密码。
1
2
3
[iyunv@node1 ~]# echo oracle |passwd --stdin  hacluster
Changing password for user hacluster.
passwd: all authentication tokens updatedsuccessfully.




进入图形化配置端配置资源:
输入如下指令:需要桌面支持。
#hb_gui &


wKioL1SpQYDADsxpAAIGid61qsY542.jpg
配置集群资源组:新建资源组
wKiom1SpQNagQ-OgAAG3BM4va4M209.jpg
wKioL1SpQaOCPcIkAAGrAk9_7po074.jpg

新建资源,VIP的定义:
wKiom1SpQPmDWtfFAALTIHuSla4209.jpg


添加新资源:
wKioL1SpQdPA8fR9AAIMVisAgu8171.jpg

wKioL1SpQeqwge3-AAIQLCspnJA753.jpg


新建一个ipvs规则的资源:
wKiom1SpQUXhLZiPAAKDfMG107s332.jpg

注意ldirecotord.cf这个配置文件在哪个目录:
wKiom1SpQWPirSL8AAKe-C-IoD8749.jpg


添加完成后就add:
wKiom1SpQXnxGEnKAAKPiEOpwwk093.jpg


定义完成后就启动资源组ipvs:
wKioL1SpQk2gpw2wAAJ3rprFG7k464.jpg


现在资源启动于node2:
wKiom1SpQabBCvlpAAJgTud4V_E509.jpg

七.测试集群
我们去客户端请求两次:
1
2
3
4
[iyunv@nfs ~]# curl http://172.16.31.180
rs2.stu31.com
[iyunv@nfs ~]# curl http://172.16.31.180
rs1.stu31.com




到node2上查看负载均衡状态信息:
1
2
3
4
5
6
7
[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





我们将后端两台RS服务器的web健康监测页面改名后查看效果:
1
2
3
4
[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



使用客户端访问:
1
2
[iyunv@nfs ~]# curl http://172.16.31.180
sorry page from lvs2




集群判断后端的RS都宕机了,就决定启用错误页面的web服务器,返回错误信息给用户。

在节点2上查看负载均衡状态信息:
1
2
3
4
5
6
7
8
[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




将错误的RS服务器权重设置为0,将本地httpd服务的权重设为1.

我们再复原:
1
2
[iyunv@rs1 html]# mv a.html .health.html
[iyunv@rs2 html]# mv a.html .health.html



访问测试:
1
2
3
4
[iyunv@nfs ~]# curl http://172.16.31.180
rs2.stu31.com
[iyunv@nfs ~]# curl http://172.16.31.180
rs1.stu31.com




查看负载均衡状态信息:
1
2
3
4
5
6
7
[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,备用节点。
wKioL1SpQwOzKy-aAAJPiqBJQwM249.jpg

点击yes后,主节点就到node1启动了:
wKiom1SpQmXCg6z5AAKLWBG5Ock537.jpg
进行访问测试:

1
2
3
4
[iyunv@nfs ~]# curl http://172.16.31.180
rs2.stu31.com
[iyunv@nfs ~]# curl http://172.16.31.180
rs1.stu31.com




到节点1查看负载均衡状态:
1
2
3
4
5
6
7
[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的监控。




运维网声明 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-40211-1-1.html 上篇帖子: corosync+pacemaker+crmsh+DRBD实现数据库服务器高可用集群构建 下篇帖子: 千万并发压测环境php5.4.34+opcache环境整合
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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