diff --git a/README.md b/README.md index 2e38d7f..c14600d 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,50 @@ services: network_mode: bridge ``` +#### Multiple nginx, same consul cluster + +To run more than one Nginx balancer against the same consul cluster you'll need +to namespace them using the `SERVICE_NAME` environment variable. (Default: `nginx`) + +Example: + +```yaml +nginx_1: + image: autopilotpattern/nginx + restart: always + mem_limit: 512m + env_file: _env + environment: + - BACKEND=example + - CONSUL_AGENT=1 + - ACME_ENV=staging + - ACME_DOMAIN=example.com + - SERVICE_NAME=nginx_1 + ports: + - 80 + - 443 + - 9090 + labels: + - triton.cns.services=nginx-1 +nginx_2: + image: autopilotpattern/nginx + restart: always + mem_limit: 512m + env_file: _env + environment: + - BACKEND=example + - CONSUL_AGENT=1 + - ACME_ENV=staging + - ACME_DOMAIN=dev.example.com + - SERVICE_NAME=nginx_2 + ports: + - 80 + - 443 + - 9090 + labels: + - triton.cns.services=nginx-2 +``` + ### Examples The `examples/` directory includes a manifest for deploying via Docker Compose to a local Docker environment and a manifest for deploying to Joyent's Triton Cloud. The `examples/backend` directory is a simple Node.js application that acts as a demonstration for registering backends and updating the Nginx configuration via watching Consul. You can build the example applications with `make build/examples`. diff --git a/bin/acme b/bin/acme index d57fead..882c113 100755 --- a/bin/acme +++ b/bin/acme @@ -4,13 +4,14 @@ pushd `dirname $0` > /dev/null SCRIPTPATH=`pwd -P` popd > /dev/null +SERVICE_NAME_DEFAULT=${SERVICE_NAME:-nginx} CONSUL_HOST_DEFAULT=${CONSUL:-consul} if [ "${CONSUL_AGENT}" != "" ]; then CONSUL_HOST_DEFAULT="localhost" fi CONSUL_HOST=${CONSUL_HOST:-$CONSUL_HOST_DEFAULT} CONSUL_ROOT="http://${CONSUL_HOST}:8500/v1" -CONSUL_KEY_ROOT="${CONSUL_ROOT}/kv/nginx" +CONSUL_KEY_ROOT="${CONSUL_ROOT}/kv/${SERVICE_NAME:-$SERVICE_NAME_DEFAULT}" SESSION_DIR_DEFAULT="/var/consul" SESSION_DIR=${SESSION_DIR:-$SESSION_DIR_DEFAULT} @@ -22,7 +23,7 @@ CERT_DIR="/var/www/ssl" ACME_ENV=${ACME_ENV:-staging} function getConsulSession () { - if [ -f $SESSION_FILE ]; then + if [ -f $SESSION_FILE ]; then SID=$(cat ${SESSION_DIR}/session) local STATUS=$(curl -s ${CONSUL_ROOT}/session/info/${SID}) if [ "${STATUS}" != "[]" ]; then @@ -39,7 +40,7 @@ function getConsulSession () { function renewConsulSession () { local SID="$(getConsulSession)" rc=$? - if [ $rc -ne 0 ]; then + if [ $rc -ne 0 ]; then createConsulSession return $? else @@ -127,7 +128,7 @@ case "$1" in acquireLeader ;; watch) - /usr/local/bin/consul-template -config /etc/acme/watch.hcl -consul $CONSUL_HOST:8500 + /usr/local/bin/consul-template -config /etc/acme/watch.hcl -consul-addr $CONSUL_HOST:8500 ;; init) if [ -f ${CERT_DIR}/fullchain.pem -a -f ${CERT_DIR}/privkey.pem ]; then diff --git a/etc/acme/dehydrated/hook.sh b/etc/acme/dehydrated/hook.sh index 962f859..2dab4a2 100755 --- a/etc/acme/dehydrated/hook.sh +++ b/etc/acme/dehydrated/hook.sh @@ -1,13 +1,14 @@ #!/usr/bin/env bash set -o pipefail +SERVICE_NAME_DEFAULT=${SERVICE_NAME:-nginx} CONSUL_HOST_DEFAULT=${CONSUL:-consul} if [ "${CONSUL_AGENT}" != "" ]; then CONSUL_HOST_DEFAULT="localhost" fi CONSUL_HOST=${CONSUL_HOST:-$CONSUL_HOST_DEFAULT} CONSUL_ROOT="http://${CONSUL_HOST}:8500/v1" -CONSUL_KEY_ROOT="${CONSUL_ROOT}/kv/nginx" +CONSUL_KEY_ROOT="${CONSUL_ROOT}/kv/${SERVICE_NAME:-$SERVICE_NAME_DEFAULT}" CHALLENGE_PATH="/.well-known/acme-challenge" function deploy_challenge { diff --git a/etc/acme/templates/cert.ctmpl b/etc/acme/templates/cert.ctmpl index 46d94d2..3e90796 100644 --- a/etc/acme/templates/cert.ctmpl +++ b/etc/acme/templates/cert.ctmpl @@ -1 +1,3 @@ -{{if key "nginx/acme/cert"}}{{key "nginx/acme/cert"}}{{end}} +{{ $service_name := env "SERVICE_NAME" }} +{{ $service_name := or $service_name "nginx" }} +{{if key (print $service_name "/acme/cert")}}{{key (print $service_name "/acme/key")}}{{end}} diff --git a/etc/acme/templates/chain.ctmpl b/etc/acme/templates/chain.ctmpl index c914597..47f1ead 100644 --- a/etc/acme/templates/chain.ctmpl +++ b/etc/acme/templates/chain.ctmpl @@ -1 +1,3 @@ -{{if key "nginx/acme/chain"}}{{key "nginx/acme/chain"}}{{end}} +{{ $service_name := env "SERVICE_NAME" }} +{{ $service_name := or $service_name "nginx" }} +{{if key (print $service_name "/acme/chain")}}{{key (print $service_name "/acme/chain")}}{{end}} diff --git a/etc/acme/templates/challenge-token.ctmpl b/etc/acme/templates/challenge-token.ctmpl index 902c5ce..c0d6853 100644 --- a/etc/acme/templates/challenge-token.ctmpl +++ b/etc/acme/templates/challenge-token.ctmpl @@ -1,3 +1,5 @@ -{{if key "nginx/acme/challenge/token-filename"}}{{key "nginx/acme/challenge/token-filename"}}{{end}} -{{if key "nginx/acme/challenge/token-value"}}{{key "nginx/acme/challenge/token-value"}}{{end}} -{{if key "nginx/acme/challenge/last-token-filename"}}{{key "nginx/acme/challenge/last-token-filename"}}{{end}} +{{ $service_name := env "SERVICE_NAME" }} +{{ $service_name := or $service_name "nginx" }} +{{if key (print $service_name "/acme/token-filename")}}{{key (print $service_name "/acme/token-filename")}}{{end}} +{{if key (print $service_name "/acme/token-value")}}{{key (print $service_name "/acme/token-value")}}{{end}} +{{if key (print $service_name "/acme/last-token-filename")}}{{key (print $service_name "/acme/last-token-filename")}}{{end}} diff --git a/etc/acme/templates/fullchain.ctmpl b/etc/acme/templates/fullchain.ctmpl index 3a785ff..17a56bd 100644 --- a/etc/acme/templates/fullchain.ctmpl +++ b/etc/acme/templates/fullchain.ctmpl @@ -1 +1,3 @@ -{{if key "nginx/acme/fullchain"}}{{key "nginx/acme/fullchain"}}{{end}} +{{ $service_name := env "SERVICE_NAME" }} +{{ $service_name := or $service_name "nginx" }} +{{if key (print $service_name "/acme/fullchain")}}{{key (print $service_name "/acme/fullchain")}}{{end}} diff --git a/etc/acme/templates/privkey.ctmpl b/etc/acme/templates/privkey.ctmpl index 0a4a20b..b0b2e99 100644 --- a/etc/acme/templates/privkey.ctmpl +++ b/etc/acme/templates/privkey.ctmpl @@ -1 +1,3 @@ -{{if key "nginx/acme/key"}}{{key "nginx/acme/key"}}{{end}} +{{ $service_name := env "SERVICE_NAME" }} +{{ $service_name := or $service_name "nginx" }} +{{if key (print $service_name "/acme/key")}}{{key (print $service_name "/acme/key")}}{{end}} diff --git a/test/testing b/test/testing new file mode 160000 index 0000000..196e0ed --- /dev/null +++ b/test/testing @@ -0,0 +1 @@ +Subproject commit 196e0ed2aa017be9850b0b2aaa61aa756fde6f5b diff --git a/test/triton-docker-cli b/test/triton-docker-cli deleted file mode 160000 index 031e98e..0000000 --- a/test/triton-docker-cli +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 031e98e50a9bf32c7d1138a9089aa3835410ce52