实验环境
操作系统
TencentOS Server 3.1 (TK4)
网络配置
[root@VM-0-6-centos ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 52:54:00:74:5b:24 brd ff:ff:ff:ff:ff:ff
inet 172.21.0.6/20 brd 172.21.15.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::5054:ff:fe74:5b24/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 20:90:6f:8a:29:24 brd ff:ff:ff:ff:ff:ff
inet 172.21.0.12/20 brd 172.21.15.255 scope global noprefixroute eth1
valid_lft forever preferred_lft forever
inet6 fe80::b55f:5ef7:fa45:c620/64 scope link noprefixroute
valid_lft forever preferred_lft forever
两个网卡
主网卡 172.21.0.6
弹性网卡 172.21.0.12
在/etc/iproute2/rt_tables
创建一个路由表 wangli
[root@VM-0-6-centos ~]# cat /etc/iproute2/rt_tables
#
# reserved values
#
255 local
254 main
253 default
0 unspec
252 wangli #<----------------here!
#
# local
#
#1 inr.ruhep
设置从辅助网卡172.21.0.12
来的流量都要使用wangli
这个表
ip rule add from 172.21.0.12 lookup wangli
给wangli
表添加一个默认路由规则,让流量走eth1
辅助网卡
ip route add table wangli default via 172.21.0.1 dev eth1
测试
[root@VM-0-16-centos ~]# ping 172.21.0.6
PING 172.21.0.6 (172.21.0.6) 56(84) bytes of data.
64 bytes from 172.21.0.6: icmp_seq=1 ttl=64 time=0.244 ms
^C
--- 172.21.0.6 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.244/0.244/0.244/0.000 ms
[root@VM-0-16-centos ~]# ping 172.21.0.12
PING 172.21.0.12 (172.21.0.12) 56(84) bytes of data.
64 bytes from 172.21.0.12: icmp_seq=1 ttl=64 time=0.220 ms
^C
--- 172.21.0.12 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.220/0.220/0.220/0.000 ms
[root@VM-0-16-centos ~]#
持久化
在/etc/rc.local
中写入刚才创建的动作,以便重启后还能生效
[root@VM-0-6-centos ~]# cat /etc/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
/usr/local/qcloud/irq/net_smp_affinity.sh >/tmp/net_affinity.log 2>&1
/usr/local/qcloud/cpuidle/cpuidle_support.sh &> /tmp/cpuidle_support.log
/usr/local/qcloud/rps/set_rps.sh >/tmp/setRps.log 2>&1
/usr/local/qcloud/irq/virtio_blk_smp_affinity.sh > /tmp/virtio_blk_affinity.log 2>&1
/usr/local/qcloud/gpu/nv_gpu_conf.sh >/tmp/nv_gpu_conf.log 2>&1
ip rule add from 172.21.0.12 lookup wangli # <---------------------------------------------here!
ip route add table wangli default via 172.21.0.1 dev eth1 # <---------------------------here!