In recent years, all of us mention about the microservices, try to refer this way in our companies. To refer the microservices, there are plenty of topics to manage our services. They’re authentication, security, logging, cache systems, proxy services, response rate limiting, request size limiting etc. For these plugins, The Kong microservices api gateway is announced by Mashape. All of plugins are ready for those. In addition, you may write individual plugins regarding your scenarios. That’s cool!
Firstly, let’s install the Kong our workspace with Docker. Docker is must installed your computer. On the other hand, i’ll show directly installation of Kong with Docker. There are another systems to install the Kong.
We should create a special Docker network for Kong. I create a network with this command. I’ve defined kong-net name.
docker network create kong-net
To use Kong, we need a database. For this, i use the Postgres.
docker run -d --name kong-database \ --network=kong-net \ -p 5432:5432 \ -e "POSTGRES_USER=kong" \ -e "POSTGRES_DB=kong" \ postgres:9.6
From now on, i could pull Kong Docker image. This image will be regarding kong-net network and postgres database.
docker run --rm \ --network=kong-net \ -e "KONG_DATABASE=postgres" \ -e "KONG_PG_HOST=kong-database" \ -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \ kong:latest kong migrations up
Next step is running our container with this command. Related ports are opened to our local machine. You could see them as below. Then, i’ve set logs files to observe the logs.
docker run -d --name kong \ --network=kong-net \ -e "KONG_DATABASE=postgres" \ -e "KONG_PG_HOST=kong-database" \ -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \ -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \ -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \ -e "KONG_PROXY_ERROR_LOG=/dev/stderr" \ -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \ -e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \ -p 8000:8000 \ -p 8443:8443 \ -p 8001:8001 \ -p 8444:8444 \ kong:latest
As long as everything is successful, you may see Kong result with this command.
curl -i http://localhost:8001/
Firstly, we need to learn services. You could think services like our apis. Each service one api. With this way, We may define service which Kong manages all of them. Well, i’ll create a service as below.
curl -i -X POST \ --url http://localhost:8001/services/ \ --data 'name=mertblog-service' \ --data 'url=https://reqres.in/api/users?page=2'
Before we start making requests against the Service, we will need to add a Route to it. Routes specify how requests are sent to their Services after they reach Kong. A single Service can have many Routes.
curl -i -X POST \ --url http://localhost:8001/services/mertblog-service/routes \ --data 'hosts=mertblog.net'
From now on, Kong is properly forwarding requests to our Service.
We need to talk about plugins. Plugins are masterpiece of the Kong’s art. Because, you are able to choose any one of them regarding your scenario. Thus, the plugin which you’ve chose is adapted to your project easily. They allow you to easily add new features to your Service or make it easier to manage.
We are going to start to do demo. There are a lot of authentication plugins. I would rather tell the key-auth plugin. It’s simplest. You should just send a secret key in the header.
To configure the key-auth plugin for the Service our configured in Kong, issue the following cURL request.
curl -i -X POST \ --url http://localhost:8001/services/mertblog-service/plugins/ \ --data 'name=key-auth'
After this, i check our endpoint and i cannot reach because of that plugin. The status is 401 Unauthorized and there is a message.
We learned how to add plugins to Kong. We’re going to learn how to add consumers to your Kong instances. Consumers are associated to individuals using your Service, and can be used for tracking, access management, and more.
To create a user named Mert the following request.
curl -i -X POST \ --url http://localhost:8001/consumers/ \ --data "username=Mert"
Cool! We’ve a consumer who we need. Now, we should create a key for our consumer Mert.
curl -i -X POST \ --url http://localhost:8001/consumers/Mert/key-auth/ \ --data 'key=this_is_mert_secret_key'
Right now, we are able to check our service. As you see, i’ve added apiKey parameter with value. That’s cool. I could reach it.
In this article, for now, we’ve learned the basics of adding Services, Routes, Consumers and enabling Plugins. Yet, Kong has many more features for us to discover and we may develop our own plugins if we need.