├── .dockerignore ├── .gitignore ├── Dockerfile ├── README.md ├── handler.sh ├── hipache.json ├── launch-host ├── no-app-app ├── index.html └── server ├── service.sh └── supervisord.conf /.dockerignore: -------------------------------------------------------------------------------- 1 | .git 2 | supervisor-logs 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | logs 2 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # Based on https://raw.githubusercontent.com/hipache/hipache/master/Dockerfile 2 | 3 | # Latest Ubuntu LTS 4 | FROM ubuntu:14.04 5 | 6 | # Install some stuff 7 | RUN apt-get -y update && \ 8 | apt-get -y install supervisor nodejs npm redis-server && \ 9 | npm install -g hipache 10 | 11 | # Download the docker CLI 12 | RUN apt-get install -y curl && \ 13 | curl https://get.docker.io/builds/Linux/x86_64/docker-latest > /usr/bin/docker && \ 14 | chmod +x /usr/bin/docker 15 | 16 | # Download and install jq 17 | RUN curl http://stedolan.github.io/jq/download/linux64/jq > /usr/bin/jq && \ 18 | chmod +x /usr/bin/jq 19 | 20 | # Install the static web server 21 | RUN npm install -g node-static 22 | 23 | # Add our supervisor configs 24 | ADD ./supervisord.conf /etc/supervisor/conf.d/supervisord.conf 25 | 26 | # Add our custom hipache configs 27 | ADD hipache.json /usr/local/lib/node_modules/hipache/config/config.json 28 | 29 | # Our docker events handler 30 | ADD ./service.sh /usr/bin/auto-lb 31 | ADD ./handler.sh /usr/bin/auto-lb-handler 32 | 33 | # The dummy app that handles unknown domains 34 | ADD ./no-app-app /no-app-app 35 | 36 | # Expose hipache 37 | EXPOSE 80 38 | 39 | # Start supervisor 40 | CMD ["supervisord", "-n"] 41 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Docker Easy Load Balancer 2 | 3 | Experimental _automagic_ load balancing for Docker web apps in less than 100 lines of bash. 4 | 5 | ![demo](http://i.imgur.com/yRrsaAM.gif) 6 | 7 | ## Initial setup 8 | 9 | On Ubuntu 14.04 hosts, you'll need to install [libnss-resolver](https://github.com/azukiapp/libnss-resolver#installing) 10 | and run: 11 | 12 | ```sh 13 | apt-get install dnsmasq 14 | echo "nameserver 127.0.0.1:5353" | sudo tee -a /etc/resolver/docker.dev 15 | ``` 16 | 17 | _I suppose you are able to do something similiar when using Macs + [Vagrant](http://www.vagrantup.com/) 18 | / [boot2docker](http://boot2docker.io/) but I don't own a Mac to put the pieces 19 | together, LMK if you know how to make this work over there and I'll update the docs 20 | accordingly_ 21 | 22 | ## Try it 23 | 24 | Launch the load balancer and DNS server: 25 | 26 | ```sh 27 | git clone https://github.com/fgrehm/docker-easy-lb.git 28 | cd docker-easy-lb 29 | ./launch-host 30 | ``` 31 | 32 | And verify if things are working: 33 | 34 | ``` 35 | # Is DNS working? 36 | $ ping hello.docker.dev 37 | 38 | PING hello.docker.dev (172.17.0.175) 56(84) bytes of data. 39 | 64 bytes from 172.17.0.175: icmp_seq=1 ttl=64 time=0.035 ms 40 | 64 bytes from 172.17.0.175: icmp_seq=2 ttl=64 time=0.072 ms 41 | 64 bytes from 172.17.0.175: icmp_seq=3 ttl=64 time=0.150 ms 42 | ^C 43 | 44 | # Can we reach the load balancer? 45 | $ curl hello.docker.dev 46 | 47 | 48 | 49 | 404 - No application configured for this subdomain 50 | 9 | 10 | 11 |
12 |

There's no application running at this address

13 |
14 | 15 | 16 | -------------------------------------------------------------------------------- /no-app-app/server: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cd /no-app-app && nodejs /usr/local/bin/static -a '0.0.0.0' 4 | -------------------------------------------------------------------------------- /service.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if ! [ -S /var/run/docker.sock ]; then 4 | echo 'Docker socket not mounted' 5 | exit 1 6 | fi 7 | 8 | echo 'Cleaning up load balancer' 9 | redis-cli del "frontend:${DOMAIN}" 10 | redis-cli rpush "frontend:${DOMAIN}" default &>/dev/null 11 | redis-cli rpush "frontend:${DOMAIN}" 'http://0.0.0.0:8080' &>/dev/null 12 | 13 | redis-cli del "frontend:*.${DOMAIN}" 14 | redis-cli rpush "frontend:*.${DOMAIN}" defsubdomain &>/dev/null 15 | redis-cli rpush "frontend:*.${DOMAIN}" 'http://0.0.0.0:8080' &>/dev/null 16 | 17 | docker events | auto-lb-handler 18 | -------------------------------------------------------------------------------- /supervisord.conf: -------------------------------------------------------------------------------- 1 | [supervisord] 2 | nodaemon=false 3 | 4 | [program:hipache] 5 | command=/usr/local/bin/hipache -c /usr/local/lib/node_modules/hipache/config/config.json 6 | stdout_logfile=/var/log/supervisor/%(program_name)s.log 7 | stderr_logfile=/var/log/supervisor/%(program_name)s.log 8 | autorestart=true 9 | 10 | [program:redis] 11 | user=redis 12 | command=/usr/bin/redis-server 13 | stdout_logfile=/var/log/supervisor/%(program_name)s.log 14 | stderr_logfile=/var/log/supervisor/%(program_name)s.log 15 | autorestart=true 16 | 17 | [program:no-app-app] 18 | command=/no-app-app/server 19 | stdout_logfile=/var/log/supervisor/%(program_name)s.log 20 | stderr_logfile=/var/log/supervisor/%(program_name)s.log 21 | autorestart=true 22 | 23 | [program:autolb] 24 | command=/usr/bin/auto-lb 25 | stdout_logfile=/var/log/supervisor/%(program_name)s.log 26 | stderr_logfile=/var/log/supervisor/%(program_name)s.log 27 | autorestart=true 28 | --------------------------------------------------------------------------------