Skip to content

Commit

Permalink
Update service discovery demos to use NGINX's REST API (#12)
Browse files Browse the repository at this point in the history
* Update service discovery demos to use NGINX's REST API
  • Loading branch information
alessfg authored Jan 30, 2019
1 parent b1bade3 commit d9ce1bd
Show file tree
Hide file tree
Showing 55 changed files with 1,342 additions and 1,308 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@

# Ignore MacOS system files
.DS_Store

# Ignore log files
*.log

# Ignore Ansible retry files
*.retry
201 changes: 97 additions & 104 deletions consul-api-demo/README.md

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions consul-api-demo/Vagrantfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(2) do |config|

config.vm.box = "ubuntu/trusty64"
config.vm.box = "ubuntu/bionic64"
config.vm.network "private_network", :ip => "10.2.2.70"

config.vm.synced_folder "../consul-api-demo/", "/srv/NGINX-Demos/consul-api-demo"

config.vm.provision :ansible do |ansible|
ansible.playbook = "./setup_consul_api_demo.yml"
ansible.extra_vars = {
ansible_python_interpreter:"/usr/bin/python3"
}
end

end
2 changes: 1 addition & 1 deletion consul-api-demo/clean_containers.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
docker rm -f `docker ps -qa`
docker rm -f $(docker ps -qa)
16 changes: 9 additions & 7 deletions consul-api-demo/create-http-service.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
http:
image: nginxdemos/hello:latest
labels:
SERVICE_80_NAME: http
SERVICE_TAGS: production
ports:
- "80"
version: '3'
services:
http:
image: nginxdemos/hello:latest
labels:
SERVICE_80_NAME: http
SERVICE_TAGS: production
ports:
- "80"
65 changes: 32 additions & 33 deletions consul-api-demo/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
nginxplus:
build: ./nginxplus
container_name: nginxplus
links:
- consul
ports:
- "80:80"
- "8080:8080"

consul:
command: "-server -bootstrap -node consul -advertise ${HOST_IP} -config-file /etc/consul.d/config.json"
image: progrium/consul:latest
container_name: consul
ports:
- "8300:8300"
- "8400:8400"
- "8500:8500"
- "8600:53/udp"
volumes:
- "./consul_watches_config.json:/etc/consul.d/config.json"
- "./script.sh:/tmp/script.sh"
environment:
- HOST_IP

registrator:
command: consul://consul:8500
image: gliderlabs/registrator:latest
container_name: registrator
links:
- consul
volumes:
- "/var/run/docker.sock:/tmp/docker.sock"

version: '3'
services:
nginxplus:
build: ./nginxplus
container_name: nginxplus
links:
- consul
ports:
- "80:80"
- "8080:8080"
consul:
command: "-server -bootstrap -node consul -advertise ${HOST_IP} -config-file /etc/consul.d/config.json"
image: progrium/consul:latest
container_name: consul
ports:
- "8300:8300"
- "8400:8400"
- "8500:8500"
- "8600:53/udp"
volumes:
- "./consul_watches_config.json:/etc/consul.d/config.json"
- "./script.sh:/tmp/script.sh"
environment:
- HOST_IP
registrator:
command: consul://consul:8500
image: gliderlabs/registrator:latest
container_name: registrator
links:
- consul
volumes:
- "/var/run/docker.sock:/tmp/docker.sock"
60 changes: 37 additions & 23 deletions consul-api-demo/nginxplus/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,35 +1,49 @@
FROM ubuntu:14.04
# For Debian 9
FROM debian:stretch-slim

MAINTAINER NGINX Docker Maintainers "[email protected]"

# Set the debconf front end to Noninteractive
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections

RUN apt-get update && apt-get install -y -q wget apt-transport-https
LABEL maintainer="NGINX Docker Maintainers <[email protected]>"

# Download certificate and key from the customer portal (https://cs.nginx.com)
# and copy to the build context
ADD nginx-repo.crt /etc/ssl/nginx/
ADD nginx-repo.key /etc/ssl/nginx/

# Get other files required for installation
RUN wget -q -O /etc/ssl/nginx/CA.crt https://cs.nginx.com/static/files/CA.crt
RUN wget -q -O - http://nginx.org/keys/nginx_signing.key | apt-key add -
RUN wget -q -O /etc/apt/apt.conf.d/90nginx https://cs.nginx.com/static/files/90nginx

RUN printf "deb https://plus-pkgs.nginx.com/ubuntu `lsb_release -cs` nginx-plus\n" >/etc/apt/sources.list.d/nginx-plus.list
COPY nginx-repo.crt /etc/ssl/nginx/
COPY nginx-repo.key /etc/ssl/nginx/

# Install NGINX Plus
RUN apt-get update && apt-get install -y nginx-plus

# forward request logs to Docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log
RUN ln -sf /dev/stderr /var/log/nginx/error.log
RUN set -x \
&& apt-get update && apt-get upgrade -y \
&& apt-get install --no-install-recommends --no-install-suggests -y apt-transport-https ca-certificates gnupg1 \
&& \
NGINX_GPGKEY=573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62; \
found=''; \
for server in \
ha.pool.sks-keyservers.net \
hkp://keyserver.ubuntu.com:80 \
hkp://p80.pool.sks-keyservers.net:80 \
pgp.mit.edu \
; do \
echo "Fetching GPG key $NGINX_GPGKEY from $server"; \
apt-key adv --keyserver "$server" --keyserver-options timeout=10 --recv-keys "$NGINX_GPGKEY" && found=yes && break; \
done; \
test -z "$found" && echo >&2 "error: failed to fetch GPG key $NGINX_GPGKEY" && exit 1; \
echo "Acquire::https::plus-pkgs.nginx.com::Verify-Peer \"true\";" >> /etc/apt/apt.conf.d/90nginx \
&& echo "Acquire::https::plus-pkgs.nginx.com::Verify-Host \"true\";" >> /etc/apt/apt.conf.d/90nginx \
&& echo "Acquire::https::plus-pkgs.nginx.com::SslCert \"/etc/ssl/nginx/nginx-repo.crt\";" >> /etc/apt/apt.conf.d/90nginx \
&& echo "Acquire::https::plus-pkgs.nginx.com::SslKey \"/etc/ssl/nginx/nginx-repo.key\";" >> /etc/apt/apt.conf.d/90nginx \
&& printf "deb https://plus-pkgs.nginx.com/debian stretch nginx-plus\n" > /etc/apt/sources.list.d/nginx-plus.list \
&& apt-get update && apt-get install -y nginx-plus \
&& apt-get remove --purge --auto-remove -y gnupg1 \
&& rm -rf /var/lib/apt/lists/* \
&& rm -rf /etc/ssl/nginx

# Forward request logs to Docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
&& ln -sf /dev/stderr /var/log/nginx/error.log

EXPOSE 80 8080 443

STOPSIGNAL SIGTERM

RUN rm -v /etc/nginx/conf.d/*
ADD app.conf /etc/nginx/conf.d/app.conf
ADD logo.png /usr/share/nginx/html/logo.png
COPY app.conf /etc/nginx/conf.d/app.conf

CMD ["nginx", "-g", "daemon off;"]
78 changes: 32 additions & 46 deletions consul-api-demo/nginxplus/app.conf
Original file line number Diff line number Diff line change
@@ -1,46 +1,32 @@
upstream backend {
zone upstream_backend 64k;
state /tmp/upstream.conf;
}

match hello {
status 200;
header Content-Type = text/html;
body ~ "Hello";
}

server {
listen 80;
status_zone backend;

location / {
proxy_pass http://backend;
health_check interval=2s match=hello; # uri=/ by default
}

location /upstream_conf {
upstream_conf;

#allow 127.0.0.1; # permit access from localhost
#deny all; # deny access from everywhere else
}
}

server {
listen 8080;
root /usr/share/nginx/html;
access_log off;

# Redirect requests for / to /status.html
location = / {
return 301 /status.html;
}

location = /status.html { }

# Everything beginning with /status (except for /status.html) is
# processed by the status handler
location /status {
status;
}
}
upstream backend {
zone upstream_backend 64k;
state /tmp/upstream.conf;
}

match hello {
status 200;
header Content-Type = text/html;
body ~ "Hello";
}

server {
listen 80;
status_zone backend;

location / {
proxy_pass http://backend;
health_check interval=2s match=hello;
}

server {
listen 8080;
root /usr/share/nginx/html;

location /api {
api write=on;
}

location = / {
return 301 /dashboard.html;
}
}
23 changes: 0 additions & 23 deletions consul-api-demo/nginxplus/index.html

This file was deleted.

Binary file removed consul-api-demo/nginxplus/logo.png
Binary file not shown.
11 changes: 5 additions & 6 deletions consul-api-demo/provision.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#!/bin/bash

if [ ${HOST_IP} ]; then
echo "HOST_IP=${HOST_IP}"
echo "HOST_IP=${HOST_IP}"
else
ipaddr=`/sbin/ifconfig eth1 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'`
echo "export HOST_IP=$ipaddr" | tee -a ~/.bash_aliases
. ~/.bash_aliases
/usr/local/bin/docker-compose up -d
ipaddr=$(ip -f inet a show enp0s8 | grep -oP "(?<=inet ).+(?=\/)")
echo "export HOST_IP=$ipaddr" | tee -a ~/.bash_aliases
. ~/.bash_aliases
/usr/local/bin/docker-compose -f /srv/NGINX-Demos/consul-api-demo/docker-compose.yml up -d
fi
Loading

0 comments on commit d9ce1bd

Please sign in to comment.