Chinaunix

标题: 使用LVS(Linux Virtual Server)在Linux上搭建负载均衡的集群服务 [打印本页]

作者: admin126com    时间: 2008-12-08 18:08
标题: 使用LVS(Linux Virtual Server)在Linux上搭建负载均衡的集群服务

使用LVS(Linux Virtual Server)在Linux上搭建负载均衡的集群服务
一、基于于NAT的LVS的安装与配置。
1. 硬件需求和网络拓扑
                        ________
                       |        |
                       | 客户端 | (互连网)
                       |________|
                           |
                        (router)
                        202.99.59.1(director GW)
                           |
                           |
                       (202.99.59.110)Virtual IP
                       ____|_____
                      |          | (控制器有两块网卡)
                      | director |
                      |__________|
                    (192.168.10.254)DIP
                           |
          -----------------+----------------
          |                |               |
         RIP1             RIP2            RIPx
    ____________     ____________     ____________
   |192.168.10.1|   |192.168.10.2|   |192.168.10.x|
   |realserver1 |   |realserver2 |   |realserverx |
   |____________|   |____________|   |____________|


2.下载软件:
(1)、内核源代码:需要下载2.4.23以后版本的内核源代码。下载地址为http://www.kerner.org。本文中下载的内核源代为:linux-2.4.30.tar.bz2。
(2)、用户配置工具ipvsadm,下载地址:http://www.linuxvirtualserver.org/software/ipvs.html。本文下载的是:http://www.linuxvirtualserver.org/software/kernel-2.4/ipvsadm-1.21-11.tar.gz
3.安装软件:
在director(控制器,202.99.59.110)上安装支持LVS的内核和配置工具ipvsadm。
(1)、在内核配置时以下选项必须选:
Networking options  --->
         Packet socket
         Netlink device emulation
       
  • TCP/IP networking
           
  •    IP: advanced router
           
  • Network packet filtering (replaces ipchains)
            IP: Netfilter Configuration  --->
                     Connection tracking (required for masq/NAT)
                     IP tables support (required for filtering/masq/NAT)
                       Full NAT
                         MASQUERADE target support
            IP: Virtual Server Configuration  --->
                     virtual server support (EXPERIMENTAL)
                    (12)   IPVS connection table size (the Nth power of 2)
                    --- IPVS scheduler
                       round-robin scheduling
                       weighted round-robin scheduling
                       least-connection scheduling
                       weighted least-connection scheduling
                       locality-based least-connection scheduling
                       locality-based least-connection with replication scheduling
                       destination hashing scheduling
                       shortest expected delay scheduling
                       never queue scheduling
    (2)、编译和安装内核
    分别执行: make bzImage;make modules;make modules_install;然后编辑启动配置文件,重新启动系统,在启动时选择新的内核。
    系统启动后要确认内核是否支持ipvs,只需要执行下面的命令即可:grep ip_vs_init /boot/System.map
    (3)、编译和安装ipvsadm
    ln -s /usr/src/linux-2.4.30 /usr/src/linux tar -zxvf ipvsadm-1.21-11.tar.gz
    cd ipvsadm-1.21-11
    make all
    make install
    然后运行:ipvsadm --version命令,应该有下面的内容输出:
    ipvsadm v1.21 2004/02/23 (compiled with popt and IPVS v1.0.12)
    4. 配置LVS
    (1)、在202.99.59.110上:
    echo "1" >/proc/sys/net/ipv4/ip_forward
    echo "0" >/proc/sys/net/ipv4/conf/all/send_redirects
    echo "0" >/proc/sys/net/ipv4/conf/default/send_redirects
    echo "0" >/proc/sys/net/ipv4/conf/eth0/send_redirects
    echo "0" >/proc/sys/net/ipv4/conf/eth1/send_redirects
    清除ipvsadm表:
    /sbin/ipvsadm -C
    使用ipvsadm安装LVS服务
    #add http to VIP with rr sheduling
    /sbin/ipvsadm -A -t 202.99.59.110:80 -s rr
    增加第一台realserver:
    #forward http to realserver 192.168.10.1 using LVS-NAT (-m), with weight=1
    /sbin/ipvsadm -a -t 202.99.59.110:80 -r 192.168.10.1:80 -m -w 1
    增加第二台realserver:
    #forward http to realserver 192.168.10.2 using LVS-NAT (-m), with weight=1
    /sbin/ipvsadm -a -t 202.99.59.110:80 -r 192.168.10.2:80 -m -w 1
    (2)、realserver配置
    在192.168.10.1(realserver1)和192.168.10.2(realserver2)上分别将其网关设置为192.168.10.254,并分别启动apache服务。
    在客户端使用浏览器多次访问:http://202.99.59.110/,然后再202.99.59.110上运行ipvsadm命令,应该有类似下面的输出:
    IP Virtual Server version 1.0.12 (size=4096)
    Prot LocalAddress:Port Scheduler Flags
      -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
    TCP  202.99.59.110:http rr
      -> 192.168.10.1:http           Masq    1      0          33        
      -> 192.168.10.2:http           Masq    1      0          33        
    从上面的结果可以看出,我们的LVS服务器已经成功运行。


    二、基于直接路由(DR)的LVS的配置 1.硬件需求和网络拓扑:
                            ________
                           |        |
                           | 客户端 | (互连网)
                           |________|
                               |
                            (router)
                            202.99.59.1(director GW)
                               |
                 __________    |
                |          |   |   (VIP=202.99.59.110, eth0:110)
                | director |---|  (控制器有一块网卡,且与realserver1和realserver2在同一网段)
                |__________|   |   DIP=202.99.59.109 (eth0)
                               |
              -----------------------------------
              |                                 |
    RIP=202.99.59.108(eth0)             RIP=202.99.59.107(eth0)
    (VIP=202.99.59.110, lo:0)          (VIP=202.99.59.110, lo:0)
        ____________                       ____________
       |            |                     |            |
       |realserver1 |                     |realserver2 |
       |____________|                     |____________|
       
    2.安装软件:
    在director(202.99.59.109)上安装上面的方法安装内核和管理软件。
    3. 配置LVS
    (1)、在202.99.59.109上:
    修改内核运行参数,即修改/etc/sysctl.conf文件的内容如下:
    net.ipv4.ip_forward = 0
    net.ipv4.conf.all.send_redirects = 1
    net.ipv4.conf.default.send_redirects = 1
    net.ipv4.conf.eth0.send_redirects = 1
    然后执行下面的命令是对内核修改的参数立即生效:
    sysctl -p
    配置VIP地址:
    /sbin/ifconfig eth0:0 202.99.59.110 broadcast 202.99.59.110 netmask 255.255.255.255 up
    /sbin/route add -host 202.99.59.110 dev eth0:0
    清除ipvsadm表:
    /sbin/ipvsadm -C
    使用ipvsadm安装LVS服务:
    /sbin/ipvsadm -A -t 192.168.1.110:http -s rr
    增加realserver:
    #forward http to realserver using direct routing with weight 1
    /sbin/ipvsadm -a -t 192.168.1.110:http -r 192.168.1.12 -g -w 1
    /sbin/ipvsadm -a -t 192.168.1.110:http -r 192.168.1.12 -g -w 1
    (2)在realserver1(202.99.59.108)和realserver2(202.99.59.107)上做下面的设置。
    修改内核运行参数,即修改/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
    配置VIP地址:
    /sbin/ifconfig lo:0 202.99.59.110 broadcast 202.99.59.110 netmask 255.255.255.255 up
    /sbin/route add -host 202.99.59.110 dev lo:0
    p>在客户端使用浏览器多次访问:http://202.99.59.110/,然后再202.99.59.110上运行ipvsadm命令,应该有类似下面的输出:
    IP Virtual Server version 1.0.12 (size=4096)
    Prot LocalAddress:Port Scheduler Flags
      -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
    TCP  192.168.100.254:http rr
      -> 192.168.100.3:http           Route   1      0          28        
      -> 192.168.100.2:http           Route   1      0          29               
    从上面的结果可以看出,我们的LVS服务器已经成功运行。


    在面的实例中ipvsadm用到的几个参数含义如下:
    -A 增加一个虚拟服务,该服务由协议、IP地址和端口号组成,例如:
    -A -t 202.99.59.110:80 (增加一格虚拟服务,其协议(-t表示tcp,-u表示udp)为TCP、IP为202.99.59.110、端口号为80。
    -s 指定服务采用的算法,常用的算法参数如下:

    /sbin/ipvsadm -a -t 192.168.1.110:http -r 192.168.1.12 -g -w 1
    -a 表示往一个服务内增加一个real server
    -r 指定real server的IP地址
    -w 表示权重
    -g 表示使用DR方式,-m表示NAT方式,-i表示tunneling方式。


    本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/86974/showart_1686124.html
    作者: tqmary    时间: 2008-12-24 10:45
    这莫好的东西居然没人顶啊,真是的

    我顶顶顶
    作者: lann53522717    时间: 2009-01-10 12:39
    好东西
    作者: ylcqen    时间: 2009-02-18 10:18
    写得很不错!支持
    作者: gaolimin_lose    时间: 2009-08-10 21:32
    确实不错
    作者: quidway668    时间: 2010-08-09 16:49
    顶上去。。。。
    作者: sky-鹏    时间: 2011-03-15 17:09
    学习~~~




    欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2