Dynamic DNS resolution using HAProxy and Docker

I am trying to configure HAProxy inside a Docker host.

Using HAProxy 1.7 and Docker 1.12

My haproxy.cfg looks like this:

# Simple configuration for an HTTP proxy listening on port 81 on all
# interfaces and forwarding requests to a single backend "servers" with a
# single server "server1" listening on 127.0.0.1:8000
global
    daemon
    maxconn 256

resolvers docker
    # nameserver dnsmasq 127.0.0.1:53
    nameserver dns 127.0.0.1:53

defaults
    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms
    default-server init-addr none

frontend http-in
    bind *:80
    default_backend www_somedomain1_com

    # Define hosts
    acl host_www_somedomain1_com hdr(host) -i somedomain1.com
    acl host_www_somedomain1_com hdr(host) -i www.somedomain1.com
    acl host_www_somedomain2_com hdr(host) -i www.somedomain2.com

    ## figure out which one to use
    use_backend www_somedomain1_com if host_www_somedomain1_com
    use_backend www_somedomain2_com if host_www_somedomain2_com

backend www_somedomain1_com
    # Utilizing the Docker DNS to resolve below host
    # server server1 www-somedomain1-com maxconn 32 check port 80
    server server1 www-somedomain1-com resolvers docker check maxconn 32

backend www_somedomain2_com
    # Utilizing the Docker DNS to resolve below host
    # server server1 www-somedomain2-com maxconn 32 check resolvers docker resolve-prefer ipv4
    server server1 www-somedomain2-com maxconn 32 check port 80

I want to use the Docker built-in DNS system, which, in my opinion, is enabled only when using a user-defined network.

So, I am creating a network (using the default bridge driver)

docker network create mynetwork

When I launch my two docker-docker containers (my-haproxy and www-somedomain1-com), I add them to this network using the -net flag.

Docker Launch Commands:

docker run --name myhaproxy --net mynetwork -p 80:80 -d haproxy
docker run --name www-somedomain1-com --net mynetwork -d nginx

I know that Docker dns is functional, because I can allow one container to another when I jump on them in the bash shell. I cannot get the correct combination / configuration in HAProxy to enable dynamic DNS resolution.

HAProxy stats , /...

, :  - "init-addr none-default-server" haproxy .

!

+4
2

, , 127.0.0.1:53 DNS- resolver, - 127.0.0.11:53.

haproxy dev:

global
    quiet

defaults
    log global
    mode http
    option forwardfor
    timeout connect 60s
    timeout client 60s
    timeout server 60s
    default-server init-addr none

resolvers docker_resolver
    nameserver dns 127.0.0.11:53

frontend https-proxy
    bind 0.0.0.0:80
    bind 0.0.0.0:443 ssl crt /usr/local/etc/haproxy/dev_server.pem
    redirect scheme https if !{ ssl_fc }

    acl is_api_server hdr(host) -i mywebsite

    use_backend api_server if is_api_server

backend api_server
    server haproxyapi api-server-dev:80 check inter 10s resolvers docker_resolver resolve-prefer ipv4
+3

nginx haproxy . docker-compose, , docker-compose.yaml .

version: "2"

networks:
  main:

volumes:
  jsdoc-data:
    driver: local
  coverage-data:
    driver: local

services:
  nginx:   
    build: ../nginx
    depends_on:
      - haproxy
    networks:
      - main
    ports:
      - 80:80
      - 443:443
    volumes:
      - jsdoc-data:/www/jsdoc
      - coverage-data:/www/coverage

  haproxy:
    image: dockercloud/haproxy:1.5.3
    ports:
      - 80
      - 1936:1936
    links:
      - app
    networks:
      - main
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - MODE=http
      - STATS_PORT=1936
      - STATS_AUTH=username:password
0

Source: https://habr.com/ru/post/1663783/


All Articles