我正在尝试运行docker-compose up --scale apiserver=2
,但 Traefic 无法对 api 服务器进行负载平衡。我在仪表板上看到这两个应用程序,但 traefic 仅对第一个应用程序进行负载平衡。当我检查日志时,我发现两个 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 仪表板时,我看到以下内容,
后端-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 实例的响应。这给了我第一台服务器的 UUID a2793ccb-daee-4a73-b4d0-6cbccb616cb9
这是我的 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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="53363e323a3f13323737213620207d303c3e" rel="noreferrer noopener nofollow">[email protected]</a>"
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"]
最佳答案
Traefik 将对您的应用程序的网络连接进行负载平衡。然而,Web 浏览器将维护持久的网络连接,并将其重新用于将来的请求。最终结果是您将看到多个客户端负载平衡,但单个 Web 浏览器将继续访问同一后端,直到持久连接关闭。
curl 起作用的原因是它每次退出时都会关闭网络连接,类似于多个客户端的行为。
关于docker - Traefik 无法对 docker 副本进行负载平衡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53544218/