我有一个环境,其中包含 3 个应用程序,2 个在 Windows 中运行,1 个在 Debian 操作系统中运行。
出于测试目的,我已经使用 VirtualBox VM(用于运行 Debian 操作系统)在仅 1 台 PC(运行 Windows 10)中构建环境。
下面是详细的网络配置:
主机 PC(使用 2 个 VirtualBox Host Only 适配器):
Ethernet adapter VirtualBox Host-Only Network:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::ec0c:3c16:4f85:1a5e%12
IPv4 Address. . . . . . . . . . . : 192.168.1.11
Subnet Mask . . . . . . . . . . . : 255.255.255.0
IPv4 Address. . . . . . . . . . . : 192.168.1.12
Subnet Mask . . . . . . . . . . . : 255.255.255.0
IPv4 Address. . . . . . . . . . . : 192.168.1.13
Subnet Mask . . . . . . . . . . . : 255.255.255.0
IPv4 Address. . . . . . . . . . . : 192.168.1.14
Subnet Mask . . . . . . . . . . . : 255.255.255.0
IPv4 Address. . . . . . . . . . . : 192.168.1.15
Subnet Mask . . . . . . . . . . . : 255.255.255.0
IPv4 Address. . . . . . . . . . . : 192.168.1.21
Subnet Mask . . . . . . . . . . . : 255.255.255.0
IPv4 Address. . . . . . . . . . . : 192.168.1.22
Subnet Mask . . . . . . . . . . . : 255.255.255.0
IPv4 Address. . . . . . . . . . . : 192.168.1.23
Subnet Mask . . . . . . . . . . . : 255.255.255.0
IPv4 Address. . . . . . . . . . . : 192.168.1.24
Subnet Mask . . . . . . . . . . . : 255.255.255.0
IPv4 Address. . . . . . . . . . . : 192.168.1.25
Subnet Mask . . . . . . . . . . . : 255.255.255.0
IPv4 Address. . . . . . . . . . . : 192.168.1.26
Subnet Mask . . . . . . . . . . . : 255.255.255.0
IPv4 Address. . . . . . . . . . . : 192.168.1.31
Subnet Mask . . . . . . . . . . . : 255.255.255.0
IPv4 Address. . . . . . . . . . . : 192.168.1.32
Subnet Mask . . . . . . . . . . . : 255.255.255.0
IPv4 Address. . . . . . . . . . . : 192.168.1.33
Subnet Mask . . . . . . . . . . . : 255.255.255.0
IPv4 Address. . . . . . . . . . . : 192.168.1.34
Subnet Mask . . . . . . . . . . . : 255.255.255.0
IPv4 Address. . . . . . . . . . . : 192.168.1.123
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
Ethernet adapter VirtualBox Host-Only Network #2:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::5d7:813f:a9a:865d%16
IPv4 Address. . . . . . . . . . . : 192.168.1.122
Subnet Mask . . . . . . . . . . . : 255.255.255.248
Default Gateway . . . . . . . . . :
Run Code Online (Sandbox Code Playgroud)
VirtualBox VM 网络配置:
/etc/network/interfaces:
auto lo
iface lo inet loopback
auto eth1
iface eth1 inet static
address 192.168.1.120
netmask 255.255.255.0
gateway 192.168.1.1
auto eth2
allow-hotplug eth2
iface eth2 inet static
address 192.168.1.10
netmask 255.255.255.0
gateway 192.168.1.1
auto eth2:0
iface eth2:0 inet static
address 192.168.1.20
netmask 255.255.255.0
auto eth2:1
iface eth2:1 inet static
address 192.168.1.30
netmask 255.255.255.0
Run Code Online (Sandbox Code Playgroud)
如果配置输出:
eth0 Link encap:Ethernet HWaddr 08:00:27:a8:08:8b
inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
...
eth1 Link encap:Ethernet HWaddr 08:00:27:41:27:73
inet addr:192.168.1.120 Bcast:192.168.1.255 Mask:255.255.255.0
...
eth2 Link encap:Ethernet HWaddr 08:00:27:0d:6b:54
inet addr:192.168.1.10 Bcast:192.168.1.255 Mask:255.255.255.0
...
eth2:0 Link encap:Ethernet HWaddr 08:00:27:0d:6b:54
inet addr:192.168.1.20 Bcast:192.168.1.255 Mask:255.255.255.0
...
eth2:1 Link encap:Ethernet HWaddr 08:00:27:0d:6b:54
inet addr:192.168.1.30 Bcast:192.168.1.255 Mask:255.255.255.0
...
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
...
Run Code Online (Sandbox Code Playgroud)
以下是当前环境下的一些网络交互流程:
环境现在工作正常,但是在阅读了 Docker 之后,我计划使用 Docker 替换 VirtualBox VM 以减少内存使用。
我成功地建立了一个 Debian 映像、设置库等......
但是,我不知道如何在 Docker 中设置网络以具有与上述 VirtualBox VM 配置类似的功能。一些难点:
你问的前三个问题是通过macvlan网络解决的。您将拥有直接连接到网络的容器,就像虚拟机一样。这是一个例子:
version: "2.1"
services:
nginx1:
image: nginx
networks:
network_1:
ipv4_address: 10.1.1.115
nginx2:
image: nginx
networks:
network_1:
ipv4_address: 10.1.1.116
networks:
network_1:
driver: macvlan
driver_opts:
parent: enp52s0 # Your network interface name
ipam:
driver: default
config:
- subnet: 10.1.1.0/24
gateway: 10.1.1.1
Run Code Online (Sandbox Code Playgroud)
在此示例中,我声明了一个名为 的 macvlan 网络network_1,该网络连接到enp52s0网络接口。这两个nginx容器使用该网络,并且每个容器都通告自己的静态 IP。
现在,如果您想为每个容器分配多个 IP,事情就会开始变得混乱。要分配附加地址,您需要一个附加网络,具有自己的 IP 范围、自己的父网络接口和自己的网关。这实际上是另一个网络。或者你可以考虑一些 hack,也许使用代理容器来监听另一个 IP 并将流量转发到所需的容器,但这有点“meh”。我想说的是,除非您愿意重新设计应用程序的连接,否则虚拟机开销不值得这么麻烦。
| 归档时间: |
|
| 查看次数: |
124 次 |
| 最近记录: |