在用上述配置创建完OpenWrt docker后,虽然宿主机和容器在同一网段,但是因为macvlan模式的限制,宿主机与容器是无法互相通信的(但与同网段下的其他机器是可以相互通信的),所以需要在宿主机创建一个新的macvlan接口,然后增加一条宿主机macvlan访问容器macvlan的静态路由。
在宿主机终端执行下面代码,
注:macvlan-proxy这个接口名称可以随便起,只需要每条命令的名字保持一致即可
宿主机创建新的macvlan接口
ip link add macvlan-proxy link eth0 type macvlan mode bridge
为新接口分配ip并启用
ip addr add 192.168.5.3 dev macvlan-proxy
ip link set macvlan-proxy up
添加静态路由使宿主机与容器使用新接口通信
ip route add 192.168.5.10 dev macvlan-proxy
配置新接口的默认网关为容器地址
route add default gw 192.168.5.10 macvlan-proxy
测试是否能ping通
在宿主机执行,
# 宿主机-->OpenWrt容器
ping 192.168.5.10 -c 3
# OpenWrt容器-->宿主机
docker exec -it openwrt bash -c "ping 192.168.5.3 -c 3"
如果可以相互ping通,则证明设置没问题。
加入启动项自启动
上述命令会在宿主机重启后失效,为了每次重启宿主机均可自动运行上述命令,需要编辑/etc/rc.local文件,将上面的命令粘贴到exit 0之前
ip link set eth0 promisc on > /dev/null 2>&1
ip link add macvlan-proxy link eth0 type macvlan mode bridge
ip addr add 192.168.5.3 dev macvlan-proxy
ip link set macvlan-proxy up
ip route add 192.168.5.10 dev macvlan-proxy
route add default gw 192.168.5.10 macvlan-proxy
最后,确保为/etc/rc.local文件增加了可执行权限且rc-local.service是自启动的状态
chmod a+x /etc/rc.local
systemctl enable rc-local
至此,Docker OpenWrt与宿主机网络互通配置已完成,可重启后验证是否生效。
评论区