双网卡虚机配置策略路由

一、两台虚机的主从网卡处于同VPC同subnet的情况

 1、环境拓扑

 

 2、目的

双网卡虚机VM1和VM2处于同VPC同sub的情况下,要求VM1的主从网卡与VM2的主从网卡网络互通。

3、配置方法

 (1)两台双网卡虚机

    VM1  主网卡eth0:192.168.0.148     从网卡:192.168.0.160

    VM2  主网卡eth0:192.168.0.145     从网卡:192.168.0.42

 

 

 

 

(2)在未进行任何配置的情况下,VM1的主网卡192.168.0.148可以和VM2的主卡192.168.0.145可以互通

 

 

 

 (3)在未进行任何配置的情况下,VM1的从网卡192.168.0.160和VM2的从网卡192.168.0.42网络不互通

 

 

(4)在虚机VM1上配置策略路由

#编辑rt_tables

echo "148 net_148 " >> /etc/iproute2/rt_tables

echo "160 net_160 " >> /etc/iproute2/rt_tables

 

#清空net_148路由表

ip route flush table net_148

# 添加一个路由规则到 net_148 表,这条规则是 net_148 这个路由表中数据包默认使用源 IP  通过 eth0  192.168.0.148 走网关 192.168.0.1

ip route add default via 192.168.0.1 dev eth0 src 192.168.0.148 table net_148

#来自 192.168.0.148 的数据包,使用 net_148 路由表的路由规则

ip rule add from 192.168.0.148 table net_148

 

#清空net_160路由表

ip route flush table net_160

#添加一个路由规则到 net_160 表,这条规则是 net_160 这个路由表中数据包默认使用源 IP 192.168.0.160 通过 eth1 走网关 192.168.0.1

ip route add default via 192.168.0.1 dev eth1 src 192.168.0.160 table net_160

#来自 192.168.0.160 的数据包,使用 net_160 路由表的路由规则

ip rule add from 192.168.0.160 table net_160

 

#ip rule查看策略路由表

 

 

#配置完成后,从VM2测试ping虚机VM1,主从网卡都可连通

 

 (5)在虚机VM2上配置策略路由

#编辑rt_tables

echo "145 net_145 " >> /etc/iproute2/rt_tables

echo "42 net_42 " >> /etc/iproute2/rt_tables

 

#清空net_145路由表

ip route flush table net_145

# 添加一个路由规则到 net_145 表,这条规则是 net_145 这个路由表中数据包默认使用源 IP  通过 eth0  192.168.0.145 走网关 192.168.0.1

ip route add default via 192.168.0.1 dev eth0 src 192.168.0.145 table net_145

#来自 192.168.0.145 的数据包,使用 net_145 路由表的路由规则

ip rule add from 192.168.0.145 table net_145

 

#清空net_42路由表  

ip route flush table net_42

#添加一个路由规则到 net_42 表,这条规则是 net_42 这个路由表中数据包默认使用源 IP 192.168.0.42 通过 eth1 走网关 192.168.0.1

ip route add default via 192.168.0.1 dev eth1 src 192.168.0.42 table net_42

#来自 192.168.0.42 的数据包,使用 net_42 路由表的路由规则

ip rule add from 192.168.0.42 table net_42

 

#ip rule 查看策略路由表

 

 

#配置完成后,从VM1测试ping虚机VM2,主从网卡都可以连通

 

(6)如果需要自启动生效可以写进配置文件也可以加入rc.local

#VM1上配置

#vi /etc/rc.local

#ip route flush table net_145

#ip route add default via 192.168.0.1 dev eth0 src 192.168.0.145 table net_145

#ip rule add from 192.168.0.145 table net_145

#ip route flush table net_42

 

#ip route add default via 192.168.0.1 dev eth1 src 192.168.0.42 table net_42

#ip rule add from 192.168.0.42 table net_42

 

#VM2上配置

#vi /etc/rc.local

#ip route flush table net_148

# ip route add default via 192.168.0.1 dev eth0 src 192.168.0.148 table net_148

# ip rule add from 192.168.0.148 table net_148

# ip route flush table net_160

# ip route add default via 192.168.0.1 dev eth1 src 192.168.0.160 table net_160

# ip rule add from 192.168.0.160 table net_160

 

二、两台虚机的主从网卡属于同VPC,其中主网卡同属一个subnet,从网卡同属于另一个subnet

1、主网卡或者从网卡进行对等访问,不需要加策略路由

 

 2、 如果是需要主从网卡交叉访问,则需要加策略路由

 

三、两台虚机的主从网卡不同VPC

这种情况下只能进行对等访问,不需要加策略路由

 

posted on 2021-09-17 10:18  辰Cheney  阅读(457)  评论(0编辑  收藏  举报