作者:NetSeek http://www.linuxtone.org(IT运维专家网|集群架构|性能调优)
欢迎转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明.
更新时间:2008-10-27
背景:
随着你的网站业务量的增长你网站的服务器压力越来越大?需要负载均衡方案!商业的硬件如F5又太贵,你们又是创业型互联公司如何有效节约成本,节省不必要的浪费?同时实现商业硬件一样的高性能高可用的功能?有什么好的负载均衡可伸张可扩展的方案吗?答案是肯定的!有!我们利用LVS+Keepalived基于完整开源软件的架构可以为你提供一个负载均衡及高可用的服务器。
LVS+Keepalived 介绍
LVS
LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项 目之一。目前有三种IP负载均衡技术(VS/NAT、VS/TUN和VS/DR);
十种调度算法(rrr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq)。
Keepalvied
Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现
二. 网站负载均衡拓朴图
IP信息列表:
名称 IP
LVS-DR-Master 61.164.122.6
LVS-DR-BACKUP 61.164.122.7
LVS-DR-VIP 61.164.122.8
WEB1-Realserver 61.164.122.9
WEB2-Realserver 61.164.122.10
GateWay 61.164.122.1
复制代码
三. 安装LVS和Keepalvied软件包
1. 下载相关软件包
#mkdir /usr/local/src/lvs
#cd /usr/local/src/lvs
#wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
#wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
复制代码
2. 安装LVS和Keepalived
#lsmod |grep ip_vs
#uname -r
2.6.18-53.el5PAE
#ln -s /usr/src/kernels/2.6.18-53.el5PAE-i686/ /usr/src/linux
#tar zxvf ipvsadm-1.24.tar.gz
#cd ipvsadm-1.24
#make && make install
#find / -name ipvsadm # 查看ipvsadm的位置
#tar zxvf keepalived-1.1.15.tar.gz
#cd keepalived-1.1.15
#./configure && make && make install
#find / -name keepalived # 查看keepalived位置
#cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
#cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
#mkdir /etc/keepalived
#cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
#cp /usr/local/sbin/keepalived /usr/sbin/
#service keepalived start|stop #做成系统启动服务方便管理.
复制代码
四. 配置LVS实现负载均衡
1. LVS-DR,配置LVS脚本实现负载均衡
注: 此脚本在于演示方便大家理解lvs,在keepalived方案中不要启动此脚本,所以看后面有注掉,很多朋友对此有问题。关于LVS的keepalvied的HA方案,完全由keepalived.conf一个文件搞定,特此声明!
#vi /usr/local/sbin/lvs-dr.sh
#!/bin/bash
# description: start LVS of DirectorServer
#Written by :NetSeek http://www.linuxtone.org
GW=61.164.122.1
# website director vip.
SNS_VIP=61.164.122.8
SNS_RIP1=61.164.122.9
SNS_RIP2=61.164.122.10
./etc/rc.d/init.d/functions
logger $0 called with $1
case "$1" in
start)
# set squid vip
/sbin/ipvsadm --set 30 5 60
/sbin/ifconfig eth0:0 $SNS_VIP broadcast $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP up
/sbin/route add -host $SNS_VIP dev eth0:0
/sbin/ipvsadm -A -t $SNS_VIP:80 -s wrr -p 3
/sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP1:80 -g -w 1
/sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP2:80 -g -w 1
touch /var/lock/subsys/ipvsadm >/dev/null 2>&1
;;
stop)
/sbin/ipvsadm -C
/sbin/ipvsadm -Z
ifconfig eth0:0 down
route del $SNS_VIP
rm -rf /var/lock/subsys/ipvsadm >/dev/null 2>&1
echo "ipvsadm stoped"
;;
status)
if [ ! -e /var/lock/subsys/ipvsadm ];then
echo "ipvsadm stoped"
exit 1
else
echo "ipvsadm OK"
fi
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
esac
exit 0
复制代码
2. 配置Realserver脚本.
#vi /usr/local/sbin/realserver.sh
#!/bin/bash
# description: Config realserver lo and apply noarp
#Written by :NetSeek http://www.linuxtone.org
SNS_VIP=61.164.122.8
. /etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
/sbin/route add -host $SNS_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)
ifconfig lo:0 down
route del $SNS_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 1
esac
exit 0
复制代码
或者采用secondary ip address方式配置
# vi /etc/sysctl.conf
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
#sysctl ?p
#ip addr add 61.164.122.8/32 dev lo
#ip add list 查看是否绑定
3. 启动lvs-dr脚本和realserver启本,在DR上可以查看LVS当前状态:
#watch ipvsadm ?ln
复制代码
五.利用Keepalvied实现负载均衡和和高可用性
1.配置在主负载均衡服务器上配置keepalived.conf
#vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
cnseek@gmail.com
}
notification_email_from sns-lvs@gmail.com
smtp_server 127.0.0.1
# smtp_connect_timeout 30
router_id LVS_DEVEL
}
# 20081013 written by :netseek
# VIP1
vrrp_instance VI_1 {
state MASTER #备份服务器上将MASTER改为BACKUP
interface eth0
virtual_router_id 51
priority 100 # 备份服务上将100改为99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
61.164.122.8
#(如果有多个VIP,继续换行填写.)
}
}
virtual_server 61.164.122.8 80 {
delay_loop 6 #(每隔10秒查询realserver状态)
lb_algo wrr #(lvs 算法)
lb_kind DR #(Direct Route)
persistence_timeout 60 #(同一IP的连接60秒内被分配到同一台realserver)
protocol TCP #(用TCP协议检查realserver状态)
real_server 61.164.122.9 80 {
weight 3 #(权重)
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 61.164.122.10 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
复制代码
2. BACKUP服务器同上配置,先安装lvs再按装keepalived,仍后配置/etc/keepalived/keepalived.conf,只需将红色标示的部分改一下即可.
3. 停掉lvs_dr.sh演示脚本
vi /etc/rc.local
#/usr/local/sbin/lvs-dr.sh 将lvs-dr.sh这个脚本注释掉。
#/usr/local/sbin/lvs-dr.sh stop 停止lvs-dr脚本
#/etc/init.d/keepalived start 启动keepalived 服务,keepalived就能利用keepalived.conf 配置文件,实现负载均衡和高可用.
整个LVS负均衡HA方案,由keepalived.conf一个文件即可搞定!
4. 查看lvs服务是否正常
#watch ipvsadm ?ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddressort Scheduler Flags
-> RemoteAddressort Forward Weight ActiveConn InActConn
TCP 61.164.122.8:80 wrr persistent 60
-> 61.164.122.10:80 Route 3 0 0
-> 61.164.122.9:80 Route 3 0 0
复制代码
#tail ?f /var/log/message 监听日志,查看状态,测试LVS负载均衡及高可用性是否有效。
5.停Master服务器的keepalived服务,查看BAKCUP服务器是否能正常接管服务。
四.相关参考
1.LVS 基础知识汇总
LVS的算法介绍 http://bbs.linuxtone.org/viewthread.php?tid=69
学习LVS的三种转发模式 http://bbs.linuxtone.org/viewthread.php?tid=77
LVS中的IP负载均衡技术 http://bbs.linuxtone.org/viewthread.php?tid=68
更多的请到http://www.linuxtone.org 负载均衡版查看
Keepalived 相关参考资料。
http://www.keepalived.org/documentation.html
提供DOC文档下载:
PDF下载: http://docs.linuxtone.org/ebooks/load%20balance/lvs/
欢迎转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明.
更新时间:2008-10-27
背景:
随着你的网站业务量的增长你网站的服务器压力越来越大?需要负载均衡方案!商业的硬件如F5又太贵,你们又是创业型互联公司如何有效节约成本,节省不必要的浪费?同时实现商业硬件一样的高性能高可用的功能?有什么好的负载均衡可伸张可扩展的方案吗?答案是肯定的!有!我们利用LVS+Keepalived基于完整开源软件的架构可以为你提供一个负载均衡及高可用的服务器。
LVS+Keepalived 介绍
LVS
LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项 目之一。目前有三种IP负载均衡技术(VS/NAT、VS/TUN和VS/DR);
十种调度算法(rrr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq)。
Keepalvied
Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现
二. 网站负载均衡拓朴图
IP信息列表:
名称 IP
LVS-DR-Master 61.164.122.6
LVS-DR-BACKUP 61.164.122.7
LVS-DR-VIP 61.164.122.8
WEB1-Realserver 61.164.122.9
WEB2-Realserver 61.164.122.10
GateWay 61.164.122.1
复制代码
三. 安装LVS和Keepalvied软件包
1. 下载相关软件包
#mkdir /usr/local/src/lvs
#cd /usr/local/src/lvs
#wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
#wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
复制代码
2. 安装LVS和Keepalived
#lsmod |grep ip_vs
#uname -r
2.6.18-53.el5PAE
#ln -s /usr/src/kernels/2.6.18-53.el5PAE-i686/ /usr/src/linux
#tar zxvf ipvsadm-1.24.tar.gz
#cd ipvsadm-1.24
#make && make install
#find / -name ipvsadm # 查看ipvsadm的位置
#tar zxvf keepalived-1.1.15.tar.gz
#cd keepalived-1.1.15
#./configure && make && make install
#find / -name keepalived # 查看keepalived位置
#cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
#cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
#mkdir /etc/keepalived
#cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
#cp /usr/local/sbin/keepalived /usr/sbin/
#service keepalived start|stop #做成系统启动服务方便管理.
复制代码
四. 配置LVS实现负载均衡
1. LVS-DR,配置LVS脚本实现负载均衡
注: 此脚本在于演示方便大家理解lvs,在keepalived方案中不要启动此脚本,所以看后面有注掉,很多朋友对此有问题。关于LVS的keepalvied的HA方案,完全由keepalived.conf一个文件搞定,特此声明!
#vi /usr/local/sbin/lvs-dr.sh
#!/bin/bash
# description: start LVS of DirectorServer
#Written by :NetSeek http://www.linuxtone.org
GW=61.164.122.1
# website director vip.
SNS_VIP=61.164.122.8
SNS_RIP1=61.164.122.9
SNS_RIP2=61.164.122.10
./etc/rc.d/init.d/functions
logger $0 called with $1
case "$1" in
start)
# set squid vip
/sbin/ipvsadm --set 30 5 60
/sbin/ifconfig eth0:0 $SNS_VIP broadcast $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP up
/sbin/route add -host $SNS_VIP dev eth0:0
/sbin/ipvsadm -A -t $SNS_VIP:80 -s wrr -p 3
/sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP1:80 -g -w 1
/sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP2:80 -g -w 1
touch /var/lock/subsys/ipvsadm >/dev/null 2>&1
;;
stop)
/sbin/ipvsadm -C
/sbin/ipvsadm -Z
ifconfig eth0:0 down
route del $SNS_VIP
rm -rf /var/lock/subsys/ipvsadm >/dev/null 2>&1
echo "ipvsadm stoped"
;;
status)
if [ ! -e /var/lock/subsys/ipvsadm ];then
echo "ipvsadm stoped"
exit 1
else
echo "ipvsadm OK"
fi
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
esac
exit 0
复制代码
2. 配置Realserver脚本.
#vi /usr/local/sbin/realserver.sh
#!/bin/bash
# description: Config realserver lo and apply noarp
#Written by :NetSeek http://www.linuxtone.org
SNS_VIP=61.164.122.8
. /etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
/sbin/route add -host $SNS_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)
ifconfig lo:0 down
route del $SNS_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 1
esac
exit 0
复制代码
或者采用secondary ip address方式配置
# vi /etc/sysctl.conf
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
#sysctl ?p
#ip addr add 61.164.122.8/32 dev lo
#ip add list 查看是否绑定
3. 启动lvs-dr脚本和realserver启本,在DR上可以查看LVS当前状态:
#watch ipvsadm ?ln
复制代码
五.利用Keepalvied实现负载均衡和和高可用性
1.配置在主负载均衡服务器上配置keepalived.conf
#vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
cnseek@gmail.com
}
notification_email_from sns-lvs@gmail.com
smtp_server 127.0.0.1
# smtp_connect_timeout 30
router_id LVS_DEVEL
}
# 20081013 written by :netseek
# VIP1
vrrp_instance VI_1 {
state MASTER #备份服务器上将MASTER改为BACKUP
interface eth0
virtual_router_id 51
priority 100 # 备份服务上将100改为99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
61.164.122.8
#(如果有多个VIP,继续换行填写.)
}
}
virtual_server 61.164.122.8 80 {
delay_loop 6 #(每隔10秒查询realserver状态)
lb_algo wrr #(lvs 算法)
lb_kind DR #(Direct Route)
persistence_timeout 60 #(同一IP的连接60秒内被分配到同一台realserver)
protocol TCP #(用TCP协议检查realserver状态)
real_server 61.164.122.9 80 {
weight 3 #(权重)
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 61.164.122.10 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
复制代码
2. BACKUP服务器同上配置,先安装lvs再按装keepalived,仍后配置/etc/keepalived/keepalived.conf,只需将红色标示的部分改一下即可.
3. 停掉lvs_dr.sh演示脚本
vi /etc/rc.local
#/usr/local/sbin/lvs-dr.sh 将lvs-dr.sh这个脚本注释掉。
#/usr/local/sbin/lvs-dr.sh stop 停止lvs-dr脚本
#/etc/init.d/keepalived start 启动keepalived 服务,keepalived就能利用keepalived.conf 配置文件,实现负载均衡和高可用.
整个LVS负均衡HA方案,由keepalived.conf一个文件即可搞定!
4. 查看lvs服务是否正常
#watch ipvsadm ?ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddressort Scheduler Flags
-> RemoteAddressort Forward Weight ActiveConn InActConn
TCP 61.164.122.8:80 wrr persistent 60
-> 61.164.122.10:80 Route 3 0 0
-> 61.164.122.9:80 Route 3 0 0
复制代码
#tail ?f /var/log/message 监听日志,查看状态,测试LVS负载均衡及高可用性是否有效。
5.停Master服务器的keepalived服务,查看BAKCUP服务器是否能正常接管服务。
四.相关参考
1.LVS 基础知识汇总
LVS的算法介绍 http://bbs.linuxtone.org/viewthread.php?tid=69
学习LVS的三种转发模式 http://bbs.linuxtone.org/viewthread.php?tid=77
LVS中的IP负载均衡技术 http://bbs.linuxtone.org/viewthread.php?tid=68
更多的请到http://www.linuxtone.org 负载均衡版查看
Keepalived 相关参考资料。
http://www.keepalived.org/documentation.html
提供DOC文档下载:
PDF下载: http://docs.linuxtone.org/ebooks/load%20balance/lvs/
相关推荐
搭建LVS+KEEPALIVED负载均衡 基于LVS(Linux Virtual Server)和KEEPALIVED的负载均衡技术,可以实现高可用性和高性能的服务器集群。下面是搭建LVS+KEEPALIVED负载均衡的详细步骤和知识点: 一、 环境准备 1. ...
随着你的网站业务量的增长你网站的服务器压力越来越大?需要负载均衡方案!商业的硬件如F5又太贵,你们又是创业型互联公司...我们利用LVS+Keepalived基于完整开源软件的架构可以为你提供一个负载均衡及高可用的服务器。
本篇文档为Mysql双主热备+LVS+Keepalived高可用操作记录,可作为线上实操手册,有需要的朋友可以拿走,希望能帮助到有用到的人~
LVS-DR+Keepalived小型网站架构(6台).md
本篇文章详细记录了Mysql双主热备+LVS+Keepalived高可用操作过程,可作为线上长期的实操手册.特此分享,希望能帮助到有用到的朋友.
【LVS+Keepalived+MySQL半同步主主复制高可用方案】 1. 方案概述 LVS(Linux Virtual Server)结合Keepalived构建的高可用解决方案,通常用于实现负载均衡和故障转移,以提高系统的整体可用性。在这个方案中,MySQL...
Lvs抗负载能力强,因为 lvs 工作方式的逻辑是非常之简单,而且工作在网络 4 层仅做请求分发之用,没有流量,所以在效率上基本不...文档简单演示了lvs+keepalived的搭建过程和负载测试,顺便记录下来。每天进步一点。
在本文档中,我们将介绍如何使用 LVS+Keepalived 实现网站负载均衡。我们的拓扑图如下所示: * LVS(master):eth0:172.24.100.6, eth1:202.168.128.101 * LVS(backup):eth0:172.24.100.7, eth1:202.168.128....
lvs+Keepalived+nginx高可用负载均衡搭建部署方案
### MySQL LVS+Keepalived+MHA 高可用群集应用部署操作手册知识点解析 #### MHA 架构介绍与工作原理 - **MHA(Master High Availability)概述** - MHA 是一套用于MySQL高可用环境下的故障切换和主从提升方案,由...
Keepalived则是一款网络服务监控程序,常与LVS配合使用,确保主服务器失效时能够自动切换到备份服务器,保证服务的不间断运行。 在LVS+keepalived负载均衡系统中,有几个核心概念和技术要点: 1. LVS工作模式:LVS...
在构建大型、高可用性的FTP(File ...通过上述步骤,我们可以构建一个基于lvs+keepalived+vsftp的FTP服务器负载均衡环境,提供高可用性和良好的扩展性。记住,实施过程中应根据实际需求和服务器环境进行适当的调整。
"LVS+KeepAlived+Nginx高可用实现方案" LVS(Linux Virtual Server)是一种虚拟服务器集群系统,旨在提供高性能、高可用的服务器解决方案。其主要特点包括可伸缩性、可靠性和可管理性。LVS通过IP负载均衡技术实现...
### lvs+keepalived+mha+mysql高可用架构配置说明 #### 第一部分 MHA介绍 **MHA**(Master High Availability)作为一种成熟的MySQL高可用性解决方案,它由日本开发者Youshimaton创建,旨在为MySQL环境提供故障...
LVS+Keepalived+Nginx+Tomcat 高可用集群项目 本文主要讲述了如何构建一个高可用集群项目,使用 LVS、Keepalived、Nginx 和 Tomcat 实现高可用性和负载均衡。该项目的架构中,Keepalived 负责对 LVS 架构中的调度器...
HAProxy+Keepalived+LVS实现负载平衡高可用集群最佳实践
【负载均衡--LVS+Keepalived(终极文档)】 负载均衡是解决高并发访问和提高服务可用性的重要手段。在本文档中,我们将深入探讨如何利用开源软件LVS(Linux Virtual Server)和Keepalived来构建一个高性能、高可用的...