How to install Open Source API Gateway – Kong in Ubuntu 20.04

Without beating around the bush, Kong is an open-source API Gateway written in Lua. It has been built on top of Nginx (taking advantage of the lua-nginx module), which is a very light and flexible web server that exposes a Rest API that can be used to manage our infrastructure in a dynamic way.

Some of its main features are:

  • Modular: Kong can be extended by adding individual components that work as separate modules.
  • Scalable because Kong has been designed to gradually add more and more computers.
  • Kong runs anywhere from cloud or on-premises environments.

So let’s get started.

Install Kong on Ubuntu 20.04

Before you start, connect via SSH to your server. Once you have done that, update the system:

sudo apt update
sudo apt upgrade

Now, we can get started with Kong.

The developers of the application provide a DEB package that we can use to install it on Ubuntu 20.04. So download it using wget.

wget -O kong.deb "https://download.konghq.com/gateway-2.x-ubuntu-focal/pool/all/k/kong/kong_2.8.1_amd64.deb"

Now install the package using APT:

sudo apt install ./kong.deb
Install Kong on Ubuntu 20.04
Install Kong on Ubuntu 20.04

To check that Kong is installed correctly, you can run

kong roar

Sample Output:

Kong installed
Kong installed

So, Kong is now installed, but we need to get it working.

Configuring Kong in Ubuntu 20.04

Kong can be run with a database driver such as PostgreSQL. This is most recommended in production environments. So install it by running:

sudo apt install postgresql-12

Then, you have to access the PostgreSQL console. To achieve this, change the session to user postgres.

sudo -i -u postgres

So, access the console

psql

And create the database, assign it appropriate permissions and set a password:

CREATE USER kong;
CREATE DATABASE kong OWNER kong;
ALTER USER kong with password 'pss';

Remember that these values are referential, and you have to change them to your own.

Now enable the configuration, copying the sample:

sudo cp /etc/kong/kong.conf.default /etc/kong/kong.conf

And now you have to edit the configuration file to set the PostgreSQL values without problems.

sudo nano /etc/kong/kong.conf

In it, you set the PostgreSQL connection settings

database = postgres  
pg_host = 127.0.0.1
pg_port = 5432
pg_user = kong
pg_password = pss
pg_database = kong

Then, export the Kong password to the system:

export KONG_PASSWORD=pss

And perform the data migration:

sudo kong migrations bootstrap -c /etc/kong/kong.conf

Output:

43 migrations processed
43 executed
Database is up-to-date

After this, start Kong.

sudo kong start -c /etc/kong/kong.conf

Output:

kong started

So, that’s it!

If you want to test if Kong is working, you can use curl.

curl -i localhost:8001/status

And you will get an output like this:

Kong is running
Kong is running

Conclusion

Kong is a tool for APIs that is important in many industries with large network infrastructures.

I hope you liked this post and help us to share it with the rest of your friends.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top