How to configure API gateway in microservice architecture inside docker containers

Viewed 535

I am fairly new and was building a microservice using nodejs. My problem is that my API gateway is throwing errors when I start my containers using docker compose. I can send request to my service containers directly and the API's are responding. But when I forward them via API gateway I am having problems. emphasized text

Following are my files in apiGateway directory: Docker file:

FROM devopsfaith/krakend

COPY krakend.json /etc/krakend/

This is a image which is built and placed on github specifically for this gateway.

{
  "version": 2,
  "extra_config": {
    "github_com/devopsfaith/krakend-cors": {
      "allow_origins": ["*"],
      "allow_headers": ["*"],
      "expose_headers": ["Content-Length"],
      "max_age": "12h",
      "allow_methods": ["GET", "HEAD", "POST"]
    }
  },
  "timeout": "3000ms",
  "cache_ttl": "300s",
  "output_encoding": "json",
  "name": "Gateway",
  "port": 3001,
  "endpoints": [
    {
      "endpoint": "/auth/signup-customer",
      "method": "POST",
      "output_encoding": "json",
      "extra_config": {},
      "backend": [
        {
          "url_pattern": "/auth/signup-customer",
          "host": ["http://auth-service:4000"],
          "method": "POST"
        }
      ]
    },
    {
      "endpoint": "/auth/signup-restaurant",
      "method": "POST",
      "output_encoding": "json",
      "extra_config": {},
      "backend": [
        {
          "url_pattern": "/auth/signup-restaurant",
          "host": ["http://auth-service:4000"],
          "method": "POST"
        }
      ]
    },
    {
      "endpoint": "/auth/login",
      "method": "POST",
      "output_encoding": "json",
      "extra_config": {},
      "backend": [
        {
          "url_pattern": "/auth/login",
          "host": ["http://auth-service:4000"],
          "method": "POST"
        }
      ]
    },
    {
      "endpoint": "/auth/check",
      "method": "GET",
      "output_encoding": "json",
      "extra_config": {},
      "backend": [
        {
          "url_pattern": "/auth/check",
          "host": ["http://auth-service:4000"],
          "method": "GET"
        }
      ]
    },
    {
      "endpoint": "/products/_search",
      "method": "POST",
      "output_encoding": "json",
      "extra_config": {},
      "backend": [
        {
          "url_pattern": "/products/_search",
          "host": ["http://elasticsearch:9200"],
          "method": "POST"
        }
      ]
    },
    {
      "endpoint": "/health",
      "method": "GET",
      "output_encoding": "json",
      "extra_config": {
        "github.com/devopsfaith/krakend/proxy": {
          "static": {
            "data": {
              "status": "OK"
            },
            "strategy": "always"
          }
        }
      },
      "backend": [
        {
          "url_pattern": "/fake",
          "encoding": "json",
          "sd": "static",
          "method": "GET",
          "extra_config": {}
        }
      ]
    }
  ]
}

The above code is a json file with my endpoints. Currently I am only test authentication api endpoints.

Auth Service Docker file:

FROM node:14

# Create app directory
WORKDIR /app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 4000
CMD ["npm","start"]

Docker compose file

version: "3"
services:
  api-gateway:
    build: "./gateway"
    ports:
      - 3001:3001

  auth-service:
    build: "./authService"
    ports:
      - 4000:4000
    environment:
      - DB_URL = mongodb://db/MagicMeal
    volumes:
      - ./authService:/app

  item-service:
    build: "./itemService"
    ports:
      - 5000:5000
    environment:
      - DB_URL = mongodb://db/MagicMeal
    volumes:
      - ./itemService:/app

  user-service:
    build: "./userService"
    ports:
      - 7000:7000
    environment:
      - DB_URL = mongodb://db/MagicMeal
    volumes:
      - ./userService:/app
#   db:
#     image: mongo:latest
#     ports:
#       - 27017:27017
#     volumes:
#       - ./MagicMeal:/data/db

# volumes:
#   MagicMeal:

Below is the error image In the image you can see an error that says connection refuse. Now this error comes when I call my api using the following url : "http://localhost:3001/auth/check" as this is my api gateway address which reroutes the request to "localhost:4000/auth/check" .My gateway is on the port 3001 and my aut service on port 4000. But if i simply hit "localhost:4000/auth/check" i get a response. But the same request initiated via gate gives error click here to open the error image

1 Answers

You should put service names and service exposed ports (both defined in docker-comose.yml) in api gateway endpoints configuration. For example, auth-service:4000 instead of localhost:4000.

When you succeed to connect to localhost:4000 you connect to you docker host (your machine, vm) which routes the requests to container directly.

Related