以下是我的网络拓扑:
+ +
| |
| |
| |
| +------------------+ |
| | | |
+-----+ firewall +-------+
eth0 | | eth1
+--------+---------+
| eth4
|
|
+-----+---------+
| |
+---------+ switch +---+
| | | |
| +-+-+-+-+-+-+-+-+ |
| | | | | | | | | |
+ + + + + + + + + +
10 Vlans
我使用扩展统计和连接标记来平衡 LAN 网络的负载。但模块标记和统计功能不太好。我的iptables如下:
#!/bin/sh
#
# delete all existing rules.
#
IPT='/sbin/iptables'
LAN_IF='eth4'
WAN_IF='eth0'
OPT_IF='eth1'
LAN_NET='192.168.10.0/24'
VLAN1_NET='192.168.101.0/24'
VLAN2_NET='192.168.102.0/24'
VLAN3_NET='192.168.103.0/24'
VLAN4_NET='192.168.104.0/24'
VLAN5_NET='192.168.105.0/24'
VLAN6_NET='192.168.106.0/24'
VLAN7_NET='192.168.107.0/24'
$IPT -F
$IPT -t nat -F
$IPT -t mangle -F
$IPT -X
#$IPT -A INPUT -j LOG --log-level 4 --log-prefix 'NETFILTER'
#$IPT -A OUTPUT -j LOG --log-level 4 --log-prefix 'NETFILTER'
$IPT -A FORWARD -j LOG --log-level 4 --log-prefix 'NETFILTER '
$IPT -P INPUT DROP
$IPT -P OUTPUT DROP
$IPT -P FORWARD DROP
# Always accept loopback traffic
$IPT -A INPUT -i lo -j ACCEPT
$IPT -A OUTPUT -o lo -j ACCEPT
# Allow for lan net
$IPT -A OUTPUT -o $LAN_IF -j ACCEPT
$IPT -A INPUT -i $LAN_IF -j ACCEPT
# Allow from local to internet
$IPT -A OUTPUT -o $WAN_IF -j ACCEPT
$IPT -A OUTPUT -o $OPT_IF -j ACCEPT
# Allow established connections, and those not coming from the outside
$IPT -A INPUT -s $LAN_NET -p icmp -j ACCEPT
$IPT -A OUTPUT -s $LAN_NET -p icmp -j ACCEPT
$IPT -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPT -A INPUT -m state --state NEW -i $LAN_IF -j ACCEPT
# Allow forward both WANT and OPT
$IPT -A FORWARD -i $WAN_IF -o $LAN_IF -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPT -A FORWARD -i $OPT_IF -o $LAN_IF -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allow outgoing connections from the LAN side.
$IPT -A FORWARD -s $LAN_NET -o $WAN_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN7_NET -o $WAN_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN6_NET -o $WAN_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN5_NET -o $WAN_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN4_NET -o $WAN_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN3_NET -o $WAN_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN2_NET -o $WAN_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN1_NET -o $WAN_IF -j ACCEPT
#$IPT -A FORWARD -s $LAN_NET -o $OPT_IF -j ACCEPT
# Allow outgoing connections from the LAN side.
$IPT -A FORWARD -s $LAN_NET -o $OPT_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN5_NET -o $OPT_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN6_NET -o $OPT_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN7_NET -o $OPT_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN4_NET -o $OPT_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN3_NET -o $OPT_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN2_NET -o $OPT_IF -j ACCEPT
$IPT -A FORWARD -s $VLAN1_NET -o $OPT_IF -j ACCEPT
$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -s $VLAN1_NET -j ACCEPT
$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -d $VLAN1_NET -j ACCEPT
$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -s $VLAN2_NET -j ACCEPT
$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -d $VLAN2_NET -j ACCEPT
$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -s $VLAN3_NET -j ACCEPT
$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -d $VLAN3_NET -j ACCEPT
$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -s $VLAN4_NET -j ACCEPT
$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -d $VLAN4_NET -j ACCEPT
$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -s $VLAN5_NET -j ACCEPT
$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -d $VLAN5_NET -j ACCEPT
$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -s $VLAN6_NET -j ACCEPT
$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -d $VLAN6_NET -j ACCEPT
$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -s $VLAN7_NET -j ACCEPT
$IPT -A FORWARD -i $LAN_IF -o $LAN_IF -d $VLAN7_NET -j ACCEPT
# Masquerade.
$IPT -t nat -A POSTROUTING -o $WAN_IF -j MASQUERADE
$IPT -t nat -A POSTROUTING -o $OPT_IF -j MASQUERADE
# load balancing
$IPT -A PREROUTING -t mangle -j CONNMARK --restore-mark
$IPT -A PREROUTING -t mangle -m mark ! --mark 0 -j ACCEPT
$IPT -A PREROUTING -p icmp -t mangle -m statistic --mode nth --every 2 --packet 0 -j MARK --set-mark 2
$IPT -A PREROUTING -p icmp -t mangle -m statistic --mode nth --every 2 --packet 1 -j MARK --set-mark 3
$IPT -A PREROUTING -t mangle -j CONNMARK --save-mark
# Enable routing.
echo 1 > /proc/sys/net/ipv4/ip_forward
我通过命令进行调试:cat /var/log/messages |grep 0x2 |wc -l
和cat /var/log/messages |grep 0x3 |wc -l
但标记为0x2的数据包和标记为0x3的数据包数量并不平衡。为什么会发生这种情况?
答案1
脚本的负载平衡部分表示:
- 如果我已经知道这种联系,就按照以前的方式进行吧
- 如果不这样做,一半的时间在一个接口上发送,一半的时间在另一个接口上发送
因此每个接口上的连接数量相同。
但有些连接只有 3 个数据包,而其他连接有 1000 个数据包时,数据包计数将不相等。此外,如果检查打开的连接数,它可能不相等,因为某些连接持续时间更长。
为了对数据包进行负载平衡,您必须删除这些行:
$IPT -A PREROUTING -t mangle -j CONNMARK --restore-mark
$IPT -A PREROUTING -t mangle -m mark ! --mark 0 -j ACCEPT
$IPT -A PREROUTING -t mangle -j CONNMARK --save-mark
但这将是一个糟糕的主意,因为您的连接将具有不同的 IP 源,并且由于不对称路由,一半的数据包不会到达目标服务。并且平衡数据包不会平衡此后发送的位。即使发送的平衡位也不会平衡接收到的位。
在我看来,你应该让你的脚本保持现在的样子,流量越多,你的链接就会越平衡。