免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 12016 | 回复: 16
打印 上一主题 下一主题

奉献--LVS在Fedora Core5上的配置指南(以Linux、FreeBSD、Solaris系统为RealServer) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-09 21:26 |只看该作者 |倒序浏览
(附件分别是分页的html格式,和压缩过的pdf.gz格式,排版比论坛上要好得多。)
1.简介

Fedora项目是由Redhat 公司赞助的开源Linux发行版本,由于Fedora系列界面简单易用,升级方便而且免费,得到笔者和很多Linux爱好者的追捧。

Fedora Core5(代号Bordeaux,内核为2.6.15-1,以下简称FC5)是Fedora最新的稳定版本,界面豪华美观,中文支持极佳。是笔者平常的办公学习用系统。

本文FC5以作为负载均衡服务器(以下简称Director),以FC5、Sun Solaris10 x86h和FreeBSD5.4作为真实服务器(以下简称RealServer),实现了LVS中的两种负载均衡方式VS/NAT、VS/DR。调度算法采用了比较简单的加权轮转算法。

由于笔者在VS/TUN方式下得出了新的结论,但正在完善之中,本文暂不使用VS/TUN方式。对于地理分布的系统而言,VS/TUN几乎是唯一的方法。

三种IP负载均衡技术的优缺点比较:

杂项         VS/NAT     VS/TUN      VS/DR
服务器操作系统    任意      支持隧道     多数(支持Non-arp )
服务器网络      私有网络    局域网/广域网  局域网
服务器数目(100M网络) 10-20      100       多(100)
服务器网关      负载均衡器   自己的路由    自己的路由
效率         一般      高        最高

如何配置Director和RealServer是本文关注的焦点,读者可以将本文当作一篇Howto文档,文中给出了实现的详细方法、配置、和步骤。本文对LVS的原理阐述的非常少,如有需要,请参考章文嵩先生的博士论文和相关文章。

性能也不是本文的重点,事实上文中的很多机器都是在VMware上运行的。

本文中的所有例子,都是采用的Apache服务,这也是LVS经常应用的场合之一。三种操作系统的ip的最后一位,Linux总是160,FreeBSD总是170,Solaris总是180。测试的手段是在客户机(Linux)下运行

links 或elinks命令,用法如下:

links -source http://VIP

这个手段非常简单,而且快速,笔者实验系统上的每个RealServer上Apache服务器上的缺省首页都不同,其中Linux上会显示"This is 160",连续执行此命令,会明显的看到ipvs在起作用。

本文原稿是用vim编辑的TEX 文件,使用了CJK宏包。文中的拓扑图用Gnome Desktop Project旗下的dia软件软件绘制。用多种LATEX 工具生成dvi、pdf和html版本的文件。笔者相信,即使在排版领域,自由软件也是最出色的。

2. 实现VS/NAT

VS/NAT 是一种最简单的方式,所有的RealServer只需要将自己的网关指向Director即可。客户端可以是任意操作系统,但此方式下,一个 Director能够带动的RealServer比较有限。在VS/NAT的方式下,Director也可以兼为一台RealServer。

2.1网络拓扑图


2.2 Director 的配置

Director的配置如下: 外部地址为192.168.10.254 内部地址为192.168.0.254 LVS在VS/NAT、VS/DR和VS/TUN3种方式下均需要打开ip_forward功能,这可以配置/etc/sysctl.conf 文件,保证其中有如下一行:

net.ipv4.ip_forward = 1

执行:

sysctl -p

可以使配置立即生效,并且重启电脑或network服务后,此设置仍然保存。

ipvs的脚本如下:

ipvsadm -C
ipvsadm -A -t 192.168.10.254:80 -s wlc
ipvsadm -a -t 192.168.10.254:80 -r 192.168.0.160:80 -m
ipvsadm -a -t 192.168.10.254:80 -r 192.168.0.170:80 -m
ipvsadm -a -t 192.168.10.254:80 -r 192.168.0.180:80 -m
ipvsadm -a -t 192.168.10.254:80 -r 192.168.0.254:80 -m


2.3Realserver的配置

3台RealServer的配置如下。

2.3.1 Linux Realserver的配置

/etc/sysconfig/network 文件的内容如下:

NETWORKING=yes
HOSTNAME=localhost.localdomain
GATEWAY=192.168.0.254

网卡配置文件{/etc/sysconfig/network-scripts/ifcfg-eth0的内容如下:

DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.160
NETMASK=255.255.255.0
BROADCAST=192.168.0.255

2.3.2 FreeBSD Realserver的配置

只需配置/etc/rc.conf,内容如下:

sshd_enable="YES"
ifconfig_lnc0="inet 192.168.0.170 netmask 255.255.255.0"
defaultrouter="192.168.0.254"
hostname="localhost.localdomain"
apache_enable="YES"

2.3.3 Solaris x86 Realserver的配置

在Solaris 上配置主机名称、ip地址、子网以及网关,要比Linux和FreeBSD要复杂,需要配置多个文件。我机器上网卡名为pcn0,如果您的网卡名称不同,就请取代相应的部分。

/etc/hosts 和/etc/hostname.pcn0 联合作用的结果是配置ip地址和子网掩码,在hostname.pcn0 中填写了子网掩码,/etc/netmasks 文件就可以留空了。 /etc/defaultrouter 文件的作用是配置网关。只需要写一个做为网关的ip地址即可。 /etc/nodename文件的内容为hostname。

/etc/hosts文件:

127.0.0.1 localhost loghost
192.168.0.180 solaris

/etc/hostname.pcn0文件:

192.168.0.180/24

/etc/defaultrouter文件

192.168.0.254

3 实现VS/DR

在很多Internet服务中,例如ftp和http,请求的流量往往远远小于回应的流量,VS/DR方式利用这种非对称的特点,只负责调度请求,而RealServer直接将相应的报文返回给客户机。

VS/DR方式是通过改写请求报文中的MAC地址部分,来实现的Director和RealServer必需在物理上有一个网卡通过不间断的局域网相连。 RealServer上绑定VIP配置在各自Non-ARP的网络设备上(如lo或tunl),Director的VIP地址对外可见,而 RealServer的VIP对外是不可见的。RealServer的地址即可以是内部地址,也可以是真实地址。

在实现VS/DR的方式中,我们配置了两台Router来模拟这种非对称路由的情况,RouterA地址为192.168.0.251, RouterB地址为192.168.0.252。当客户机访问Director时,由RouterA进入,当RealServer返回给客户机信息时,由RouterB出去,根据实际情况您也可以只使用一台或使用多台Router。只要能确保客户发送信息到Director,而且RealServer可以发送信息到客户即可。

3.1 网络拓扑图


3.2 Director的配置

Director 为单网卡,其中eth0的地址为192.168.0.254/32,eth0:0为192.168.0.200/32。网关为 192.168.0.251。这里的VIP地址就是192.168.0.200,当客户访问此地址,根据相应的调度算法,会访问到不同的 RealServer。

ipvs的脚本如下:

#!/bin/bash
ipvsadm -C
ipvsadm -A -t 192.168.0.200:80 -s wlc
ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.160 -g
ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.170 -g
ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.180 -g

/etc/sysconfig/network 内容:

NETWORKING=yes
HOSTNAME=localhost.localdomain
GATEWAY=192.168.0.251

/etc/sysconfig/network-scripts/ifcfg-eth0 文件内容:

DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.254
NETMASK=255.255.255.0

/etc/sysconfig/network-scripts/ifcfg-eth0:0 内容如下:

DEVICE=eth0:0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.200
NETMASK=255.255.255.255

3.3 Realserver的配置

3台RealServer的网关均指向另一路由器,地址为192.168.0.252

3.3.1Linux Realserver的配置

Linux Realserver配置的关键之一在于关闭arp,在Fedora Core5 中,不需要打任何补丁。但要配置/etc/sysctl.conf文件来关闭arp。/etc/sysctl.conf 内容如下:

net.ipv4.ip_forward = 1
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

在/etc/sysconfig/network-scripts/ifcfg-eth0中配置ip地址和网关。

DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.160
NETMASK=255.255.255.0
BROADCAST=192.168.0.255
GATEWAY=192.168.0.252

配置VIP的网卡为/etc/sysconfig/network-scripts/ifcfg-lo:0。


DEVICE=lo:0
IPADDR=192.168.0.200
NETMASK=255.255.255.255
ONBOOT=yes

3.3.2 FreeBSD Realserver的配置

FreeBSD上的配置最为简单,只需要配置一个/etc/rc.conf文件即可。lnc0为真实网卡,lo0的别名上配置的网卡为VIP。/etc/rc.conf 内容如下:


....
ifconfig_lnc0="inet 192.168.0.170 netmask 0xffffff00"
ifconfig_lo0_alias0="inet 192.168.10.200 netmask 255.255.255.255"
defaultrouter="192.168.0.252"
hostname="localhost.localdomain"
apache_enable="YES"
....

3.3.3 Solaris x86 Realserver的配置

Solaris上的网卡ip为192.168.0.180/24。lo0:1为VIP 192.168.0.200/32,另外需配置Solaris的缺省网关为192.168.0.252。

/etc/hosts 文件内容如下:

127.0.0.1 localhost loghost
192.168.0.180 test180.tt.com
192.168.0.200 vip

/etc/hostname.pcn0文件内容如下:

192.168.0.180/24

hostname.lo0:1 文件用来配置lo网卡上的第二个P地址(VIP),文件内容

192.168.10.200/32

/etc/defaultrouter 文件内容

192.168.0.50

4 后记

最近突然对LVS感兴趣了,根据本站上的英文文档。以FC5、FreeBSD 5.4和Solaris 10 x86作为RealServer,以Fedora Core5 Linux上为Load Balance Server,成功配通了VS/NAT、VS/DR和VS/TUN方式,就完成了本文档。

如果文中如有任何错误,无论是技术错误还是笔误,都欢迎email给作者(maluyao at gmail dot com)

本文及其插图的版权为GPL第二版。保留一切权利。

2006年8月11日

[ 本帖最后由 maluyao 于 2006-8-11 21:43 编辑 ]

lvs_html.tar.gz

165.27 KB, 下载次数: 380

lvs.pdf.gz

272.76 KB, 下载次数: 677

论坛徽章:
0
2 [报告]
发表于 2006-08-16 12:59 |只看该作者
好贴要顶

论坛徽章:
0
3 [报告]
发表于 2006-08-17 14:43 |只看该作者
咋就没有看到LVS/TUN的部分捏

论坛徽章:
0
4 [报告]
发表于 2006-08-18 15:08 |只看该作者

LVS/TUN mode with FreeBSD and Solaris realserver

原来是用英文写的,贴到 LVS 主站的wiki上了。

过段时间把文章改成中文的。pdf格式的附件层次比较清晰。

LVS/TUN mode with FreeBSD and Solaris realserver

Author: Ma Luyao RHCE maluyao at gmail dot com

August 16, 2006

Introduction

The Linux Virtual Server Project (LVS) is a project started by Dr. Wensong Zhang. LVS implements three load balance technology.

    Network Address Translation (VS/NAT)
    Direct Routing (VS/DR)
    IP Tunneling (VS/TUN)


VS/NAT is easy to setup. The load balancer may be a bottleneck of the whole system where the number of servers is more than 20, because both the request packets and response packets need to be rewritten by the load balancer.

VS/DR has the best performance. VS/DR uses MAC-spoofing technology, so it requires that one of the load balancer’s NIC and the real server’s NIC must be in the same IP network segment, and physical segment as well.

VS/TUN is the most scalable. The advantage is since servers connect each other by using IP-tunneling, the load balance and real servers can reside on different LAN , or even WAN.

I plan to setup a set of servers with load balance technique. Realserver run FreeBSD and Solaris system in different places on our business. So it is the only choice for us to use VS/TUN.

We install Fedora Core 5 Linux on our Load Balance server. Kernel-2.6.15 is default.

FreeBSD version is 5.4, we recompile the kernel in order to improve the performance. The default kernel can work too.

Solaris box runs Solaris 10 x86.

Normal Linux Solaris/FreeBSD IP-tunneling

IP tunneling (IP encapsulation) is a technique to encapsulate IP datagram within IP datagram, which allows datagram destined for one IP address to be wrapped and redirected to another IP address. Many modern OS support IP tunneling, such as Linux, FreeBSD and Solaris.

Let’s do some test to setup normal Linux-Solaris and Linux-FreeBSD IPtunneling without LVS.

Testing environment

  1. OS                   IP        Address         Tunnel IP Address
  2. Fedora Core 5 Linux  10.0.0.1           172.16.0.1
  3. Solaris 10 x86        10.0.0.2           172.16.0.2.2
  4. FreeBSD 5.4          10.0.0.3           172.16.0.3.3
复制代码


Config Linux box

In order to create an IP-Tunneling between Linux box and Solaris box, run command:

  1. ip tunnel add tun0 mode ipip remote 10.0.0.2 local 10.0.0.1
  2. ifconfig tun0 172.16.0.1 pointopoint 172.16.0.2
  3. ip tunnel add tun1 mode ipip remote 10.0.0.3 local 10.0.0.1
  4. ifconfig tun1 172.16.0.1 pointopoint 172.16.0.3
复制代码

The ”tun0” is a virtual NIC device name. It can be replaced by other names, such as ”abc0” or ”def0”.

Config Solaris box

To create an IP-tunneling, we have two methods, First method of creating tunnel is using three commands as below.

  1. /sbin/ifconfig ip.tun0 plumb
  2. /sbin/ifconfig ip.tun0 tsrc 10.0.0.2 tdst 10.0.0.1
  3. /sbin/ifconfig ip.tun0 172.16.0.2 172.16.0.1
复制代码

It takes effect immediately.

Second method is creating /etc/hostname.ip.tun0 file which contains two lines as below.

  1. tsrc 10.0.0.2 tdst 10.0.0.1
  2. 172.16.0.2 172.16.0.1 netmask 255.255.255.255 up
复制代码

If we use the second method, the tunnel will exist automatically after we reboot system.

Config FreeBSD box

On FreeBSD box, we also have two methods to create an IP-Tunneling, one way is to run three commands:

  1. ifconfig gif0 create
  2. ifconfig gif0 tunnel 10.0.0.3 10.0.0.1
  3. ifconfig gif0 inet 172.16.0.3 172.168.0.1 netmask 255.255.255.0
复制代码

If we use the second method, the tunnel will exist automatically after we reboot system.

Add three lines on /etc/rc.conf file as below, the tunnel will exist automatically after we reboot system.

  1. gif_interfaces=”gif0”
  2. gifconfig_gif0=”10.0.0.3 10.0.0.1”
  3. ifconfig_gif0=”172.16.0.3 172.16.0.1 netmask 255.255.255.0”
复制代码


Using IP-tunneling

In all the cases on Linux/FreeBSD/Solaris, the netmask value will be default if we ignore to set it.

After creating two tunnels, we can ping 10.0.0.2 and 10.0.0.3 at Linux box successfully, and ping 10.0.0.1 at Solaris or FreeBSD box successfully. All applications layers protocol will work correctly.

Working example of VS/TUN

In VS/TUN mode, because realserver don’t send any datagram to Load balancer, we need not create any tunnel on Linux box and must create proper tunnel on FreeBSD/Solaris box.




We can treat all machines on internet and have their real ip. The client can send their requests to VIP of LB Server. LB Server redirects the requests realserver through IP-tunneling. Realserver can send datagram to client.

Because both the gif0 of FreeBSD and ip.tun0 both are NO-ARP device, they are invisible to client.

Config FreeBSD box

On FreeBSD box, edit /etc/rc.conf as below:


  1. ...

  2. ifconfig_lnc0=”inet 192.168.0.170 netmask 0xffffff00”

  3. gif_interfaces=”gif0”

  4. gifconfig_gif0=”192.168.0.170 192.168.10.100”

  5. ifconfig_gif0=”192.168.10.200 192.168.10.99 netmask 0xffffffff”

  6. apache_enable=”YES”

  7. ...
复制代码


192.168.10.99 looks like a terminal of this tunnel. It can be any idle IP and never use.


Config Solaris box

On Solaris , edit /etc/hostname.ip.tun0 as below:

  1. tsrc 192.168.0.180 tdst 192.168.10.100

  2. 192.168.10.200 192.168.10.99 netmask 255.255.255.255 up

  3. 192.168.10.99 looks like a terminal of this tunnel. It can be any IP address and never use it.
复制代码


Config Load balancer

Run ipvsadm command on Linux Box: Linux kernel must have ip forward enable.

Edit /etc/sysctl.conf and set to:

  1. net.ipv4.ip_forward = 1
复制代码

While running command:

  1. sysctl -p
复制代码

It will take effect.

Run the scripts:

  1. ipvsadm -C
  2. ipvsadm -A -t 192.168.10.200:80 -s wlc
  3. ipvsadm -a -t 192.168.10.200:80 -r 192.168.0.170 -i
  4. ipvsadm -a -t 192.168.10.200:80 -r 192.168.0.180 -i
复制代码

In this script, the port of LB Server and realerver must be same. Good idea is not setting the port of realserver.Now VS/TUN is finished. When a client access 192.168.10.200 at port 80, the datagram will be sent to 192.168.0.170 or 192.168.180 port 80.

Conclusions and Future Work

I believe AIX and HP-UX can also work with VS/TUN, I have no Aix or HP-UX servers. Who can help to provide me with such an environment?

References:

[1] http://www.linuxvirtualserver.org

[2] http://www.triload.com/en/manual/index.html

[3] http://blog.kovyrin.net/2006/03/17/how-to-create-ip-ip-tunnel-between-freebsd-and-linux/

[4] http://docs.sun.com/app/docs/doc/817-0573/6mgc65bcq?a=view

[ 本帖最后由 maluyao 于 2006-9-3 01:00 编辑 ]

fbsolvs.pdf

95.8 KB, 下载次数: 341

论坛徽章:
0
5 [报告]
发表于 2006-08-18 15:43 |只看该作者
写好了,在此:
http://bbs.chinaunix.net/viewthr ... p;extra=#pid5649722

原帖由 Linuxcn.com 于 2006-8-17 14:43 发表
咋就没有看到LVS/TUN的部分捏

论坛徽章:
0
6 [报告]
发表于 2006-08-18 16:47 |只看该作者
我合并了.

论坛徽章:
0
7 [报告]
发表于 2006-09-24 18:45 |只看该作者
这么好的文章没人关注呀,已经在http://zh.linuxvirtualserver.org/node/96这看到你的大做了。
最近这段时间来研究下FC5下的LVS-DR。

论坛徽章:
0
8 [报告]
发表于 2006-10-16 16:31 |只看该作者
Tun模式的是不是就相当于DNS轮巡了?从你的图中看着像.

论坛徽章:
0
9 [报告]
发表于 2006-10-16 19:56 |只看该作者
不是,Tun模式主要是针对RealServer在不同地理位置的情况,效率比DR稍差。

原帖由 dgvri 于 2006-10-16 16:31 发表
Tun模式的是不是就相当于DNS轮巡了?从你的图中看着像.

论坛徽章:
0
10 [报告]
发表于 2006-10-17 09:21 |只看该作者
在不同地理位置怎么去分配访问?就你sina 一样,你如果用nslookup查的话,它有好多IP,这就是在不同地址位置的服务器,那这个又是谁去分配它访问的?总感觉TUN模式就是不同地点的服务器,然后由DNS轮巡的.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP