I have to showcase a load-balancer demo using docker containers of my spring-boot simple rest Api

  docker, haproxy, java, load-balancing, spring-boot

So I wrote a simple spring boot rest app. Now I need to create several containers of it and simulate a delay in some so that other’s can be accessed using load-balancer (programming team recruitment task). I feel like all should be happening locally but I might be wrong. I’m a beginner programmer.

here is what I tried in docker-compose

version: '3.3'

networks:
  frontend:
    driver: bridge
  backend:
    driver: bridge

services:
  haproxy:
    build:
      context: ./
      dockerfile: HAProxyDockerfile
    container_name: haproxy
    restart: always
    ports:
      - 80:80
    depends_on:
      - web1_app
      - web2_app
    networks:
      - frontend
      - backend

  database:
    image: mysql:5.7
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=pass123
      - MYSQL_USER=user
      - MYSQL_PASSWORD=pass123
      - MYSQL_DATABASE=currencyconverterdb
    networks:
      - backend
      - frontend

  web1_app:
    image: spring-boot-converter
    container_name: web1_app
    ports:
      - 8082:8080
    build:
      context: ./
      dockerfile: Dockerfile
    depends_on:
      - database
    environment:
      - DATABASE_HOST=database
      - DATABASE_USER=user
      - DATABASE_PASSWORD=pass123
      - DATABASE_NAME=currencyconverterdb
      - DATABASE_PORT=3306
    networks:
      - frontend
      - backend

  web2_app:
    image: spring-boot-converter
    container_name: web2_app
    ports:
      - 8081:8080
    build:
      context: ./
      dockerfile: Dockerfile
    depends_on:
      - database
    environment:
      - DATABASE_HOST=database
      - DATABASE_USER=user
      - DATABASE_PASSWORD=pass123
      - DATABASE_NAME=currencyconverterdb
      - DATABASE_PORT=3306
    networks:
      - frontend
      - backend

Here is how I build the HAProxy container

FROM haproxy:1.8

RUN apt-get update && apt-get install -y 
    inetutils-ping 
    curl && 
    mkdir /srv/logs && 
    touch /srv/logs/haproxy

COPY ./haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg

and here is what I write in haproxy.cfg

global
    daemon
    log /srv/logs/haproxy local0
    log /srv/logs/haproxy local1 notice

defaults
    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms
    option http-keep-alive

frontend http-in

        bind :80

        # springboot 1
        acl host_web1 hdr(host) -i 127.0.0.1
        use_backend web1_be if host_web1

backend web1_be

    http-request set-header X-Forwarded-Port %[dst_port]
    http-request add-header X-Forwarded-Proto https if { ssl_fc }
    server node1 web1_app
    server node1 web2_app

When i access localhost:80 I get 503 message with "No server is available to handle this request."

Would be thankful for any advices / solutions. I only got one day left.

Source: Docker Questions

LEAVE A COMMENT