解决openwrt和宿主机不能互通的问题

解决openwrt和宿主机不能互通的问题
出自:https://aoyouer.com/posts/macvlan-host/
之前为了解决这个问题,百度了很久,都是看不懂的废话
还是这个blog言简意赅,直接上代码,搞定

1
2
3
4
ip link add mynet link eth0 type macvlan mode bridge
ip addr add 192.168.0.50(为虚拟网卡分配的ip) dev mynet
ip link set mynet up
ip route add 192.168.0.20(容器的ip) dev mynet

上面的命令执行后,立刻就能双向ping通了
重启后消失,可以加入/etc/rc.local

2023.11.8日更新,推荐阅读。
完整的教程出自https://blog.oddbit.com/post/2018-03-12-using-docker-macvlan-networks/#host-access
为openwrt创建macvlan时注意限定范围

1
2
3
4
5
6
docker network create -d macvlan -o parent=eth0 \
--subnet 192.168.1.0/24 \
--gateway 192.168.1.254 \
--ip-range 192.168.1.2/27(openwrt容器ip) \
--aux-address 'host=192.168.1.3'(宿主机用于与容器连接的) \
openwrtnet

新建桥接网络,使宿主机和容器连接,可以加入/etc/rc.local

1
2
3
ip link add openwrtnet-shim link eth0 type macvlan  mode bridge
ip addr add 192.168.1.3/32 dev openwrtnet-shim
ip link set openwrtnet-shim up

原文:
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
2
3
4
5
6
eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 64:00:6a:7d:06:1a brd ff:ff:ff:ff:ff:ff
inet 192.168.1.24/24 brd 192.168.1.255 scope global dynamic eno1
valid_lft 73303sec preferred_lft 73303sec
inet6 fe80::b2c9:3793:303:2a55/64 scope link
valid_lft forever preferred_lft forever

To create a macvlan network named mynet attached to that interface, you might run something like this:

1
2
3
4
docker network create -d macvlan -o parent=eno1 \
--subnet 192.168.1.0/24 \
--gateway 192.168.1.1 \
mynet

…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
2
3
4
5
docker network create -d macvlan -o parent=eno1 \
--subnet 192.168.1.0/24 \
--gateway 192.168.1.1 \
--ip-range 192.168.1.192/27 \
mynet

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
2
3
4
5
6
docker network create -d macvlan -o parent=eno1 \
--subnet 192.168.1.0/24 \
--gateway 192.168.1.1 \
--ip-range 192.168.1.192/27 \
--aux-address 'host=192.168.1.223' \
mynet

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
ip link add mynet-shim link eno1 type macvlan  mode bridge

Now we need to configure the interface with the address we reserved and bring it up:

1
2
ip addr add 192.168.1.223/32 dev mynet-shim
ip link set mynet-shim up

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
ip route add 192.168.1.192/27 dev mynet-shim

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.


解决openwrt和宿主机不能互通的问题
https://hexo.psorai.eu.org/2023/06/06/解决openwrt和宿主机不能互通的问题/
Author
Sora
Posted on
June 6, 2023
Licensed under