office documentation:
http://www.linuxvirtualserver.org/VS-DRouting.html
http://wenku.baidu.com/view/f352412e2af90242a895e565.html
http://wenku.baidu.com/view/30f7115c3b3567ec102d8abb.html?re=view
http://wenku.baidu.com/view/f13a142c453610661ed9f41e.html
http://wenku.baidu.com/view/b9c7cd52f01dc281e53af027.html
LVS DR(direct routing):性能最好
server | ip(eth0) | vip |
director server | 192.168.1.105 | eth0:0 192.168.1.111 |
real server 1 | 192.168.1.102 | lo:0 192.168.1.111 |
real server 2 | 192.168.1.104 | lo:0 192.168.1.111 |
install lvs on director server
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz
tar xzvf ipvsadm-1.26.tar.gz
cd ipvsadm-1.26
ln -s /usr/src/kernels/$(uname -r) /usr/src/linux #注意內核版本號,編譯時lvs通過linux軟連接找到相應的頭文件和庫文件
make
make install
check lvs
ipvsadm
lsmod | grep -i ip_vs #you cannot check the ipvsadm by "ps -ef | grep ipvsadm"
change the kernel parameters on network in /etc/sysctl.conf
net.ipv4.ip_forward = 0
net.ipv4.conf.all.send_redirects = 1
net.ipv4.conf.eth0.send_redirects = 1
net.ipv4.conf.default.send_redirects = 1
sysctl -p #生效
sysctl -a #檢測參數值
配置VIP和路由
ifconfig eth0:0 192.168.1.111 broadcast 192.168.1.111 netmask 255.255.255.255 up
route add -host 192.168.1.111 dev eth0:0
配置lvs(通過ipvsadm配置,就像netfilter是通過iptables配置的一樣)
ipvsadm -C
ipvsadm -A -t 192.168.1.111:80 -s rr #add a vitual ip and port,and use round-robin
ipvsadm -a -t 192.168.1.111:80 -r 192.168.1.102:80 -g
ipvsadm -a -t 192.168.1.111:80 -r 192.168.1.104:80 -g
啓動lvs
ipvsadm
ipvsadm -ln #check lvs狀態
====================================================================================
work on real server 1 and 2, the same setup on both 1 and 2
change kernel parmater on network in /etc/sysctl.conf
net.ipv4.ip_forward = 0
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 #生效
sysctl -a #confirm
配置VIP&路由
ifconfig lo:0 192.168.1.111 broadcast 192.168.1.111 netmask 255.255.255.255 up
route add -host 192.168.1.111 dev lo:0
start web server
nginx
=====================================================================================
work on client end to test
open 10 tabs to access http://192.168.1.111
=====================================================================================
work on director server
stop lvs
ipvsadm -C
ifconfig eth0:0 down
caution: in prd env, use script to configure LVS's start and stop