简体   繁体   English

Docker macvlan 网络,无法上网

[英]Docker macvlan network, unable to access internet

I have a dedicated server with multiple IP addresses, some IP's have mac address associated while others(in a subnetwork) doesn't have mac addresses.我有一个具有多个 IP 地址的专用服务器,一些 IP 具有关联的 mac 地址,而其他(在子网中)没有 mac 地址。 I have created docker macvlan network using:我已经使用以下方法创建了 docker macvlan 网络:

docker network create -d macvlan -o macvlan_mode=bridge --subnet=188.40.76.0/26 --gateway=188.40.76.1 -o parent=eth0 macvlan_bridge

I have ip: 88.99.102.115 with mac: 00:50:56:00:60:42.我有 ip: 88.99.102.115 与 mac: 00:50:56:00:60:42。 Created a container using:使用以下方法创建容器:

docker run --name cont1 --net=macvlan_bridge --ip=88.99.102.115 --mac-address 00:50:56:00:60:42 -itd nginx

This works, I can access nginx hosted at that ip address from outside.这有效,我可以从外部访问托管在该 IP 地址上的 nginx。

Case with IP which doesn't have mac address and the gateway is out of subnet. IP 没有 mac 地址且网关在子网外的情况。

subnet: 88.99.114.16/28, gateway: 88.99.102.103子网:88.99.114.16/28,网关:88.99.102.103

Unable to create network using:无法使用以下方法创建网络:

docker network create -d macvlan -o macvlan_mode=bridge --subnet=88.99.114.16/28 --gateway=88.99.102.103 -o parent=eth0 mynetwork

Throws error:抛出错误:

no matching subnet for gateway 88.99.102.103

Tried with increasing subnet scope to include gateway:尝试增加子网范围以包括网关:

docker network create -d macvlan -o macvlan_mode=bridge --subnet=88.99.0.0/16 --gateway=88.99.102.103 -o parent=eth0 mynetwork

Network got created, then started nginx container using 'mynetwork' and well I dont have mac address for 88.99.114.18 so used some random mac address 40:1c:0f:bd:a1:d2.创建了网络,然后使用“mynetwork”启动了 nginx 容器,我没有 88.99.114.18 的 mac 地址,所以使用了一些随机的 mac 地址 40:1c:0f:bd:a1:d2。

docker run --name cont1 --net=mynetwork --ip=88.99.114.18 --mac-address 40:1c:0f:bd:a1:d2 -itd nginx

Can't reach nginx(88.99.102.115).无法访问 nginx(88.99.102.115)。

  1. How do I create a macvlan docker network if my gateway is out of my subnet?如果我的网关在我的子网之外,如何创建 macvlan docker 网络?
  2. How do I run a container using macvlan network when I have only IP address but no mac address?当我只有 IP 地址而没有 mac 地址时,如何使用 macvlan 网络运行容器?

I don't have much knowledge in networking, it will be really helpful if you explain in detail.我对网络没有太多的了解,如果你详细解释一下会很有帮助。

My /etc/network/interfaces file:我的 /etc/network/interfaces 文件:

### Hetzner Online GmbH - installimage
# Loopback device:
auto lo
iface lo inet loopback
iface lo inet6 loopback
# device: eth0
auto  eth0
iface eth0 inet static
  address   88.99.102.103
  netmask   255.255.255.192
  gateway   88.99.102.65
  # default route to access subnet
  up route add -net 88.99.102.64 netmask 255.255.255.192 gw 88.99.102.65 eth0

iface eth0 inet6 static
  address 2a01:4f8:221:1266::2
  netmask 64
  gateway fe80::1

You might want to start by reading up on simple routing concepts or subnets and routing您可能希望首先阅读简单的路由概念子网和路由

How do I create a macvlan docker network if my gateway is out of my subnet?如果我的网关在我的子网之外,如何创建 macvlan docker 网络?

A gateway address must be on the same subnet as an interface.网关地址必须与接口位于同一子网上。 To use this new subnet you will need to use up one of the IP addresses and assign it somewhere on the host as a gateway.要使用这个新子网,您需要使用其中一个 IP 地址并将其分配在主机上的某处作为网关。

Subnet routing to a bridge network.子网路由到桥接网络。

From the hosting screen shot, the 88.99.114.16/28 subnet has been setup to route via your host 88.99.102.103.从托管屏幕截图中,88.99.114.16/28 子网已设置为通过主机 88.99.102.103 进行路由。 You need to create an interface somewhere on your host to use as the gateway if you want Docker to use the rest of the IP addresses in the subnet.如果您希望 Docker 使用子网中的其余 IP 地址,则需要在主机上的某处创建一个接口以用作网关。

Create a bridge network for Docker to use, the bridge will be assigned the gateway address 88.99.114.17创建一个桥接网络供 Docker 使用,桥接将被分配网关地址 88.99.114.17

docker network create \
  --driver=bridge \
  --subnet 88.99.114.16/28 \
  --gateway=88.99.114.17 \
  name0

You may also need to enable IP forwarding for routing to work.您可能还需要启用 IP 转发才能使路由工作。 Configure ip forwarding in /etc/sysctl.conf :/etc/sysctl.conf配置 ip 转发:

net.ipv4.ip_forward = 1

and Apply the new setting并应用新设置

sysctl -p /etc/sysctl.conf

Then run a container on the new bridge with your routed network should be able to access the gateway and the internet然后在新网桥上运行一个容器,你的路由网络应该能够访问网关和互联网

docker run --net=name0 --rm busybox \
  sh -c "ip ad sh && ping -c 4 88.99.114.17 && wget api.ipify.org"

You may need to allow access into the subnet in iptables, depending on your default FORWARD policy您可能需要允许访问 iptables 中的子网,具体取决于您的默认FORWARD策略

iptables -I DOCKER -d 88.99.114.16/28 -j ACCEPT

Services on the subnet will be accessible from the outside world子网上的服务可以从外部访问

docker run --net=name0 busybox \
  nc -lp 80 -e echo -e "HTTP/1.0 200 OK\nContent-Length: 3\n\nHi\n"

Then outside然后在外面

○→ ping -c 2  88.99.114.18
PING 88.99.114.18 (88.99.114.18): 56 data bytes
64 bytes from 88.99.114.18: icmp_seq=0 ttl=63 time=0.527 ms
64 bytes from 88.99.114.18: icmp_seq=1 ttl=63 time=0.417 ms

--- 88.99.114.18 ping statistics ---
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.417/0.472/0.527/0.055 ms

○→ curl 88.99.114.18
Hi

No need for macvlan interface mapping.不需要 macvlan 接口映射。

How do I run a container using macvlan network when I have only IP address but no mac address?当我只有 IP 地址而没有 mac 地址时,如何使用 macvlan 网络运行容器?

macvlan is use to map a physical/host interface into a container. macvlan 用于将物理/主机接口映射到容器中。 As you don't have a physical interface for these addresses it will be hard to map one into a container.由于您没有这些地址的物理接口,因此很难将它们映射到容器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM