Giter Site home page Giter Site logo

pouyanh / autodns Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 1.0 36 KB

Automated dns resolver and load balancer for Docker containers using jwilder's docker-gen

Home Page: https://pouyanh.github.io/autodns/

Smarty 100.00%
haproxy docker-container hostname dns docker dns-server

autodns's Introduction

Automatic DNS and Load Balancer for Docker Containers

Enables access to docker containers over virtual internal network using a domain name, e.g., myapp.tst just by setting an environment variable, i.e., HOSTNAME on that container, instead of ip address. It automatically adds dns record on container start and removes the dns record on container stop and balances load between scaled containers using haproxy. No docker network sharing or containers linkage required.

Setup

  1. Reveal docker daemon's ip address:

    Command below reveals docker daemon's ip address:

    ip addr show docker0 | grep inet | awk '{ print $2 }' | awk -F'/' '{ print $1 }'
  2. Change default configurations if necessary (Optional):

    Create a docker-compose.override.yml file to override default configurations. You can copy the sample docker-compose.override.yml.sample file and modify it according to your situation. Here are some scenarios which require an override file:

    • Docker daemon's ip address differs from default, i.e., 172.17.0.1:

      Set HOST_IP environment variable for dnsmasq-conf service.

    • Port 53 is already in-use by another dns service such as bind or dnsmasq (Ubuntu's default network manager has a builtin dnsmasq):

      dnsmasq service automatically binds to port 53 on all interfaces, INADDR_ANY, i.e., 0.0.0.0 & ::. Bridge network's binding ip address can be set to ip address of docker0 interface by setting com.docker.network.bridge.host_binding_ipv4 under networks.default.driver_opts element.

  3. Start & Enable autodns:

    All autodns services automatically start whenever docker service starts, e.g., os startup or docker service restart. To start & enable it run:

    docker-compose up -d
  4. Add autodns to list of active dns servers:

    Either dnsmasq service binds to port 53 of all interfaces, INADDR_ANY, or just the docker0 interface it's almost reachable from docker0 interface ip address (step 0). Considering customizations in step 1, if bridge network's binding ip address is something different from INADDR_ANY or docker0 interface ip address you have to know it. Anyway let's name it AUTODNS_DNS_SERVER_IP_ADDRESS.

    On linux there are several ways to specify dns servers manually. It's important to make sure that autodns having the highest priority in list of dns servers, i.e., it should be the primary dns server.

    Don't worry about corruption in the global dns resolve procedure because it automatically switches to next dns server if current one fails in resolving the request. dnsmasq service only knows ip addresses of running docker containers which want to be resolvable by autodns, i.e., have the HOSTNAME environment variable. So it says IDK for any other hosts not being listed by itself and lets next dns server get involved.

    Here are some alternative ways to add a dns server manually:

    • DNS resolver configurator, i.e., resolvconf (recommended):

      Add AUTODNS_DNS_SERVER_IP_ADDRESS to /etc/resolvconf.conf:

      name_servers="AUTODNS_DNS_SERVER_IP_ADDRESS [other dns servers]"
      

      Then run:

      sudo resolvconf -u
    • Operating System's network manager:

      Set AUTODNS_DNS_SERVER_IP_ADDRESS as the primary dns server of the active interface

    • System preferences

    • Directly writing to DNS resolver configuration file (not recommended):

      Add AUTODNS_DNS_SERVER_IP_ADDRESS to top of /etc/resolv.conf:

      nameserver AUTODNS_DNS_SERVER_IP_ADDRESS
      

      Caution: Content of /etc/resolv.conf may become overridden by other tools such as resolvconf

Usage

Set HOSTNAME environment variable on your container:

docker run -e HOSTNAME=cms.jst wordpress:latest

Your container is reachable by value of HOSTNAME, i.e., cms.jst in this example. Test it by:

dig cms.jst

View status

  • View list of dns records listed in dnsmasq:

    docker exec autodns-dns-1 cat /etc/dnsmasq.d/default
  • View haproxy live configuration:

    docker exec autodns-haproxy-1 cat /usr/local/etc/haproxy/haproxy.cfg
  • View haproxy live statistics:

    Visit http://haproxy.local/haproxy?stats in a browser to see haproxy statistics

autodns's People

Contributors

faridehghani avatar pouyanh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

faridehghani

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.