IPVS—LVS On FreeBSD的安装,配置

虽然LVS很早就出来了.不过近日听朋友提起,觉得比较有意思,就看了一下,顺便装来玩一下.因为是看文档操作的,过程有点闷.
但幸庆的是,终于是配置成功了.

测试环境:
VMWare 4.5.2
Load-Balancer : FreeBSD 6.1 (IPVS-0.4.0_2)
Real Server-01 : FreeBSD 6.1 (Apache 2, ModPerl 2, Mysql 5 Client)
Real Server-02 : FreeBSD 6.1 (Apache 2, ModPerl 2, Mysql 5 Client)

过程:
freebsd-lb#cd /usr/ports/net/ipvs/
freebsd-lb#make install clean
上面的过程会出现错误,于是我将ipvs-0.4.0_2.tar.gz拷贝到/tmp目录下.
freebsd-lb#cp /usr/ports/distfiles/ipvs-0.4.0_2.tar.gz /tmp/
再解压
freebsd-lb#tar -vzxf
ipvs-0.4.0_2.tar.gz
这样只是巧合不用再到指定地方下载ipvs-0.4.0_2.tar.gz这个包哈.
按照官方文档,正确过程应该是对内核打补丁,再重新编译后安装IPVS的.
下面就按照官方文档所说的过程操作吧,不过我把其中一些错误给修正了.
1. Patch the FreeBSD kernel:
freebsd-lb#cd /tmp/ipvs-0.4.0_2
freebsd-lb#cp patch/OS-RELEASE/* /usr/src
OS-RELEASE对应你的系统版本号,其中有5.3-RELEASE,5.4-RELEASE,6.0-RELEASE,7.0-CURRENT
我找不到6.1-RELEASE对应的目录,所以用了6.0的,后来证明6.0与6.1的patch在uipc_socket.c.path有点不一样.稍后再说.
进入源代码目录,打补丁:

freebsd-lb#cd /usr/src
freebsd-lb#patch -p0 < uipc_socket.c.patch
freebsd-lb#patch -p0 < ip_input.c.patch
freebsd-lb#patch -p0 < socketvar.h.patch
freebsd-lb#patch -p0 < sys_conf_options.patch

因为6.1的uipc_socket.c与6.0的uipc_socket.c文件不一样,所以补丁没全部打上,我们要手动修改uipc_socket.c.patch

freebsd-lb#vi sys/kern/uipc_socket.c

找到#include “opt_compat.h”这一行,在下面加上#include “opt_nfsockopt.h”
另外,6.0的目录下没有sys_i386_conf_GENERIC.patch文件,所以要在内核里加上options NF_SOCKOPT #nf_sockopt API这一项.或者直接使用5.3-RELEASE下的sys_i386_conf_GENERIC.patch文件.不过我用另一个文件名,所以我手动修改这个内核文件.
下面就可以重新编译内核了.
2. Rebuilding FreeBSD kernel:
freebsd-lb#cd /usr/src/sys/arch/conf
arch要用i386/ia64这些替换,我就用的是i386.
安全起见,将旧的内核存为新的一份,编译新内核.

freebsd-lb#cp GENERIC MYKERNEL
freebsd-lb#config MYKERNEL
freebsd-lb#cd ../compile/MYKERNEL
freebsd-lb#make depend
freebsd-lb#make
freebsd-lb#make install

3. Compile ipvs module:

freebsd-lb#cd /tmp/ipvs-0.4.0_2/ipvs
freebsd-lb#make
freebsd-lb#make install
freebsd-lb#cd ipvsadm
freebsd-lb#mkdir -p -m 777 /man/man8
freebsd-lb#make
freebsd-lb#make install

4. Compile ipvs scheduler module:

freebsd-lb#cd ../schedulers
freebsd-lb#cd SCHEDULER-NAME
SCHEDULER-NAME要用指定的名称替换,我选择用rr算法,所以我是cd rr
freebsd-lb#make
freebsd-lb#make install

5. Reboot with new kernel:

freebsd-lb#reboot

启动ipvs前的网络配置.

Load-Balancer : lnc0(VIP:192.168.72.122/32, DIR:192.168.72.123/24)
Real Server-01: lnc0(RIP:192.168.72.125/24), lo0(VIP:192.168.72.122/32, NOARP)
Real Server-02: lnc0(RIP:192.168.72.126/24), lo0(VIP:192.168.72.122/32, NOARP)

执行下面指令设置VIP

freebsd-lb#ifconfig lnc0 alias 192.168.72.122 netmask 0xffffffff up
freebsd-server01#ifconfig lo0 alias 192.168.72.122 netmask 0xffffffff -arp up
freebsd-server02#ifconfig lo0 alias 192.168.72.122 netmask 0xffffffff -arp up

启动ipvs步骤:
我使用的是LVS/DR:

1. freebsd-lb#kldload /boot/kernel/ipvs.ko
2. freebsd-lb#kldload /boot/kernel/ip_vs_rr.ko
3. freebsd-lb#ipvsadm -A -t 192.168.72.122:80 -s rr
4. freebsd-lb#ipvsadm -a -t 192.168.72.122:80 -r 192.168.72.125:80 -g
5. freebsd-lb#ipvsadm -a -t 192.168.72.122:80 -r 192.168.72.126:80 -g

停止ipvs步骤:

1. freebsd-lb#ipvsadm -C
2. freebsd-lb#kldunload ip_vs_rr
3. freebsd-lb#kldunload ipvs