网络和负载均衡器如何在 docker 集群模式下工作?

How do networking and load balancer work in docker swarm mode?

我是 Docker 和容器的新手。我正在阅读 docker 的教程并偶然发现了这些信息。 https://docs.docker.com/get-started/part3/#docker-composeyml

   networks:
     - webnet
networks:
  webnet:

什么是网络?文件说

Instruct web’s containers to share port 80 via a load-balanced network called webnet. (Internally, the containers themselves will publish to web’s port 80 at an ephemeral port.)

那么,默认情况下,覆盖网络在 docker 集群中是负载均衡的?使用什么负载平衡算法?

其实我也不清楚为什么我们要在overlay网络上做负载均衡。

不确定我能比文档更清楚,但也许重新措辞会有所帮助。

首先,您在此处关注的文档使用了所谓的 docker 的 swarm mode

什么是 swarm mode

A swarm is a cluster of Docker engines, or nodes, where you deploy services. The Docker Engine CLI and API include commands to manage swarm nodes (e.g., add or remove nodes), and deploy and orchestrate services across the swarm.

来自 SO 文档:

A swarm is a number of Docker Engines (or nodes) that deploy services collectively. Swarm is used to distribute processing across many physical, virtual or cloud machines.

因此,在 swarm 模式下,您有一个多主机(虚拟机 and/or 物理)集群,一台机器通过 docker engine.

相互通信

Q1。什么是网络?

webnet 是启动堆栈时创建的 overlay network 的名称。

Overlay networks manage communications among the Docker daemons participating in the swarm

在您的机器集群中,创建了一个虚拟网络,其中每个服务都有一个 ip - 映射到一个内部 DNS 条目(即服务名称),并允许 docker 将传入的数据包路由到正确的容器,在群(集群)中无处不在。

Q2。所以,默认情况下,覆盖网络在 docker 集群中是负载平衡的 ?

是的,如果您使用覆盖网络,但您也可以删除服务 networks 配置以绕过它。然后你必须publish你想要公开的服务的端口。

Q3。使用什么负载平衡算法?

来自这个 answered by swarm master bmitch ;):

The algorithm is currently round-robin and I've seen no indication that it's pluginable yet. A higher level load balancer would allow swarm nodes to be taken down for maintenance, but any sticky sessions or other routing features will be undone by the round-robin algorithm in swarm mode.

Q4。其实我不清楚为什么我们在覆盖网络上有负载平衡

docker群模式/服务的目的是允许编排复制服务,这意味着我们可以扩展/缩小部署在群中的容器。

再次来自docs

Swarm mode has an internal DNS component that automatically assigns each service in the swarm a DNS entry. The swarm manager uses internal load balancing to distribute requests among services within the cluster based upon the DNS name of the service.

因此您可以部署 10 个完全相同的容器(假设 nginx 与您的应用 html/js),而无需处理私有网络 DNS 条目、端口配置等...任何传入请求都将自动负载平衡到参与 swarm 的主机。

希望对您有所帮助!