在容器中或不在容器中部署具有微服务架构的 Go 应用程序?

Deploying Go apps with micro-service architecture in containers or not in containers?

我是 DevOps 的新手,特别是使用 golang 和微服务架构。

我想知道 go 应用程序是否应该部署在容器中 (Docker)。在这种情况下,我有一个使用微服务架构构建的系统。例如这里,我有 2 个 Web 服务,A 和 B。我还有另一个 Web 服务器充当这两个服务前面的网关。

A 和 B 都需要访问数据库,例如 MySQL。 A处理tableA,B处理tableB.

我知道在 Go 中,源代码被编译成一个 executable 二进制文件。因为我这里有 3 个服务,所以我有 3 个二进制文件。所有三个 运行 作为 Web 服务器公开 JSON REST API.

我的问题是:

I'm wondering if go applications should or should not be deployed in containers (Docker)
Why should I deploy and run those binaries inside containers like Docker?

当然,前提是您将构建与实际的最终图像分开(以便不包含在所说的最终图像构建依赖项中)
参见“Golang, Docker and multistage build" from Matteo Madeddu.

Can I deploy these servers together in one host running on different ports?

实际上,他们都可以 运行 在他们自己的容器中,在他们自己的端口上,即使那个端口是相同的。
使用 EXPOSEd port,容器内通信将起作用。 但是,如果它们是从外部访问的,那么它们的 published port 确实需要不同。

What about scalibility and high availability without using Docker?
And what about the scalability and availability of using Docker?

一旦谈到动态状态,就会涉及某种编排:请参阅 Docker Swarm or Kubernetes 以了解高效的集群管理。
Both are available with the latest docker.

示例:

VonC 的回答很棒。我只想补充一件事:Golang 非常适合容器,因为它内置了对自我维持二进制文件的支持。这意味着您可以构建只有几 MB 大小的容器,而不是通常有数百 MB 的带有 alpine 等的典型容器。

看看这个 read