日期:2014-05-16 浏览次数:20850 次
# ================================================================ real server 1 #网络配置 DEVICE=eth0 BOOTPROTO=static ONBOOT=yes HWADDR=00:0C:29:6D:BA:77 NETMASK=255.255.255.0 IPADDR=192.168.97.12 GATEWAY=192.168.97.1 TYPE=Ethernet #apache 服务 监听端口为 80 当然,已经配置好欢迎页面 #=================================================================== real server 2 #网络配置 DEVICE=eth0 BOOTPROTO=static ONBOOT=yes HWADDR=00:0C:29:BC:17:F5 NETMASK=255.255.255.0 IPADDR=192.168.97.11 GATEWAY=192.168.97.1 #apache 服务 监听端口为 80 当然,已经配置好欢迎页面 #==================================================================== ipvsadm 所在的服务器 这个服务器上 , 我设置了2个虚拟的网卡. # 第一张虚拟网卡配置如下 DEVICE=eth0 BOOTPROTO=static IPADDR=192.168.96.138 NETMASK=255.255.255.0 HWADDR=00:0C:29:C9:53:D0 GATEWAY=192.168.96.2 ONBOOT=yes # 第二张虚拟网卡配置如下 DEVICE=eth1 BOOTPROTO=static IPADDR=192.168.97.1 NETMASK=255.255.255.0 HWADDR=00:0c:29:c9:53:da GATEWAY=192.168.97.1 ONBOOT=yes # 另外我在第二张网卡上面配置了一个虚拟ip ifconfig eth1:1 192.168.96.139 netmask 255.255.255.0 up #首先看下ipvsadm是否加入内核 [root@localhost init.d]# lsmod | grep ip_vs ip_vs_rr 6081 0 ip_vs 78081 2 ip_vs_rr #接下来ipvsadm的版本 [root@localhost init.d]# ipvsadm IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn # 开启路由机制 vi /etc/sysctl.conf net.ipv4.ip_forward = 1 并执行sysctl -p 后生效 # 配置一个启动ipvsadm的脚本 vim /etc/init.d/lvsnat 内容如下: #! /bin/bash # LVS script NAT VIP=192.168.96.139 RIP1=192.169.97.11 RIP2=192.169.97.12 case "$1" in start) echo "1" > /proc/sys/net/ipv4/ip_forward /sbin/ipvsadm -C /sbin/ipvsadm -A -t $VIP:80 -s rr /sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -m /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -m /sbin/ipvsadm echo "start LVS of DirectorServer NAT" ;; stop) echo "0" > /proc/sys/net/ipv4/ip_forward /sbin/ipvsadm -C echo "stop LVS of DirectorServer NAT" ;; *) echo "Usage: $0 {start|stop}" exit 1 ;; esac # 用脚本启动ipvsadm [root@localhost init.d]# ./lvsnat start IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.96.139:http rr -> 192.169.97.12:http Masq 1 0 0 -> 192.169.97.11:http Masq 1 0 0 start LVS of DirectorServer NAT # 查看配置 [root@localhost init.d]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.96.139:80 rr -> 192.169.97.12:80 Masq 1 0 0 -> 192.169.97.11:80 Masq 1 0 0 # ================================================================================