Kong API Gateway 101

Chaiwat Tungtongsoontorn
Tri Petch Digital
Published in
4 min readJun 13, 2022
Kong’s diagrams
Figure a

You may look for tools to manage and split your services to support your applications, but some of the process might get your head spin around the proxy configurations.

How many processes do you have when you deploy some instance into your cluster? You might have tons of processes to initiate your app or your api as the list below.

- Creating new services
- Setting the network configuration
- Mapping the port between egresses and instances
- Mapping sub domains to your services

Here comes, Kong API Gateway is the tool that help you optimize and distribute your microservices. Now this might be your tool that you are looking for.

Before we dive down this content, please allow me to explain the benefits of Kong

  • We now have traffic control between your instance and your network.
  • Our micro services have been more managed.
  • Your applications are now loosely coupled and isolated.
  • Code versioning on proxy configurations (on DB-less mode).
  • One single domain but many services.
  • Popular API gateway which you can ask kong’s community to resolve your issues.

Now we know Kong’s benefits, so let’s dive in and get detail on how Kong’s works. Get your hands dirty!

figure b

We are going to simulate some scenarios to describe how kong’s config in your network. From “figure b” above you can see that many requests are handled via kong’s gateway. And then kong service is the criteria your request where to go to the upstream services. This is because Kong Gateway is a Lua application running in Nginx and distributed with openResty.

First of all, please pull this repository and run it on your local machine.
In Docker network, we created nginx server as upstream services and declare it as Microservice A. This is the same to Microservice B. All of your microservices are now behind private networks which are not public to the internet. The only service which connects to internet is Kong proxy.

From the repository, you might guess the question, “Where can I expose my service through kong gateway?”. Now here they come, other words you need to know. Service and Route objects.

- Service is its URL, where the service listens for requests.- Route is the rule which Kong determines how requests are sent to their services.

To expose your service, you must

  • Add service to kong’s configuration.
  • Add route.

Add your upstream via Curl command

You can add service via Curl to kong’s admin port by this request

curl -i -X POST http://<kong-admin-host>:8001/services \
--data name=your_service_name \
--data url='http://yourdomain.com'

And then you must add a route by this curl.

curl -i -X POST http://<admin-hostname>:8001/services/webA/routes \
--data 'paths[]=/weba' \
--data name=web-a-route

Another way to expose your service via kong.conf file when Kong runs on DBLess mode.

In this repository, there is Kong-UI for managing your kong via GUI. After you run services by Docker. Just go to this url http://localhost:1337 . This url will bring you to Konga, The GUI for kong admin api.

  • step 1) register when you access your konga GUI for the first time.
  • step 2) login to the dashboard which account you just created.
  • step 3) connect to Kong server

Due to this, the repository runs inside Docker environment and it has own dns to communicate each services under the hood. That is why the Kong admin’s url is http://kong:8001 , according to service name kong as docker-compose.yml config has shown.

And then click the button “ACTIVATE”.

  • step 4) add service
    - go to tab SERVICES.
    - click + ADD NEW SERVICE button
    - config as below and then press SUBMIT SERVICE button
  • step 5) add route by this config below then press SUBMIT ROUTE button

Congratulations!, you can access your service through your kong proxy by this url http://localhost:8000/weba . Your response looks like this

You can review and run our repository on your local machine with this Link
https://github.com/NickTriPetch/kong-api-gateway-101

Connect with us on
- https://www.tripetchgroup.com/en/tripetchitsolutions

Special supports from
- K’Somsak: https://medium.com/@soemsak_l

--

--