使用复杂的网络配置从 Virtualbox 迁移构建 docker 环境

phi*_*o37 7 docker

我有一个环境,其中包含 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)

以下是当前环境下的一些网络交互流程:

  • 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构建环境的解决方案,请帮助指出一些方向。
非常感谢!

ane*_*yte 4

你问的前三个问题是通过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”。我想说的是,除非您愿意重新设计应用程序的连接,否则虚拟机开销不值得这么麻烦。