3.ubuntu server的网卡绑定和桥接

添加网卡

为了方便做这个实验,我在虚拟机上配置了4块网卡,其中eth0和eth1配置为NAT模式,eth2和eth3配置为“仅主机模式”。eth0和eth1可以通过NAT转换连接外网,网段为192.168.36.X,eth2和eth3不能连接外网网段为172.16.22.X。创建网桥之前要先安装bridge-utils:sudo apt install bridge-utils。网卡配置如下:

虚拟机网卡设置

ubuntu server 18.04版本的网络配置通过修改配置文件/etc/netplan/00-installer-config.yaml来完成。

单网卡静态地址

只给eth0配置一个地址,配置文件如下:

# This is the network config written by 'subiquity'
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      dhcp6: false
      addresses: [192.168.36.20/24]
      gateway4: 192.168.36.2
      nameservers:
        addresses: [202.106.196.115]

修改配置文件后,执行sudo netplan apply使配置生效,此时网络配置如下:

~$ ifconfig
eth0: flags=4163  mtu 1500
        inet 192.168.36.20  netmask 255.255.255.0  broadcast 192.168.36.255
        inet6 fe80::20c:29ff:fedc:7176  prefixlen 64  scopeid 0x20
        ether 00:0c:29:dc:71:76  txqueuelen 1000  (Ethernet)
        RX packets 134  bytes 21001 (21.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 205  bytes 29124 (29.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 209  bytes 16900 (16.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 209  bytes 16900 (16.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

keone@keone:~$ ping www.baidu.com -c 4
PING www.a.shifen.com (61.135.185.32) 56(84) bytes of data.
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=1 ttl=128 time=12.3 ms
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=2 ttl=128 time=18.8 ms
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=3 ttl=128 time=15.6 ms
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=4 ttl=128 time=13.5 ms

--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3007ms
rtt min/avg/max/mdev = 12.326/15.073/18.802/2.459 ms

双网卡静态IP

把eth0和eth2配置上静态IP,配置文件如下:

# This is the network config written by 'subiquity'
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      dhcp6: false
      addresses: [192.168.36.20/24]
      gateway4: 192.168.36.2
      nameservers:
        addresses: [202.106.196.115]
    eth2:
      dhcp4: false
      dhcp6: false
      addresses: [172.16.22.20/24]
      routes:
        - to: 192.168.36.0/24 #配置路由
          via: 172.16.22.1

执行sudo netplan apply后,两个网卡的网络都是通的,网络配置如下

$ ping 172.16.22.1 -c 4
PING 172.16.22.1 (172.16.22.1) 56(84) bytes of data.
64 bytes from 172.16.22.1: icmp_seq=1 ttl=128 time=0.472 ms
64 bytes from 172.16.22.1: icmp_seq=2 ttl=128 time=0.479 ms
64 bytes from 172.16.22.1: icmp_seq=3 ttl=128 time=0.564 ms
64 bytes from 172.16.22.1: icmp_seq=4 ttl=128 time=0.483 ms

--- 172.16.22.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3061ms
rtt min/avg/max/mdev = 0.472/0.499/0.564/0.043 ms
$ ping www.baidu.com -c 4
PING www.a.shifen.com (61.135.185.32) 56(84) bytes of data.
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=1 ttl=128 time=14.7 ms
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=2 ttl=128 time=14.9 ms
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=3 ttl=128 time=20.3 ms
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=4 ttl=128 time=13.3 ms

--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 10754ms
rtt min/avg/max/mdev = 13.338/15.835/20.331/2.669 ms
$ ifconfig
eth0: flags=4163  mtu 1500
        inet 192.168.36.20  netmask 255.255.255.0  broadcast 192.168.36.255
        inet6 fe80::20c:29ff:fedc:7176  prefixlen 64  scopeid 0x20
        ether 00:0c:29:dc:71:76  txqueuelen 1000  (Ethernet)
        RX packets 2583  bytes 293260 (293.2 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2017  bytes 333746 (333.7 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth2: flags=4163  mtu 1500
        inet 172.16.22.20  netmask 255.255.255.0  broadcast 172.16.22.255
        inet6 fe80::20c:29ff:fedc:718a  prefixlen 64  scopeid 0x20
        ether 00:0c:29:dc:71:8a  txqueuelen 1000  (Ethernet)
        RX packets 18  bytes 1650 (1.6 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 28  bytes 2446 (2.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 221  bytes 17964 (17.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 221  bytes 17964 (17.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

单网卡桥接

创建一个网桥br0,然后将br0桥接到eth0上。配置文件如下:

# This is the network config written by 'subiquity'
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      dhcp6: false

  bridges:
    br0:
      dhcp4: false
      dhcp6: false
      addresses: [192.168.36.20/24]
      gateway4: 192.168.36.2
      nameservers:
        addresses: [202.106.196.115]
      interfaces:
        - eth0

执行sudo netplan apply后,两个网卡的网络都是通的,网络配置如下(修改网桥配置后,有时执行sudo netplan apply后网络不通,需要重启服务器)

$ ping www.baidu.com -c 4
PING www.a.shifen.com (61.135.185.32) 56(84) bytes of data.
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=1 ttl=128 time=13.2 ms
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=2 ttl=128 time=14.7 ms
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=3 ttl=128 time=14.9 ms
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=4 ttl=128 time=15.5 ms

--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3061ms
rtt min/avg/max/mdev = 13.281/14.626/15.502/0.832 ms
$ ifconfig
br0: flags=4163  mtu 1500
        inet 192.168.36.20  netmask 255.255.255.0  broadcast 192.168.36.255
        inet6 fe80::482:37ff:fe67:6c4  prefixlen 64  scopeid 0x20
        ether 06:82:37:67:06:c4  txqueuelen 1000  (Ethernet)
        RX packets 194  bytes 22722 (22.7 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 189  bytes 29317 (29.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163  mtu 1500
        ether 00:0c:29:dc:71:76  txqueuelen 1000  (Ethernet)
        RX packets 195  bytes 25492 (25.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 192  bytes 29599 (29.5 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 203  bytes 16344 (16.3 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 203  bytes 16344 (16.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

多网卡桥接

创建两个网桥br0和br1,然后分别桥接到eth0和eth2上。配置文件如下:

# This is the network config written by 'subiquity'
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      dhcp6: false
    eth2:
      dhcp4: false
      dhcp6: false

  bridges:
    br0:
      dhcp4: false
      dhcp6: false
      addresses: [192.168.36.20/24]
      gateway4: 192.168.36.2
      nameservers:
        addresses: [202.106.196.115]
      interfaces:
        - eth0
    br1:
      dhcp4: false
      dhcp6: false
      addresses: [172.16.22.20]
      routes:
        - to: 192.168.36.0/24
          via: 172.168.22.1
      interfaces:
        - eth2

测试两个网段的网络,都是通的。

$ ping www.baidu.com -c 4
PING www.a.shifen.com (61.135.185.32) 56(84) bytes of data.
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=1 ttl=128 time=13.7 ms
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=2 ttl=128 time=25.6 ms
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=3 ttl=128 time=14.5 ms
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=4 ttl=128 time=14.4 ms

--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3006ms
rtt min/avg/max/mdev = 13.740/17.101/25.622/4.932 ms
$ ping 172.16.22.1 -c 4
PING 172.16.22.1 (172.16.22.1) 56(84) bytes of data.
64 bytes from 172.16.22.1: icmp_seq=1 ttl=128 time=0.172 ms
64 bytes from 172.16.22.1: icmp_seq=2 ttl=128 time=0.230 ms
64 bytes from 172.16.22.1: icmp_seq=3 ttl=128 time=0.382 ms
64 bytes from 172.16.22.1: icmp_seq=4 ttl=128 time=0.351 ms

--- 172.16.22.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3063ms
rtt min/avg/max/mdev = 0.172/0.283/0.382/0.088 ms
~$ ifconfig
br0: flags=4163  mtu 1500
        inet 192.168.36.20  netmask 255.255.255.0  broadcast 192.168.36.255
        inet6 fe80::482:37ff:fe67:6c4  prefixlen 64  scopeid 0x20
        ether 06:82:37:67:06:c4  txqueuelen 1000  (Ethernet)
        RX packets 52722  bytes 78560921 (78.5 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 27239  bytes 1643227 (1.6 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

br1: flags=4163  mtu 1500
        inet 172.16.22.20  netmask 255.255.255.0  broadcast 172.16.22.255
        inet6 fe80::646f:e3ff:fea7:1072  prefixlen 64  scopeid 0x20
        ether 66:6f:e3:a7:10:72  txqueuelen 1000  (Ethernet)
        RX packets 146  bytes 75747 (75.7 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 31  bytes 2238 (2.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163  mtu 1500
        ether 00:0c:29:dc:71:76  txqueuelen 1000  (Ethernet)
        RX packets 55639  bytes 79456547 (79.4 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 27240  bytes 1798733 (1.7 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth2: flags=4163  mtu 1500
        ether 00:0c:29:dc:71:8a  txqueuelen 1000  (Ethernet)
        RX packets 146  bytes 77791 (77.7 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 31  bytes 2418 (2.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 249  bytes 21282 (21.2 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 249  bytes 21282 (21.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

双网卡绑定

网卡绑定共有7中模式,概念如下:

  • 第⼀种模式:mod=0,即:(balance-rr) Round-robin policy(平衡抡循环策略)
    特点:传输数据包顺序是依次传输(即:第1个包⾛eth0,下⼀个包就⾛eth1….⼀直循环下去,直到最后⼀个传输完毕),此模式提供负载平衡和容错能⼒。
  • 第⼆种模式:mod=1,即: (active-backup) Active-backup policy(主-备份策略)
    特点:只有⼀个设备处于活动状态,当⼀个宕掉另⼀个⻢上由备份转换为主设备。mac地址是外部可⻅得,从外⾯看来,bond的MAC地址是唯⼀的,以避免switch(交换机)发⽣混乱。此模式只提供了容错能⼒;由此可⻅此算法的优点是可以提供⾼⽹络连接的可⽤性,但是它的资源利⽤率较低,只有⼀个接⼝处于⼯作状态,在有 N 个⽹络接⼝的情况下,资源利⽤率为1/N。
  • 第三种模式:mod=2,即:(balance-xor) XOR policy(平衡策略)
    特点:基于指定的传输HASH策略传输数据包。缺省的策略是:(源MAC地址 XOR ⽬标MAC地址) % slave数量。其他的传输策略可以通过xmit_hash_policy选项指定,此模式提供负载平衡和容错能⼒。
  • 第四种模式:mod=3,即:broadcast(⼴播策略)
    特点:在每个slave接⼝上传输每个数据包,此模式提供了容错能⼒。
  • 第五种模式:mod=4,即:(802.3ad) IEEE 802.3adDynamic link aggregation(IEEE 802.3ad 动态链接聚合)
    特点:创建⼀个聚合组,它们共享同样的速率和双⼯设定。根据802.3ad规范将多个slave⼯作在同⼀个激活的聚合体下。
    必要条件:
    条件1:ethtool⽀持获取每个slave的速率和双⼯设定。
    条件2:switch(交换机)⽀持IEEE 802.3ad Dynamic link aggregation。
    条件3:⼤多数switch(交换机)需要经过特定配置才能⽀持802.3ad模式。
  • 第六种模式:mod=5,即:(balance-tlb) Adaptive transmit load balancing(适配器传输负载均衡)
    特点:不需要任何特别的switch(交换机)⽀持的通道bonding。在每个slave上根据当前的负载(根据速度计算)分配外出流量。如果正在接受数据的slave出故障了,另⼀个slave接管失败的slave的MAC地址。
    该模式的必要条件:ethtool⽀持获取每个slave的速率。
  • 第七种模式:mod=6,即:(balance-alb) Adaptive load balancing(适配器适应性负载均衡)
    特点:该模式包含了balance-tlb模式,同时加上针对IPV4流量的接收负载均衡(receive load balance,
    rlb),⽽且不需要任何switch(交换机)的⽀持。
    将网卡eth0和eth1绑定,配置文件如下:
# This is the network config written by 'subiquity'
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      dhcp6: false
    eth1:
      dhcp4: false
      dhcp6: false

  bonds:
    bond0:
      interfaces:
        - eth0
        - eth1
      addresses: [192.168.36.20/24]
      gateway4: 192.168.36.2
      nameservers:
        addresses: [202.106.196.115]
      parameters: #参数,包括bond模式,速率等
       mode: active-backup
       mii-monitor-interval: 100 #检测间隔时间,单位ms,如果100ms检测不到另一块网卡,就将传输接管

测试网络,没有问题。

$ ping www.baidu.com -c 4
PING www.a.shifen.com (61.135.185.32) 56(84) bytes of data.
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=1 ttl=128 time=15.9 ms
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=2 ttl=128 time=14.9 ms
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=3 ttl=128 time=17.8 ms
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=4 ttl=128 time=13.7 ms

--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 13.750/15.639/17.860/1.508 ms
$ ifconfig
bond0: flags=5187  mtu 1500
        inet 192.168.36.20  netmask 255.255.255.0  broadcast 192.168.36.255
        inet6 fe80::bc79:b0ff:fede:7ced  prefixlen 64  scopeid 0x20
        ether be:79:b0:de:7c:ed  txqueuelen 1000  (Ethernet)
        RX packets 248  bytes 32021 (32.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 258  bytes 38249 (38.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=6211  mtu 1500
        ether be:79:b0:de:7c:ed  txqueuelen 1000  (Ethernet)
        RX packets 3  bytes 180 (180.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=6211  mtu 1500
        ether be:79:b0:de:7c:ed  txqueuelen 1000  (Ethernet)
        RX packets 245  bytes 31841 (31.8 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 258  bytes 38249 (38.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 210  bytes 17075 (17.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 210  bytes 17075 (17.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

双网卡绑定+桥接

⽹卡绑定⽤于提供⽹卡接⼝冗余以及⾼可⽤和端⼝聚合功能,桥接⽹卡再给需要桥接设备的服务使⽤。将网卡eth0和eth1绑定为bond0,再创建一个网桥br0,与bond0桥接。配置文件如下:

# This is the network config written by 'subiquity'
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      dhcp6: false
    eth1:
      dhcp4: false
      dhcp6: false

  bonds:
    bond0:
      interfaces:
        - eth0
        - eth1
      parameters:
       mode: active-backup
       mii-monitor-interval: 100

  bridges:
    br0:
      dhcp4: false
      dhcp6: false
      addresses: [192.168.36.20/24]
      gateway4: 192.168.36.2
      nameservers:
        addresses: [202.106.196.115]
      interfaces:
        - bond0

测试网络,没有问题

$ ping www.baidu.com -c 4
PING www.a.shifen.com (61.135.185.32) 56(84) bytes of data.
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=1 ttl=128 time=15.9 ms
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=2 ttl=128 time=14.9 ms
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=3 ttl=128 time=17.8 ms
64 bytes from 61.135.185.32 (61.135.185.32): icmp_seq=4 ttl=128 time=13.7 ms

--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 13.750/15.639/17.860/1.508 ms
$ ifconfig 
bond0: flags=5187  mtu 1500
        ether be:79:b0:de:7c:ed  txqueuelen 1000  (Ethernet)
        RX packets 1207  bytes 144515 (144.5 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 934  bytes 143395 (143.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

br0: flags=4163  mtu 1500
        inet 192.168.36.20  netmask 255.255.255.0  broadcast 192.168.36.255
        inet6 fe80::482:37ff:fe67:6c4  prefixlen 64  scopeid 0x20
        ether 06:82:37:67:06:c4  txqueuelen 1000  (Ethernet)
        RX packets 33  bytes 5280 (5.2 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 24  bytes 2660 (2.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=6211  mtu 1500
        ether be:79:b0:de:7c:ed  txqueuelen 1000  (Ethernet)
        RX packets 6  bytes 543 (543.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=6211  mtu 1500
        ether be:79:b0:de:7c:ed  txqueuelen 1000  (Ethernet)
        RX packets 1201  bytes 143972 (143.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 934  bytes 143395 (143.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 210  bytes 17075 (17.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 210  bytes 17075 (17.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

内外网双网卡绑定

把eth0和eth1绑定为bond0,连接NAT网络,通过共享主机网络上网模拟外网;eth2和eth3绑定为bond1,连接仅主机网络模拟内网。配置文件如下:

# This is the network config written by 'subiquity'
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      dhcp6: false
    eth1:
      dhcp4: false
      dhcp6: false
    eth2:
      dhcp4: false
      dhcp6: false
    eth3:
      dhcp4: false
      dhcp6: false

  bonds:
    bond0:
      interfaces:
        - eth0
        - eth1
      addresses: [192.168.36.20/24]
      gateway4: 192.168.36.2
      nameservers:
        addresses: [202.106.196.115]
      parameters:
        mode: active-backup
        mii-monitor-interval: 100
    bond1:
      interfaces:
        - eth2
        - eth3
      addresses: [172.16.22.20/24]
      routes:
        - to: 192.168.36.0/24
          via: 172.16.22.1
      parameters:
        mode: active-backup
        mii-monitor-interval: 100

测试网络:

$ ping www.baidu.com -c 4
PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=128 time=13.4 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=128 time=13.5 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=3 ttl=128 time=13.9 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=4 ttl=128 time=13.9 ms

--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3009ms
rtt min/avg/max/mdev = 13.459/13.738/13.963/0.262 ms
$ ping 172.16.22.1 -c 4
PING 172.16.22.1 (172.16.22.1) 56(84) bytes of data.
64 bytes from 172.16.22.1: icmp_seq=1 ttl=128 time=0.664 ms
64 bytes from 172.16.22.1: icmp_seq=2 ttl=128 time=0.693 ms
64 bytes from 172.16.22.1: icmp_seq=3 ttl=128 time=0.432 ms
64 bytes from 172.16.22.1: icmp_seq=4 ttl=128 time=0.860 ms

--- 172.16.22.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3026ms
rtt min/avg/max/mdev = 0.432/0.662/0.860/0.153 ms
$ ifconfig
bond0: flags=5187  mtu 1500
        inet 192.168.36.20  netmask 255.255.255.0  broadcast 192.168.36.255
        ether be:79:b0:de:7c:ed  txqueuelen 1000  (Ethernet)
        RX packets 625  bytes 185486 (185.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 356  bytes 50153 (50.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

bond1: flags=5187  mtu 1500
        inet 172.16.22.20  netmask 255.255.255.0  broadcast 172.16.22.255
        ether da:a0:c2:91:32:3c  txqueuelen 1000  (Ethernet)
        RX packets 229  bytes 144986 (144.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 27  bytes 2158 (2.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=6211  mtu 1500
        ether be:79:b0:de:7c:ed  txqueuelen 1000  (Ethernet)
        RX packets 9  bytes 723 (723.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=6211  mtu 1500
        ether be:79:b0:de:7c:ed  txqueuelen 1000  (Ethernet)
        RX packets 616  bytes 184763 (184.7 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 356  bytes 50153 (50.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth2: flags=6211  mtu 1500
        ether da:a0:c2:91:32:3c  txqueuelen 1000  (Ethernet)
        RX packets 2  bytes 303 (303.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth3: flags=6211  mtu 1500
        ether da:a0:c2:91:32:3c  txqueuelen 1000  (Ethernet)
        RX packets 227  bytes 144683 (144.6 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 27  bytes 2158 (2.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 13582  bytes 999569 (999.5 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 13582  bytes 999569 (999.5 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

内外网双网卡绑定+桥接

把eth0和eth1绑定为bond0,连接NAT网络,通过共享主机网络上网模拟外网;eth2和eth3绑定为bond1,连接仅主机网络模拟内网,创建网桥br0和br1,并分别桥接到bond0和bond1上。配置文件如下:

# This is the network config written by 'subiquity'
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      dhcp6: false
    eth1:
      dhcp4: false
      dhcp6: false
    eth2:
      dhcp4: false
      dhcp6: false
    eth3:
      dhcp4: false
      dhcp6: false

  bonds:
    bond0:
      interfaces:
        - eth0
        - eth1
      parameters:
        mode: active-backup
        mii-monitor-interval: 100
    bond1:
      interfaces:
        - eth2
        - eth3
      parameters:
        mode: active-backup
        mii-monitor-interval: 100

  bridges:
    br0:
      dhcp4: false
      dhcp6: false
      addresses: [192.168.36.20/24]
      gateway4: 192.168.36.2
      nameservers:
        addresses: [202.106.196.115]
      interfaces:
        - bond0
    br1:
      dhcp4: false
      dhcp6: false
      addresses: [172.16.22.20/24]
      routes:
        - to: 192.168.36.0/24
          via: 172.16.22.1
      interfaces:
        - bond1

测试网络:

$ ping www.baidu.com -c 4
PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=128 time=13.4 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=128 time=13.5 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=3 ttl=128 time=13.9 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=4 ttl=128 time=13.9 ms

--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3009ms
rtt min/avg/max/mdev = 13.459/13.738/13.963/0.262 ms
$ ping 172.16.22.1 -c 4
PING 172.16.22.1 (172.16.22.1) 56(84) bytes of data.
64 bytes from 172.16.22.1: icmp_seq=1 ttl=128 time=0.664 ms
64 bytes from 172.16.22.1: icmp_seq=2 ttl=128 time=0.693 ms
64 bytes from 172.16.22.1: icmp_seq=3 ttl=128 time=0.432 ms
64 bytes from 172.16.22.1: icmp_seq=4 ttl=128 time=0.860 ms

--- 172.16.22.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3026ms
rtt min/avg/max/mdev = 0.432/0.662/0.860/0.153 ms
$ ifconfig
bond0: flags=5187  mtu 1500
        ether be:79:b0:de:7c:ed  txqueuelen 1000  (Ethernet)
        RX packets 869  bytes 110354 (110.3 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 728  bytes 117373 (117.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

bond1: flags=5187  mtu 1500
        ether da:a0:c2:91:32:3c  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 20  bytes 1612 (1.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

br0: flags=4163  mtu 1500
        inet 192.168.36.20  netmask 255.255.255.0  broadcast 192.168.36.255
        inet6 fe80::482:37ff:fe67:6c4  prefixlen 64  scopeid 0x20
        ether 06:82:37:67:06:c4  txqueuelen 1000  (Ethernet)
        RX packets 34  bytes 2944 (2.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 28  bytes 3152 (3.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

br1: flags=4163  mtu 1500
        inet 172.16.22.20  netmask 255.255.255.0  broadcast 172.16.22.255
        inet6 fe80::646f:e3ff:fea7:1072  prefixlen 64  scopeid 0x20
        ether 66:6f:e3:a7:10:72  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7  bytes 586 (586.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=6211  mtu 1500
        ether be:79:b0:de:7c:ed  txqueuelen 1000  (Ethernet)
        RX packets 5  bytes 300 (300.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=6211  mtu 1500
        ether be:79:b0:de:7c:ed  txqueuelen 1000  (Ethernet)
        RX packets 864  bytes 110054 (110.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 728  bytes 117373 (117.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth2: flags=6211  mtu 1500
        ether da:a0:c2:91:32:3c  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth3: flags=6211  mtu 1500
        ether da:a0:c2:91:32:3c  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 20  bytes 1612 (1.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 174  bytes 13570 (13.5 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 174  bytes 13570 (13.5 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

总结:以上就是ubuntu1804网卡静态IP地址、bond、桥接、bond+桥接的配置方式。查看网络可以用ifconfig命令,当前哪个模式的网卡(网桥、bond)工作,哪个模式的网卡(网桥、bond)就会显示有IP地址。通过配置文件或有IP地址的设备都可以确定网卡的工作模式。

你可能感兴趣的:(3.ubuntu server的网卡绑定和桥接)