我正在构建基于微服务的PHP Web应用程序,目前,进入我应用程序的路径位于不同的端口上。这对于开发来说很好,但是一旦我启动它就不会那么可靠。我有:
我希望这些都在同一个端口上。我最近发现Traefik看起来非常灵活,能够满足我未来的需求。我很感激我可以做simple proxying with 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
。嗯,公平地说,我可能错误地配置了一些东西。所以我将loglevel重置为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
上不会输出额外的日志,所以我无法分辨出问题所在。我不认为这是我的服务之一。
我猜我的后端可能应该指出处理该服务的容器的名称,所以我可以删除&#34;跳过&#34;错误?我还应该注意到我没有使用Kubernetes或类似的东西 - 它现在只是普通的Docker Compose。
我没有发现什么会让我前进?
-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
答案 0 :(得分:1)
好的,首先如果你不使用Docker配置,那么即使你的服务在容器中运行,你也不需要为它提供套接字(但是当你对Traefik更熟悉时,你应该把它看作是这真的很有用)
第二件事是日志告诉你确切的问题,你的前端没有被实际使用,因为他们没有指定入口点。您可以添加
defaultEntryPoints = ["everything"]
在您的入口点定义之上或添加
entrypoints = ["everything"]
到你们每个前端。
另一个问题是你有2个没有任何规则的前端因此它可能无法工作,因为没有办法区分它们。您可以使用主机规则或向WS前端添加路径前缀。
最后,您需要让Traefik知道要使用哪个配置系统。把它放在[frontends]
:
[file]
basics页面上的架构很好地描述了一切如何协同工作。