macvlan的原理是在宿主机物理网卡上虚拟出多个子网卡,通过不同的MAC地址在数据链路层(Data Link Layer)进行网络数据转发的,它是比较新的网络虚拟化技术,需要较新的内核支持(Linux kernel v3.9–3.19 and 4.0+
)。
1. 准备实验环境
使用 node-1 和 host2 上单独的网卡ens38 创建 macvlan。为保证多个 MAC 地址的网络包都可以从 ens38通过,需要打开网卡的混杂模式。
ip link set ens38 promisc on
确保 ens38
状态 UP
并且 promisc
模式已经生效。
2. 创建macvlan网络
在 node-2 中也要执行相同的命令。
root@node-2:~# docker network create -d macvlan --subnet=172.16.86.0/24 --gateway=172.16.86.1 -o parent=ens38 mac_net1
fc9f85149d08638194aef4722d255e0cbda8a982653ad858d9a76fa96b00f43e
root@node-2:~#
① -d macvlan
指定 driver 为 macvlan。
② macvlan 网络是 local 网络,为了保证跨主机能够通信,用户需要自己管理 IP subnet。
③ 与其他网络不同,docker 不会为 macvlan 创建网关,这里的网关应该是真实存在的,否则容器无法路由。
④ -o parent
指定使用的网络 interface。
root@ubuntu:~# docker network ls
NETWORK ID NAME DRIVER SCOPE
128956db798a bridge bridge local
19fb1af129e6 host host local
2509b3717813 mac_net1 macvlan local
d5b0798e725e none null local
在 node-1 中运行容器 bbox1 并连接到 mac_net1。
由于 node-1 中的 mac_net1 与 node-2 中的 mac_net1 本质上是独立的,为了避免自动分配造成 IP 冲突,需要通过 --ip
指定 bbox1 地址为 172.16.86.11
。
在 node-2 中运行容器 bbox2,指定 IP 172.16.86.10
。
验证 bbox1 和 bbox2 的连通性。
root@ubuntu:~# docker exec bbox1 ping -c 2 172.16.86.10
PING 172.16.86.10 (172.16.86.10): 56 data bytes
64 bytes from 172.16.86.3: seq=0 ttl=64 time=0.641 ms
64 bytes from 172.16.86.3: seq=1 ttl=64 time=0.393 ms
--- 172.16.86.10 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.393/0.517/0.641 ms
3. macvlan 网络结构分析
macvlan 不依赖 Linux bridge,brctl show
可以确认没有创建新的 bridge。
查看一下容器 bbox1 的网络设备:
除了 lo,容器只有一个 eth0,请注意 eth0 后面的 @if4
,这表明该 interface 有一个对应的 interface,其全局的编号为 4
。根据 macvlan 的原理,这个 interface 就是主机的 ens38,确认如下:
容器的 eth0 就是 ens38 通过 macvlan 虚拟出来的 interface。容器的 interface 直接与主机的网卡连接,这种方案使得容器无需通过 NAT 和端口映射就能与外网直接通信(只要有网关),在网络上与其他独立主机没有区别。当前网络结构如图所示:
4. 用 sub-interface 实现多 macvlan 网络 (VLAN Bridge模式)
macvlan 会独占主机的网卡,也就是说一个网卡只能创建一个 macvlan 网络,否则会报错:
macvlan 不仅可以连接到 interface(如 enp0s8),也可以连接到 sub-interface(如 enp0s8.xx
)
VLAN 是现代网络常用的网络虚拟化技术,它可以将物理的二层网络划分成多达 4094 个逻辑网络,这些逻辑网络在二层上是隔离的,每个逻辑网络(即 VLAN)由 VLAN ID 区分,VLAN ID 的取值为 1-4094。
Linux 的网卡也能支持 VLAN(apt-get install vlan
),同一个 interface 可以收发多个 VLAN 的数据包,不过前提是要创建 VLAN 的 sub-interface。
比如希望 enp0s8 同时支持 VLAN10 和 VLAN20,则需创建 sub-interface enp0s8.100
和 enp0s8.200
。
在交换机上,如果某个 port 只能收发单个 VLAN 的数据,该 port 为 Access 模式,如果支持多 VLAN,则为 Trunk 模式,所以接下来实验的前提是:
enp38要接在交换机的 trunk 口上。如果用的是 VirtualBox 虚拟机,则不需要额外配置了。
在 enp0s8.100 和 enp0s8.200 上创建 macvlan 网络。
首先编辑 node-1和node-2 的 /etc/network/interfaces
,配置 sub-auto enp0s8
然后启用 sub-interface:
ifup enp0s8.100
ifup enp0s8.200
创建 macvlan 网络:
docker network create -d macvlan --subnet=172.16.10.0/24 --gateway=172.16.10.1 -o parent=enp0s8.100 mac_net10
docker network create -d macvlan --subnet=172.16.20.0/24 --gateway=172.16.20.1 -o parent=enp0s8.200 mac_net20
在 node-1中运行容器:
docker run -itd --name bbox1 --ip=172.16.10.10 --network mac_net10 busybox
docker run -itd --name bbox2 --ip=172.16.20.10 --network mac_net20 busybox
在 node-2中运行容器:
docker run -itd --name bbox3 --ip=172.16.10.11 --network mac_net10 busybox
docker run -itd --name bbox4 --ip=172.16.20.11 --network mac_net20 busybox
当前网络结构如图所示
5. macvlan的网络隔离和联通
macvlan 之间的连通性
bbox1 能 ping 通 bbox3,bbox2 能 ping 通 bbox4。即:同一 macvlan 网络能通信。
bbox1 无法 ping 通 bbox2 和 bbox4。即:不同 macvlan 网络之间不能通信。但更准确的说法应该是:不同 macvlan 网络不能 在二层上 通信。
在三层上可以通过网关将 macvlan 连通,新建虚拟机(192.168.113.5
)做路由
将192.168.113.5 配置成一个虚拟路由器,设置网关并转发 VLAN10 和 VLAN20 的流:首先确保操作系统 IP Forwarding 已经启用。
ifconfig enp0s8.100 172.16.10.1 netmask 255.255.255.0 up
ifconfig enp0s8.200 172.16.20.1 netmask 255.255.255.0 up
需要开启防火墙
然后执行sysctl -w net.ipv4.ip_forward=1
添加 iptables 规则,转发不同 VLAN 的数据包。
iptables -t nat -A POSTROUTING -o enp0s8.100 -j MASQUERADE
iptables -t nat -A POSTROUTING -o enp0s8.200 -j MASQUERADE
iptables -A FORWARD -i enp0s8.100 -o enp0s8.200 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i enp0s8.200 -o enp0s8.100 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i enp0s8.100 -o enp0s8.200 -j ACCEPT
iptables -A FORWARD -i enp0s8.200 -o enp0s8.100 -j ACCEPT
① 因为 bbox1 与 bbox4 在不同的 IP 网段,跟据 bbox1 的路由表:
root@node-1:~# docker exec bbox1 ip route
default via 172.16.10.1 dev eth0
172.16.10.0/24 dev eth0 scope link src 172.16.10.10
数据包将发送到网关 172.16.10.1
。
② 路由器从 enp0s8.100 收到数据包,发现目的地址是 172.16.20.11
,查看自己的路由表:
[root@localhost ~]# ip route
default via 192.168.113.1 dev enp0s3 proto dhcp metric 100
172.16.10.0/24 dev enp0s8.100 proto kernel scope link src 172.16.10.1
172.16.20.0/24 dev enp0s8.200 proto kernel scope link src 172.16.20.1
192.168.56.0/24 dev enp0s8 proto kernel scope link src 192.168.56.122 metric 101
192.168.113.0/24 dev enp0s3 proto kernel scope link src 192.168.113.2 metric 100
于是将数据包从 enp0s8.200 转发出去。
③ 通过 ARP 记录的信息,路由器能够得知 172.16.20.11
在 host2 上,于是将数据包发送给 host2。
④ host2 根据目的地址和 VLAN 信息将数据包发送给 bbox4。
macvlan 网络的连通和隔离完全依赖 VLAN、IP subnet 和路由,docker 本身不做任何限制,用户可以像管理传统 VLAN 网络那样管理 macvlan。
参考:
https://www.cnblogs.com/bakari/p/10893589.html
https://www.cnblogs.com/mrwuzs/p/9614558.html
最后编辑:admin 更新时间:2025-04-03 11:32