docker-compose scale 不适用于 haproxy

docker-compose scale does not work with haproxy

我正在玩 docker-compose haproxy 和 nginx。 我想要完成的是,如果我将 nginx 容器缩放为

docker-compose scale nginx=2

haproxy 容器还代理了新的 nginx 容器。但不知何故,它无法识别新容器。

这是我的配置:

docker-compose.yml:

version: '2.1'
services:

  nginx:
    image: 'bitnami/nginx:latest'
    healthcheck:
      test: "curl -f http://localhost/?healthcheck"
    expose:
      - "80"

  haproxy:
    image: haproxy
    restart: always
    depends_on:
      nginx:
        condition: service_healthy
    links:
      - nginx
    ports:
      - "80:80"
    volumes:
      - ./haproxy:/usr/local/etc/haproxy/
    expose:
      - "80" 

haproxy.cfg

global
  log 127.0.0.1 local0
  log 127.0.0.1 local1 notice

defaults
  log global
  mode http
  option httplog
  timeout connect 5000
  timeout client 10000
  timeout server 10000

frontend localnodes
  bind 0.0.0.0:80
  mode http
  default_backend nodes

backend nodes
  mode http
  option forwardfor
  balance roundrobin
  server nginx nginx:80 check
  option httpchk GET /?haproxyselfcheck
  http-check expect status 200 

我该怎么做才能让 haproxy 识别新容器,而无需手动将另一个 nginx 添加到 docker-compose 文件中?

经过一些研究后,我找到了与 docker swarm 一起工作的解决方案。 Docker swarm 有自己的负载均衡器。但是要反向代理到最合适的服务似乎是 docker-flow-proxy 来自传奇的 Victor Farcic https://github.com/vfarcic/docker-flow-proxy

然后只需将新服务添加到代理即可。