负载均衡--LVS安装配置

      前言:这是四年前工作中学习负载均衡时,记录在笔记本上的LVS安装过程,最近重新过了一遍。

     直接上.......


一、负载均衡服务器端安装ipvsadm
1、下载符合内核版本的ipvsadm安装包:
[root@nginx-2 ~]#wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz
2、解压到/usr/loacl下:
[root@nginx-2 ~]#tar -zxvf ipvsadm-1.26.tar.gz -C /usr/local/
3、创建kernel软链接:
[root@nginx-2 ~]#ln -s /usr/src/kernels/2.6.32-504.23.4.el6.x86_64 /usr/src/linux
注:如果/usr/local/kernel下没有内核,可以运行“yum install -y kernel-devel”进行安装后再建立软链接
4、安装ipvsadm:
[root@nginx-2 ~]#cd /usr/local/ipvsadm-1.26
[root@nginx-2 ipvsadm-1.26]#make
如果有报错,则需要安装依赖包:yum install -y libnl* libpopt* popt*
[root@nginx-2 ipvsadm-1.26]#make install
5、查看ipvsadm:
[root@nginx-2 ipvsadm-1.26]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
6、设置ip转发等功能:
[root@nginx-2 ipvsadm-1.26]#echo "1" >/proc/sys/net/ipv4/ip_forward
[root@nginx-2 ipvsadm-1.26]#echo "0" >/proc/sys/net/ipv4/conf/all/send_redirects
[root@nginx-2 ipvsadm-1.26]#echo "0" >/proc/sys/net/ipv4/conf/default/send_redirects
[root@nginx-2 ipvsadm-1.26]#echo "0" >/proc/sys/net/ipv4/conf/eth0/send_redirects
[root@nginx-2 ipvsadm-1.26]#echo "0" >/proc/sys/net/ipv4/conf/eth1/send_redirects
7、先清除表中的所有记录
[root@nginx-2 ipvsadm-1.26]#ipvsadm -C


二、配置ipvsadm
1、vs/nat模式下各服务器的配置:
NAT模式需要负载均衡设备有两块网卡,
试验环境如下:
lvsserver            10.204.105.202(外网eht1)     10.204.116.167(内网eth0)
serverA         10.204.116.151(eth0)
serverB         10.204.116.160(eth0)
1.1、负载均衡器配置:
注意:内网网卡不需要配置网关,外网配置外网网关。
ipvsadm -C
ipvsadm -At 10.204.105.202:80 -s rr
ipvsadm -at 10.204.105.202:80 -r 10.204.116.151:80 -m -w 1
ipvsadm -at 10.204.105.202:80 -r 10.204.116.160:80 -m -w 1
ipvsadm-save > /etc/sysconfig/ipvsadm
1.2、被代理端配置:
route add default gw 10.204.116.167

2、vs/dr模式下各服务器的配置 :
DR模式,负载均衡设备只有一块网卡
试验环境如下:
lvsserver       10.204.116.167(eth0) VIP:10.204.116.166
serverA         10.204.116.151(eth0)
serverB         10.204.116.160(eth0)
2.1、负载均衡器配置:
ifconfig eth0:0 10.204.116.166 netmask 255.255.255.255 up
route add -host 10.204.116.166 dev eth0:0
ipvsadm -C
ipvsadm -A -t 10.204.116.166:http -s rr
ipvsadm -a -t 10.204.116.166:http -r 10.204.116.151:http -g -w 1
ipvsadm -a -t 10.204.116.166:http -r 10.204.116.160:http -g -w 1
ipvsadm-save > /etc/sysconfig/ipvsadm
2.2、被代理端配置:
ifconfig lo:0 10.204.116.166 netmask 255.255.255.255 up
route add -host 10.204.116.166 dev lo:0
被代理端sysctl.conf设置:
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2

3、vs/tun模式下各服务器的配置:
tunlk模式,负载均衡设备只有一块网卡
试验环境如下:
lvsserver       10.204.116.167(eth0) VIP:10.204.116.166
serverA         10.204.116.151(eth0)
serverB         10.204.116.160(eth0)
3.1、负载均衡器配置:
ifconfig tunl0 10.204.116.166 netmask 255.255.255.255 up
route add -host 10.204.116.166 dev tunl0
ipvsadm -C
ipvsadm -A -t 10.204.116.166:http -s rr
ipvsadm -a -t 10.204.116.166:http -r 10.204.116.151:http -i -w 1
ipvsadm -a -t 10.204.116.166:http -r 10.204.116.160:http -i -w 1
ipvsadm-save > /etc/sysconfig/ipvsadm
3.2、被代理端配置:
ifconfig tunl0 10.204.116.166 netmask 255.255.255.255 up
route add -host 10.204.116.166 dev tunl0
被代理端sysctl.conf设置:
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2

你可能感兴趣的:(负载均衡技术)