Using KrakenD with local nodejs server

1.8k Views Asked by At

I have a up and running nodejs server (with one API) on my local machine .

I have created the new docker container for krakend using

docker run -p 8080:8080 -v $PWD:/etc/krakend/ devopsfaith/krakend run --config /etc/krakend/krakend.json 

Although, I have to make some changes in above command because I am working on windows.

I have created a krakend.json file and it's content are

{
  "version": 3,
  "timeout": "3s",
  "cache_ttl": "300s",
  "port": 8080,
  "default_hosts": ["http://localhost:3001"],
  "endpoints": [
    {
      "endpoint": "/contacts",
      "output_encoding": "json",
      "extra_config": {
        "qos/ratelimit/router": {
          "max_rate": 5000
        }
      },
      "backend": [
        {
          "host": [
          "http://localhost:3001", 
          "http://cotacts:3001"
          ],
          "url_pattern": "/contacts",
          "is_collection": "true",
          "encoding": "json",
          "extra_config": {
            "backend/http": {
              "return_error_details": "backend_alias"
            }
          }
        }
      ]
    }
  ]
}

But when I am hitting the url http://localhost:8080/contacts using postman I am getting

[KRAKEND] 2022/03/14 - 07:26:30.305 ▶ ERROR [ENDPOINT: /contacts] Get "http://localhost:3001/contacts": dial tcp 127.0.0.1:3001: connect: connection refused

I found a relevant one over here connection refused error with Krakend api-gateway?

but, I am not getting what to change in my case

1

There are 1 best solutions below

2
On

Inside the backend you have two hosts in the load balancer. KrakenD will try one and the other in a round-robin fashion.

"host": [
          "http://localhost:3001", 
          "http://cotacts:3001"
          ],

If you have started KrakenD as you have written in your message, then neither of the names are available.

  1. localhost is KrakenD itself (not the host machine starting KrakenD). KrakenD does not have any port 3001 so it's expected that it cannot connect. You should write your host IP.
  2. I am guessing cotacts:3001 is some outside service. If you need to access this service by name you need to use it through a docker compose.

The problem you have is Docker connectivity, and is not related to KrakenD. KrakenD is just complaining that it cannot connect to those services.

Finally, the "default_hosts" is something that it does not exist in KrakenD, it makes no effect in the configuration, you can delete that line. If you want to have a default host without needing to declare it in every backend use just host. In summary, your configuration should look like:

{
    "$schema": "https://www.krakend.io/schema/v3.json",
    "version": 3,
    "timeout": "3s",
    "cache_ttl": "300s",
    "port": 8080,
    "host": [
        "http://1.2.3.4:3001"
    ],
    "endpoints": [
        {
            "endpoint": "/contacts",
            "extra_config": {
                "qos/ratelimit/router": {
                    "max_rate": 5000
                }
            },
            "backend": [
                {
                    "url_pattern": "/contacts",
                    "is_collection": "true",
                    "extra_config": {
                        "backend/http": {
                            "return_error_details": "backend_alias"
                        }
                    }
                }
            ]
        }
    ]
}

And replace 1.2.3.4 with the IP of the machine running the Node.