我正在学习 Traefik 基础知识,但无法进行健康检查。我有四台服务器,它们是 docker 容器。轮换运行良好,刷新 localhost 页面时,每次都显示不同的服务器。即使使用Apache Benchmark启动多个请愿并使用ctop监控它们,也显示了正确的工作负载平衡。当我停止其中一台服务器时,问题就来了。假设 Traefik 应该检测到离线服务器并重定向到其中一个活动服务器,但是当轮换到达离线服务器时,它会显示“Bad Gateway”消息,因此健康检查根本不起作用。我试图复制文档示例,但肯定我遗漏了一些东西。
这是我的 docker-compose.yml 文件:
version: '2.4'
services:
traefik:
image: asn/traefikv1
build: traefikv1/
ports:
- "80:80"
- "8080:8080" # Web GUI
volumes:
# Traefik static config file
- ./traefikv1/traefik.toml:/etc/traefik/traefik.toml
wwwserver:
build: .
image: phpwebapp
ports:
# Random external port for HTTP direct access
- "80"
- "8080"
scale: 4
mem_limit: 50m
mem_reservation: 25m
volumes:
- ./html:/var/www/html
depends_on:
- mariadb
mariadb:
image: mariadb:latest
environment:
- MYSQL_ROOT_PASSWORD=secret
- MYSQL_DATABASE=notesdb
- MYSQL_USER=usuario
- MYSQL_PASSWORD=secreto
ports:
- 3306:3306
这是我的 traefik.toml,其中定义了健康检查:
# Enable API and dashboard (:8080)
[api]
# Enable logs
[traefikLog]
[accessLog]
# Configure timeouts
[respondingTimeouts]
[file]
#Manual frontend/backend configuration
[frontends]
[frontends.frontend1]
passHostHeader = true
entryPoints = ["http"]
backend = "wwwservers"
[backends]
[backends.wwwservers]
# Define www servers
[backends.wwwservers.servers.server1]
url = "http://pruebastraefik_wwwserver_1:80"
[backends.wwwservers.servers.server2]
url = "http://pruebastraefik_wwwserver_2:80"
[backends.wwwservers.servers.server3]
url = "http://pruebastraefik_wwwserver_3:80"
[backends.wwwservers.servers.server4]
url = "http://pruebastraefik_wwwserver_4:80"
[backends.wwwservers.loadBalancer.healthCheck]
path = "/health"
interval = "10s"
timeout = "3s"
port= 80