要求:用户访问VIP:80 能够调度到RIP:80 ,然后在RS上配置iptables 实现端口转发,即将请求转发到RIP:8080
实验平台:Ubuntu12.04
LVS/DR 模式
一、安装配置ipvsadm
root@node1:~# apt-get install -y gcc g++ make libssl-dev openssl libpopt-dev libnl-dev daemon
root@node1:~# apt-get install ipvsadm -y
刚装完的ipvsadm 需要配置一下
root@node1:~# dpkg-reconfigure ipvsadm
是否自动配置IPVS
选择工作模式
选择连接网卡
配置ipvsadm
root@node1:~# ipvsadm -A -t 192.168.10.254:80 -s rr
root@node1:~# ipvsadm -a -t 192.168.10.254:80 -r 192.168.10.103 -g
root@node1:~# ipvsadm -a -t 192.168.10.254:80 -r 192.168.10.104 -g
root@node1:~# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.10.254:http rr
-> 192.168.10.103:http Route 1 0 0
-> 192.168.10.104:http Route 1 0 0
配置VIP
root@node1:~# ifconfig lo:1 192.168.10.254 netmask 255.255.255.255 broadcast 192.168.10.254
二、配置web01、web02
root@node3:~# apt-get install apache2 -y
root@node3:~# vim /etc/apache2/ports.conf
NameVirtualHost *:80
Listen 80
修改为
NameVirtualHost *:8080
Listen 8080
root@node3:~# vim /etc/apache2/sites-enabled/000-default
<VirtualHost *:80>
修改为
<VirtualHost *:8080>
启动服务
root@node3:~# /etc/init.d/apache2 start
关闭ARP 响应
root@node3:~# echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
root@node3:~# echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
root@node3:~# echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
root@node3:~# echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
root@node3:~# sysctl -p
配置VIP
root@node3:~# ifconfig lo:1 192.168.10.254 netmask 255.255.255.255 broadcast 192.168.10.254
root@node3:~# route add -host 192.168.10.254 dev lo:1
注:web02上的操作同web01
分别为web01和web02配置两个不同的页面
root@node3:~# cat /var/www/index.html
<h1>Welcome to Web01</h1>
root@node4:~# cat /var/www/index.html
<h1>Welcome to Web02</h1>
三、配置iptables
在RS上配置iptables 实现端口转发(80 --> 8080)
root@node3:~# iptables -t nat -I PREROUTING -p tcp --dport 80 -j DNAT --to :8080
root@node4:~# iptables -t nat -I PREROUTING -p tcp --dport 80 -j DNAT --to :8080
四、测试
打开浏览器访问http://192.168.10.254:80
root@node1:~# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.10.254:http rr
-> 192.168.10.103:http Route 1 1 5
-> 192.168.10.104:http Route 1 1 4