Traefik无法负载均衡Docker副本

时间:2018-11-29 17:09:16

标签: docker traefik

我正在尝试运行docker-compose up --scale apiserver=2,但是Traefic无法对api服务器进行负载平衡。我在仪表板上看到了两个应用程序,但是流量只在第一个负载均衡。当我检查日志时,我看到两个nodejs应用程序都已启动

apiserver_1_cff59924db38 | Listening on port 3000. ContainerId: a2793ccb-daee-4a73-b4d0-6cbccb616cb9
apiserver_2_2164f88b7ed4 | Listening on port 3000. ContainerId: a92f516a-d66c-4672-b1b9-e0d8e182b46f

当我查看Traefik仪表板时,看到以下内容,

backend-apiserver-dockertest

|Server                |Weight|
|----------------------|------|
|http://172.18.0.3:3000| 1    |
|http://172.18.0.6:3000| 1    |


Load Balancer
Method wrr

但是,每当我打${API_NAME}时,我只会从第一个API Server实例得到响应。这给了我第一台服务器a2793ccb-daee-4a73-b4d0-6cbccb616cb9

的UUID

这是我的docker-compse.yml文件

version: '3.6'
services:
    traefik:
        container_name: traefik
        image: traefik
        ports:
            - 80:80
            - 443:443
            - 8080:8080 #dashboard
        networks:
            - proxy
        environment:
            - DUCKDNS_TOKEN=${DUCKDNS_TOKEN}
        volumes:
            - /var/run/docker.sock:/var/run/docker.sock
            - ./traefik/traefik.toml:/traefik.toml
            - ./traefik/acme/acme.json:/etc/traefik/acme.json
            - ./log:/var/log/traefik
        labels:
            - traefik.enable=true
            - traefik.port=8080
            - traefik.frontend.rule=Host:${TRAEFIK_NAME}
        restart: unless-stopped

    apiserver:
        build: ./api-server
        networks:
            - proxy
        labels:
            - traefik.frontend.rule=Host:${API_NAME}
            - traefik.frontend.entryPoints=https
            - traefik.docker.network=proxy
            - traefik.protocol=http
            - traefik.enable=true
            - traefik.port=3000
        restart: unless-stopped

    website:
        container_name: website
        build: ./website
        networks:
            - proxy
        labels:
            - traefik.frontend.rule=Host:${DOMAIN_NAME}
            - traefik.frontend.entryPoints=https
            - traefik.docker.network=proxy
            - traefik.protocol=http
            - traefik.enable=true
            - traefik.port=80
        restart: unless-stopped

networks:
    proxy:
        name: proxy

traefik.toml

debug = true
logLevel = "DEBUG"
checkNewVersion = true
defaultEntryPoints = ["http", "https"]

[api]
  # Name of the related entry point
  #
  # Optional
  # Default: "traefik"
  #
  entryPoint = "traefik"

  # Enable Dashboard
  #
  # Optional
  # Default: true
  #
  dashboard = true

[entryPoints]
  [entryPoints.http]
    address = ":80"
    [entryPoints.http.redirect]
      entryPoint = "https"
  [entryPoints.https]
    address = ":443"
    [entryPoints.https.tls]

[docker]
  endpoint = "unix:///var/run/docker.sock"
  exposedbydefault = false
  watch = true

[acme]
  email = "email@address.com"
  storage = "/etc/traefik/acme.json"
  entryPoint = "https"
  acmeLogging = false
  [acme.dnsChallenge]
    provider = "duckdns"
    delayBeforeCheck = 0

[[acme.domains]]
  main = "*.mydomain.duckdns.org"
  sans = ["mydomain.duckdns.org"]

1 个答案:

答案 0 :(得分:2)

Traefik将负载平衡网络连接到您的应用程序。但是,Web浏览器将保持持久的网络连接,并将其重新用于将来的请求。最终结果是您将看到多个客户端负载均衡,但是单个Web浏览器将继续访问同一后端,直到该持久连接关闭。

curl起作用的原因是因为每次curl退出时都会关闭网络连接,类似于多个客户端的行为。