Giter Club home page Giter Club logo

docker-easy-lb's Introduction

Docker Easy Load Balancer

Experimental automagic load balancing for Docker web apps in less than 100 lines of bash.

demo

Initial setup

On Ubuntu 14.04 hosts, you'll need to install libnss-resolver and run:

apt-get install dnsmasq
echo "nameserver 127.0.0.1:5353" | sudo tee -a /etc/resolver/docker.dev

I suppose you are able to do something similiar when using Macs + Vagrant / boot2docker but I don't own a Mac to put the pieces together, LMK if you know how to make this work over there and I'll update the docs accordingly

Try it

Launch the load balancer and DNS server:

git clone https://github.com/fgrehm/docker-easy-lb.git
cd docker-easy-lb
./launch-host

And verify if things are working:

# Is DNS working?
$ ping hello.docker.dev

PING hello.docker.dev (172.17.0.175) 56(84) bytes of data.
64 bytes from 172.17.0.175: icmp_seq=1 ttl=64 time=0.035 ms
64 bytes from 172.17.0.175: icmp_seq=2 ttl=64 time=0.072 ms
64 bytes from 172.17.0.175: icmp_seq=3 ttl=64 time=0.150 ms
^C

# Can we reach the load balancer?
$ curl hello.docker.dev

<html>
  <head>
    <title>404 - No application configured for this subdomain</title>
    <style>


# Lets start a web server exposing the 3000 port
$ docker run -d --name hello -h hello -p 3000 ubuntu:14.04 python3 -m 'http.server' 3000
4e2731f1d2919e9a1259d9c4439e8cfb9953e8d6debbe9f64f66b8455b2ea002

# And verify that the app has been addded to the load balancer
$ curl hello.docker.dev

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ascii">
<title>Directory listing for /</title>
</head>
<body>
<h1>Directory listing for /</h1>
<hr>
<ul>
....

# Don't forget to clean up
$ docker rm -f hello

How does it work?

When the launch-host script gets run, it will start a dnsmasq server on your machine pointing any address under *.docker.dev to a fgrehm/easy-lb container that has the Docker socket /var/run/docker.sock bind mounted inside it. From there it will use supervisord to kick off Redis + Hipache + a "service" that will register a Docker events listener that responsible for registering / deregistering containers that expose a port normally used by web apps.

It does not depend on any other tool apart from Docker and dnsmasq itself but it plays really well with devstep, fig and basically anything that creates Docker containers that is able to expose the following ports:

  • 80
  • 3000
  • 4000
  • 4567
  • 5000
  • 8080
  • 9000
  • 9292

Troubleshooting

For some reason that I don't know yet the DNS / load balancer combo stopped working every now and then during the early days of this project when a container got removed. Even my Chrome browser crashed sometimes when the dnsmasq started misbehaving.

When that happens I first try restarting the dnsmasq server launched by the launch-host script and if it still doesn't work I run:

sudo dpkg-reconfigure ubuntu14-libnss-resolver
docker rm -f easy-lb
./launch-host

If it still doesn't make things work please create an issue with as much information as possible on how to consistently reproduce your problem.

TODO

  • Configurable domains
  • Register existing containers when bringing the load balancer up
  • Create a small app that lists configured apps from docker.dev
  • Redirect to list of configured apps in case an invalid domain is accessed
  • Provide an example of an init script
  • Create an entrypoint that performs some validation before starting supervisord
  • Clean up / document code
  • Try to setup dnsmasq as part of the load balancer container

Inspiration

https://github.com/crosbymichael/skydock

docker-easy-lb's People

Contributors

fgrehm avatar

Stargazers

Jason Odoom avatar Naonak avatar Thiago Rodrigues avatar  avatar Alexandre Gomes avatar Richard Metzler avatar Jaehoon You avatar Joonathan Mägi avatar Justin Honold avatar Javier Juarez avatar Michał Bachowski avatar Tim T.Y. Lin avatar Alek Monstovich avatar  avatar Derek Reynolds avatar Evgeny Shcherbinin avatar Brittany Martin avatar Jānis Ābele avatar Michael Smith avatar JP Boily avatar Raphaël Valyi avatar Gianluca Pacchiella avatar Diogo Gomes avatar Bruno Tavares avatar  avatar

Watchers

 avatar James Cloos avatar  avatar

Forkers

sudhish74

docker-easy-lb's Issues

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.