Showing posts with label Kong. Show all posts
Showing posts with label Kong. Show all posts

Wednesday, July 1, 2020

How to increase a Kong timeout with no down time?

I have a Kong API gateway deployment with several services. The default Kong timeout is set to 1 mins, which fails several of our API requests. Now I want to change them to 10 mins, without going through too much trouble. What should I do? I found this nice tool called Deck, thanks to a blogpost.

The default status of a service configuration
First, install Deck.

Then, we test backup, alter, and restore Kong configurations.

To test, let's create a service definition.
$ curl -i -X POST --url http://localhost:8001/services/ --data 'name=v4' --data 'url=http://172.20.11.223:9099/services/v4'

Check the services via a browser (or a curl command, if you prefer it that way).

http://localhost:8001/services

Notice the timeouts are 60,000. This is 60,000 ms. i.e., 1 min by default. Let's increase it to 10 min, with Deck!

Steps:

1. Dump the configuration:


$ deck dump

This creates a kong.yaml in the current directory.

$ cat kong.yaml
_format_version: "1.1"
services:
- connect_timeout: 60000
  host: 172.20.11.223
  name: v4
  path: /services/v4
  port: 9099
  protocol: http
  read_timeout: 60000
  retries: 5
  write_timeout: 60000



  Modify the connect_timeout, read_timeout, and write_timeout as 600000 (from their default value of 60000) via a text editor.

2.  Check the changes:


  $ deck diff

updating service v4  {
-  "connect_timeout": 60000,
+  "connect_timeout": 600000,
-  "created_at": 1.593636546e+09,
   "host": "172.20.11.223",
   "id": "5c25f92f-d3d0-486f-9a8e-9c406a130501",
   "name": "v4",
   "path": "/services/v4",
   "port": 9099,
   "protocol": "http",
-  "read_timeout": 60000,
+  "read_timeout": 600000,
   "retries": 5,
-  "updated_at": 1.593636546e+09,
-  "write_timeout": 60000
+  "write_timeout": 600000
 }

Summary:
  Created: 0
  Updated: 1
  Deleted: 0


3. Commit the changes:


$ deck sync

Confirm the changes are reflected, from the browser. Yep, it worked. Everything updated with zero downtime.

Timeouts are set to 10 mins (600,000) now, from 1 mins!

Tuesday, September 11, 2018

Configuring Kong API Gateway for your Service Endpoints

Kong provides a complete documentation on its installation and a quick-start guide to start using it, such as configuring a service in Kong

In this post we will briefly look into configuring Kong using its Docker container as an API gateway for your backend services. This document uses Kong version 0.14.0-alpine, which is the current latest version of Kong. Kong can be configured with Postgres or Cassandra backend for its persistent storage. Here we will configure Kong with Postgres.

Install and Start Kong with Postgres

You have 2 options. If you want to get it done quick, I recommend choosing the option #2 - installing via Docker containers.

1. Download Kong's native installation for your operating system

Download and install Kong for your respective operating system 

Configure with Postgres:
$ psql -U postgres
postgres=# CREATE USER kong; CREATE DATABASE kong OWNER kong;


Run the Kong migrations:
$ kong migrations up

Start Kong
$ kong start

You may choose to start with verbose logs:
$ kong start -vv

You may need to create a kong configuration file to load Kong with custom configurations:
$ sudo mkdir /etc/kong

$ sudo touch /etc/kong/kong.conf

Now your Kong is running. Confirm that by,

 

2.  Install Kong via Docker containers.

We have our own "kong-ldap" repository with scripts that will install and configure Kong with Postgres in a container. 

$ cd kong-ldap

$ sh buildRun.sh

Optionally, you may also use these scripts to configure OpenDJ LDAP directory as a Docker container a well, connected to Docker. However, the commands are commented out - therefore you will need to uncomment the relevant commands in the script buildRun script to get it working.

Kong has two interfaces. One is the user-facing interface and the other one is the admin interface. The admin interface by default listens to the port 8001 and should not be exposed to the public. The admin interface is used by the administrators to create and configure the routes to the services in the Kong API gateway. The user-facing interface by default listens to the port 8000. It is exposed to the public, and the users can consume the services defined by the admin previously using the user-facing interface. The user-facing interface is completely separated by Kong from the admin interface.

Configure Kong for your Services

Configuring Kong as an API gateway for your services is a 2-step procedure, starting from Kong 0.14. Previous versions of Kong provided a unified approach through its "api" objects, which is current depreciated and replaced by two entities known as "services" and "routes".

Make sure that you have Kong up and running. Then,  execute the below two commands from the server that hosts Kong.

First, you need to define a "service" in Kong to each of your service/API groups (i.e., your backend applications or web services).


1) Create a Kong Service using the Kong Admin API

$ curl -i -X POST --url http://localhost:8001/services/ --data 'name=radiology' --data 'url=http://172.20.11.223:9099/services/v4/TCIA/query/'

Above, we assume your web application is hosted in http://172.20.11.223:9099/  and it has a set of services under the base path /services/v4/TCIA/query/. We pass the complete url of the backend of the original service deployments through the "url" flag, as shown above. We create a service named "radiology" in Kong for these services, using the Kong admin interface for the creation of services. Here, we assume Kong is hosted in localhost, with its admin interface using the default port option of 8001. /services/ let you create/modify the service definitions in Kong.


Second, you should add a "route" to the "radiology" service that you created.

2) Add a Route to the Kong Service

$ curl -i -X POST --url http://localhost:8001/services/radiology/routes --data 'paths=/radiology'

 As shown above, we use the KONG-ADMIN-INTERFACE/services/SERVICE-NAME/routes to configure the routes to the service that we defined above. Here the SERVICE-NAME is "radiology" as we defined above in the previous step. The "paths" flag indicate the paths that Kong should match to the service that we defined in the previous step.


Accessing your services via Kong

Now, you may access your services via Kong
http://172.20.11.222:8000/radiology/getImage
the same way you access it directly
http://172.20.11.223:9099/services/v4/TCIA/query/getImage

Here, we assume that you have deployed Kong in a server with a public address http://172.20.11.222 and your services are in http://172.20.11.223.

You may of course do additional tasks such as rate limiting, request/response transformations, logging, and analytics with the Kong API Gateway. You may refer to the Kong documentation on pointers to achieving this.

Thursday, May 12, 2016

Configuring Kong with Cassandra

Install Kong

Install Cassandra

You may need to install Cassandra Driver

pip install --pre cassandra-driver

pip install --pre --upgrade cassandra-driver


Kong does not work with apache-cassandra-3.x as of now.

root@llovizna:/usr/bin# kong start
[INFO] kong 0.8.1
[INFO] Using configuration: /etc/kong/kong.yml
[INFO] Setting working directory to /usr/local/kong
[INFO] database...........cassandra contact_points=127.0.0.1:9042 data_centers= ssl=verify=false enabled=false port=9042 timeout=5000 replication_strategy=SimpleStrategy keyspace=kong replication_factor=1 consistency=ONE
[INFO] Leaving cluster..
[ERR] [Invalid] unconfigured table schema_keyspaces
[ERR] Could not start Kong


It does work well with
apache-cassandra-2.2.6

Install and start the kong-dashboard for an interactive GUI.
npm start

Installing Kong from Source on Ubuntu

root@llovizna:/home/pradeeban/programs/kong# make install
/bin/sh: 4: luarocks: not found
make: *** [install] Error 127

root@llovizna:/home/pradeeban/programs/kong# apt-get install luarocks


make install again.

Error: Failed installing dependency: http://luarocks.org/repositories/rocks/lrexlib-pcre-2.7.2-1.src.rock - Could not find expected file pcre.h for PCRE -- you may have to install PCRE in your system and/or pass PCRE_DIR or PCRE_INCDIR to the luarocks command. Example: luarocks install lrexlib-pcre PCRE_DIR=/usr/local
make: *** [install] Error 1

apt-get install libpcre3 libpcre3-dev

Missing dependencies for kong:
lua_uuid ~> 0.2.0-2
..
gcc -O2 -fPIC -I/usr/include/lua5.1 -c lua_uuid.c -o lua_uuid.o
lua_uuid.c:9:23: fatal error: uuid/uuid.h: No such file or directory
 #include
                       ^
compilation terminated.

Error: Failed installing dependency: http://luarocks.org/repositories/rocks/lua_uuid-0.2.0-2.rockspec - Build error: Failed compiling object lua_uuid.o
make: *** [install] Error 1


apt-get install uuid-dev

Now
make install
again, which is successful.

When running make dev.
/usr/bin/env: luajit: No such file or directory
make: *** [dev] Error 127

apt-get install luajit

Modify the
kong/kong.yml
accordingly to configure the Kong execution.