现在的位置: 首页 > Linux > 服务器集群技术 > 正文
LVS之四:DR模型实现
2013年10月08日 服务器集群技术 ⁄ 共 6349字 LVS之四:DR模型实现已关闭评论 ⁄ 被围观 16,868 views+

试验拓扑:

LVS-DR模型网络拓扑

说明:路由器采用linux模拟,使用iptables的NAT实现

一、实验环境准备:

1.1 准备虚拟机:

准备四台linux虚拟机(我这里使用centos 6.4 x86_64),并为路由器linux添加两块网卡,一块接外网,一块接内网。其他服务器都只需要一块网卡。router的内网网卡和集群服务器的的网卡需划分到一个独立的vm交换机上,这点需要注意。

具体如下:

lvs-dr-nat

lvs-dr-rs-net

然后启动各虚拟机,按上述拓扑规划配置好IP地址。

1.2 配置路由器(linux):

使用iptables实现NAT配置较为简单,主要如下:

1.2.1 开启转发功能:

[root@router ~]# echo 1 >/proc/sys/net/ipv4/ip_forward

如果需永久修改,则需要修改sysctl.conf文件。并使用sysctl -p 使其立即生效。

1.2.2 配置iptables:

清除所有规则:

[root@router ~]# iptables –F

配置NAT功能:

[root@router ~]# iptables -t nat -A POSTROUTING -o eth0 -s 172.16.1.0/24 -j SNAT --to 192.168.8.254
[root@router ~]# iptables -t nat -A PREROUTING -d 192.168.8.254 -p tcp --dport 80 -j DNAT --to-destination 172.16.1.253:80

##为了方便使用ssh客户端连接,我还将lvs集群中各服务器的22端口映射到了不同端口,具体如下:

[root@router ~]# iptables -t nat -A PREROUTING -d 192.168.8.254 -p tcp --dport 2022 -j DNAT --to-destination 172.16.1.252:22
[root@router ~]# iptables -t nat -A PREROUTING -d 192.168.8.254 -p tcp --dport 2122 -j DNAT --to-destination 172.16.1.101:22
[root@router ~]# iptables -t nat -A PREROUTING -d 192.168.8.254 -p tcp --dport 2222 -j DNAT --to-destination 172.16.1.102:22

配置转发功能:

[root@router ~]# iptables -A FORWARD -p ip -j ACCEPT

保存规则:

[root@router ~]# service iptables save

此时,Director和RealServer都已可以正常访问外网。

1.3 配置本地windows,添加路由使其能访问lvs集群:

由于试验中物理主机(windows)和试验用路由器(linux)的默认网关指向的是真实的路由器,因此物理主机(windows)是无法访问lvs集群网络的,为了能让物理机(windows)访问到集群网络方便后面测试,因此需要添加路由,命令如下:

C:\windows\system32>route add 172.16.1.0/24 192.168.8.254
操作完成!

二、配置Director

2.1 启用转发:

[root@lvs ~]# echo 1 > /proc/sys/net/ipv4/ip_forward

如果想永久生效,则需要修改/etc/sysctl.conf文件。

2.2 配置VIP和路由:

配置路由:

[root@lvs ~]# route add -host 172.16.1.253 dev eth0:0

配置VIP:

[root@lvs ~]# ifconfig eth0:1 172.16.1.253 netmask 255.255.255.255 up

2.3 清除原有iptables和ipvs规则:

[root@lvs ~]# iptables -F

[root@lvs ~]# iptables -Z

[root@lvs ~]# ipvsadm -C

2.4 配置集群服务:

[root@lvs ~]# ipvsadm -A -t 172.16.1.253:80 -s wlc

2.5 添加RS至集群服务:

[root@lvs ~]# ipvsadm -a -t 172.16.1.253:80 -r 172.16.1.101 -g

[root@lvs ~]# ipvsadm -a -t 172.16.1.253:80 -r 172.16.1.102 -g

2.6 将上述过程编写为一个服务脚本,直接在Director上实现开机启动:

#!/bin/bash
#
# LVS script for VS/DR
# chkconfig: - 90 10
#
. /etc/rc.d/init.d/functions
#
VIP=172.16.1.253
DIP=172.16.1.252
RIP1=172.16.1.101
RIP2=172.16.1.102
PORT=80
RSWEIGHT1=5
RSWEIGHT2=5

#
case "$1" in
start)          

  /sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up
  /sbin/route add -host $VIP dev eth0:1

# Since this is the Director we must be able to forward packets
  echo 1 > /proc/sys/net/ipv4/ip_forward

# Clear all iptables rules.
  /sbin/iptables -F

# Reset iptables counters.
  /sbin/iptables -Z

# Clear all ipvsadm rules/services.
  /sbin/ipvsadm -C

# Add an IP virtual service for VIP 192.168.0.219 port 80
# In this recipe, we will use the round-robin scheduling method.
# In production, however, you should use a weighted, dynamic scheduling method.
  /sbin/ipvsadm -A -t $VIP:80 -s wlc

# Now direct packets for this VIP to
# the real server IP (RIP) inside the cluster
  /sbin/ipvsadm -a -t $VIP:80 -r $RIP1 -g -w $RSWEIGHT1
  /sbin/ipvsadm -a -t $VIP:80 -r $RIP2 -g -w $RSWEIGHT2

  /bin/touch /var/lock/subsys/ipvsadm &> /dev/null
;;

stop)
# Stop forwarding packets
  echo 0 > /proc/sys/net/ipv4/ip_forward

# Reset ipvsadm
  /sbin/ipvsadm -C

# Bring down the VIP interface
  /sbin/ifconfig eth0:0 down
  /sbin/route del $VIP
 
  /bin/rm -f /var/lock/subsys/ipvsadm
 
  echo "ipvs is stopped..."
;;

status)
  if [ ! -e /var/lock/subsys/ipvsadm ]; then
    echo "ipvsadm is stopped ..."
  else
    echo "ipvs is running ..."
    ipvsadm -L -n
  fi
;;
*)
  echo "Usage: $0 {start|stop|status}"
;;
esac

将上述内容保存在/etc/init.d/lvs-director文件中,然后添加到服务:

[root@lvs ~]# chmod +x /etc/init.d/lvs-director

[root@lvs ~]# chkconfig --add lvs-director

[root@lvs ~]# chkconfig lvs-director on

[root@lvs ~]# /etc/init.d/lvs-director start
[root@lvs ~]# /etc/init.d/lvs-director status
ipvs is running ...
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.16.1.253:80 wlc
  -> 172.16.1.101:80              Route   5      0          0        
  -> 172.16.1.102:80              Route   5      0          0

三、配置RS:

3.1 配置ARP广播:

[root@rs1 ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@rs1 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@rs1 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@rs1 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce

3.2 配置VIP和路由:

[root@rs1 ~]# ifconfig lo:0 172.16.1.253 netmask 255.255.255.255 up

[root@rs1 ~]# route add -host 172.16.1.253 dev lo:0

3.3 编写为脚本:

#!/bin/bash
#
# Script to start LVS DR real server.
# chkconfig: - 90 10
# description: LVS DR real server
#
.  /etc/rc.d/init.d/functions

VIP=172.16.1.253

host=`/bin/hostname`

case "$1" in
start)
       # Start LVS-DR real server on this machine.
        /sbin/ifconfig lo down
        /sbin/ifconfig lo up
        echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
        echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce

        /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
        /sbin/route add -host $VIP dev lo:0

;;
stop)

        # Stop LVS-DR real server loopback device(s).
        /sbin/ifconfig lo:0 down
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce

;;
status)

        # Status of LVS-DR real server.
        islothere=`/sbin/ifconfig lo:0 | grep $VIP`
        isrothere=`netstat -rn | grep "lo:0" | grep $VIP`
        if [ ! "$islothere" -o ! "isrothere" ];then
            # Either the route or the lo:0 device
            # not found.
            echo "LVS-DR real server Stopped."
        else
            echo "LVS-DR real server Running."
        fi
;;
*)
            # Invalid entry.
            echo "$0: Usage: $0 {start|status|stop}"
            exit 1
;;
esac

保存至/etc/init.d/lvs-rs,并赋予执行权限,然后添加为开机启动:

[root@rs1~]# chmod +x /etc/init.d/lvs-rs

[root@rs1 ~]# chkconfig --add lvs-rs

[root@rs1 ~]# chkconfig lvs-rs on

[root@rs1 ~]# /etc/init.d/lvs-rs start

3.4 在RS2上采用相同的配置即可

至此就全部完成了lvs的DR模型配置。

4 测试LVS集群

4.1 配置web服务器:

4.1.1 为rs1节点添加web主页:

[root@rs1 ~]# service httpd start
正在启动 httpd:httpd: apr_sockaddr_info_get() failed for rs1
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
                                                           [确定]
[root@rs1 ~]# echo "This is RS1..." >/var/www/html/index.html

4.1.2 为rs2节点添加web主页:

[root@rs2 ~]# echo "This is RS2..." >/var/www/html/index.html

4.2 访问测试:

访问时观察ipvs状态:

Every 1.0s: ipvsadm -L -n                                                                        Tue Oct  8 19:58:42 2013

IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.16.1.253:80 wlc
  -> 172.16.1.101:80              Route   5     2          8
  -> 172.16.1.102:80              Route   5     1          8

抱歉!评论已关闭.

×