歡迎您光臨本站 註冊首頁

奉獻--LVS在Fedora Core5上的配置指南(以Linux、FreeBSD、Solaris系統為RealServer)

(附件分別是分頁的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網路拓撲圖
http://zh.linuxvirtualserver.org/files/img3.png

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 網路拓撲圖
http://zh.linuxvirtualserver.org/files/img12.png

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/TUN的部分捏
《解決方案》

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

OS                   IP        Address         Tunnel IP Address
Fedora Core 5 Linux  10.0.0.1           172.16.0.1
Solaris 10 x86        10.0.0.2           172.16.0.2.2
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:

ip tunnel add tun0 mode ipip remote 10.0.0.2 local 10.0.0.1
ifconfig tun0 172.16.0.1 pointopoint 172.16.0.2
ip tunnel add tun1 mode ipip remote 10.0.0.3 local 10.0.0.1
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.

/sbin/ifconfig ip.tun0 plumb
/sbin/ifconfig ip.tun0 tsrc 10.0.0.2 tdst 10.0.0.1
/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.

tsrc 10.0.0.2 tdst 10.0.0.1
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:

ifconfig gif0 create
ifconfig gif0 tunnel 10.0.0.3 10.0.0.1
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.

gif_interfaces=」gif0」
gifconfig_gif0=」10.0.0.3 10.0.0.1」
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.


http://kb.linuxvirtualserver.org/images/thumb/8/8f/Topo.png/600px-Topo.png

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:


...

ifconfig_lnc0=」inet 192.168.0.170 netmask 0xffffff00」

gif_interfaces=」gif0」

gifconfig_gif0=」192.168.0.170 192.168.10.100」

ifconfig_gif0=」192.168.10.200 192.168.10.99 netmask 0xffffffff」

apache_enable=」YES」

...


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:

tsrc 192.168.0.180 tdst 192.168.10.100

192.168.10.200 192.168.10.99 netmask 255.255.255.255 up

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:

net.ipv4.ip_forward = 1

While running command:

sysctl -p

It will take effect.

Run the scripts:

ipvsadm -C
ipvsadm -A -t 192.168.10.200:80 -s wlc
ipvsadm -a -t 192.168.10.200:80 -r 192.168.0.170 -i
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:

http://www.linuxvirtualserver.org

http://www.triload.com/en/manual/index.html

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

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

[ 本帖最後由 maluyao 於 2006-9-3 01:00 編輯 ]
《解決方案》

寫好了,在此:
http://bbs.chinaunix.net/viewthread.php?tid=813853&page=1&extra=#pid5649722

原帖由 Linuxcn.com 於 2006-8-17 14:43 發表
咋就沒有看到LVS/TUN的部分捏
《解決方案》

我合併了.
《解決方案》

這麼好的文章沒人關注呀,已經在http://zh.linuxvirtualserver.org/node/96這看到你的大做了。
最近這段時間來研究下FC5下的LVS-DR。
《解決方案》

Tun模式的是不是就相當於DNS輪巡了?從你的圖中看著像.
《解決方案》

不是,Tun模式主要是針對RealServer在不同地理位置的情況,效率比DR稍差。

原帖由 dgvri 於 2006-10-16 16:31 發表
Tun模式的是不是就相當於DNS輪巡了?從你的圖中看著像.
《解決方案》

在不同地理位置怎麼去分配訪問?就你sina 一樣,你如果用nslookup查的話,它有好多IP,這就是在不同地址位置的伺服器,那這個又是誰去分配它訪問的?總感覺TUN模式就是不同地點的伺服器,然後由DNS輪巡的.

[火星人 ] 奉獻--LVS在Fedora Core5上的配置指南(以Linux、FreeBSD、Solaris系統為RealServer)已經有942次圍觀

http://coctec.com/docs/service/show-post-6216.html