HA Proxy simple forwarding with Docker

2019-09-20 11:12发布

I am attempting to learn how to forward http traffic with HA Proxy. To get started I thought I would use Docker and have come stuck.

The forwarding traffic im attempting to do looks like this

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

When I load the ha proxy container from my browser with the url http://localhost:81/ The error message I get is

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

My setup looks like the following.

nginx - container

When I load http://localhost:8088/ I get the correct Welcome to nginx! home page.

Docker command im using for that is. Im using --net=host so it binds it the host network.

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

Ha Proxy

Dockerfile

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

Starting HA Proxy

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

My thinking is that I have something wrong with the ha proxy config file haproxy.cfg. Any advice much appreciated.

2条回答
太酷不给撩
2楼-- · 2019-09-20 11:19

I went down the route of adding it to its own docker network as per Miq suggestion. However this was not enough on its own so I also simplified the ha config.

Below is what it looks like now

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
查看更多
霸刀☆藐视天下
3楼-- · 2019-09-20 11:34

you cannot use localhost in haproxy config file. For haproxy, localhost means my container, not your host. Instead localhost, use the nginx's docker service or container name. Of course place both containers in this same docker network.

查看更多
登录 后发表回答