A dynamic configurable Nginx with Consul.
MIT License
DockerHub Repository: https://registry.hub.docker.com/u/shufo/nginx-consul-template/
This repository contains a scripts for creating configurable load balancer Consul Key-Value store API with nginx and consul-template.
docker run -d -p 80:80 \
--link consul:consul \
-e "CONSUL_KV_PREFIX=nginx" \
shufo/nginx-consul-template
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
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
curl -X PUT -d "/var/log/nginx/error.log" http://consul_host:8500/v1/kv/nginx/error_log
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
This repository contains a example dockerize application build with vagrant machine.(Requires Vagrant and Ansible)
make install
curl -X PUT -d "/var/log/nginx/error.log" http://172.17.9.101:8500/v1/kv/nginx/error_log
docker exec -it nodes_nginx_1 cat /etc/nginx/nginx.conf
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.
make restart
docker run -d -v /mnt:/data progrium/consul:latest -server -bootstrap -data-dir /data -ui-dir /ui
# 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 }}
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
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)