docker - 构建 docker 环境从 Virtualbox 迁移,网络配置复杂
问题描述
我有一个环境,其中包含 3 个应用程序,2 个在 Windows 中运行,1 个在 Debian OS 中运行。
出于测试目的,我已经在只有 1 台 PC(运行 Windows 10)和 VirtualBox VM(用于运行 Debian OS)中构建了环境。
下面是详细的网络配置:
主机 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 . . . . . . . . . :
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
如果配置输出:
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
...
下面是当前环境下的一些网络交互流程:
- App1在 VM 中运行并创建多个套接字服务器:
-侦听 IP 192.168.1.10、192.168.1.20、192.168.1.30 端口 1448
-侦听 IP 192.168.1.120 端口 1449 - App2在主机中运行并创建多个连接:
-
在 192.168.1.123 端口 1448 中创建套接字服务器
- 连接到 VM 中的套接字服务器:192.168.1.10 端口 1448(使用绑定 ip = 192.168.1.11)
- 连接到 VM 中的套接字服务器: 192.168.1.20 端口 1448(使用绑定 ip=192.168.1.12)
...
- 连接到 VM 中的套接字服务器:192.168.1.120 端口 1449 - App3在主机上运行并创建多个连接:
- 连接到套接字服务器 192.168.1.123 端口 1448 (App2)
- 连接到套接字服务器 192.168.1.120 端口 1449 (App1)
现在环境运行正常,但是在阅读了 Docker 之后,我打算用 Docker 替换 VirtualBox VM 以减少内存使用。
我成功构建了 Debian 映像、设置库等...
但是,我不知道如何在 Docker 中设置网络以具有与上述 VirtualBox VM 配置类似的功能。一些难点:
- 我们可以使用 Docker 创建类似于“以太网适配器 VirtualBox Host-Only Network”的网络接口吗?
- 如果我们无法创建这样的网络接口,可以将这 2 个 VirtualBox Host Only Adapter 保留在环境中。但我不确定如何将这些网络接口“连接”到 Docker 容器中?
- 为 docker 容器分配静态 IP 地址似乎是不可能的,这是正确的吗?
(这无济于事:将静态 IP 分配给 Docker 容器) - 似乎也不可能为 docker 容器分配多个 IP 地址,对吗?
因此,使用 Docker 构建上述环境似乎非常困难。
如果有人有仍然能够使用 Docker 构建环境的解决方案,请帮助指出一些方向。
非常感谢你!
解决方案
您提出的前三个问题已通过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
在这个例子中,我声明了一个名为 的 macvlan 网络network_1
,它连接到enp52s0
网络接口。这两个nginx
容器使用该网络,每个容器都发布自己的静态 IP。
现在,如果您想为每个容器分配多个 IP,事情就会变得一团糟。要分配额外的地址,您需要一个额外的网络,它有自己的 IP 范围、自己的父网络接口和自己的网关。这实际上是另一个网络。或者您可以考虑一些技巧,也许使用代理容器来侦听另一个 IP 并将流量转发到所需的容器,但这有点“meh”。我想说,除非您愿意重新设计应用程序的连接性,否则 VM 开销不值得这么麻烦。
推荐阅读
- java - java2ws 长类路径 Maven
- java - 带有 Eclipselink 的 Apache Aries JPA:java.lang.LinkageError
- javascript - 如何在 Javascript 中使用“window.open”打开新的 chrome
- sql - SQL - 我应该将目标存储在历史记录中还是加入到 BI 工具中?什么更有效率?
- sql - SQL:通过附加索引进行可选搜索
- c# - 如何将表值函数映射到 EF Core 2.1 中的查询类型
- postgresql - 使用 SSP 类的数据表服务器端 PHP - 加入后如何分组
- javascript - WCF 和 Javascript CORS 问题无法设置源通配符
- scala - Scala toMap 类型转换问题
- android - 如何使用自定义认证在 Proxy 后面设置 Android Emulator