Trouble with Dockerized Microservices Setup: Unable to Reach One Backend Service

57 views
Skip to first unread message

hrai

unread,
Jun 4, 2024, 8:46:29 AM6/4/24
to KrakenD Community

Seeking advice on troubleshooting and resolving an backend connectivity issue. Any suggestions would be appreciated. Thanks in advance

Description: I'm running into an issue with my Dockerized microservices setup. Here's a breakdown:

  • I have two sets of services:
    1. KrakenD API Gateway instances.
    2. Faker services, acting as mock backend services.
  • Each set is running in its Docker container.
  • Additionally, I've set up two NGINX instances:
    • One as a load balancer for KrakenD instances.
    • Another specifically for the Faker services.

Issue: When I send requests to the KrakenD API Gateway through the NGINX load balancer, I receive a 500 Internal Server Error. The response indicates a connection refusal to one of the Faker backend services.

Steps Taken:

  1. Checked container logs for errors.
  2. Tested accessing both Faker services directly with curl, finding one unresponsive.
  3. Reviewed Docker network configuration and NGINX load balancer settings.
  4. Checked KrakenD configuration to ensure proper backend endpoint specification.


Albert Lombarte

unread,
Jun 4, 2024, 9:11:43 AM6/4/24
to KrakenD Community, hrai
Hi,

Could you provide details of the logs you mention and the configuration you are using? A connection refusal might be as simple as KrakenD not being able to reach the IP address (90% of the times people working in a local environment, write "localhost" in the "hosts" configuration but they don't take into account that localhost in KrakenD itself, and the service name is needed instead)

El dia dimarts, 4 de juny del 2024 a les 14:46:29 UTC+2, hrai va escriure:

hrai

unread,
Jun 5, 2024, 1:31:00 AM6/5/24
to KrakenD Community, Albert Lombarte, hrai
sure,
Get "http://faker2:3031/v1/users": dial tcp X.X.X.X:3031: connect: connection refused

Albert Lombarte

unread,
Jun 5, 2024, 3:24:59 AM6/5/24
to KrakenD Community, hrai, Albert Lombarte
You haven't shared any configurations or logs, just one partial line of the log. But in any case, the problem is that KrakenD cannot reach this server, the service is down, or its port is not accessible from KrakenD. It is a networking issue.

El dia dimecres, 5 de juny del 2024 a les 7:31:00 UTC+2, hrai va escriure:

hrai

unread,
Jun 5, 2024, 3:41:17 AM6/5/24
to KrakenD Community, Albert Lombarte, hrai
sorry that was just the response I has received
here are the conf
Dockerfile
----------------------------
FROM devopsfaith/krakend:2.0
COPY krakend.json /etc/krakend/krakend.json
---------------------------------
Docker Compose (docker-compose.yml):
-------------------------------------------
version: '3.8'
services:
  krakend1:
    build: .
    ports:
      - "8081:8080"
    networks:
      - krakend-net
      - faker-net

  krakend2:
    build: .
    ports:
      - "8082:8080"
    networks:
      - krakend-net
      - faker-net

  loadbalancer:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    depends_on:
      - krakend1
      - krakend2
    networks:
      - krakend-net

  fakerloadbalancer:
    image: nginx:latest
    ports:
      - "4040:4040"
    volumes:
      - ./nginx-faker.conf:/etc/nginx/nginx.conf
    depends_on:
      - faker1
      - faker2
    networks:
      - faker-net

  faker1:
    image: dotronglong/faker:stable
    ports:
      - "3030:3030"
    volumes:
      - ./mocks:/app/mocks
    networks:
      - faker-net

  faker2:
    image: dotronglong/faker:stable
    ports:
      - "3031:3030"
    volumes:
      - ./mocks2:/app/mocks
    networks:
      - faker-net

networks:
  krakend-net:
  faker-net:
---------------------------------------------------------
KrakenD Configuration (krakend.json):
-------------------------------------------------------------
{
  "$schema": "https://www.krakend.io/schema/krakend.json",
  "version": 3,
  "name": "KrakenD - API Gateway",
  "timeout": "3000ms",
  "port": 8080,
  "output_encoding": "no-op",
  "extra_config": {
    "router": {
      "return_error_msg": true
    }
  },
  "endpoints": [
    {
      "endpoint": "/api/users",
      "method": "GET",
      "extra_config": {
        "security/cors": {
          "allow_origins": ["*"],
          "allow_methods": ["GET", "HEAD", "POST"],
          "expose_headers": ["Content-Length", "Content-Type"],
          "allow_headers": ["Accept-Language"],
          "max_age": "12h",
          "allow_credentials": false,
          "debug": false
        },
        "github.com/devopsfaith/krakend-ratelimit/juju/router": {
          "max_rate": 2,
          "client_max_rate": 1,
          "strategy": "ip"
        }
      },
      "backend": [
        {
          "url_pattern": "/v1/users",
          "method": "GET",
          "host": [
            "http://fakerloadbalancer:4040"
          ],
          "extra_config": {
            "github.com/devopsfaith/krakend-ratelimit/juju/proxy": {
              "max_rate": 2,
              "every": "1s",
              "capacity": 2
            }
          }
        }
      ]
    }
  ]
}
------------------------------------------------------------------------------
NGINX Configuration for Faker Load Balancer (nginx-faker.conf):
-------------------------------------------------------------------------------
upstream faker_backend {
    server faker1:3030;
    server faker2:3030;
}

server {
    listen 4040;

    location / {
        proxy_pass http://faker_backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
--------------------------------------------------------------------------------------
NGINX Configuration for KrakenD Load Balancer (nginx.conf):
------------------------------------------------------------------------------------
upstream krakend_backend {
    server krakend1:8080;
    server krakend2:8080;
}

server {
    listen 80;

    location / {
        proxy_pass http://krakend_backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
--------------------------------------------------------------------------------------------------
Mock Data (mocks/users.json and mocks2/users.json):
----------------------------------------------------------
{
  "request": {
    "method": "GET",
    "path": "/v1/users"
  },
  "response": {
    "body": [
      { "id": 1, "name": "John" },
      { "id": 2, "name": "Marry" },
      { "source": "faker1" }  
    ]
  }
}

{
  "request": {
    "method": "GET",
    "path": "/v1/users"
  },
  "response": {
    "body": [
      { "id": 1, "name": "John" },
      { "id": 2, "name": "Marry" },
      { "source": "faker2" }  
    ]
  }
}
----------------------------------------------------------------------------------

this is what I am trying to reach
User request -> NGINX load balancer -> KrakenD API Gateway -> Faker load balancer -> Faker service -> Response sent back along the same path.

Albert Lombarte

unread,
Jun 5, 2024, 3:54:22 AM6/5/24
to KrakenD Community, hrai, Albert Lombarte
The faker2 service listens on port 3030, not 3031. The port 3031 is the mapped port in your local host machine, not the port used inside the Docker network. If you change the port to 3030 KrakenD will be able to connect.




El dia dimecres, 5 de juny del 2024 a les 9:41:17 UTC+2, hrai va escriure:

hrai

unread,
Jun 5, 2024, 5:34:54 AM6/5/24
to KrakenD Community, Albert Lombarte, hrai
Thanks it worked
Reply all
Reply to author
Forward
0 new messages