How to Install Kong API Gateway on AWS EC2
- What is Kong
Kong is an open-source API gateway and microservices management layer that enables developers to create and manage API endpoints for their applications. It acts as a middleware layer between backend services and client applications, providing features such as authentication, rate limiting, load balancing, and monitoring. Kong is designed to be highly scalable and customizable, with a plugin architecture that allows developers to extend its functionality to meet their specific requirements. It can be deployed on-premises or in the cloud and supports a variety of deployment configurations, including Kubernetes.
2. Use of Kong API Gateway and its advantages
There are several reasons why developers choose to use Kong as their API gateway and microservices management layer:
- Scalability: Kong is designed to handle high volumes of traffic and can scale horizontally to accommodate growing demands. This makes it a good choice for applications that need to handle a large number of API requests.
- Security: Kong provides several security features out of the box, such as authentication and authorization, rate limiting, and encryption. This helps to ensure that API endpoints are secure and protected from unauthorized access.
- Customization: Kong’s plugin architecture allows developers to customize its functionality to meet their specific requirements. This means that developers can add new features or modify existing ones to suit their needs.
- Monitoring: Kong provides built-in monitoring capabilities that allow developers to track API usage and performance in real-time. This can help identify issues and improve the overall performance of the application.
- Integration: Kong integrates with a wide range of tools and services, including Kubernetes, Prometheus, and Grafana. This makes it easy to incorporate Kong into existing development workflows and toolchains.
Overall, the advantages of using Kong include improved scalability, security, customization, monitoring, and integration capabilities for APIs and microservices.
3. Now Login into AWS account and Launch AWS Ubuntu Instance I am using Ubuntu 22.04 version for this demo
4. In Security Group Open Following Port
5. Now Launch you EC2 and Login to that system
6. Before you start, connect via SSH to your server. Once you have done that, update the system:
sudo apt update
sudo apt upgrade
7. 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.
curl -Lo kong-enterprise-edition-3.2.2.1.amd64.deb "https://download.konghq.com/gateway-3.x-ubuntu-$(lsb_release -sc)/pool/all/k/kong-enterprise-edition/kong-enterprise-edition_3.2.2.1_amd64.deb"
8. Now install the package using APT:
sudo apt install -y ./kong-enterprise-edition-3.2.2.1.amd64.deb
9. To check that Kong is installed correctly, you can run
kong roar
10. Kong can be run with a database driver such as PostgreSQL. This is most recommended in production environments. So install it by running:
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt update
sudo apt install postgresql-14
psql --version
11. Then, you have to access the PostgreSQL console. To achieve this, change the session to user postgres.
sudo -i -u postgres
psql
12. 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';
13. Now enable the configuration, copying the sample:
sudo cp /etc/kong/kong.conf.default /etc/kong/kong.conf
14. And now you have to edit the configuration file to set the PostgreSQL values without problems.
sudo vi /etc/kong/kong.conf
#Uncomment this line => #admin_listen = 0.0.0.0:8001 reuseport backlog=16384, 0.0.0.0:8444 http2 ssl reuseport backlog=16384
#add these line on botton "Shift+G" =>
database = postgres
pg_host = 127.0.0.1
pg_port = 5432
pg_user = kong
pg_password = pss
pg_database = kong
:wq
15. Then, export the Kong password to the system:
export KONG_PASSWORD=pss
16. And perform the data migration:
sudo kong migrations bootstrap -c /etc/kong/kong.conf
17. After this, start Kong.
sudo kong start -c /etc/kong/kong.conf
18. If you want to test if Kong is working, you can use curl. or you can hit public ip with port
curl -i localhost:8001/status