How to setup Kong API Gateway and access multiple APIS in Kubernetes

Madhavi Weerasekara
3 min readJun 15, 2020

--

This provides complete, step by step guide on how to setup Kong API gateway(DB-less) in kubernetes and how to access multiple micro-services in one application domain through Kong.

What is Kong API Gateway ?

Kong is one of the most popular, cloud-native, fast, scalable, and distributed Microservice Abstraction Layer (also known as an API Gateway or API Middleware) that capable of handling load balancing, logging, authentication, rate-limiting, transformations, and more through plugins.

Kong used a database to store its configured entities such as Routes, Services and Plugins up to version 1.1. But From version 1.1 on words kong has capability of running without database, using only in-memory storage for entities which is called as DB-less mode. Here it uses declarative configuration to deal with he configuration of entities. The advantage of Kong DB less mode and declarative configuration can be found here

Hope you got heads up on Kong API Gateway. Lets get brief introduction on kubernetes as well.

What is Kubernetes ?

kubernetes is a open source container orchestration platform which can manage workloads and services. It facilitate declarative configuration and automation as well. In simple term kubernetes automate many of the process involved in container deployment, scaling and load balancing. There are plenty of resources to know about kubernete[1],[2].

Kong can be used as a gateway to access microservices/APIs in kubernetes.

Minikube

Minikube is a open source tool that enable us to run a single-node Kubernetes cluster inside a Virtual Machine (VM) on your laptop. This is an ideal tool to use for for people who start learning kubernetes and who like to do experiments.

In this guide/tutorial I will be using minikube to demonstrate how Kong API gateway works with kubenetes.

Let’s start getting our hands dirty with Kong and Kubernetes. In this tutorial we will be using Kong community edition.

1. Install minikube and create kubernetes cluster

First we need to install minikube and Kubernetes command line tool on your matching. Following are the complete references that I followed when installing above

  • Minikube installation guide can be found here.
  • Kubernetes command line tool can be found here

After installing both lets create minikube cluster.

minikube start

2. Install Kong (DB less) in kubernetes

To deploy Kong via kubectl, use:

kubectl apply -f https://bit.ly/kong-ingress-dbless

This will successfully install Kong on kubernetes and will deploy Kong Ingress Controller. Lets verify whether the Kong is accessible

$ export PROXY_IP=$(minikube service -n kong kong-proxy --url | head -1)

$ curl -i $PROXY_IP
HTTP/1.1 404 Not Found
Date: Fri, 15Jun 2020 17:01:07 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Content-Length: 48
Server: kong/2.0.4

{"message":"no Route matched with those values"}

Here Kong returns back a HTTP 404 Not Found because we have not set up how Kong should proxy the requests yet.

You can checkout this configuration file to install Kong with Kong Admin API set up.

3. Create Simple API on kubenetes

Lets create simple NodeJs API. You can checkout the code for crating Node JS API from here. After checking out the code you can build the docker image.we are tring to deploy locally built docker image.

First we need to set the environment variable using eval because minikube docker daemon and actual docker daemon on your machine are different.

$ eval $(minikube docker-env)
$ docker build -t project-service .

Now lets try to deploy this in kubernetes using following manifest file.

Copy above configuration in to manifest.yml file and run following command to create the deployment

kubectl create -f manifest.yml

You can verify the deployment details from the minikube dashboard.

But you will not be able to access the API until ingress rule is created.

$ curl -i $PROXY_IP/projectHTTP/1.1 404 Not Found
Date: Fri, 15Jun 2020 17:01:07 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Content-Length: 48
Server: kong/2.0.4

{"message":"no Route matched with those values"}

4. Access API through Kong

To access the newly created API, we need to create ingress rule.

Copy above configuration in to ingress.yml file and run following command to create the deployment

kubectl create -f ingress.yml

Cheres. Now you can access the API through Kong

$ curl -i $PROXY_IP/project
<h1>Hello Project</h1>

5. Access Multiple APIS through Kong

We can deploy multiple APIs and include ingress rule to access them through Kong API gateway

References

  1. https://kubernetes.io/docs/concepts/
  2. https://kubernetes.io/docs/tutorials/kubernetes-basics/
  3. https://konghq.com/kong/
  4. https://github.com/Kong/kubernetes-ingress-controller/tree/master/docs/deployment#kong-for-kubernetes
  5. https://itnext.io/kong-proof-of-concept-using-minikube-5f8ca932083f

--

--

Madhavi Weerasekara

Software Engineer | Computer Science and Engineering Graduate @ University Of Moratuwa