主机名
IP
所需软件
master
eth0==>192.168.30.140(Nat)
eth0:1==>192.168.17.130(Nat)
ipvsadm
node-1
eth0==>192.168.17.131
httpd
node-2
eth0==>192.168.17.132
在master上安装及开启ipvsadm
1[root@master ~]# yum install -y ipvsadm 2[root@master ~]# cd /etc/sysconfig/network-scripts/ 3[root@master network-scripts]# cp ifcfg-eth0 ifcfg-eth:1 4[root@master network-scripts]# vim ifcfg-eth0:1 #只需修改绿色部分三处,其他不要动 5DEVICE=eth0:1 6TYPE=Ethernet 7UUID=a186cc9d-8d79-4027-a180-a1b17d884a7b 8ONBOOT=yes 9NM_CONTROLLED=yes 10BOOTPROTO=none 11HWADDR=00:0C:29:4D:47:50 12IPADDR=192.168.30.130 13PREFIX=24 14GATEWAY=192.168.30.2 15DNS1=119.29.29.29 16DEFROUTE=yes 17IPV4_FAILURE_FATAL=yes 18IPV6INIT=no 19NAME="System eth0:1" 20重启网络服务 21[root@master ~]# service network restart 22Shutting down interface eth0: Device state: 3 (disconnected) 23 [ OK ] 24Shutting down interface eth1: [ OK ] 25Shutting down loopback interface: [ OK ] 26Bringing up loopback interface: [ OK ] 27Bringing up interface eth0: Active connection state: activated 28Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/1 29 [ OK ] 30[root@master ~]# ifconfig 31eth0 Link encap:Ethernet HWaddr 00:0C:29:4D:47:50 32 inet addr:192.168.30.140 Bcast:192.168.30.255 Mask:255.255.255.0 33 inet6 addr: fe80::20c:29ff:fe4d:4750/64 Scope:Link 34 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 35 RX packets:81 errors:0 dropped:0 overruns:0 frame:0 36 TX packets:105 errors:0 dropped:0 overruns:0 carrier:0 37 collisions:0 txqueuelen:1000 38 RX bytes:8687 (8.4 KiB) TX bytes:8194 (8.0 KiB) 39 40eth0:1 Link encap:Ethernet HWaddr 00:0C:29:4D:47:50 #MAC地址一定要与eth0的一样 41 inet addr:192.168.30.130 Bcast:192.168.30.255 Mask:255.255.255.0 42 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
添加规则
1确保这里是0 2[root@master ~]# sysctl -p 3net.ipv4.ip_forward = 0 4.......... 5添加规则 6[root@master ~]# ipvsadm -A -t 192.168.30.130:80 -s rr 7[root@master ~]# ipvsadm -a -t 192.168.30.130:80 -r 192.168.30.131 -g 8[root@master ~]# ipvsadm -a -t 192.168.30.130:80 -r 192.168.30.132 -g 9[root@master ~]# ipvsadm -L -n 10IP Virtual Server version 1.2.1 (size=4096) 11Prot LocalAddress:Port Scheduler Flags 12 -> RemoteAddress:Port Forward Weight ActiveConn InActConn 13TCP 192.168.30.130:80 rr 14 -> 192.168.30.131:80 Route 1 0 0 15 -> 192.168.30.132:80 Route 1 0 0 16保存规则 17[root@master ~]# /etc/init.d/ipvsadm save 18ipvsadm: Saving IPVS table to /etc/sysconfig/ipvsadm: [ OK ]
在realserver(node-1和node-2)上
1生成ifcfg-lo:1文件 2[root@node-1 ~]# cd /etc/sysconfig/network-scripts/ 3[root@node-1 network-scripts]# cp ifcfg-lo ifcfg-lo:1 4[root@node-1 network-scripts]# vim ifcfg-lo:1 5DEVICE=lo:1 6IPADDR=192.168.30.130 7NETMASK=255.255.255.255 8# If you're having problems with gated making 127.0.0.0/8 a martian, 9# you can change this to something else (255.255.255.255, for example) 10ONBOOT=yes 11NAME=loopback
关闭ARP转发
1[root@node-1 ~]# vim /etc/sysctl.conf #末未添加 2net.ipv4.conf.eth0.arp_ignore = 1 3net.ipv4.conf.eth0.arp_announce = 2 4[root@node-1 ~]# sysctl -p #使立即生效 5net.ipv4.ip_forward = 0 6..... 7net.ipv4.conf.eth0.arp_ignore = 1 8net.ipv4.conf.eth0.arp_announce = 2
修改网关
1[root@node-1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 2GATEWAY=192.168.30.2 3#网关修改为指向外网出口的路由器IP
创建测试页面
[root@node-1 ~]# echo "<h1>This is the node-1 Web-Server:192.168.17.131</h1>" > /var/www/html/index.html
在node-2上
1[root@node-2 ~]# cd /etc/sysconfig/network-scripts/ 2[root@node-2 network-scripts]# cp ifcfg-lo ifcfg-lo:1 3[root@node-2 network-scripts]# vim ifcfg-lo:1 4DEVICE=lo:1 5IPADDR=192.168.30.130 6NETMASK=255.255.255.255 7# If you're having problems with gated making 127.0.0.0/8 a martian, 8# you can change this to something else (255.255.255.255, for example) 9ONBOOT=yes 10NAME=loopback 11[root@node-2 ~]# service network restart 12Shutting down interface eth0: Device state: 3 (disconnected) 13 [ OK ] 14Shutting down interface eth1: [ OK ] 15Shutting down loopback interface: [ OK ] 16Bringing up loopback interface: [ OK ] 17Bringing up interface eth0: Active connection state: activated 18Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/3 19 [ OK ] 20[root@node-2 ~]# ifconfig 21eth0 Link encap:Ethernet HWaddr 00:50:56:2F:84:4D 22 inet addr:192.168.30.132 Bcast:192.168.30.255 Mask:255.255.255.0 23 inet6 addr: fe80::250:56ff:fe2f:844d/64 Scope:Link 24................... 25lo:1 Link encap:Local Loopback 26 inet addr:192.168.30.130 Mask:255.255.255.255 27 UP LOOPBACK RUNNING MTU:16436 Metric:1
关闭ARP转发通node-1操作一样;
创建测试页面
1[root@node-2 ~]# echo "<h1>This is the node-2 Web-Server:192.168.17.132</h1>" > /var/www/html/index.html 2[root@node-2 ~]# service httpd restart 3Stopping httpd: [ OK ] 4Starting httpd: [ OK ]
物理机使用浏览器访问测试



