docker-compose + traefik - 将流量定向到 docker-compose 网络之外的服务

docker-compose + traefik - direct traffic to services outside the docker-compose network

我提前为篇幅道歉 - 但这很有趣,尤其是对于架构师和 traefik 奇才。

挑战:

底线是,我希望有一个设置,我们可以在其中利用 traefik 的动态配置功能来:

自动检测容器何时停止,并知道回退到开发人员机器/外部主机上的外部服务。

或者 - 当本地数据库容器启动时,知道优先使用它而不是来自外部源的数据库服务。

全貌

我需要为几个团队优化工作流程。

约束条件:

这需要在开发人员机器上 运行 绝对最低要求。 当前的解决方案依靠 docker-compose 和 traefik@1.x,我被要求保留这个,但是,我相信升级到 traefik@2.x 会被接受。

追求的用例

实际上,我在这里与您探讨两个用例,我相信它们具有相同的解决方案 - 至少在理论上...

用例一——多个微服务的本地开发

Dev-Mode = running a process in debug mode, with IDE, file-watch live reloads, etc.

AS A    - developer of services in mid layers of the service mesh
I WANT  - to be able to direct traffic to services under development to
          native processes on my machine, and keep the rest in the compose 
          network
SO THAT - I can run in Dev-Mode only services under development, 
          while all the rest in the mesh run using production-level docker 
          images in docker-compose.

用例二 - 本地开发和外部数据库

AS A    - developer of data brokers in the mesh
I WANT  - to control if containers get to the shared db or to local 
          db container using traefik
SO THAT - I can keep applications in the containers completely agnostic to 
          the topography in which they run

那么缺少什么?

作为开始 - 由于 Traefik 是所有容器间通信通过的服务总线 - 我正在寻找一种方法来配置 traefik 以使用来自 外部的服务 docker-组成网络。

第一个用例意味着使用 traefik 作为服务总线——知道每个服务在哪里的唯一实体 运行s,它可以是容器,也可以是服务外部的服务docker-组成网络。

第二个用例意味着设置 traefik 将流量定向到 docker-compose 网络之外的进程 运行s,但特别是在开发人员的机器上,这在反过来,使用来自 compose 网络的服务(为此,dev compose 不同于生产 compose - 为网格中的每个节点公开一个入口点,包括 用于仅在内部生产时使用的服务)。

两个买一个

我知道这里有两个问题,但我想不出另一种方法来分别提出每个问题并获得与全貌相关的答案...

我希望有一个设置,我们可以在其中利用 traefik 的动态配置功能来:

detect automatically when a container is stopped, and know to fallback to external services on developer machine or any other host for the matter. Alternatively - when a db container is started, knows to prefer it over external db service.

我们可以要求开发人员将记录添加到他们的 hosts 文件中,但是基于 DNS 的解决方案会更好,特别是如果我们可以使用 docker 内置的 DNS :)

求助?

任何帮助或指出正确的方向将不胜感激!

我做到了。

查看此处演示的孤立解决方案:

https://github.com/osher/lcdev-using-compose-and-traefik

长话短说:

  1. 开发者主机以 host.docker.internal.
  2. 的形式存在于 docker-compose 网络中
  3. 文件提供者为团队维护的每项服务提供服务和路由。 文件提供程序中的路由设置为低优先级。
  4. docker 提供商为每个 docker-compose 容器提供服务和路由。 以这种方式创建的路由设置为高优先级。

结果:

  • 当容器存在时 - 它被使用。
  • 当容器停止时 - traefik 将尝试开发者机器。