天天看点

LVS-DR VIP和RIP不同网段的配置方法

在网上流传的很多文章中,LVS-DR模式都要求VIP和RIP在同一网段,很少有在不同网段配置成功的案例。但章文嵩博士已经在官网上明

段的网络连接起来就可以,真实服务器可以通过自己的路由器将响应报文发送给客户。也就是说Director至少要有两个网口才满足VIP和RIP

不在同一网段。

从realserver到client路由,可以是另外的,也可能通过director,但要使用director做路由必须对director的内核打forward_shared

如果使用另外的路由,则只要求能从realserver到client就行了,不需要得到client的响应,因此realserver可以放在防火墙后面。

下面是具体的配置过程:

[@more@]

director有两块网卡:

eth0 192.168.2.254 用于跟realserver连接

eth1 61.180.79.110 外网IP,VIP也绑定到此网卡

director的配置

1、安装ipvsadm

ipvsadm是用来管理lvs设置的命令行工具,可以用来查看当前状态。

yum install ipvsadm

2、安装keepalived

keepalived可以用于双机热备的自动故障转移,也可以管理lvs的realserver列表,自动对realserver做健康检查。

tar zxvf keepalived-1.2.2.tar.gz

cd keepalived-1.2.2

./configure --prefix=/usr --sysconfdir=/etc

如果最后显示

Use IPVS Framework : No

IPVS sync daemon support : No

请指定内核源码目录再运行configure

./configure --prefix=/usr --sysconfdir=/etc --with-kernel-dir=/usr/src/kernels/linux-2.6.35.9

make && make install 

将keepalived加入服务中

chkconfig --add keepalived 

chkconfig --level 35 keepalived on

修改配置文件

mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.old 

vi /etc/keepalived/keepalived.conf

填入以下内容

! Configuration File for keepalived 

global_defs { 

notification_email { 

smtp_server 127.0.0.1 

smtp_connect_timeout 30 

router_id LVS_DEVEL 

}

vrrp_instance VI_1 { 

state MASTER #备份服务器上将MASTER改为BACKUP 

interface eth1 #HA监测网络接口(绑定VIP的网口)

virtual_router_id 1 #主、备机的virtual_router_id一定要相同,必须相同!!! 

priority 100 #主、备机取不同的优先级,主机值较大,备份机值较小 

advert_int 1 #VRRP Multicast广播周期秒数 

authentication { 

auth_type PASS #VRRP认证方式 

auth_pass 1111 #VRRP口令字 

virtual_ipaddress { 

61.180.79.18 #LVS虚拟地址,可写多个VIP,一行一个VIP 

virtual_server 61.180.79.18 80 { 

delay_loop 2 #延时等待时间 

lb_algo wrr #轮询算法 

lb_kind DR #传输模式 

persistence_timeout 0 #单一链接重连保持时间秒 

protocol TCP

real_server 192.168.2.11 80 { 

weight 1 #权重 

TCP_CHECK { #realserve的状态检测设置部分,单位是秒 

connect_timeout 3 

nb_get_retry 3 

delay_before_retry 3 

real_server 192.168.2.12 80 { 

realserver的配置

rs1和rs2的IP分别是192.168.2.11 192.168.2.12

1、安装nginx用于测试

tar zxvf nginx-0.8.54.tar.gz

cd nginx-0.8.54

./configure --prefix=/usr/local/nginx

make && make install

启动nginx

/usr/local/nginx/sbin/nginx

2、绑定VIP到lo

ifconfig lo:0 61.180.79.18 broadcast 61.180.79.18 netmask 255.255.255.255 up

3、将绑定VIP的网卡设置为no_arp

vi /etc/sysctl.conf

增加

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

4、设置realserver能到达client的路由网关

route add default gw 192.168.2.1

我开始测试的时候,使用一个tp-link的无线宽带路由器,一直配置不成功,后来用tcpdump分析才发现,这个路由器就是不转发从

realserver到client的数据包,不知道什么原因,但直接ping client又是通的,后来直接用一台linux做路由就可以了

(net.ipv4.ip_forward = 1,而且这台路由启用NAT也不影响)。

如果要使用director做realserver到client的网关,就需要给director的内核打补丁,我是使用最新的内核编译的,过程如下:

tar -xjvf linux-2.6.35.9.tar.bz2

mv linux-2.6.35.9 /usr/src/kernels

为内核打forward_shared补丁

cd /usr/src/kernels

patch -p1 < ../forward_shared-2.6.34-2.diff

cd

为了方便编译配置,将boot目录下的配置文件复制到 /usr/src/kernels/linux-2.6.35.9

cp /boot/config-2.6.18-194.32.1.el5 /usr/src/kernels/linux-2.6.35.9/.config

make menuconfig 选择 Load an Alternate Configuration 回车确认载入

默认情况下启动时会提示如下错误:

mount: could not find filesystem ‘/dev/root’

setuproot: moving /dev failed: No such file or directory

setuproot: error mounting /proc: No such file or directory

setuproot: error mounting /sys: No such file or directory

switchroot: mount failed: No such file or directory

Kernel panic – not syncing: Attempted to kill init!

解决方法:

执行完make menuconfig后,修改/usr/src/kernels/linux-2.6.35.9/.config

将#CONFIG_SYSFS_DEPRECATED_V2 is not set

默认被注释掉的,将其改为y。即修改为

CONFIG_SYSFS_DEPRECATED_V2=y

根据需要再选择编译选项;

make (这个过程相当的漫长,起码半个小时以上)

如果没有错误,执行模块编译安装

make modules_install

编译内核安装

make install

sh /usr/src/kernels/linux-2.6.35.9/arch/x86/boot/install.sh 2.6.35.9 arch/x86/boot/bzImage 

System.map "/boot"

看到如上提示表示编译成功,

如果没有看到错误,可以修改启动项 让 centos 启动时加载 新内核, (如有编译错误请勿修改启动配置,否则启动失败)

vi /boot/grub/grub.conf

default=0 (修改为0 即可)

不知为什么这个内核启动的时候会报错

insmod: error inserting '/lib/dm-region-hash.ko' : -1 File exists

解决办法如下:

1、解压initrd文件

[root@vhost ~]# cp /boot/initrd-2.6.35.4.img /tmp

[root@vhost ~]# cd /tmp/

[root@vhost tmp]# ls

initrd-2.6.35.4.img

[root@vhost tmp]# mkdir newinitrd

[root@vhost tmp]# cd newinitrd/

[root@vhost newinitrd]# zcat ../initrd-2.6.35.4.img |cpio -i

11282 blocks

释放之后看到如下内容

[root@vhost newinitrd]# ls

bin dev etc init lib proc sbin sys sysroot

2、下边就是编辑init,删掉其中重复的四行中的两行

echo "Loading dm-region-hash.ko module"

insmod /lib/dm-region-hash.ko

3、重新打包initrd

[root@vhost newinitrd]# find .|cpio -c -o > ../initrd

[root@vhost newinitrd]# cd ..

[root@vhost tmp]# gzip -9 < initrd > initrd.img

initrd-2.6.35.4.img initrd initrd.img newinitrd

initrd.img就是重新打包的initrd了,然后把initrd.img拷贝到/boot

[root@vhost tmp]# mv /boot/initrd-2.6.35.4.img /boot/initrd-2.6.35.4.img.bak

[root@vhost tmp]# mv initrd.img /boot/initrd-2.6.35.4.img

[root@vhost tmp]# reboot

这样“insmod: error inserting '/lib/dm-region-hash.ko' : -1 File exists” 就不会有了

使用新内核重启后

修改或增加

net.ipv4.ip_forward = 1

net.ipv4.conf.default.rp_filter = 0

net.ipv4.conf.eth0.forward_shared = 1

net.ipv4.conf.all.forward_shared = 1

注意查看forward_shared选项是否成功设置

这样,就可以将realserver的网关改为192.168.2.254了

本文转自 yntmdr 51CTO博客,原文链接:http://blog.51cto.com/yntmdr/1588328,如需转载请自行联系原作者

继续阅读