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/
Services
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.
Plugins
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.
Consumers
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.
To Conclusion
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.
Top comments (5)
Good article! My personal feeling is the better use UI such as Konga to do and learn Kong.
I appreciate that. In fact, my goal is going to the roots of the Kong for next step. As you said, Konga is useful UI.
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 Unable to find image 'kong:latest' locally
latest: Pulling from library/kong
0a6724ff3fcd: Pull complete 274efec6805c: Pull complete 87d5cdce85c4: Pull complete 83d64491ed12: Pull complete Digest: sha256:255db29e37ed7db52d0599252e635e6757480b28ea3b0973d535813fec153297
Status: Downloaded newer image for kong:latest
Error: [PostgreSQL error] failed to retrieve PostgreSQL server_version_num: temporary failure in name resolution
Without a detailed discussion of services this doesn't actually provide value
why i am getting this error