腾讯云服务器绑定两块网卡分别连接公网IP实现双网卡通讯

背景

云服务商都喜欢搞点小活动,大家能够以一折的价格买到一年或两三年的服务器,对于像我一样的广大学生党来说,这是好事,但是云服务商低价服务器是包年的,网络只能按照带宽计费而不能按照流量计费,鉴于中国带宽费用高所以我们的云服务器往往是鬼速,这给我们的学习带来来很大的烦恼。如何利用现有成本解决我们的服务器上网问题成了我们不得不面对的现实。

操作

1.拥有一台腾讯云服务器,预装Ubuntu 18.04操作系统
为什么要用腾讯云服务器?(审核大大慢慢往下看,不是打广告)阿里云的小配置无法绑定第二个网卡,好像也无法绑定第二个IP,其他服务商的我也没有去尝试,所以便选择了腾讯云作为实验环境。为什么使用Ubuntu 18.04操作系统?因为Ubuntu从18.04起使用netplan来管理网络,详见netplan官网。我的网络renderer是Server预装的systemd-networkd,没有选择NetworkManager。
2.在同一VPC下划分新的子网,申请新的弹性公网IP,在新子网下申请一个新的弹性网卡,将弹性IP绑定到弹性网卡下,再将弹性网卡绑定到虚拟机
为什么要划分新的子网?我也不知道,因为我在没有划分新的子网时实验失败了,具体原因不确定是子网划分问题。我的直连虚拟机的子网是172.16.0.0/20,我的新建子网是172.16.16.0/20。原网卡的内网IP是172.16.0.3,新建网卡的内网IP是172.16.16.9。
3.在netplan配置文件中添加新的网卡

ubuntu@VM-0-3-ubuntu:~$ cat /etc/netplan/50-cloud-init.yaml
# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    version: 2
    ethernets:
        eth0:
            dhcp4: true
            match:
                macaddress: 52:54:00:e9:94:ac
            set-name: eth0
        eth1:
            dhcp4: true
            match:
                macaddress: 20:90:6f:74:6f:ad
            set-name: eth1

其中第二个网卡的MAC地址可以通过ip a命令来查看。配置文件更新后,使用sudo netplan apply命令应用添加网卡的更改。
4.执行以下命令,在“/etc/iproute2/rt_tables”文件中增加两张路由表名称(net1和net2)和路由表优先级(252和251,优先级数值越小表示优先级越高)。

ubuntu@VM-0-3-ubuntu:~$ sudo vim /etc/iproute2/rt_tables
# added for dual net
252     net1
251     net2

5.执行以下命令,在“/etc/rc.local”文件中增加网卡的路由信息。

ubuntu@VM-0-3-ubuntu:~$ sudo vim /etc/rc.local 
#!/bin/bash

/usr/local/qcloud/irq/net_smp_affinity.sh >/tmp/net_affinity.log 2>&1
/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
# Request IP address for eth1
dhclient eth1
# Add routes
ip route flush table net1
ip route add default via 172.16.0.3 dev eth0 src 172.16.0.3 table net1
ip route add 172.16.0.0/20 dev eth0 src 172.16.0.3 table net1
ip rule add from 172.16.0.3 table net1

ip route flush table net2
ip route add default via 172.16.16.9 dev eth1 src 172.16.16.9 table net2
ip route add 172.16.16.0/20 dev eth1 src 172.16.16.9 table net2
ip rule add from 172.16.16.9 table net2

6.给/etc/rc.local授予可执行权限,重启计算机
7.在本地电脑使用ping工具检测两张网卡的通讯情况

ubuntu@debian:~$ ping -c 5 203.195.161.196
PING 203.195.161.196 (203.195.161.196) 56(84) bytes of data.
64 bytes from 203.195.161.196: icmp_seq=1 ttl=52 time=42.2 ms
64 bytes from 203.195.161.196: icmp_seq=2 ttl=52 time=39.9 ms
64 bytes from 203.195.161.196: icmp_seq=3 ttl=52 time=83.6 ms
64 bytes from 203.195.161.196: icmp_seq=4 ttl=52 time=48.0 ms
64 bytes from 203.195.161.196: icmp_seq=5 ttl=52 time=40.6 ms

--- 203.195.161.196 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4004ms
rtt min/avg/max/mdev = 39.956/50.908/83.628/16.604 ms
ubuntu@debian:~$ ping -c 5 118.89.43.142
PING 118.89.43.142 (118.89.43.142) 56(84) bytes of data.
64 bytes from 118.89.43.142: icmp_seq=1 ttl=53 time=40.2 ms
64 bytes from 118.89.43.142: icmp_seq=2 ttl=53 time=40.9 ms
64 bytes from 118.89.43.142: icmp_seq=3 ttl=53 time=38.9 ms
64 bytes from 118.89.43.142: icmp_seq=4 ttl=53 time=40.4 ms
64 bytes from 118.89.43.142: icmp_seq=5 ttl=53 time=40.3 ms

--- 118.89.43.142 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4005ms
rtt min/avg/max/mdev = 38.973/40.213/40.966/0.700 ms

8.注意再检查一下自己的控制台自己的第二张网卡是按照流量来计费的。

总结

这个实验花了我好久,也懂了很多以前不懂的东西,重点是以后可以使服务器拥有更好的网络,年轻就是要折腾啊。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值