通过LVS实现http的负载均衡
1、摘要
本文不考虑双机互备,通过脚本和lvs实现http的负载均衡,实际生产环境使用集群
2、安装
1)、软件包准备
ipvsadm-1.26.tar.gz
httpd-2.2.23.tar.gz
2)、配置Director Server
a、检查kernel是否已经支持LVS的ipvs模块
# modprobe -l |grep ipvs
kernel/net/netfilter/ipvs/ip_vs.ko
kernel/net/netfilter/ipvs/ip_vs_rr.ko
kernel/net/netfilter/ipvs/ip_vs_wrr.ko
kernel/net/netfilter/ipvs/ip_vs_lc.ko
kernel/net/netfilter/ipvs/ip_vs_wlc.ko
kernel/net/netfilter/ipvs/ip_vs_lblc.ko
kernel/net/netfilter/ipvs/ip_vs_lblcr.ko
kernel/net/netfilter/ipvs/ip_vs_dh.ko
kernel/net/netfilter/ipvs/ip_vs_sh.ko
kernel/net/netfilter/ipvs/ip_vs_sed.ko
kernel/net/netfilter/ipvs/ip_vs_nq.ko
kernel/net/netfilter/ipvs/ip_vs_ftp.ko
kernel/net/netfilter/ipvs/ip_vs_pe_sip.ko
b、在Director Server中安装LVS
# ln -s /usr/src/kernels/2.6.32-431.el6.x86_64/ /usr/src/linux #解决ipvsadm找不到内核
# yum install -y libnl*
# yum install -y popt*
# rpm -ivh popt-static-1.13-7.el6.x86_64.rpm #前三步是解决安装ipvsadm-1.26报错的软件包
# tar zxvf ipvsadm-1.26.tar.gz
# cd ipvsadm-1.26
# make
# make install
# ipvsadm --help #出现ipvsadm帮助提示表示ipvsadm已经安装成功
c、lvs启停脚本
vim /etc/init.d/lvsDR
#!/bin/sh
# description: Start LVS of Director server
VIP=10.0.2.200
RIP1=10.0.2.203
RIP2=10.0.2.204
chmod 755 /etc/rc.d/init.d/functions
/etc/rc.d/init.d/functions
case "$1" in
start)
echo " start LVS of Director Server"
# set the Virtual IP Address and sysctl parameter
/sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eth0:0
echo "1" >/proc/sys/net/ipv4/ip_forward
#Clear IPVS table
/sbin/ipvsadm -C
#set LVS
/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
#Run LVS
/sbin/ipvsadm
;;
stop)
echo "close LVS Directorserver"
echo "0" >/proc/sys/net/ipv4/ip_forward
/sbin/ipvsadm -C
/sbin/route del -host $VIP dev eth0:0
/sbin/ifconfig eth0:0 down
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
# chmod 755 /etc/init.d/lvsDR
# /etc/init.d/lvsDR start
# service ipvsadm save #保存lvs记录到/etc/sysconfig/ipvsadm中
# service ipvsadm start
3)、配置Real Server服务器
a、安装http服务
# tar -zxvf httpd-2.2.23.tar.gz
# cd httpd-2.2.23
# ./configure --prefix=/usr/local/apache
# make && make install
b、配置httpd
# cp /usr/local/apache/bin/apachectl /etc/init.d/httpd
# vim /etc/init.d/httpd
修改文件,在文件中‘#!/bin/sh‘后面加入下面两条规则:
#!/bin/sh
#
# chkconfig: 2345 85 15
# description: Apache is a World Wide Web Server
#
# chkconfig --add httpd
# chkconfig --list httpd
# service httpd start
c、配置vip和arp
# vim /etc/init.d/lvsrs
#!/bin/bash
#description : Start Real Server
VIP=10.0.2.200
/etc/rc.d/init.d/functions
case "$1" in
start)
echo " Start LVS of Real Server"
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 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
;;
stop)
/sbin/ifconfig lo:0 down
/sbin/route del -host $VIP dev lo:0
echo "close LVS Director server"
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 "Usage: $0 {start|stop}"
exit 1
esac
# chmod 755 /etc/init.d/lvsrs
# /etc/init.d/lvsrs start
本文出自 “kimileonis” 博客,请务必保留此出处http://kimileonis.blog.51cto.com/5531747/1538839
通过LVS实现http的负载均衡,布布扣,bubuko.com
原文:http://kimileonis.blog.51cto.com/5531747/1538839