Skip to content

Commit

Permalink
Final state based on blog entry
Browse files Browse the repository at this point in the history
If following blog at "https://www.nginx.com/blog/nginx-tutorial-deploy-configure-microservices/" starting from branch mm23-twelve-factor-start this is the final state
  • Loading branch information
4141done committed Oct 5, 2023
1 parent 56aa89e commit a9759ae
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 0 deletions.
10 changes: 10 additions & 0 deletions consul/client.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"node_name": "consul-client",
"data_dir": "/consul/data",
"retry_join": [
"consul-server"
],
"addresses": {
"http": "0.0.0.0"
}
}
12 changes: 12 additions & 0 deletions consul/server.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"node_name": "consul-server",
"server": true,
"bootstrap": true,
"ui_config": {
"enabled": true
},
"data_dir": "/consul/data",
"addresses": {
"http": "0.0.0.0"
}
}
24 changes: 24 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@ services:
networks:
- mm_2023

consul-server:
image: hashicorp/consul:1.14.4
container_name: consul-server
restart: always
volumes:
- ./consul/server.json:/consul/config/server.json:ro
ports:
- "8500:8500"
- "8600:8600/tcp"
- "8600:8600/udp"
command: "agent"
networks:
- mm_2023

consul-client:
image: hashicorp/consul:1.14.4
container_name: consul-client
restart: always
volumes:
- ./consul/client.json:/consul/config/client.json:ro
command: "agent"
networks:
- mm_2023

volumes:
rabbit-data:
rabbit-log:
Expand Down
30 changes: 30 additions & 0 deletions load_balancer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
FROM nginx:1.23.1

ARG CONSUL_TEMPLATE_VERSION=0.30.0

# Set an environment variable for the location of the Consul
# cluster. By default, it tries to resolve to consul-client:8500
# which is the behavior if Consul is running as a container in the
# same host and linked to this NGINX container (with the alias
# consul, of course). But this environment variable can also be
# overridden as the container starts if we want to resolve to
# another address.

ENV CONSUL_URL consul-client:8500

# Download the specified version of Consul template
ADD https://releases.hashicorp.com/consul-template/${CONSUL_TEMPLATE_VERSION}/consul-template_${CONSUL_TEMPLATE_VERSION}_linux_amd64.zip /tmp

RUN apt-get update \
&& apt-get install -y --no-install-recommends dumb-init unzip \
&& unzip /tmp/consul-template_${CONSUL_TEMPLATE_VERSION}_linux_amd64.zip -d /usr/local/bin \
&& rm -rf /tmp/consul-template_${CONSUL_TEMPLATE_VERSION}_linux_amd64.zip

COPY consul-template-config.hcl ./consul-template-config.hcl
COPY nginx.ctmpl /usr/templates/nginx.ctmpl

EXPOSE 8085

STOPSIGNAL SIGQUIT

CMD ["dumb-init", "consul-template", "-config=consul-template-config.hcl"]
15 changes: 15 additions & 0 deletions load_balancer/consul-template-config.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
consul {
address = "consul-client:8500"

retry {
enabled = true
attempts = 12
backoff = "250ms"
}
}
template {
source = "/usr/templates/nginx.ctmpl"
destination = "/etc/nginx/conf.d/default.conf"
perms = 0600
command = "if [ -e /var/run/nginx.pid ]; then nginx -s reload; else nginx; fi"
}
15 changes: 15 additions & 0 deletions load_balancer/nginx.ctmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
upstream messenger_service {
{{- range service "messenger" }}
server {{ .Address }}:{{ .Port }};
{{- end }}
}

server {
listen 8085;
server_name localhost;

location / {
proxy_pass http://messenger_service;
add_header Upstream-Host $upstream_addr;
}
}

0 comments on commit a9759ae

Please sign in to comment.