diff --git a/docker-compose.yml b/docker-compose.yml index e1c34f0..501d387 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: diff --git a/load_balancer/Dockerfile b/load_balancer/Dockerfile new file mode 100644 index 0000000..9a398d7 --- /dev/null +++ b/load_balancer/Dockerfile @@ -0,0 +1,16 @@ +FROM nginx:1.23 +ARG CONSUL_TEMPLATE_VERSION=0.30.0 +# we define an environment variable with the location of our Consul cluster. By default, it will try to resolve to +# consul-client:8500 which would be the behavior if we have Consul running as a container in the same host and we link it to this +# Nginx container (with the alias consul, of course). But this environment variable can also be overridden when we run the +# container if we want to point somewhere else. +ENV CONSUL_URL consul-client:8500 + +# download the latest version of Consul Template and we put it on /usr/local/bin +ADD https://releases.hashicorp.com/consul-template/${CONSUL_TEMPLATE_VERSION}/consul-template_${CONSUL_TEMPLATE_VERSION}_linux_amd64.zip /usr/bin/ +RUN unzip /usr/bin/consul-template_${CONSUL_TEMPLATE_VERSION}_linux_amd64.zip && \ + mv consul-template /usr/local/bin/consul-template && \ + rm -rf /usr/bin/consul-template_${CONSUL_TEMPLATE_VERSION}_linux_amd64.zip + + +COPY nginx.conf /etc/nginx/nginx.conf diff --git a/load_balancer/nginx.conf b/load_balancer/nginx.conf new file mode 100644 index 0000000..70bd068 --- /dev/null +++ b/load_balancer/nginx.conf @@ -0,0 +1,41 @@ + +#user nobody; +worker_processes 1; + +events { + worker_connections 1024; +} + +http { + default_type application/json; + + keepalive_timeout 65; + + server { + listen 80; + + location / { + proxy_pass http://localhost:8080; + } + + location /health { + access_log off; + add_header 'Content-Type' 'text/plain'; + return 200 "OK"; + } + } + + server { + listen 8080; + + location / { + return 200 "Hello, world"; + } + + location /health { + access_log off; + add_header 'Content-Type' 'text/plain'; + return 200 "OK"; + } + } +}