Thursday, February 23, 2023

Getting Started with KrakenD API Gateway on Docker

 If you want to develop an application in cloud environment and want to implement micro-services architecture pattern then sooner or later you will face with API Gateways. There are many API Gateway outside that we can use it and in terms of open source such as Kong, KrakenD, Tyk. In this session, I will use KrakenD API Gateway on Docker to implement API Gateway. As an example, I will use https://jsonplaceholder.typicode.com for accessing data through KrakenD as API Gateway.  

For KrakenD documentation, you can read it from here

For JSONPlaceholder guide, you can read it from here

 

Configuration KrakenD Gateway

To configure KrakenD json gateway can be done by using KrakenD designer. You can design and download the file configuration easily by online.


KrakenD API Gateway designer


Here is some endpoint that I have made it through KrakenD API gateway designer


 Endpoint in KrakenD designer

 

 

Route Mapping

 I download it the file and rename it with krakend.json shown as below


{
  "$schema": "https://www.krakend.io/schema/v3.json",
  "name": "KrakenD-API Gateway",
  "version": 3,
  "timeout": "3000ms",
  "cache_ttl": "300s",
  "port": 8080,
  "endpoints": [
    {
      "endpoint": "/v1/api/posts",
      "output_encoding": "no-op",
      "backend": [
        {
          "host": [
              "https://jsonplaceholder.typicode.com"
          ],
          "url_pattern": "/posts",
          "is_collection": "true",
          "encoding": "no-op"
        }
      ]
    },
    {
      "endpoint": "/v1/api/posts/{userId}",
      "output_encoding": "no-op",
      "backend": [
        {
          "host": [
              "https://jsonplaceholder.typicode.com"
          ],
          "url_pattern": "/posts/{userId}",
          "encoding": "no-op"
        }
      ]
    }
  ],
  "extra_config": {
    "github_com/devopsfaith/krakend-cors": {
      "allow_methods": [
        "GET",
        "HEAD",
        "POST",
        "PUT",
        "DELETE",
        "CONNECT",
        "OPTIONS",
        "TRACE",
        "PATCH"
      ]
    }
  },
  "output_encoding": "no-op"
}


Installing Docker

Installing docker on Windows is easy, you just downloaded the binary and install it by following the instruction. I have installed Docker Desktop on my windows and it is including docker compose. 

My Docker version shown as below:

C:\>docker --version
Docker version 20.10.22, build 3a2c30b

C:\>docker-compose version
Docker Compose version v2.15.1

 

Dockerfile as below


FROM devopsfaith/krakend:2.0.5
COPY krakend.json /etc/krakend/krakend.json



Implementing

Create a new folder with name krakend then copy Dockerfile and krakend.json into that folder. 

From folder krakend, run this command:

 

docker build . -t api-gateway-v1


Result docker build

 

Make sure your port setting is the same as krakend.json

 docker run -it -p "8080:8080" api-gateway-v1


Result docker run


Docker desktop will create the container and images as shown as below

 

 




Testing

To check KrakenD is running well, it can be done by inputting 

http://localhost:8080/__health 

to your browser, shown as below

 


KrakenD is running well


To check the endpoint KrakenD, it can be done in browser by inputting 

http://localhost:8080/v1/api/posts

 



 To check another endpoint by inputting 

http://localhost:8080/v1/api/posts/1

 

 

Summary

I think KrakenD has bright future. All features that you need in API Gateway is covered by KrakenD. If you have any problem in implementing KrakenD, they have very good community to support you. I hope this article will help you in implementing KrakenD. Thank you

 

 

No comments:

Post a Comment