jz22
jz22

Reputation: 2638

Traefik 2.1 does not forward request

I'm would like to use Traefik as load balancer for the backend, which is listening on port 2500. There should be two backend containers that handle requests on port 80. The Traefik dashboard should be available on port 8080. I also want the backend containers to automatically restart when they've crashed.

When I run the docker-compose file below with docker-compose --compatibility up --build, I can access both backends on localhost 2500 and 2501. However, the backends should only be available through Traefik on port 80. Unfortunately, I can't access the backend on port 80. Thanks for your help.

version: "3.4"

services:
  backend:
    deploy:
        replicas: 2
    build: .
    ports:
      - "2500-2501:2500"
    restart: always
    healthcheck:
      test: curl http://127.0.0.1:2500 -s -f -o /dev/null || exit 1
      interval: 10s
      timeout: 10s
      retries: 3
  reverse-proxy:
    image: traefik
    command:
      - --api.insecure=true
      - --providers.docker=true
      - --entrypoints.web.address=:80
    ports:
      - 80:80
      - 8080:8080
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

Upvotes: 1

Views: 737

Answers (1)

Semih Kekül
Semih Kekül

Reputation: 407

The exposed ports are removed from backend service. Added backend router working on 127.0.0.1:80 --> 127.0.0.1:2500 . Also exposed 2500 port for traefik.

version: "3.4"

services:
  backend:
    deploy:
        replicas: 2
    build: .
    #############################################################################
    #ports:
    #  - "2500-2501:2500"
    #############################################################################
    labels:
        traefik.enable: true
        traefik.http.routers.backend.rule: Host(`127.0.0.1`)
        traefik.http.routers.backend.entrypoints: web
        traefik.http.services.backend_service.loadbalancer.server.port: 2500
    #############################################################################        
    restart: always
    healthcheck:
      test: curl http://127.0.0.1:2500 -s -f -o /dev/null || exit 1
      interval: 10s
      timeout: 10s
      retries: 3
  reverse-proxy:
    image: traefik
    command:
      - --api.insecure=true
      - --providers.docker=true
      - --entrypoints.web.address=:80
    ports:
      - 80:80
      - 8080:8080
    #############################################################################
      - 2500:2500
    #############################################################################
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

Upvotes: 1

Related Questions