https://www.jianshu.com/p/cc481870236b
macvlan 本身是 linux 内核的模块,本质上是一种网卡虚拟化的技术,其功能是允许在同一个物理网卡上虚拟出多个网卡,通过不同的 MAC 地址在数据链路层进行网络数据的转发。
一块网卡上配置多个 MAC 地址,即多个接口,每个接口都可以配置自身的 IP 地址。Docker 的 macvlan 网络实际上就是使用了 Linux 提供的 macvlan 驱动功能。
因为多个 MAC 地址的网络数据包都是从同一个网卡上传输,因此需要打开网卡的混杂模式,即 ip link set eth0 promisc on
在 Docker 下创建 macvlan 网络不同于创建 bridge 网络模式,需要指定网段与网关,且网段与网关需是物理上存在的。
macvlan 网络模式不依赖于网桥,所以使用 brctl show 将看到并没有创建新的网桥。但是查看容器内的网络,将会看到每个虚拟网卡都对应一个容器外部的接口。
/ # ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
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
627: eth2@if3: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:c0:a8:54:e1 brd ff:ff:ff:ff:ff:ff
inet 192.168.84.225/24 scope global eth2
valid_lft forever preferred_lft forever
inet6 fe80::42:c0ff:fea8:54e1/64 scope link
valid_lft forever preferred_lft forever
628: eth1@if4: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:c0:a8:55:e1 brd ff:ff:ff:ff:ff:ff
inet 192.168.85.225/24 scope global eth1
valid_lft forever preferred_lft forever
inet6 fe80::42:c0ff:fea8:55e1/64 scope link
valid_lft forever preferred_lft forever
629: eth0@if2: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:c0:a8:53:e1 brd ff:ff:ff:ff:ff:ff
inet 192.168.83.225/24 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::42:c0ff:fea8:53e1/64 scope link
valid_lft forever preferred_lft forever
/ #
比如,以上是在容器内执行 ip addr 指令后显示的各个虚拟网口内容。比如虚拟网口显示“629: eth0@if2”,其中 if2 表示物理网卡的编号为 2 的网口,即如下所示,编号为 2 的网口即 eth0 网口。
[root@jamza_vm_lp0_litepaas master]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
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 pfifo_fast state UP qlen 1000
link/ether 52:54:83:cc:dd:00 brd ff:ff:ff:ff:ff:ff
inet 192.168.83.85/24 brd 192.168.83.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::5054:83ff:fecc:dd00/64 scope link
valid_lft forever preferred_lft forever
查看宿主机的网口信息:
[root@jamza_vm_master_litepaas master]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.6.252 netmask 255.255.255.0 broadcast 0.0.0.0
inet6 fe80::42:c6ff:fe05:2363 prefixlen 64 scopeid 0x20<link>
ether 02:42:c6:05:23:63 txqueuelen 0 (Ethernet)
RX packets 570 bytes 35680 (34.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 223 bytes 19338 (18.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
docker_gwbridge: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.18.0.1 netmask 255.255.240.0 broadcast 0.0.0.0
inet6 fe80::42:42ff:fe41:eebc prefixlen 64 scopeid 0x20<link>
ether 02:42:42:41:ee:bc txqueuelen 0 (Ethernet)
RX packets 3574777 bytes 185891316 (177.2 MiB)
RX errors 0 dropped 15 overruns 0 frame 0
TX packets 1530 bytes 99712 (97.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.83.83 netmask 255.255.255.0 broadcast 192.168.83.255
inet6 fe80::5054:83ff:feaa:bb00 prefixlen 64 scopeid 0x20<link>
ether 52:54:83:aa:bb:00 txqueuelen 1000 (Ethernet)
RX packets 83683524 bytes 364816891747 (339.7 GiB)
RX errors 0 dropped 15 overruns 0 frame 0
TX packets 94961933 bytes 213362123667 (198.7 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth20: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.84.83 netmask 255.255.255.0 broadcast 192.168.84.255
inet6 fe80::5054:83ff:feaa:bb20 prefixlen 64 scopeid 0x20<link