解决openwrt和宿主机不能互通的问题
解决openwrt和宿主机不能互通的问题
出自:https://aoyouer.com/posts/macvlan-host/
之前为了解决这个问题,百度了很久,都是看不懂的废话
还是这个blog言简意赅,直接上代码,搞定
1 | |
上面的命令执行后,立刻就能双向ping通了
重启后消失,可以加入/etc/rc.local
2023.11.8日更新,推荐阅读。
完整的教程出自https://blog.oddbit.com/post/2018-03-12-using-docker-macvlan-networks/#host-access
为openwrt创建macvlan时注意限定范围
1 | |
新建桥接网络,使宿主机和容器连接,可以加入/etc/rc.local中
1 | |
原文:
A question that crops up regularly on #docker is “How do I attach a container directly to my local network?” One possible answer to that question is the macvlan network type, which lets you create “clones” of a physical interface on your host and use that to attach containers directly to your local network. For the most part it works great, but it does come with some minor caveats and limitations. I would like to explore those here.
For the purpose of this example, let’s say we have a host interface eno1 that looks like this:
1 | |
To create a macvlan network named mynet attached to that interface, you might run something like this:
1 | |
…but don’t do that.
Address assignment
When you create a container attached to your macvlan network, Docker will select an address from the subnet range and assign it to your container. This leads to the potential for conflicts: if Docker picks an address that has already been assigned to another host on your network, you have a problem!
You can avoid this by reserving a portion of the subnet range for use by Docker. There are two parts to this solution:
You must configure any DHCP service on your network such that it will not assign addresses in a given range.
You must tell Docker about that reserved range of addresses.
How you accomplish the former depends entirely on your local network infrastructure and is beyond the scope of this document. The latter task is accomplished with the --ip-range option to docker network create.
On my local network, my DHCP server will not assign any addresses above 192.168.1.190. I have decided to assign to Docker the subset 192.168.1.192/27, which is a range of 32 address starting at 192.168.1.192 and ending at 192.168.1.223. The corresponding docker network create command would be:
1 | |
Now it is possible to create containers attached to my local network without worrying about the possibility of ip address conflicts.
Host access
With a container attached to a macvlan network, you will find that while it can contact other systems on your local network without a problem, the container will not be able to connect to your host (and your host will not be able to connect to your container). This is a limitation of macvlan interfaces: without special support from a network switch, your host is unable to send packets to its own macvlan interfaces.
Fortunately, there is a workaround for this problem: you can create another macvlan interface on your host, and use that to communicate with containers on the macvlan network.
First, I’m going to reserve an address from our network range for use by the host interface by using the --aux-address option to docker network create. That makes our final command line look like:
1 | |
This will prevent Docker from assigning that address to a container.
Next, we create a new macvlan interface on the host. You can call it whatever you want, but I’m calling this one mynet-shim:
1 | |
Now we need to configure the interface with the address we reserved and bring it up:
1 | |
The last thing we need to do is to tell our host to use that interface when communicating with the containers. This is relatively easy because we have restricted our containers to a particular CIDR subset of the local network; we just add a route to that range like this:
1 | |
With that route in place, your host will automatically use ths mynet-shim interface when communicating with containers on the mynet network.
Note that the interface and routing configuration presented here is not persistent – you will lose if if you were to reboot your host. How to make it persistent is distribution dependent.