首页 > 解决方案 > 构建 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
          ...

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

现在环境运行正常,但是在阅读了 Docker 之后,我打算用 Docker 替换 VirtualBox VM 以减少内存使用。
我成功构建了 Debian 映像、设置库等...
但是,我不知道如何在 Docker 中设置网络以具有与上述 VirtualBox VM 配置类似的功能。一些难点:


因此,使用 Docker 构建上述环境似乎非常困难。
如果有人有仍然能够使用 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 开销不值得这么麻烦。


推荐阅读