RHEL 5.4下部署LVS(DR)+keepalived实现高性能高可用负载均衡

长平狐 发布于 2012/09/06 15:10
阅读 383
收藏 1

一、简介

LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一。

目前有三种IP负载均衡技术(VS/NAT、VS/TUN和VS/DR);十种调度算法(rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq)。

Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现。

LVS+Keepalived能实现的功能:利用LVS控制器主备模式避免单点故障以及自动删除故障WEB服务器结点并当它恢复后再自动添加到群集中。

拓扑图:

二、系统环境

系统平台:RHEL 5.4

LVS版本:ipvsadm-1.24

keepalived版本:keepalived-1.1.15

三、安装

1、在两台LVS Server上安装lvs、keepalived软件(LVS_MASTER & LVS_BACKUP)
lvs install -------------

[root@LVS-MASTER ~]# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz

[root@LVS-MASTER ~]# ln -s /usr/src/kernels/2.6.18-194.el5-i686//usr/src/linux/

[root@LVS-MASTER ~]# tar zxvf ipvsadm-1.24.tar.gz

[root@LVS-MASTER ~]# cd ipvsadm-1.24

[root@LVS-MASTER ipvsadm-1.24]# make && make install

Keepalived install -------------

[root@LVS-MASTER ~]# wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz

[root@LVS-MASTER ~]# tar zxvf keepalived-1.1.15.tar.gz

[root@LVS-MASTER ~]# cd keepalived-1.1.15

[root@LVS-MASTER keepalived-1.1.15]# ./configure && make && make install

######### 将keepalived做成启动服务,方便管理##########

[root@LVS-MASTER ~]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/

[root@LVS-MASTER ~]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

[root@LVS-MASTER ~]# mkdir /etc/keepalived/

[root@LVS-MASTER ~]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

[root@LVS-MASTER ~]# cp /usr/local/sbin/keepalived /usr/sbin/

[root@LVS-MASTER ~]# service keepalived start | stop

2、keepalived的配置

###################  MASTER ###################

! Configuration File for keepalivedglobal_defs { notification_email { root@linux.tang.chao #设置报警邮件地址,可以设置多个,每行一个。 mchina_tang@qq.com #需开启本机的sendmail服务 } notification_email_from keepalived@localhost #设置邮件的发送地址 smtp_server 127.0.0.1 #设置smtp server地址 smtp_connect_timeout 30 #设置连接smtp server的超时时间 router_id LVS_DEVEL #表示运行keepalived服务器的一个标识。发邮件时显示在邮件主题的信息}vrrp_instance VI_1 { state MASTER #指定keepalived的角色,MASTER表示此主机是主服务器,BACKUP表示此主机是备用服务器 interface eth0 #指定HA监测网络的接口 virtual_router_id 51 #虚拟路由标识,这个标识是一个数字,同一个vrrp实例使用唯一的标识。即同一vrrp_instance下,MASTER和BACKUP必须是一致的 priority 100 #定义优先级,数字越大,优先级越高,在同一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级 advert_int 1 #设定MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒 authentication { #设置验证类型和密码 auth_type PASS #设置验证类型,主要有PASS和AH两种 auth_pass 1111 #设置验证密码,在同一个vrrp_instance下,MASTER与BACKUP必须使用相同的密码才能正常通信 } virtual_ipaddress { #设置虚拟IP地址,可以设置多个虚拟IP地址,每行一个 10.0.0.148 }}virtual_server 10.0.0.148 80 { #设置虚拟服务器,需要指定虚拟IP地址和服务端口,IP与端口之间用空格隔开 delay_loop 6 #设置运行情况检查时间,单位是秒 lb_algo rr #设置负载调度算法,这里设置为rr,即轮询算法 lb_kind DR #设置LVS实现负载均衡的机制,有NAT、TUN、DR三个模式可选 persistence_timeout 50 #会话保持时间,单位是秒。这个选项对动态网页是非常有用的,为集群系统中的session共享提供了一个很好的解决方案。 #有了这个会话保持功能,用户的请求会被一直分发到某个服务节点,直到超过这个会话的保持时间。 #需要注意的是,这个会话保持时间是最大无响应超时时间,也就是说,用户在操作动态页面时,如果50秒内没有执行任何操作, #那么接下来的操作会被分发到另外的节点,但是如果用户一直在操作动态页面,则不受50秒的时间限制 protocol TCP #指定转发协议类型,有TCP和UDP两种 real_server 10.0.0.137 80 { #配置服务节点1,需要指定real server的真实IP地址和端口,IP与端口之间用空格隔开 weight 3 #配置服务节点的权值,权值大小用数字表示,数字越大,权值越高,设置权值大小可以为不同性能的服务器 #分配不同的负载,可以为性能高的服务器设置较高的权值,而为性能较低的服务器设置相对较低的权值,这样才能合理地利用和分配系统资源 TCP_CHECK { #realserver的状态检测设置部分,单位是秒 connect_timeout 10 #表示3秒无响应超时 nb_get_retry 3 #表示重试次数 delay_before_retry 3 #表示重试间隔 connect_port 80 } } real_server 10.0.0.139 80 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } }}

#################### BACKUP ###################

! Configuration File for keepalivedglobal_defs { notification_email { root@linux.tang.chao mchina_tang@qq.com } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL}vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.148 }}virtual_server 10.0.0.148 80 { delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 50 protocol TCP real_server 10.0.0.137 80 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 10.0.0.139 80 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } }}

3、在两台Web Server上执行realserver.sh脚本,为lo:0绑定VIP地址10.0.0.148、抑制ARP广播。

[root@web1 ~]# cat realserver.sh

#!/bin/bash#description: Config realserverVIP=10.0.0.148 /etc/rc.d/init.d/functions case "$1" instart) /sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP /sbin/route add -host $VIP dev lo:0 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce sysctl -p >/dev/null 2> &1 echo "RealServer Start OK" ;;stop) /sbin/ifconfig lo:0 down /sbin/route del $VIP >/dev/null 2> &1 echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce echo "RealServer Stoped" ;;*) echo "Usage: $0 {start|stop}" exit 1esac exit 0

[root@web1 ~]# sh realserver.sh start

4、分别在LVS-MASTER、LVS-BACKUP上执行service keepalived start启动keepalived就可实现负载均衡及高可用集群了;

[root@LVS-MASTER keepalived]# service keepalived start

[root@LVS-MASTER keepalived]# ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.0.0.148:http rr persistent 50
  -> 10.0.0.139:http              Route   3      0          0         
  -> 10.0.0.137:http              Route   3      0          0         
[root@LVS-MASTER keepalived]#

查看日志/var/log/messages

[root@LVS-MASTER keepalived]# tail -n 30/var/log/messages
Apr 19 03:59:58 LVS-MASTER avahi-daemon[2845]: Withdrawing address record for10.0.0.148 on eth0.
Apr 19 04:00:19 LVS-MASTER Keepalived: Starting Keepalived v1.2.1 (04/17,2012)
Apr 19 04:00:19 LVS-MASTER Keepalived: Starting Healthcheck child process,pid=5960
Apr 19 04:00:19 LVS-MASTER Keepalived_healthcheckers: Netlink reflector reportsIP 10.0.0.136 added
Apr 19 04:00:19 LVS-MASTER Keepalived_healthcheckers: Netlink reflector reportsIP 10.0.0.159 added
Apr 19 04:00:19 LVS-MASTER Keepalived_healthcheckers: Registering Kernelnetlink reflector
Apr 19 04:00:19 LVS-MASTER Keepalived_healthcheckers: Registering Kernelnetlink command channel
Apr 19 04:00:19 LVS-MASTER Keepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'.
Apr 19 04:00:19 LVS-MASTER Keepalived_healthcheckers: Configuration is using :12257 Bytes
Apr 19 04:00:19 LVS-MASTER Keepalived_healthcheckers: Using LinkWatch kernelnetlink reflector...
Apr 19 04:00:19 LVS-MASTER Keepalived_healthcheckers: Activating healtcheckerfor service [10.0.0.137:80]
Apr 19 04:00:19 LVS-MASTER Keepalived_healthcheckers: Activating healtcheckerfor service [10.0.0.139:80]
Apr 19 04:00:19 LVS-MASTER Keepalived_vrrp: Netlink reflector reports IP10.0.0.136 added
Apr 19 04:00:19 LVS-MASTER Keepalived_vrrp: Netlink reflector reports IP10.0.0.159 added
Apr 19 04:00:19 LVS-MASTER Keepalived_vrrp: Registering Kernel netlinkreflector
Apr 19 04:00:19 LVS-MASTER Keepalived_vrrp: Registering Kernel netlink commandchannel
Apr 19 04:00:19 LVS-MASTER Keepalived_vrrp: Registering gratutious ARP sharedchannel
Apr 19 04:00:19 LVS-MASTER Keepalived_vrrp: Opening file'/etc/keepalived/keepalived.conf'.
Apr 19 04:00:19 LVS-MASTER Keepalived_vrrp: Configuration is using : 36698Bytes
Apr 19 04:00:19 LVS-MASTER Keepalived_vrrp: Using LinkWatch kernel netlinkreflector...
Apr 19 04:00:19 LVS-MASTER Keepalived_vrrp: VRRP sockpool: [ifindex(2),proto(112), fd(11,12)]
Apr 19 04:00:19 LVS-MASTER Keepalived: Starting VRRP child process, pid=5961
Apr 19 04:00:20 LVS-MASTER Keepalived_vrrp: VRRP_Instance(VI_1) Transition toMASTER STATE
Apr 19 04:00:21 LVS-MASTER Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTERSTATE
Apr 19 04:00:21 LVS-MASTER Keepalived_vrrp: VRRP_Instance(VI_1) settingprotocol VIPs.
Apr 19 04:00:21 LVS-MASTER Keepalived_vrrp: VRRP_Instance(VI_1) Sendinggratuitous ARPs on eth0 for 10.0.0.148
Apr 19 04:00:21 LVS-MASTER Keepalived_healthcheckers: Netlink reflector reportsIP 10.0.0.136 added
Apr 19 04:00:21 LVS-MASTER avahi-daemon[2845]: Registering new address recordfor 10.0.0.148 on eth0.
Apr 19 04:00:21 LVS-MASTER Keepalived_vrrp: Netlink reflector reports IP10.0.0.136 added
Apr 19 04:00:26 LVS-MASTER Keepalived_vrrp: VRRP_Instance(VI_1) Sendinggratuitous ARPs on eth0 for 10.0.0.148
[root@LVS-MASTER keepalived]#

四、测试

接下来做高可用性测试&故障切换测试......

####高可用性测试####

模拟故障,将LVS-MASTER上的keepalived服务停掉,然后观察LVS-BACKUP上的日志,信息如下

[root@LVS-BACKUP keepalived]# tail -f /var/log/messages
Apr 19 03:33:04 LVS-BACKUP Keepalived_vrrp: VRRP_Instance(VI_1)Transition to MASTER STATE
Apr 19 03:33:05 LVS-BACKUP Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTERSTATE
Apr 19 03:33:05 LVS-BACKUP Keepalived_vrrp: VRRP_Instance(VI_1) settingprotocol VIPs.
Apr 19 03:33:05 LVS-BACKUP Keepalived_vrrp: VRRP_Instance(VI_1) Sendinggratuitous ARPs on eth0 for 10.0.0.148
Apr 19 03:33:05 LVS-BACKUP Keepalived_vrrp: Netlink reflector reports IP10.0.0.157 added
Apr 19 03:33:05 LVS-BACKUP Keepalived_healthcheckers: Netlink reflector reportsIP 10.0.0.157 added
Apr 19 03:33:05 LVS-BACKUP avahi-daemon[2825]: Registering new address recordfor 10.0.0.148 on eth0.
Apr 19 03:33:10 LVS-BACKUP Keepalived_vrrp: VRRP_Instance(VI_1) Sendinggratuitous ARPs on eth0 for 10.0.0.148

从日志中可知,主机出现故障后,备机立刻检测到,此时备机变为MASTER角色,并且接管了主机的虚拟IP资源,最后将虚拟IP绑定在etho设备上。

将LVS-MASTER 上的keepalived服务开启后,LVS-BACKUP的日志状态。

Apr 19 03:34:02 LVS-BACKUP Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert
Apr 19 03:34:02 LVS-BACKUP Keepalived_vrrp: VRRP_Instance(VI_1)Entering BACKUP STATE
Apr 19 03:34:02 LVS-BACKUP Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.
Apr 19 03:34:02 LVS-BACKUP Keepalived_vrrp: Netlink reflector reports IP 10.0.0.157 removed
Apr 19 03:34:02 LVS-BACKUP Keepalived_healthcheckers: Netlink reflector reports IP 10.0.0.157 removed
Apr 19 03:34:02 LVS-BACKUP avahi-daemon[2825]: Withdrawing address record for 10.0.0.148 on eth0.

从日志可知,备机在检测到主机重新恢复正常后,释放了虚拟IP资源重新成为BACKUP角色

####故障切换测试####

故障切换是测试当某个节点出现故障后,Keepalived监制模块是否能及时发现然后屏蔽故障节点,同时将服务器转移到正常节点来执行。

将web2节点服务停掉,假设这个节点出现故障,然后主、备机日志信息如下

Apr 19 03:35:04 LVS-MASTER Keepalived_healthcheckers:TCP connection to [10.0.0.139:80] failed!!!
Apr 19 03:35:04 LVS-MASTER Keepalived_healthcheckers:Removing service [10.0.0.139:80] from VS [10.0.0.148:80]
Apr 19 03:35:04 LVS-MASTER Keepalived_healthcheckers: Remote SMTP server [127.0.0.1:25] connected.

[root@LVS-MASTER keepalived]# ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.0.0.148:http rr persistent 50
  -> 10.0.0.137:http              Route   3      0          0         
[root@LVS-MASTER keepalived]#

从以上可以看出,Keepalived监控模块检测到10.0.0.139这台主机出现故障后,将些web2从集群系统中剔除掉了。 此时访问http://10.0.0.148只能看到web1了)

keepalived

重新启动web2节点的服务,日志信息如下:

Apr 19 03:38:22 LVS-MASTER Keepalived_healthcheckers:TCP connection to [10.0.0.139:80] success.
Apr 19 03:38:22 LVS-MASTER Keepalived_healthcheckers:Adding service [10.0.0.139:80] to VS [10.0.0.148:80]
Apr 19 03:38:22 LVS-MASTER Keepalived_healthcheckers: Remote SMTP server [127.0.0.1:25] connected.

[root@LVS-MASTERkeepalived]# ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.0.0.148:http rr persistent 50
  -> 10.0.0.139:http              Route   3      0          0         
  -> 10.0.0.137:http              Route   3      0          0         
[root@LVS-MASTER keepalived]#

Keepalived监控模块检测到10.0.0.139这台主机恢复正常后,又将此节点加入集群系统中,再次访问就可以访问到web2页面了)

lvs

 


CentOS 6.3下部署LVS(NAT)+keepalived实现高性能高可用负载均衡

http://www.cnblogs.com/mchina/archive/2012/08/27/2644391.html


原文链接:http://www.cnblogs.com/mchina/archive/2012/05/23/2514728.html
加载中
返回顶部
顶部