rochacon / docker-nginx-lb

Simple NGINX load balancer for Docker containers.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

nginx-lb

This is a simple NGINX load balancer that automatically reconfigures itself according to running containers.

How it works

  1. nginx-lb search for running containers and group them by application name*
  2. Configures an NGINX virtual host for every app load balancing between its Docker instances.
  3. Monitors Docker for events
  4. Repeat setup when an event is found
  • An application name is detected by splitting containers name with _ and discarting the last bit. The application name is used as the app domain inside NGINX. A few examples of valid container names: myapp.127.0.0.1.xip.io_1 myapp.127.0.0.1.xip.io_2 dev.myapp.com_1. For those examples the app name would be myapp.127.0.0.1.xip.io, myapp.127.0.0.1.xip.io and dev.myapp.com, respectively. Note that myapp.127.0.0.1.xip.io has two instances to be load balanced to.

Install

nginx-lb is an automated Docker image. Pull it with:

docker pull rochacon/nginx-lb

Configuration

  • DOCKER_HOST - HTTP address of the Docker host. e.g http://172.42.17.1:2375
  • PORTS - Comma separated list of containers ports to monitor. e.g 80,8080

Usage

Launch nginx-lb

docker run -d -p 80:80 --name nginx-lb \
    -v /var/run/docker.sock:/var/run/docker.sock:ro \
    -e PORTS=80,8080 \
    rochacon/nginx-lb

Use the DOCKER_HOST environment to monitor a Docker daemon listening on TCP.

Launch some app instances

docker run -p 80 --name myapp.example.com_1 myimage
docker run -p 80 --name myapp.example.com_2 myimage
docker run -p 80 --name myapp.example.com_3 myimage

Hit application

curl myapp.example.com

Checking current setup

docker exec nginx-lb cat /etc/nginx/nginx.conf

License

MIT

About

Simple NGINX load balancer for Docker containers.

License:MIT License


Languages

Language:Python 98.1%Language:Shell 1.9%