2013年8月23日星期五

我也来玩LVS之NAT

神马都是浮云,LVS 架构看过N次了,就是没有时间折腾,今天抽空,整理NAT模式,涉及的有双网卡路由吧。
我的环境:VMware workstation + 3台 Ubuntu Server 10 版本的。以下相关IP 及主机名:

系统     主机名         IP                内存           硬盘        安装服务
ubuntu    web1        192.168.104.129   512MB     15GB      LAMP+OpenSSH  (网关设置为192.168.104.132)
ubuntu    web2        192.168.104.130   512MB     15GB      LAMP+OpenSSH  (网关设置为192.168.104.132)
ubuntu    dispatch    192.168.104.132   384MB     10GB      OpenSSH+lvsadm (192.168.1.140 为dispatch 外网地址 )

VIP   192.168.1.118

    ubuntu 服务器安装就不多说了,敲着回车键一路过,到最后选择软件包的时候注意下就行了(ipvsadm 需要等系统装好后再安装)

调度机器上的配置过程如下:

使用root 用户(普通用户使用 sudo方式安装iplvsadm)

root@dispatch:~# apt-get install ipvsadm
Reading package lists... Done
Building dependency tree      
Reading state information... Done
Suggested packages:
  heartbeat keepalived ldirectord
The following NEW packages will be installed:
  ipvsadm
0 upgraded, 1 newly installed, 0 to remove and 60 not upgraded.
Need to get 45.6 kB of archives.
After this operation, 197 kB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu/ natty/main ipvsadm i386 1:1.25.clean-1ubuntu1 [45.6 kB]
Fetched 45.6 kB in 2s (16.1 kB/s) 
Preconfiguring packages ...
Selecting previously deselected package ipvsadm.
(Reading database ... 48251 files and directories currently installed.)
Unpacking ipvsadm (from .../ipvsadm_1%3a1.25.clean-1ubuntu1_i386.deb) ...
Processing triggers for man-db ...
Processing triggers for ureadahead ...
ureadahead will be reprofiled on next reboot
Setting up ipvsadm (1:1.25.clean-1ubuntu1) ...
update-rc.d: warning: ipvsadm start runlevel arguments (2 3 4 5) do not match LSB Default-Start values (2 3 5)
* ipvsadm is not configured to run. Please run dpkg-reconfigure ipvsadm

编辑lvsnat 配置文件(安装时不会有的,手工创建):

root@dispatch:~# vi /etc/init.d/lvsnat

意思不多说,内容如下

#!/bin/bash

#LVS script NAT

VIP=192.168.1.118

RIP1=192.168.104.129
RIP2=192.168.104.130

#./etc/rc.d/init.d/functions

case "$1" in

        start)

        echo "1">/proc/sys/net/ipv4/ip_forward

        /sbin/iptables -F

        /sbin/iptables -t nat -F

        /sbin/iptables -t nat -A POSTROUTING -s 192.168.104.0/24 -o eth1 -j MASQUERADE

        /sbin/ifconfig eth0:0 $VIP netmask 255.255.255.0 up

        /sbin/ipvsadm -A -t $VIP:80 -s rr            

        /sbin/ipvsadm -a -t $VIP:80 -r $RIP1 -m     

        /sbin/ipvsadm -a -t $VIP:80 -r $RIP2 -m

        /sbin/ipvsadm

        echo "start LVS of DirectorServer NAT"

        ;;

        stop)

        echo "0" >/proc/sys/net/ipv4/ip_forward

        /sbin/ipvsadm -C

        /sbin/ifconfig eth0:0 down

        echo "stop LVS of DirectorServer NAT"

        ;;

        *)

        echo "Usage:$0{start|stop}"

        exit 1

esac



给个执行权限:

root@dispatch:~# chmod +x /etc/init.d/lvsnat

启动该服务:

root@dispatch:~# /etc/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.1.118:www rr
  -> 192.168.104.129:www Masq 1 0 0        
  -> 192.168.104.130:www Masq 1 0 0        
start LVS of DirectorServer NAT    
root@dispatch:~#


奇怪的是查看iptable 时,看不到记录,这个ubuntu 还没完全琢磨透底,有时间再看看吧!
真实服务器web1、web2只要配置好web服务即可。(LAMP,不会装,选上总可以把)

建立一个测试主页面,两个页面的内容可以不一样(便于看效果)
打开IE,浏览器,输入http://192.168.1.118 多刷新几次(用Ctrl + F5 吧)

root@dispatch:~# ipvsadm -lnc
IPVS connection entries
pro expire state source virtual destination
TCP 00:03 CLOSE 192.168.1.219:51356 192.168.1.118:80 192.168.104.130:80
TCP 01:34 TIME_WAIT 192.168.1.219:51353 192.168.1.118:80 192.168.104.129:80
TCP 14:57 ESTABLISHED 192.168.1.219:51358 192.168.1.118:80 192.168.104.129:80


root@dispatch:~# ipvsadm --list
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.1.118:www rr
  -> 192.168.104.129:www Masq 1 0 1        
  -> 192.168.104.130:www Masq 1 1 0  

没有评论:

发表评论