2

多网关、策略路由、负载均衡的实际应用

 5 months ago
source link: https://bajie.dev/posts/20240111-route_policy2/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

多网关、策略路由、负载均衡的实际应用

2024-01-11 1 分钟阅读
  • 公司有两条线路,一条是鹏博士的,一条是电信的。
  • 鹏博士是100兆共享线路,电信是10兆独享线路。
  • 鹏博士对端的地址是1.1.1.1,本地服务器地址是1.1.1.2
  • 电信对端的地址是2.2.2.2,本地服务器是2.2.2.3

所以导致有两个网关,大部分公司同事用得是100兆的共享,10兆的被客服独用,但是使用率很低。那能否两边都用呢?

答案是可以的,用策略路由就可以。

策略路由需要用到iproute2,没有的话先安装一下。

定义策略路由表

cd /etc/iproute2/  
echo "101 pengboshi" >> rt_tables  
echo "102 dianxin" >> rt_tables  

定义策略路由

ip route add default via 1.1.1.1 table 101  
ip route add default via 2.2.2.2 table 102  

ok,流量分为两个方向,进来的和出去的,先定义简单的,进来的:

ip rule add from 1.1.1.1 table 101  
ip rule add from 2.2.2.2 table 102  

出就比较麻烦了,100兆和10兆按权重来分,8:2吧

ip route replace default scope global \  
nexthop via 1.1.1.1 dev eth0 weight 8 \  
nexthop via 2.2.2.2 dev eth0:1 weight 2  

注意,因为我们是用了一台路由器来连接上面两个网关的,服务器实际是一个网卡上连到交换机,然后再上连路由器的,如下图:

我们把这一切固定下来,编辑/etc/rc.d/rc.local即可

# cat /etc/rc.local
#!/bin/sh
touch /var/lock/subsys/local  
/sbin/ip route replace default scope global nexthop via 1.1.1.1 dev eth0 weight 8 nexthop via 2.2.2.2 dev eth0:1 weight 2

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK