LVS大家应该都有所耳闻,它是一个很好实现负载均衡的平台。节约成本,又容易操作。是一款经济实用的虚拟软件,而且也飞上容易上手。那么,我们现在就来具体叙述一下实现负载均衡的代码,其中我们把具体的步骤分为三大部分,结构很清晰。
IP明细: 1.LVS-DR-Master 61.164.122.6 2.LVS-DR-BACKUP 61.164.122.7 3.LVS-DR-VIP 61.164.122.8 4.WEB1-Realserver 61.164.122.9 5.WEB2-Realserver 61.164.122.10 6.GateWay 61.164.122.1
安装LVS和Keepalvied软件包
1.下载相关软件包
1.#mkdir /usr/local/src/lvs 2.#cd /usr/local/src/lvs 3.#wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz 4.#wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
2.安装LVS和Keepalived
1.#lsmod |grep ip_vs 2.#uname -r 3.2.6.18-53.el5PAE 4.#ln -s /usr/src/kernels/2.6.18-53.el5PAE-i686//usr/src/linux 5. 6.#tar zxvf ipvsadm-1.24.tar.gz 7.#cd ipvsadm-1.24 8.#make && make install 9.#find / -name ipvsadm# 查看ipvsadm的位置 10. 11.#tar zxvf keepalived-1.1.15.tar.gz 12.#cd keepalived-1.1.15 13.#./configure&& make && make install 14.#find / -name keepalived# 查看keepalived位置 15. 16.#cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ 17.#cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ 18.#mkdir /etc/keepalived 19.#cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ 20.#cp /usr/local/sbin/keepalived /usr/sbin/ 21.#service keepalived start|stop#做成系统启动服务方便管理.
#p#配置LVS实现负载均衡 1.LVS-DR,配置LVS脚本实现负载均衡 注: 此脚本在于演示方便大家理解lvs,在keepalived方案中不要启动此脚本,所以看后面有注掉,很多朋友对此有问题。关于LVS的 keepalvied的HA方案,完全由keepalived.conf 一个文件搞定,特此声明!
1.#vi /usr/local/sbin/lvs-dr.sh 2.#!/bin/bash 3.# description: start LVS of DirectorServer 4.#Written by :NetSeek http://www.linuxtone.org 5. 6.GW=61.164.122.1 7.# website director vip. 8.SNS_VIP=61.164.122.8 9.SNS_RIP1=61.164.122.9 10.SNS_RIP2=61.164.122.10 11. 12../etc/rc.d/init.d/functions 13. 14.logger $0 called with $1 15. 16.case "$1" in 17. 18.start) 19.# set squid vip 20./sbin/ipvsadm --set 30 5 60 21./sbin/ifconfig eth0:0 $SNS_VIP broadcast $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP up 22./sbin/route add -host $SNS_VIP dev eth0:0 23./sbin/ipvsadm -A -t $SNS_VIP:80 -s wrr -p 3 24./sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP1:80 -g -w 1 25./sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP2:80 -g -w 1 26.touch /var/lock/subsys/ipvsadm >/dev/null 2>&1 27. 28.;; 29.stop) 30./sbin/ipvsadm -C 31./sbin/ipvsadm -Z 32.ifconfig eth0:0 down 33.route del $SNS_VIP 34.rm -rf /var/lock/subsys/ipvsadm >/dev/null 2>&1 35.echo "ipvsadm stoped" 36.;; 37. 38.status) 39. 40.if [ ! -e /var/lock/subsys/ipvsadm ];then 41.echo "ipvsadm stoped" 42.exit 1 43.else 44.echo "ipvsadm OK" 45.fi 46.;; 47. 48.*) 49.echo "Usage: $0 {start|stop|status}" 50.exit 1 51.esac 52. 53.exit 0 54.
2.配置Realserver脚本.
1.#vi /usr/local/sbin/realserver.sh 2.#!/bin/bash 3.# description: Config realserver lo and apply noarp 4.#Written by :NetSeek http://www.linuxtone.org 5. 6.SNS_VIP=61.164.122.8 7. 8../etc/rc.d/init.d/functions 9. 10.case "$1" in 11.start) 12.ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP 13./sbin/route add -host $SNS_VIP dev lo:0 14.echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore 15.echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce 16.echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore 17.echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce 18.sysctl -p >/dev/null 2>&1 19.echo "RealServer Start OK" 20. 21.;; 22.stop) 23.ifconfig lo:0 down 24.route del $SNS_VIP >/dev/null 2>&1 25.echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore 26.echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce 27.echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore 28.echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce 29.echo "RealServer Stoped" 30.;; 31.*) 32.echo "Usage: $0 {start|stop}" 33.exit 1 34.esac 35. 36.exit 0
或者采用secondary ip address方式配置
# vi /etc/sysctl.conf 1.net.ipv4.conf.lo.arp_ignore = 1 2.net.ipv4.conf.lo.arp_announce = 2 3.net.ipv4.conf.all.arp_ignore = 1 4.net.ipv4.conf.all.arp_announce = 2 5.[code] 6.#sysctl –p 7.#ip addr add 61.164.122.8/32 dev lo 8.#ip add list 查看是否绑定 9.3.启动lvs-dr脚本和realserver启本,在DR上可以查看LVS当前状态: 10.#watch ipvsadm –ln
#p#利用Keepalvied实现负载均衡和和高可用性 1.配置在主负载均衡服务器上配置keepalived.conf
#vi /etc/keepalived/keepalived.conf 1.! Configuration File for keepalived 2. 3.global_defs { 4.notification_email { 5.cnseek@gmail.com 6.} 7.notification_email_from sns-lvs@gmail.com 8.smtp_server 127.0.0.1 9.# smtp_connect_timeout 30 10.router_id LVS_DEVEL 11.} 12. 13.# 20081013 written by :netseek 14.# VIP1 15.vrrp_instance VI_1 { 16.state MASTER#备份服务器上将MASTER改为BACKUP 17.interface eth0 18.virtual_router_id 51 19.priority 100# 备份服务上将100改为99 20.advert_int 1 21.authentication { 22.auth_type PASS 23.auth_pass 1111 24.} 25.virtual_ipaddress { 26.61.164.122.8 27.#(如果有多个VIP,继续换行填写.) 28.} 29.} 30. 31.virtual_server 61.164.122.8 80 { 32.delay_loop 6 #(每隔10秒查询realserver状态) 33.lb_algo wrr #(lvs 算法) 34.lb_kind DR #(Direct Route) 35.persistence_timeout 60#(同一IP的连接60秒内被分配到同一台realserver) 36.protocol TCP#(用TCP协议检查realserver状态) 37. 38.real_server 61.164.122.9 80 { 39.weight 3#(权重) 40.TCP_CHECK { 41.connect_timeout 10#(10秒无响应超时) 42.nb_get_retry 3 43.delay_before_retry 3 44.connect_port 80 45.} 46.} 47.real_server 61.164.122.10 80 { 48.weight 3 49.TCP_CHECK { 50.connect_timeout 10 51.nb_get_retry 3 52.delay_before_retry 3 53.connect_port 80 54.} 55.} 56.}
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 1.IP Virtual Server version 1.2.1 (size=4096) 2.Prot LocalAddressort Scheduler Flags 3.-> RemoteAddressort Forward Weight ActiveConn InActConn 4.TCP61.164.122.8:80 wrr persistent 60 5.-> 61.164.122.10:80Route 3 00 6.-> 61.164.122.9:80Route 3 00 #tail –f /var/log/message监听日志,查看状态,测试LVS负载均衡及高可用性是否有效。
5.停Master服务器的keepalived服务,查看BAKCUP服务器是否能正常接管服务。
发表评论