Skip to content

Commit 8d9f314

Browse files
Gabriel MonroyMatthew Fisher
authored andcommitted
feat(router): add nginx router driven by confd/etcd
1 parent 0a081cd commit 8d9f314

8 files changed

Lines changed: 208 additions & 0 deletions

File tree

router/Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM deis/base
2+
MAINTAINER Gabriel Monroy <gabriel@opdemand.com>
3+
4+
# install nginx
5+
RUN apt-get install -yq python-software-properties
6+
RUN add-apt-repository ppa:chris-lea/redis-server -y
7+
RUN add-apt-repository ppa:nginx/stable -y
8+
RUN apt-get update
9+
RUN apt-get install -yq nginx
10+
11+
ADD . /app
12+
WORKDIR /app
13+
EXPOSE 80
14+
CMD ["/app/bin/boot"]

router/Makefile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
build:
2+
docker build -t deis/router .
3+
4+
env:
5+
export ROUTER_HOST=`etcdctl -C $${ETCD:-127.0.0.1:4001} get /deis/router/host`
6+
export ROUTER_PORT=`etcdctl -C $${ETCD:-127.0.0.1:4001} get /deis/router/port`
7+
export ROUTER_WORKER_CONNECTIONS=`etcdctl -C $${ETCD:-127.0.0.1:4001} get /deis/router/workerConnections`
8+
export ROUTER_WORKER_PROCESSES=`etcdctl -C $${ETCD:-127.0.0.1:4001} get /deis/router/workerProcesses`
9+
env
10+
11+
config:
12+
etcdctl -C $${ETCD:-127.0.0.1:4001} set /deis/router/host $${ROUTER_HOST:-127.0.0.1}
13+
etcdctl -C $${ETCD:-127.0.0.1:4001} set /deis/router/port $${ROUTER_PORT:-80}
14+
etcdctl -C $${ETCD:-127.0.0.1:4001} set /deis/router/workerConnections $${ROUTER_WORKER_CONNECTIONS:-1024}
15+
etcdctl -C $${ETCD:-127.0.0.1:4001} set /deis/router/workerProcesses $${ROUTER_WORKER_PROCESSES:-2}
16+
17+
run:
18+
docker run -d -p 80:80 -e ETCD=$${ETCD:-172.17.42.1:4001} -e HOST=$${HOST:-172.17.42.1} --name deis-router deis/router
19+
20+
shell:
21+
docker run -t -i -rm deis/router /bin/bash
22+
23+
kill:
24+
docker kill deis-router
25+
docker rm deis-router
26+
27+
clean:
28+
docker rmi deis/router
29+
30+
test:
31+
docker build .

router/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Deis Router
2+
3+
A Docker image containing nginx for use in the [Deis](http://deis.io) open source PaaS.
4+
5+
This Docker image is based on the trusted build [deis/base](https://index.docker.io/u/deis/base/), which itself is based on the official [ubuntu:12.04](https://index.docker.io/_/ubuntu/) base image.
6+
7+
## Usage
8+
9+
Coming Soon!
10+
11+
## License
12+
13+
Copyright 2014 OpDemand LLC
14+
15+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at <http://www.apache.org/licenses/LICENSE-2.0>
16+
17+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

router/bin/boot

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/bash
2+
#
3+
# This script is designed to be run inside the container
4+
#
5+
6+
# set debug based on envvar
7+
[[ $DEBUG ]] && set -x
8+
9+
# configure etcd
10+
export ETCD=${ETCD:-172.17.42.1:4001}
11+
export ETCD_PATH=${ETCD_PATH:-/deis/router}
12+
export ETCD_TTL=${ETCD_TTL:-10}
13+
14+
# configure service discovery
15+
export HOST=${HOST:-localhost}
16+
export PORT=${PORT:-80}
17+
export PROTO=${PROTO:-tcp}
18+
19+
# wait for etcd to be available
20+
until etcdctl -C $ETCD ls >/dev/null; do
21+
echo "waiting for etcd at $ETCD..."
22+
sleep $(($ETCD_TTL/2)) # sleep for half the TTL
23+
done
24+
25+
# wait until etcd has discarded potentially stale values
26+
sleep $(($ETCD_TTL+1))
27+
28+
# seed initial service configuration if necessary
29+
$(dirname ${BASH_SOURCE[0]})/seed >/dev/null 2>&1
30+
31+
# wait for confd to run once and install initial templates
32+
until confd -onetime -node $ETCD -config-file /app/confd.toml; do
33+
echo "waiting for confd to write initial templates..."
34+
sleep $(($ETCD_TTL/2)) # sleep for half the TTL
35+
done
36+
37+
# spawn the service in the background
38+
/usr/sbin/nginx &
39+
SERVICE_PID=$!
40+
41+
# smart shutdown on SIGINT and SIGTERM
42+
function on_exit() {
43+
kill -TERM $SERVICE_PID
44+
wait $SERVICE_PID 2>/dev/null
45+
}
46+
trap on_exit INT TERM
47+
48+
# spawn confd in the background to update services based on etcd changes
49+
confd -node $ETCD -config-file /app/confd.toml &
50+
CONFD_PID=$!
51+
52+
wait

router/bin/seed

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
#
3+
4+
# make services directory for confd polling
5+
etcdctl -C $ETCD mkdir /deis/services
6+
7+
# seed etcd with default configuration
8+
etcdctl -C $ETCD set $ETCD_PATH/port ${PORT:-80}
9+
etcdctl -C $ETCD set $ETCD_PATH/workerConnections 1024
10+
etcdctl -C $ETCD set $ETCD_PATH/workerProcesses 2
11+
12+
exit 0

router/conf.d/nginx.conf.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[template]
2+
src = "nginx.conf"
3+
dest = "/etc/nginx/nginx.conf"
4+
uid = 0
5+
gid = 0
6+
mode = "0644"
7+
keys = [
8+
"/deis/services",
9+
]
10+
check_cmd = "/usr/sbin/nginx -t -c {{ .src }}"
11+
reload_cmd = "/usr/sbin/nginx -s reload"

router/confd.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[confd]
2+
confdir = "/app"
3+
interval = 5
4+
prefix = "/"
5+
quiet = true
6+
debug = false

router/templates/nginx.conf

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# required to run in a container
2+
daemon off;
3+
4+
user www-data;
5+
worker_processes 4;
6+
pid /run/nginx.pid;
7+
8+
events {
9+
worker_connections 768;
10+
# multi_accept on;
11+
}
12+
13+
http {
14+
# basic settings
15+
sendfile on;
16+
tcp_nopush on;
17+
tcp_nodelay on;
18+
keepalive_timeout 65;
19+
types_hash_max_size 2048;
20+
server_names_hash_bucket_size 64;
21+
gzip on;
22+
gzip_disable "msie6";
23+
24+
include /etc/nginx/mime.types;
25+
default_type application/octet-stream;
26+
27+
# send logs to STDOUT so they can be seen using 'docker logs'
28+
access_log /dev/stdout;
29+
error_log /dev/stdout;
30+
31+
32+
server {
33+
listen 80 default_server;
34+
server_name _; # will never match
35+
server_name_in_redirect off;
36+
}
37+
38+
# service definitions for each application
39+
40+
{{ range $service := .deis_services }}{{ if $service.Nodes }}
41+
upstream {{ Base $service.Key }} {
42+
{{ range $upstream := $service.Nodes }}server {{ $upstream.Value }};
43+
{{ end }}
44+
}
45+
46+
server {
47+
server_name ~^{{ Base $service.Key }}\.(?<domain>.+)$;
48+
49+
server_name_in_redirect off;
50+
port_in_redirect off;
51+
52+
location / {
53+
proxy_buffering off;
54+
proxy_set_header Host $host;
55+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
56+
proxy_redirect off;
57+
proxy_connect_timeout 10;
58+
proxy_send_timeout 30;
59+
proxy_read_timeout 30;
60+
61+
proxy_pass http://{{ Base $service.Key }};
62+
}
63+
}
64+
{{ end }}{{ end }}
65+
}

0 commit comments

Comments
 (0)