Coder Social home page Coder Social logo

shufo / nginx-consul-template Goto Github PK

View Code? Open in Web Editor NEW
76.0 11.0 19.0 22 KB

A dynamic configurable Nginx with Consul.

Home Page: https://registry.hub.docker.com/u/shufo/nginx-consul-template/

License: MIT License

Makefile 22.38% Shell 64.28% Dockerfile 6.60% Jinja 6.74%
nginx consul-template docker

nginx-consul-template's Introduction

nginx-consul-template

DockerHub Repository: https://registry.hub.docker.com/u/shufo/nginx-consul-template/

Overview

This repository contains a scripts for creating configurable load balancer Consul Key-Value store API with nginx and consul-template.

Requirements

Usage

  • Run
docker run -d -p 80:80 \
    --link consul:consul \
    -e "CONSUL_KV_PREFIX=nginx" \
    shufo/nginx-consul-template
  • Custom nginx config

Inside the container there is a file config.json which contains default values for all of the configuration options that are read by the consul template. When this container starts these default values are pre-loaded into consul, so that when it comes to reading these values when the template is parsed, there are already default values.

However you can change these default values by providing your own config.json:

docker run -d -p 80:80 \
    -v /path/to/config.json:/config.json \
    --link consul:consul \
    -e "CONSUL_KV_PREFIX=nginx" \
    shufo/nginx-consul-template
  • Custom template
docker run -d -p 80:80 \
    -v $(pwd)/config.json:/config.json \
    -v /path/to/nginx.conf.ctmpl:/etc/nginx/nginx.conf.ctmpl \
    -e "CONSUL_KV_PREFIX=nginx" \
    shufo/nginx-consul-template
  • Dynamic configuration change via Consul API.
curl -X PUT -d "/var/log/nginx/error.log" http://consul_host:8500/v1/kv/nginx/error_log
  • Changing the consul hostname

By default this container tries to connect to a consul server running on localhost. If you want to specify a different location for the consul server, set the CONSUL_PORT_8500_TCP_ADDR environmental variable. If you are linking this container to the consul container then you should set the CONSUL_PORT_8500_TCP_ADDR variable to the alias of the consul container:

docker run -d -p 80:80 \
    -v /path/to/config.json:/config.json \
    --link consul:consul \
    -e "CONSUL_KV_PREFIX=nginx" \
    -e "CONSUL_PORT_8500_TCP_ADDR=consul" \
    shufo/nginx-consul-template

Example

This repository contains a example dockerize application build with vagrant machine.(Requires Vagrant and Ansible)

  • Setup VM and docker containers.
make install
  • Change configuration.
curl -X PUT -d "/var/log/nginx/error.log" http://172.17.9.101:8500/v1/kv/nginx/error_log
  • Ensure nginx conf is properly rewrited.
docker exec -it nodes_nginx_1 cat /etc/nginx/nginx.conf
  • Set tag to application upstream.
curl -X PUT -d "0.0.1" http://172.17.9.101:8500/v1/kv/nginx/http/server/httpd/upstream/current

This will set upstream of service named httpd to tagged with 0.0.1 containers. Service registration is automated by registrator.

  • If something goes wrong, restart all containers
make restart

How to

  • Consul data persistence
docker run -d -v /mnt:/data progrium/consul:latest -server -bootstrap -data-dir /data -ui-dir /ui
  • Set multiple value in same key
# Edit config.json
# Set JSON key-value pair as a Value.
"location": {
"proxy_set_header": "{\"X-Real-IP\": \"$remote_addr\", \"X-Forwarded-For\": \"$proxy_add_x_forwarded_for\", \"X-Forwarded-Proto\": \"$scheme\", \"Host\": \"$host\"}"
}

# Edit consul template file
{{ range $key, $pairs := tree "location" | byKey }}
  {{ range $pair := $pairs }}
    {{ range $key, $value := printf "location/%s"  $pair.Key | key | parseJSON }}
    	{{ $key }}{{ $value }}
    {{ end }}
  {{ end }}
{{ end }}
  • Run with Host networking
docker run -d --net host -v $(pwd)/config.json:/config.json -e "CONSUL_KV_PREFIX=nginx" -e "CONSUL_PORT_8500_TCP_ADDR=CONSUL_HOST_IP" shufo/nginx-consul-template

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Contributors

shufo
Shuhei Hayashibara
emmetog
Emmet O'Grady
smiller171
Scott Miller
malaiwah
Michel Belleau

nginx-consul-template's People

Contributors

emmetog avatar malaiwah avatar shufo avatar smiller171 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nginx-consul-template's Issues

make install error for ansible role

tried running the make install to get vagrant up but ran into this error.

ERROR! the role 'defunctzombie.coreos-bootstrap' was not found in /Users/sam.flint/Documents/repos/nginx-consul-template/ansible/roles:/Users/sam.flint/Documents/repos/nginx-consul-template/ansible:/usr/local/etc/ansible/roles

The error appears to have been in '/Users/sam.flint/Documents/repos/nginx-consul-template/ansible/coreos.yml': line 8, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

roles:
- defunctzombie.coreos-bootstrap
^ here

Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.

Limit which services are put in template

I've been playing around with this container and it looks great! The docs are pretty sparse though.

In particular I would like to know how to limit which services appear in the template. For example I have a consul server on a host which has different containers; a few web containers and a few databases for example. I don't want the mysql services to be put in the template, since nginx won't be routing traffic to the database. I only want certain web containers to be in the template.

Can this be done? If so, could you improve the docs?

make install error for ansible role

tried running the make install to get vagrant up but ran into this error.

ERROR! the role 'defunctzombie.coreos-bootstrap' was not found in /Users/sam.flint/Documents/repos/nginx-consul-template/ansible/roles:/Users/sam.flint/Documents/repos/nginx-consul-template/ansible:/usr/local/etc/ansible/roles

The error appears to have been in '/Users/sam.flint/Documents/repos/nginx-consul-template/ansible/coreos.yml': line 8, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

roles:
- defunctzombie.coreos-bootstrap
^ here

Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.

docs unclear

might be the docs are unclear but more likely case is I'm misreading but if I am it's possible I'm not the only one :)

Tried the following:

docker run -d --name consul \
    --net=host --restart=always \
    progrium/consul:latest \
    -server -bootstrap-expect 1 -advertise 10.10.10.201 -dc us-east-1 -recursor 8.8.8.8 -ui-dir /ui -data-dir /data/consul

docker run -d --name=registrator \
    --net=host \
    --volume=/var/run/docker.sock:/tmp/docker.sock \
    gliderlabs/registrator:latest \
    consul://localhost:8500

docker run -d --name=consul-template-nginx \
    --net=host \
    -p 80:80 \
    shufo/nginx-consul-template

docker run -d --name nginx -P \
    -e "SERVICE_NAME=httpd" -e "SERVICE_TAG=0.0.2" \
    nginx:stable

curl -X PUT -d "0.0.2" http://localhost:8500/v1/kv/nginx/http/server/httpd/upstream/current

relevant parts of the resulting nginx:

  upstream httpd-443 {

  }

  upstream httpd-80 {

  }

  server {
    listen 80; 
    server_name "localhost"; 
    root /dev/null;
    # root
    location / {
      proxy_pass http://httpd-80/;
    }
  }

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.