HAProxy 简单转发 Docker

HA Proxy simple forwarding with Docker

我正在尝试学习如何使用 HA 代理转发 http 流量。开始时,我想我会使用 Docker 并且卡住了。

我尝试做的转发流量看起来像这样

ha-proxy container port 81
>>> forward to
nginx container port 8088

当我使用 url http://localhost:81/ 从我的浏览器加载 ha 代理容器时,我收到的错误消息是

503 Service Unavailable No server is available to handle this request.

My setup looks like the following.

nginx - 容器

当我加载 http://localhost:8088/ 时,我得到了正确的 Welcome to nginx! 主页。

Docker 我正在使用的命令是。我正在使用 --net=host,因此它将它绑定到主机网络。

docker run --name myapp-backend -p 8088:80 -d --net=host  nginx:1.15.0-alpine  

哈代理

Docker文件

FROM haproxy:alpine
COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg

haproxy.cfg

global
    log 127.0.0.1 local0
    maxconn 4096

defaults
    log global
    mode http
    option httplog
    option dontlognull
    retries 3
    redispatch
    maxconn 2000
    contimeout 5000
    clitimeout 50000
    srvtimeout 50000

frontend http-in
    bind *:81
    acl myapp-frontend hdr(host) -i localhost
    use_backend myapp-backend if myapp-frontend

backend myapp-backend
    balance roundrobin
    option http-server-close
    server myapp-server-1 localhost:8088 check

正在启动 HA 代理

   docker build -t rob-haproxy .
   docker run --name ha-proxy -p 81:81  --net=host -d rob-haproxy

我的想法是 ha 代理配置文件有问题 haproxy.cfg。非常感谢任何建议。

您不能在 haproxy 配置文件中使用 localhost。对于 haproxy,localhost 表示 my container,而不是您的主机。相反 localhost,使用 nginx 的 docker 服务或容器名称。当然,将两个容器都放在同一个 docker 网络中。

我按照 Miq 的建议将其添加到自己的 docker 网络中。然而,这本身还不够,所以我还简化了 ha 配置。

下面是现在的样子

global
  quiet

defaults
  mode http
  maxconn 5000

  timeout connect 5s
  timeout client  20s
  timeout server  20s

frontend public
    bind *:81
    default_backend apps

backend apps
    server myapp-backend myapp-backend:80 check

bash

docker network create elk || true
docker run --name myapp-backend -p 8088:80 -d --net=elk  nginx:1.15.0-alpine
docker run --name rob-haproxy -p 81:81 --net=dev-d rob-haproxy-image