What is an API Gateway?
Its is tool or application or program which sits between a client of your application and your backend services and manages traffics based on your defined rules. It acts as front desk office which is responsible to deal with outside requests and route them to appropriate department/division in your organisation.
There are many performance and security related logics you supposed to implement in your code, but now you can transfer them to API Gateway. For example, rate limiting, URL changes, Data validation in incoming request, authenticating incoming request etc.
There are many API Gateways like Zuul, KrakenD, Tyk and so on, but I found Kong more suitable for my need till now. And the good part of Kong is, It can work without database and can work on Kubernetes clusters.
Kong API Gateway is popular and more preferred API gateway which solves the problem of managing outside traffics for your application. Here are the simple steps how I configured it for nginx backend service.
First: Install
$ helm repo add kong https://charts.konghq.com
$ helm repo update
$ helm install kong/kong --generate-name --set ingressController.installCRDs=false
Second: test if its installed success fully.
In the first step, Kong deployment would create service which will start on 80 port and you can hit the http://localhost on your local system to see some response from kong. ON GKE, Kong will get public IP automatically after success full deployment and you can hit that IP address the check if Kong is running there.
Third: Backend Service
Create and Simple backend service using nginx. Herein the yaml file for that.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
Forth: Create Service for deployed backend service in previous step
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx-service
name: nginx-service
spec:
selector:
app: nginx
type: ClusterIP
ports:
- name: proxy-server-port
port: 8080
targetPort: 80
Fifth: Create Ingress resource and mention kong in annotations as ingress class
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: demo-ingress
annotations:
kubernetes.io/ingress.class: kong
spec:
rules:
- http:
paths:
- path: /
backend:
serviceName: nginx-service
servicePort: 8080