Traefik:跨三个节点的负载平衡 Docker Swarm

Traefik: Load Balance Across Three Node Docker Swarm

我正在为我支持的 Web 应用程序设置三节点 Docker 群。最初,我们将 Traefik 设置为反向代理。 Traefik 和 Web 应用程序都 运行 在同一个 Web 服务器上,并且 Web 服务器位于单个节点 docker 群中。我们正在尝试添加两个额外的节点以提高应用程序的稳定性。

目前,我只是想了解 Traefik 负载平衡以及 Docker Swarm。我正在部署 Traefik v1.7 堆栈并包括 whoami 应用程序。第一个过去的 docker-compose 文件如下所示:

version: "3.7"
services:
  traefik:
    image: traefik:1.7.10
    command:
      - "--logLevel=INFO"
      - "--defaultentrypoints=http"
      - "--entryPoints=Name:http Address::80"
      - "--api"
      - "--ping"
      - "--docker"
      - "--docker.swarmMode=true"
      - "--docker.watch=true"
      - "--docker.endpoint=unix:///var/run/docker.sock"
    volumes:
      - type: bind
        source: /var/run/docker.sock
        target: /var/run/docker.sock
      - type: bind
        source: /var/lib/docker/containers
        target: /var/lib/docker/containers
    ports:
      - 6080:8080
      - target: 80
        published: 80
        mode: host
    deploy:
      placement:
        constraints:
          - node.role == manager
    networks:
      shared_network:
        aliases:
          - traefik

  whoami:
    image: containous/whoami
    networks:
      - shared_network
    deploy:
      replicas: 3
      placement:
        constraints:
          - node.role == manager
      labels:
        - "traefik.port=80"
        - "traefik.docker.network=shared_network"
        - "traefik.enable=true"
        - "traefik.frontend.rule=PathPrefix:/whoami"
        - "traefik.backend=whoami"
        - "traefik.frontend.backend=whoami"

networks:
  shared_network:
    external: true

如您所见,我在管理器节点上安装了 Traefik 和三个 whoami 服务 运行ning。有了这个,我可以 curl url http://***.***.***.***/whoami 并收到三个不同的响应

回复一:

# curl http://[Server IP Address]/whoami
Hostname: 6d559ddeee2f
IP: 127.0.0.1
IP: 10.0.1.18
IP: 172.18.0.7
RemoteAddr: 10.0.1.14:50320
GET /whoami HTTP/1.1
...

回复二:

# curl http://[Server IP Address]/whoami
Hostname: ade08aec0180
IP: 127.0.0.1
IP: 10.0.1.18
IP: 172.18.0.7
RemoteAddr: 10.0.1.14:50320
GET /whoami HTTP/1.1
...

回复三:

# curl http://[Server IP Address]/whoami
Hostname: fbcc6371383b
IP: 127.0.0.1
IP: 10.0.1.17
IP: 172.18.0.10
RemoteAddr: 10.0.1.14:55568
GET /whoami HTTP/1.1
...

但是,如果我更改 docker-compose 文件并允许副本分布在 swarm 中,行为就会改变。

撰写文件的修改部分

...
  whoami:
    image: containous/whoami
    networks:
      - shared_network
    deploy:
      replicas: 3
...

我得到了一个很好的回复,但随后的调用 return 出现了 Gateway Timeout 错误。我希望每次调用 curl http://[Server IP Address]/whoami 都会得到 swarm

中任何一个主机的响应

如何确保 Traefik 和 Docker 在所有 swarm 节点之间正确负载平衡?

显然 Traefik 无法在更新期间耗尽连接(也许它无法访问健康检查和群信息?)。

要实现零停机滚动更新,您应该将负载平衡委托给 docker swarm 本身:

# docker-compose.yml

services:
  your_service:
    deploy:
      labels:
        - traefik.docker.lbswarm=true

来自文档:

Enables Swarm's inbuilt load balancer (only relevant in Swarm Mode).

If you enable this option, Traefik will use the virtual IP provided by docker swarm instead of the containers IPs. Which means that Traefik will not perform any kind of load balancing and will delegate this task to swarm.

更多信息:

https://github.com/traefik/traefik/issues/41

https://github.com/traefik/traefik/issues/1480