searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

iptables -m statistic --mode nth 实现流量分担

2023-10-07 09:35:59
37
0

有这么一种情况了,使用linux作为一个网关,假如有多个弹性ip,那么如何使得经过网关的流量从几个弹性ip分流出去呢

有一种办法就是解决使用Linux系统自带的的iptables的statistic match,而不用引入其他组件


假设有2个弹性ip
必须严格按照顺序来(否则达不到要的效果)  --every 2 --packet 0 在前,--every 1 --packet 0在后面
添加规则
sudo iptables -t nat -A PREROUTING -m state --state NEW -m statistic --mode nth --every 2 --packet 0 -j SNAT --to-destination 10.50.208.92
sudo iptables -t nat -A PREROUTING -m state --state NEW -m statistic --mode nth --every 1 --packet 0 -j SNAT --to-destination 10.50.208.93


删除规则
sudo iptables -t nat -D PREROUTING -m state --state NEW -m statistic --mode nth --every 2 --packet 0 -j SNAT --to-destination 10.50.208.92
sudo iptables -t nat -D PREROUTING -m state --state NEW -m statistic --mode nth --every 1 --packet 0 -j SNAT --to-destination 10.50.208.93

 


假设有3个弹性ip
注意规则的顺序,由先到后的顺序是
--mode nth --every 3 --packet 0
--mode nth --every 2 --packet 0
--mode nth --every 1 --packet 0

sudo iptables -t nat -A PREROUTING -m state --state NEW -m statistic --mode nth --every 3 --packet 0 -j SNAT --to-destination 10.50.208.92
sudo iptables -t nat -A PREROUTING -m state --state NEW -m statistic --mode nth --every 2 --packet 0 -j SNAT --to-destination 10.50.208.93
sudo iptables -t nat -A PREROUTING -m state --state NEW -m statistic --mode nth --every 1 --packet 0 -j SNAT --to-destination 10.50.208.94

删除规则
sudo iptables -t nat -D PREROUTING -m state --state NEW -m statistic --mode nth --every 3 --packet 0 -j SNAT --to-destination 10.50.208.92
sudo iptables -t nat -D PREROUTING -m state --state NEW -m statistic --mode nth --every 2 --packet 0 -j SNAT --to-destination 10.50.208.93
sudo iptables -t nat -D PREROUTING -m state --state NEW -m statistic --mode nth --every 1 --packet 0 -j SNAT --to-destination 10.50.208.94

0条评论
0 / 1000