我正在构建一个基于微服务的 PHP Web 应用程序,目前,进入我的应用程序的路由位于不同的端口上。这对开发来说很好,但是一旦我启动它就不会那么可靠了。我有:
- 端口 10001 上的 SPA 前端
- 端口 10000 上的 HTTP API
- 10002 上的 Web 套接字
我希望这些都在同一个端口上。我最近发现Traefik看起来非常灵活,可以满足我未来的需求。我很欣赏我可以使用 NginX 进行简单的代理,但我非常喜欢能够告诉代理通过 API 重定向流量的想法(将来会有用)。
但是,我已经设置了 Traefik 配置,但它似乎并没有像我想要的那样进行代理。我正在努力让它生成日志来说明它出了什么问题,我正在寻找一个“速赢”来鼓励我继续坚持下去。
我已经为它配置了一个主机到容器的卷,因此它可以使用-v /var/run/docker.sock:/var/run/docker.sock
) 来探索 Docker。我真的不明白为什么它需要了解我的容器,因为我打算在我的traefik.toml
.
所以,正如我(模糊地)理解的那样,我需要设置入口点(流量进来的地方)、前端(决定如何路由它)和后端(做事的服务器)。我最初的尝试如下所示:
# Entrypoints definition
#
# It looks like we have one entry point, and use
# frontends to filter WS/HTTP
[entryPoints]
[entryPoints.everything]
address = ":10005"
# Frontends definition
[frontends]
[frontends.http]
backend = "backend-spa"
[frontends.http-api]
backend = "backend-http-api"
rule = "PathPrefix: /v1/captcha"
[frontends.ws]
backend = "backend-ws"
# Backends definition
[backends]
[backends.backend-spa]
url = "http://missive-interface:80"
[backends.backend-http-api]
url = "http://missive-controller:8080"
[backends.backend-ws]
url = "http://missive-controller:8081"
目前我每个服务只有一个容器。我在模式匹配器上区分 HTTP API,我想我也会在某些东西上匹配套接字服务器(也许是Upgrade
标头)。
所以,当我访问时,http://localhost:10005/
我得到404 page not found
. 好吧,很公平,我可能配置错误。所以我将日志级别重置为INFO
,我得到了这个:
time="2017-09-06T13:45:35Z" level=info msg="Using TOML configuration file /etc/traefik/traefik.toml"
time="2017-09-06T13:45:35Z" level=info msg="Traefik version v1.4.0-rc1 built on 2017-08-29_08:35:24AM"
time="2017-09-06T13:45:35Z" level=info msg="Preparing server everything &{Network: Address::10005 TLS:<nil> Redirect:<nil> Auth:<nil> WhitelistSourceRange:[] Compress:false ProxyProtocol:false} with readTimeout=0s writeTimeout=0s idleTimeout=3m0s"
time="2017-09-06T13:45:35Z" level=info msg="Starting provider *docker.Provider {"Watch":true,"Filename":"","Constraints":null,"Trace":false,"DebugLogGeneratedTemplate":false,"Endpoint":"unix:///var/run/docker.sock","Domain":"","TLS":null,"ExposedByDefault":true,"UseBindPortIP":false,"SwarmMode":false}"
time="2017-09-06T13:45:35Z" level=info msg="Starting provider *web.Provider {"Address":":8080","CertFile":"","KeyFile":"","ReadOnly":false,"Statistics":null,"Metrics":null,"Path":"","Auth":null,"Debug":false,"CurrentConfigurations":{},"Stats":{"Uptime":"2017-09-06T13:45:35.383750945Z","Pid":1,"ResponseCounts":{},"TotalResponseCounts":{},"TotalResponseTime":"0001-01-01T00:00:00Z"},"StatsRecorder":null}"
time="2017-09-06T13:45:35Z" level=info msg="Starting server on :10005"
time="2017-09-06T13:45:35Z" level=error msg="No entrypoint defined for frontend frontend-Host-missive-controller-app, defaultEntryPoints:[]"
time="2017-09-06T13:45:35Z" level=error msg="Skipping frontend frontend-Host-missive-controller-app..."
time="2017-09-06T13:45:35Z" level=error msg="No entrypoint defined for frontend frontend-Host-missive-interface-app, defaultEntryPoints:[]"
time="2017-09-06T13:45:35Z" level=error msg="Skipping frontend frontend-Host-missive-interface-app..."
time="2017-09-06T13:45:35Z" level=error msg="No entrypoint defined for frontend frontend-Host-missive-storage-app, defaultEntryPoints:[]"
time="2017-09-06T13:45:35Z" level=error msg="Skipping frontend frontend-Host-missive-storage-app..."
time="2017-09-06T13:45:35Z" level=error msg="No entrypoint defined for frontend frontend-Host-missive-transmitter-app, defaultEntryPoints:[]"
time="2017-09-06T13:45:35Z" level=error msg="Skipping frontend frontend-Host-missive-transmitter-app..."
time="2017-09-06T13:45:35Z" level=info msg="Server configuration reloaded on :10005"
但是,当触发 404 时,没有额外的日志输出stdout
,所以我无法判断问题是什么。我认为这不是我的服务之一。
我猜也许我的后端应该指示处理该服务的容器的名称,这样我就可以删除“跳过”错误?我还应该注意,我没有使用 Kubernetes 或类似的东西——它现在只是普通的 Docker Compose。
我没有发现什么会推动我前进?
更新
如果我不让 Traefik 访问我的 Docker 配置,它似乎很不高兴。如果我打开-v
音量开关,docker run
我会每隔一秒左右将其添加到日志中:
time="2017-09-06T17:18:20Z" level=error msg="Failed to retrieve information of the docker client and server host: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?"
time="2017-09-06T17:18:20Z" level=error msg="Provider connection error Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?, retrying in 443.292653ms"
我还找到了如何[accessLogs]
在配置中打开,并将其写入stdout
. 这些显示我的请求正在访问代理,但它们没有提供有关它们如何被路由的任何信息,也没有提供我的前端和后端定义是否被识别为有效的任何信息。
172.17.0.1 - - [06/Sep/2017:17:57:44 +0000] "GET / HTTP/1.1" %!d(<nil>) %!d(<nil>) - "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0" 1 - - 0ms
172.17.0.1 - - [06/Sep/2017:17:57:49 +0000] "GET /index.php HTTP/1.1" %!d(<nil>) %!d(<nil>) - "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0" 2 - - 0ms
172.17.0.1 - - [06/Sep/2017:17:58:09 +0000] "GET /index.php HTTP/1.1" %!d(<nil>) %!d(<nil>) - "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0" 3 - - 0ms