Introducing and installing Kong as the Gateway for System Microservices
- Tram Ho
In this article, I will introduce the Kong API Gateway and run the Kong API Gateway on Docker.
Why is Kong?
Introduce
Kong is an open-source API Gateway and platform, Kong is written in Lua language and built on NGINX. Kong supports many plugins to make microservices easier to implement, such as authentication, rate-limiting, transformation, logging, etc. I can write plugins for Kong by Lua depending on my needs.
Advantages of Kong
Easy scalability
Kong server is stateless, we can add or delete as many nodes as we want, as long as they point to a datastores. Kong Datastore can choose 1 of 2 types of DB
- Postgres : when it comes to building a centralized, simple, efficient Api Gateway system. By default, Kong will use Postgres as a datastore.
- Cassandra : Using cassandra when building a distributed gateway api system, high availability, good load, easy scaling. Cassandra will run best on heavily configured servers.
Great performance
In tests performance, Kong is one of the most efficient API Gateway, it can handle a lot of requests / s.
Many plugins
Kong supports a wide range of plugins depending on functions such as authen, logging, traffic control, analytics & monitoring … to help manage and track microservices more effectively and easily instead of just taking care of each routing requests.
Free
Kong has two versions, Community and Enterprise. The Enterprise edition supports more things like Kong Admin GUI, online support, using Plugins Enterprises and of course it takes money, lots of money is different. However, with the need to use normally, the free version of it is available for use, and we can use Konga to manage and configure Kong with GUI instead of sending requests to Kong Admin (Kong Commnity does not have GUI, only Enterprise version available)
Setting
Kong supports a variety of platforms to be installed such as Ubuntu, Centos, Red Hat, Kubernetes, Docker … more specifically, you can go here to download the version that suits you.
Kong on Docker
The installation of Kong has very detailed and detailed instructions on the homepage, we will have to install Kong Server
, Kong Datastore
, Konga
to manage Kong, Database
for Konga, quite a lot of things, to make it easier for all I will provide “Instant Kong” on Docker
using Docker
-compose. All you need is a computer with Docker
, Docker
docker-compose
and internet installed.
docker-compose.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | version: "3" networks: kong-net: driver: bridge services: ####################################### # Postgres: The database used by Kong ####################################### kong-database: image: postgres: 9.6 restart: always networks: - kong-net environment: POSTGRES_USER: kong POSTGRES_DB: kong ports: - "5432: 5432" healthcheck: test: ["CMD", "pg_isready", "-U", "kong"] interval: 5s timeout: 5s retries: 5 ####################################### # Kong database migration ####################################### migration-migration: image: kong: latest command: "kong migrations bootstrap" networks: - kong-net restart: on-failure environment: KONG_PG_HOST: kong-database links: - kong-database depends_on: - kong-database ####################################### # Kong: The API Gateway ####################################### kong: image: kong: latest restart: always networks: - kong-net environment: KONG_PG_HOST: kong-database KONG_DATABASE: postgres KONG_PROXY_LISTEN: 0.0.0.0:8000 KONG_PROXY_LISTEN_SSL: 0.0.0.0:8443 KONG_ADMIN_LISTEN: 0.0.0.0:8001 depends_on: - kong-migration - kong-database healthcheck: test: ["CMD", "curl", "-f", "http: // kong: 8001"] interval: 5s timeout: 2s retries: 15 ports: - "8001: 8001" - "8000: 8000" ####################################### # Konga database prepare ####################################### konga-prepare: image: pantsel / konga: next command: "-c prepare -a postgres -u postgresql: // kong @ kong-database: 5432 / konga_db" networks: - kong-net restart: on-failure links: - kong-database depends_on: - kong-database ####################################### # Konga: Kong GUI ####################################### konga: image: pantsel / konga: next restart: always networks: - kong-net environment: DB_ADAPTER: postgres DB_HOST: kong-database DB_USER: kong TOKEN_SECRET: km1GUr4RkcQD7DewhJPNXrCuZwcKmqjb DB_DATABASE: konga_db NODE_ENV: production depends_on: - kong-database ports: - "1337: 1337" |
Explain the services used in docker-compose file and how to customize it
- kong-database
- Overview: Database is used as a datastore for Kong.
- Details: This service uses
Postgre
Version 9.6 from docker hub, is used as a datastore for Kong Server.- Variant environment:
- POSTGRES_USER: database user name (default: kong).
- POSTGRES_DB: database name (default: kong).
- POSTGRES_PASSWORD: database user password (default: kong).
- Variant environment:
- kong
- Overview: Kong Server.
- Details: This service uses
Kong Comunity Edittion
latestKong Comunity Edittion
image from docker hub (linux alpine version) https://hub.docker.com/_/kong - Variant environment:
- KONG_DATABASE:
postgres
/cassandra
– config database used by Kong, here we usepostgres
. - KONG_PG_HOST: Postgres database address.
- KONG_PG_PASSWORD: Postgres database user password.
- KONG_PROXY_LISTEN: The address to which we will send
http
requests to Kong for processing and routing (default: 0.0.0.0:8000, on the production environment using 0.0.0.0:80). - KONG_PROXY_LISTEN_SSL: Address to which we will send
https
requests to Kong for processing and routing (default: 0.0.0.0:8443, on the production environment using 0.0.0.0:443 - KONG_ADMIN_LISTEN: The address we will use to manage Kong through sending a request or using Konga. – konga
- Overview: Konga GUI.
- Detail: This service uses
konga
image from dockerhub to provide a monitoring and management interface for Kong, instead of a manual request to Kong Admin.
- KONG_DATABASE:
- kong-migration
- Overview: Migrate database for Kong can use – konga-prepare
- Overview db Prepare db to use for Konga
Install Kong
Clone file docker-compose on
1 | git clone https://github.com/huyhoc1310/kong-docker-ce.git |
Change directory to directory containing docker-compose file
1 | cd kong-docker-ce |
Run docker-compose
1 | docker-compose up --build |
We will wait a while for the services to be run, check to see if Kong has been successfully run by sending a request to Kong Admin.
1 | curl -i http: // localhost: 8001 |
Next we go to Konga (port 1337) to start using Kong using the browser and go to http://localhost:1337
. Konga will ask us to create an Admin account to use
After creating the account and logging in, the first thing we need to do is to connect Konga to Kong Server via Kong Admin by filling in the form, with the Kong admin url being the address of Kong admin, in this case We will have to use the local network address of the device we are using instead of localhost
because Konga is running in Docker container. To check the address of the machine, we can use the ifconfig
command (with Unix, installed nettools). My device is 192.168.5.14
After creating the connection, we were able to manage the Kong server from Konga.
summary
In this article, I have introduced a very popular and powerful API Gateway now that is the Kong API Gateway and how to install it to run on Docker. The next article will guide you how to configure and use Kong to manage the system microservices effectively.
Source : viblo