共计 10061 个字符,预计需要花费 26 分钟才能阅读完成。
LVS 是 LINUX VIRTUL SERVER 的简称,是由章文嵩博士主持的著名开放源码项目,一个实现“三高”系统的解决方案。LVS 旨在解决高速发展的 Web 商务中日益凸现的问题:如 何在有限资金投入的情况下,最大幅度的提高 Web 站点的潜在服务性能。核心就是通过一组服务器来进行负载均衡,通过前端的负载调度器(Load Balancer),无缝地将网络请求调度到真实服务器上,从而使得服务器集群的结构对客户是透明的,客户访问集群系统提供的网络服务就像访问一台高性 能、高可用的服务器一样。客户程序不受服务器集群的影响不需作任何修改。
系统的伸缩性通过在服务机群中透明地加入和删除一个节点来达到,通过检测节点或服务进程故障和正确地重置系统达到高可用性。由于我们的负载调度技术是在 Linux 内核中实现的,我们称之为 Linux 虚拟服务器(Linux Virtual Server)。
LVS 的设计根据透明性、性能、高可用性、可管理性和可编程性的指导思想实现的。
我们把前面那台负载均衡机器叫做:director server(DR)。后面的实际服务机器叫做:real server(RS)
架构图:
所需软件:
ipvsadm-1.24-10.x86_64.rpm
heartbeat-2.1.3-3.el5.CentOS.x86_64.rpm
heartbeat-pils-2.1.3-3.el5.centos.x86_64.rpm
heartbeat-stonith-2.1.3-3.el5.centos.x86_64.rpm
PyXML-0.8.4-4.x86_64.rpm
系统环境:CentOS 5.9-64bit
192.168.137.14 虚拟 ip
192.168.137.134 lvsmaster lvs 主
192.168.137.135 lvsbackup lvs 从
192.168.137.130 realserver1 web 服务器
192.168.137.131 realserver2 web 服务器
1、lvs 安装
yum -y install ipvsadmyum -y install libnetyum -y install e2fsprogsyum -y install heartbeat
yum -y install heartbeat-devel
yum -y install heartbeat-ldirectord
echo “192.168.137.135 lvsmaster”>>/etc/hosts
echo “192.168.137.135 lvsbackup”>>/etc/hosts
2、配置步骤主
lvs 配置
(1)lvsdr-basic 脚本配置
[root@lvsmaster ~]#cat /etc/init.d/lvsdr-basic#!/bin/sh
VIP=192.168.137.14
RIP1=192.168.137.130
RIP2=192.168.137.131
/etc/rc.d/init.d/functions
case “$1” in
start)
echo “start [OK]”
/sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eth0:1
echo “1” > /proc/sys/net/ipv4/ip_forward
/sbin/ipvsadm -C
/sbin/ipvsadm -A -t$VIP:80 -s rr
/sbin/ipvsadm -a -t$VIP:80 -r $RIP1:80 -g
/sbin/ipvsadm -a -t$VIP:80 -r $RIP2:80 -g
/sbin/ipvsadm
;;
stop)
echo “stop [OK]”
/sbin/ipvsadm -C
;;
*)
echo “Usage:$0 {start|stop}”
exit 1
esac
[root@lvsmaster ~]#chmod u+x /etc/init.d/lvsdr-basic
(2)ha.cf 配置
[root@lvsmaster ~]#cat /etc/ha.d/ha.cf
debugfile /var/log/ha-debu
logfile /var/log/ha-log
logfacility local0
keepalive 2
warntime 10
deadtime 30
initdead 120
hopfudge 1
udpport 694
bcast eth0
ucast eth0 192.168.137.135
IPauto_failback on
node lvsmaster
node lvsbackup
ping 192.168.137.1
respawn root /usr/lib/heartbeat/ipfail
apiauth ipfail gid=root uid=root
(3)authkeys 认证文件的配置
[root@lvsmaster ~]# cat /etc/ha.d/authkeys
auth 3
3 md5 9bf2f23aae3a63c16ea681295ffd7666[root@lvsmaster ~]#chmod u+x 600 authkeys
(4)ldirectord.cf 文件配置
[root@lvsmaster ~]# cat /etc/ha.d/ldirectord.cf
checktimeout=3
checkinterval=1
autoreload=no
logfile=”/var/log/ldirectord.log”
quiescent=no
virtual=192.168.137.14:80
real=192.168.137.130:80 gate
real=192.168.137.131:80 gate
service=http
request=”test.html”
receive=”Test”
scheduler=sh
protocol=tcp
(5)haresources 文件配置[root@lvsmaster ~]# cat /etc/ha.d/haresources
lvsmaster IPaddr::192.168.137.14/24/eth0 ldirectord lvsdr-basic
(6) 关闭 ldirectord 服务开启 heartbeat 服务。
[root@lvsmaster ~]# chkconfig –del ldirectord[root@lvsmaster ~]# chkconfig –level 2345 ldirectord off[root@lvsmaster ~]# chkconfig –level 2345 heartbeat on
备份 lvs 配置(1)switchdr 配置
[root@lvsbackup ~]#cat /etc/init.d/switchdr#!/bin/sh
# description: close lo0 and arp_ignore
VIP=192.168.137.14
/etc/rc.d/init.d/functions
case “$1” in
start)
echo “************* start director server and close tunl ***********”
/sbin/ifconfig lo:0 down
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
;;
stop)
echo “start Real Server”
/sbin/ifconfig eth0:0 down
/sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP up
/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
;;
*)
echo “Usage: switchdr {start|stop}”
exit 1
esac
[root@lvsbackup ~]#chmod u+x /etc/init.d/switchdr
(2)ha.cf 配置
[root@lvsbackup ~]#cat /etc/ha.d/ha.cf
debugfile /var/log/ha-debu
logfile /var/log/ha-log
logfacility local0
keepalive 2
warntime 10
deadtime 30
initdead 120
hopfudge 1
udpport 694
bcast eth0
ucast eth0 192.168.137.134
auto_failback on
node lvsmaster
node lvsbackup
ping 192.168.137.1
respawn root /usr/lib/heartbeat/ipfail
apiauth ipfail gid=root uid=root
(3)authkeys 配置
[root@lvsbackup ~]# cat /etc/ha.d/authkeys
auth 3
3 md5 9bf2f23aae3a63c16ea681295ffd7666[root@lvsbackup ~]#chmod 600 authkeys
推荐阅读:
Linux 高可用(HA)集群之 heartbeat 基于 crm 进行资源管理详解 http://www.linuxidc.com/Linux/2013-08/89167.htm
Heartbeat+httpd+NFS 实现高可用的 Web 服务器 http://www.linuxidc.com/Linux/2013-08/88520.htm
Linux 高可用(HA)集群之 Heartbeat 详解 http://www.linuxidc.com/Linux/2013-08/88521.htm
Linux 高可用性方案之 Heartbeat 的 CRM 配置 http://www.linuxidc.com/Linux/2012-05/60838.htm
高可用集群 Heartbeat v1 实例 http://www.linuxidc.com/Linux/2013-09/90757.htm
LVS+heartbeat+ldirectord 高可用负载均衡集群解决方案 http://www.linuxidc.com/Linux/2011-09/42911.htm
(4) ldirectord.cf 配置
[root@lvsbackup ~]# cat /etc/ha.d/ldirectord.cf
checktimeout=3
checkinterval=1
autoreload=no
logfile=”/var/log/ldirectord.log”
quiescent=no
virtual=192.168.137.14:80
real=192.168.137.130:80 gate
real=192.168.137.131:80 gate
service=http
request=”test.html”
receive=”Test”
scheduler=sh
protocol=tcp
(5) 配置 haresources
[root@lvsbackup ~]# cat /etc/ha.d/haresources
lvsmaster IPaddr::192.168.137.14/24/eth0 ldirectord switchdr
(6)关闭 ldirectord 服务开启 heartbeat 服务。
[root@lvsbackup ~]# chkconfig –del ldirectord[root@lvsbackup ~]# chkconfig –level 2345 ldirectord off[root@lvsbackup ~]# chkconfig –level 2345 heartbeat on
3、客户端配置 ip 192.168.137.131 192.168.137.130
#cat /etc/init.d/realserver#!/bin/bash
#description : start realserver
VIP=192.168.137.14
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0echo “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#chmod u+x /etc/init.d/realserver 执行
# /etc/init.d/realserver
查看 VIP 虚拟 ip 是否已经绑定:
# ifconfig lo:0
lo:0 Link encap:Local Loopback
inet addr:192.168.137.14 Mask:255.255.255.255
UP LOOPBACK RUNNING MTU:16436 Metric:1
4、启动服务
lvs 主
/etc/init.d/heartbeat start
lvs 备份
/etc/ini.d/switchdr stop
/etc/init.d/heartbeat start
测试(1)停掉主备份会接替主, 当主恢复时备份自动转为备份状态。
(2)停止一台 apache 真实服务器。ipvsadm 将会自动把这一台机器剔除。
得出结论:不断刷新 192.168.137.14,会轮流显示 realserver1 web 和 realserver2 web 页面
LVS 是 LINUX VIRTUL SERVER 的简称,是由章文嵩博士主持的著名开放源码项目,一个实现“三高”系统的解决方案。LVS 旨在解决高速发展的 Web 商务中日益凸现的问题:如 何在有限资金投入的情况下,最大幅度的提高 Web 站点的潜在服务性能。核心就是通过一组服务器来进行负载均衡,通过前端的负载调度器(Load Balancer),无缝地将网络请求调度到真实服务器上,从而使得服务器集群的结构对客户是透明的,客户访问集群系统提供的网络服务就像访问一台高性 能、高可用的服务器一样。客户程序不受服务器集群的影响不需作任何修改。
系统的伸缩性通过在服务机群中透明地加入和删除一个节点来达到,通过检测节点或服务进程故障和正确地重置系统达到高可用性。由于我们的负载调度技术是在 Linux 内核中实现的,我们称之为 Linux 虚拟服务器(Linux Virtual Server)。
LVS 的设计根据透明性、性能、高可用性、可管理性和可编程性的指导思想实现的。
我们把前面那台负载均衡机器叫做:director server(DR)。后面的实际服务机器叫做:real server(RS)
架构图:
所需软件:
ipvsadm-1.24-10.x86_64.rpm
heartbeat-2.1.3-3.el5.CentOS.x86_64.rpm
heartbeat-pils-2.1.3-3.el5.centos.x86_64.rpm
heartbeat-stonith-2.1.3-3.el5.centos.x86_64.rpm
PyXML-0.8.4-4.x86_64.rpm
系统环境:CentOS 5.9-64bit
192.168.137.14 虚拟 ip
192.168.137.134 lvsmaster lvs 主
192.168.137.135 lvsbackup lvs 从
192.168.137.130 realserver1 web 服务器
192.168.137.131 realserver2 web 服务器
1、lvs 安装
yum -y install ipvsadmyum -y install libnetyum -y install e2fsprogsyum -y install heartbeat
yum -y install heartbeat-devel
yum -y install heartbeat-ldirectord
echo “192.168.137.135 lvsmaster”>>/etc/hosts
echo “192.168.137.135 lvsbackup”>>/etc/hosts
2、配置步骤主
lvs 配置
(1)lvsdr-basic 脚本配置
[root@lvsmaster ~]#cat /etc/init.d/lvsdr-basic#!/bin/sh
VIP=192.168.137.14
RIP1=192.168.137.130
RIP2=192.168.137.131
/etc/rc.d/init.d/functions
case “$1” in
start)
echo “start [OK]”
/sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eth0:1
echo “1” > /proc/sys/net/ipv4/ip_forward
/sbin/ipvsadm -C
/sbin/ipvsadm -A -t$VIP:80 -s rr
/sbin/ipvsadm -a -t$VIP:80 -r $RIP1:80 -g
/sbin/ipvsadm -a -t$VIP:80 -r $RIP2:80 -g
/sbin/ipvsadm
;;
stop)
echo “stop [OK]”
/sbin/ipvsadm -C
;;
*)
echo “Usage:$0 {start|stop}”
exit 1
esac
[root@lvsmaster ~]#chmod u+x /etc/init.d/lvsdr-basic
(2)ha.cf 配置
[root@lvsmaster ~]#cat /etc/ha.d/ha.cf
debugfile /var/log/ha-debu
logfile /var/log/ha-log
logfacility local0
keepalive 2
warntime 10
deadtime 30
initdead 120
hopfudge 1
udpport 694
bcast eth0
ucast eth0 192.168.137.135
IPauto_failback on
node lvsmaster
node lvsbackup
ping 192.168.137.1
respawn root /usr/lib/heartbeat/ipfail
apiauth ipfail gid=root uid=root
(3)authkeys 认证文件的配置
[root@lvsmaster ~]# cat /etc/ha.d/authkeys
auth 3
3 md5 9bf2f23aae3a63c16ea681295ffd7666[root@lvsmaster ~]#chmod u+x 600 authkeys
(4)ldirectord.cf 文件配置
[root@lvsmaster ~]# cat /etc/ha.d/ldirectord.cf
checktimeout=3
checkinterval=1
autoreload=no
logfile=”/var/log/ldirectord.log”
quiescent=no
virtual=192.168.137.14:80
real=192.168.137.130:80 gate
real=192.168.137.131:80 gate
service=http
request=”test.html”
receive=”Test”
scheduler=sh
protocol=tcp
(5)haresources 文件配置[root@lvsmaster ~]# cat /etc/ha.d/haresources
lvsmaster IPaddr::192.168.137.14/24/eth0 ldirectord lvsdr-basic
(6) 关闭 ldirectord 服务开启 heartbeat 服务。
[root@lvsmaster ~]# chkconfig –del ldirectord[root@lvsmaster ~]# chkconfig –level 2345 ldirectord off[root@lvsmaster ~]# chkconfig –level 2345 heartbeat on
备份 lvs 配置(1)switchdr 配置
[root@lvsbackup ~]#cat /etc/init.d/switchdr#!/bin/sh
# description: close lo0 and arp_ignore
VIP=192.168.137.14
/etc/rc.d/init.d/functions
case “$1” in
start)
echo “************* start director server and close tunl ***********”
/sbin/ifconfig lo:0 down
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
;;
stop)
echo “start Real Server”
/sbin/ifconfig eth0:0 down
/sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP up
/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
;;
*)
echo “Usage: switchdr {start|stop}”
exit 1
esac
[root@lvsbackup ~]#chmod u+x /etc/init.d/switchdr
(2)ha.cf 配置
[root@lvsbackup ~]#cat /etc/ha.d/ha.cf
debugfile /var/log/ha-debu
logfile /var/log/ha-log
logfacility local0
keepalive 2
warntime 10
deadtime 30
initdead 120
hopfudge 1
udpport 694
bcast eth0
ucast eth0 192.168.137.134
auto_failback on
node lvsmaster
node lvsbackup
ping 192.168.137.1
respawn root /usr/lib/heartbeat/ipfail
apiauth ipfail gid=root uid=root
(3)authkeys 配置
[root@lvsbackup ~]# cat /etc/ha.d/authkeys
auth 3
3 md5 9bf2f23aae3a63c16ea681295ffd7666[root@lvsbackup ~]#chmod 600 authkeys
推荐阅读:
Linux 高可用(HA)集群之 heartbeat 基于 crm 进行资源管理详解 http://www.linuxidc.com/Linux/2013-08/89167.htm
Heartbeat+httpd+NFS 实现高可用的 Web 服务器 http://www.linuxidc.com/Linux/2013-08/88520.htm
Linux 高可用(HA)集群之 Heartbeat 详解 http://www.linuxidc.com/Linux/2013-08/88521.htm
Linux 高可用性方案之 Heartbeat 的 CRM 配置 http://www.linuxidc.com/Linux/2012-05/60838.htm
高可用集群 Heartbeat v1 实例 http://www.linuxidc.com/Linux/2013-09/90757.htm
LVS+heartbeat+ldirectord 高可用负载均衡集群解决方案 http://www.linuxidc.com/Linux/2011-09/42911.htm