上一節咱們建立了兩個 macvlan 並部署了容器,網絡結構以下:docker
本節驗證 macvlan 之間的連通性。網絡
root@host1:~# docker exec bbox01 ping -c 2 172.16.10.11 root@host1:~# docker exec bbox02 ping -c 2 172.16.20.11 不通
bbox1 能 ping 通 bbox3,bbox2 能 ping 通 bbox4。即:同一 macvlan 網絡能通訊。ui
root@host1:~# docker exec bbox01 ping -c 2 172.16.10.10 PING 172.16.10.10 (172.16.10.10): 56 data bytes 64 bytes from 172.16.10.10: seq=0 ttl=64 time=0.059 ms 64 bytes from 172.16.10.10: seq=1 ttl=64 time=0.042 ms root@host1:~# docker exec bbox02 ping -c 2 172.16.20.10 PING 172.16.20.10 (172.16.20.10): 56 data bytes 64 bytes from 172.16.20.10: seq=0 ttl=64 time=0.084 ms 64 bytes from 172.16.20.10: seq=1 ttl=64 time=0.061 ms
bbox1 沒法 ping 通 bbox2 和 bbox4。即:不一樣 macvlan 網絡之間不能通訊。但更準確的說法應該是:不一樣 macvlan 網絡不能 在二層上 通訊。在三層上能夠經過網關將 macvlan 連通,下面咱們就啓用網關。操作系統
咱們會將 Host 10.0.0.20 配置成一個虛擬路由器,設置網關並轉發 VLAN10 和 VLAN20 的流量。固然也能夠使用物理路由器達到一樣的效果。首先確保操做系統 IP Forwarding 已經啓用。code
cuiyongchao@cuiyongchao:~$ sysctl net.ipv4.ip_forward net.ipv4.ip_forward = 1 cuiyongchao@cuiyongchao:~$ 輸出爲 1 則表示啓用,若是爲 0 可經過以下命令啓用: sysctl -w net.ipv4.ip_forward=1
在 /etc/network/interfaces 中配置 vlan sub-interface:blog
auto eth38 iface eth38 inet manual auto eth38.10 iface eth38.10 inet manual vlan-raw-device eth38 auto eth38.20 iface eth38.20 inet manual vlan-raw-device eth38
啓用 sub-interface:ip
ifconfig eth38.10路由
ifconfig eth38.20部署
將網關 IP 配置到 sub-interface:table
ifconfig eth38.10 172.16.10.1 netmask 255.255.255.0 up ifconfig eth38.20 172.16.20.1 netmask 255.255.255.0 up
添加 iptables 規則,轉發不一樣 VLAN 的數據包。
iptables -t nat -A POSTROUTING -o eth38.10 -j MASQUERADE iptables -t nat -A POSTROUTING -o eth38.20 -j MASQUERADE iptables -A FORWARD -i eth38.10 -o eth38.20 -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A FORWARD -i eth38.20 -o eth38.10 -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A FORWARD -i eth38.10 -o eth38.20 -j ACCEPT iptables -A FORWARD -i eth38.20 -o eth38.10 -j ACCEPT
如今 host1 上位於 mac_net10 的 bbox1 已經能夠與 host2 上位於 mac_net20 的 bbox4 通訊了。
下面咱們分析數據包是如何從 bbox1(172.16.10.10)到達 bbox4(172.16.20.11)的。整個過程以下圖所示: