$ docker network ls NETWORK ID NAME DRIVER SCOPE e13c13e22f73 bridge bridge local a56cfbac20ed docker_gwbridge bridge local 0b703c3d9cb8 host host local abdb4b9f751c none null local 0ab39da89dd6 macnet macvlan local 856b6dbf7e83 weave weavemesh local
$ docker network ls NETWORK ID NAME DRIVER SCOPE e13c13e22f73 bridge bridge local a56cfbac20ed docker_gwbridge bridge local 0b703c3d9cb8 host host local 2b2067e669d4 macnet macvlan local abdb4b9f751c none null local 856b6dbf7e83 weave weavemesh local
# 为物理网卡enp0s5创建Macvlan子接口 $ ip link add link enp0s5 name enp0s5.200 type vlan id 200
# 将MACVLAN设备加入到容器的network space $ ip link list enp0s5.200 33: ip enp0s5.200@enp0s5: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 00:1c:42:97:53:2a brd ff:ff:ff:ff:ff:ff
# 启用enp0s5.200 $ ip link set enp0s5.200 up
$ ip link list enp0s5.200 33: enp0s5.200@enp0s5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000 link/ether 00:1c:42:97:53:2a brd ff:ff:ff:ff:ff:ff
# 设置enp0s5.200为混杂模式 $ ip link set enp0s5.200 promisc on $ ifconfig enp0s5.200 enp0s5.200 Link encap:Ethernet HWaddr 00:1c:42:97:53:2a inet6 addr: fe80::21c:42ff:fe97:532a/64 Scope:Link UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:8 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:648 (648.0 B)
设置macvlan的ip和网关
1 2 3 4
$ ip addr add 192.168.200.10/24 dev enp0s5.200 # 删除原默认路由,否则下面加默认路由时会报错。 $ ip route del default $ ip route add default via 192.168.200.1 dev enp0s5.200
$ docker network ls NETWORK ID NAME DRIVER SCOPE 6dc2160d4026 bridge bridge local a56cfbac20ed docker_gwbridge bridge local 0b703c3d9cb8 host host local 9282605b5107 macnet macvlan local d129db56fcc1 macvlan200 macvlan local abdb4b9f751c none null local 856b6dbf7e83 weave weavemesh local
创建两个使用macvlan容器
创建容器c5
1 2
$ docker run --net=macvlan200 --ip=192.168.200.100 -id --name c5 busybox sh 28ee719d4784696eff10805f1f9d992b245b7b886b031c1781dbaf4037bbb231
创建容器c6
1 2
$ docker run --net=macvlan200 --ip=192.168.200.101 -id --name c6 busybox sh 7bd2a36e24846b383ab3aca6b9b48227600aed7352956c2c974db1ceb97efb54
# 为物理网卡enp0s5创建Macvlan子接口 $ ip link add link enp0s5 name enp0s5.200 type vlan id 200
# 将MACVLAN设备加入到容器的network space $ ip link list enp0s5.200 24: enp0s5.200@enp0s5: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 00:1c:42:67:23:07 brd ff:ff:ff:ff:ff:ff
# 启用enp0s5.200 $ ip link set enp0s5.200 up
$ ip link list enp0s5.200 24: enp0s5.200@enp0s5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000 link/ether 00:1c:42:67:23:07 brd ff:ff:ff:ff:ff:ff
# 设置enp0s5.200为混杂模式 $ ip link set enp0s5.200 promisc on $ ifconfig enp0s5.200 enp0s5.200 Link encap:Ethernet HWaddr 00:1c:42:67:23:07 inet6 addr: fe80::21c:42ff:fe67:2307/64 Scope:Link UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:8 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:648 (648.0 B)
设置macvlan的ip和网关
1 2 3 4
$ ip addr add 192.168.200.11/24 dev enp0s5.200 # 删除原默认路由,否则下面加默认路由时会报错。 $ ip route del default $ ip route add default via 192.168.200.1 dev enp0s5.200
# 查看macvlan是否创建成功 $ docker network ls NETWORK ID NAME DRIVER SCOPE 4886f72179a8 bridge bridge local 0b703c3d9cb8 host host local 149f5dcc20b3 macnet macvlan local 8ee3174430ab macvlan200 macvlan local abdb4b9f751c none null local bce1daa0a925 weave weavemesh local
创建两个使用macvlan容器
创建容器c7
1 2
$ docker run --net=macvlan200 --ip=192.168.200.102 -id --name c7 busybox sh 28ee719d4784696eff10805f1f9d992b245b7b886b031c1781dbaf4037bbb231
创建容器c8
1 2
$ docker run --net=macvlan200 --ip=192.168.200.103 -id --name c8 busybox sh 7bd2a36e24846b383ab3aca6b9b48227600aed7352956c2c974db1ceb97efb54
PING 192.168.200.11 (192.168.200.11): 56 data bytes 64 bytes from 192.168.200.11: seq=0 ttl=64 time=0.535 ms 64 bytes from 192.168.200.11: seq=1 ttl=64 time=0.368 ms
--- 192.168.200.11 ping statistics --- 2 packets transmitted, 2 packets received, 0% packet loss round-trip min/avg/max = 0.368/0.451/0.535 ms
测试结果:通
ping本主机容器
1 2 3 4 5 6 7 8 9
$ docker exec c5 ping -c2 192.168.200.100
PING 192.168.200.100 (192.168.200.100): 56 data bytes 64 bytes from 192.168.200.100: seq=0 ttl=64 time=0.091 ms 64 bytes from 192.168.200.100: seq=1 ttl=64 time=0.046 ms
--- 192.168.200.100 ping statistics --- 2 packets transmitted, 2 packets received, 0% packet loss round-trip min/avg/max = 0.046/0.068/0.091 ms
测试结果:通
ping另一主机容器
1 2 3 4 5 6 7 8
$ docker exec c5 ping -c2 192.168.200.102 PING 192.168.200.102 (192.168.200.102): 56 data bytes 64 bytes from 192.168.200.102: seq=0 ttl=64 time=0.815 ms 64 bytes from 192.168.200.102: seq=1 ttl=64 time=1.051 ms
--- 192.168.200.102 ping statistics --- 2 packets transmitted, 2 packets received, 0% packet loss round-trip min/avg/max = 0.815/0.933/1.051 ms
测试结果:通
使用容器名ping本主机容器
1 2 3 4 5 6 7 8
$ docker exec c5 ping -c2 c6 PING c6 (192.168.200.101): 56 data bytes 64 bytes from 192.168.200.101: seq=0 ttl=64 time=0.152 ms 64 bytes from 192.168.200.101: seq=1 ttl=64 time=0.108 ms
--- c6 ping statistics --- 2 packets transmitted, 2 packets received, 0% packet loss round-trip min/avg/max = 0.108/0.130/0.152 ms