如何为在 docker 容器中运行的 haproxy 配置出站请求

时间:2021-07-14 13:10:16

标签: docker haproxy

我想使用在 docker 容器中运行的 haproxy 将请求重定向到端口 127.0.0.1:80 到侦听 127.0.0.1:3001 的后端服务器。

这是我的haproxy.cfg

global
  maxconn 50000
  log stdout local0

defaults
  mode http
  log global
  option httplog
  option forwardfor
  timeout connect 5s
  timeout client 5s
  timeout server 5s

frontend www
  bind :80
  default_backend webservers

backend webservers
  server b1 127.0.0.1:3001 check

这是我用来构建容器镜像的 Dockerfile:

FROM haproxy:2.2.14
COPY haproxy.cfg /usr/local/etc/haproxy
EXPOSE 80
EXPOSE 3001

我像这样运行容器 docker run -p 8081:80 -p 127.0.0.1:3001:3001 ${docker_image_id}

容器日志:

<133>Jul 14 13:07:11 haproxy[1]: Proxy www started.
<133>Jul 14 13:07:11 haproxy[1]: Proxy webservers started.
[NOTICE] 194/130711 (1) : New worker #1 (10) forked
[WARNING] 194/130711 (10) : Server webservers/b1 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 0 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
[NOTICE] 194/130711 (10) : haproxy version is 2.2.14-a07ac36
[NOTICE] 194/130711 (10) : path to executable is /usr/local/sbin/haproxy
[ALERT] 194/130711 (10) : backend 'webservers' has no server available!
<129>Jul 14 13:07:11 haproxy[10]: Server webservers/b1 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 0 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
<128>Jul 14 13:07:11 haproxy[10]: backend webservers has no server available!

现在我执行以下请求(IntelliJ 请求文件):

GET localhost:8081/user/all

由此产生的额外日志输出如下:

[...]

<129>Jul 14 13:07:11 haproxy[10]: Server webservers/b1 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 0 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
<128>Jul 14 13:07:11 haproxy[10]: backend webservers has no server available!
<134>Jul 14 13:08:46 haproxy[10]: 172.17.0.1:61364 [14/Jul/2021:13:08:46.905] www webservers/<NOSRV> 0/-1/-1/-1/0 503 222 - - SC-- 1/1/0/0/0 0/0 "GET /user/all HTTP/1.1"

我不知道这里出了什么问题。如何使服务器发现适用于 haproxy?

0 个答案:

没有答案