Configuring a Service

In this section, you’ll be adding an API to Kong. In order to do this, you’ll first need to add a Service; that is the name Kong uses to refer to the upstream APIs and microservices it manages.

For the purpose of this guide, we’ll create a Service pointing to the Mockbin API. Mockbin is an “echo” type public website which returns the requests it gets back to the requester, as responses. This makes it helpful for learning how Kong proxies your API requests.

Before you can start making requests against the Service, you will need to add a Route to it. Routes specify how (and if) requests are sent to their Services after they reach Kong. A single Service can have many Routes.

After configuring the Service and the Route, you’ll be able to make requests through Kong using them.

Kong exposes a RESTful Admin API on port :8001. Kong’s configuration, including adding Services and Routes, is made via requests on that API.

Before you start

You have installed and started Kong Gateway, either through the Docker quickstart or a more comprehensive installation.

1. Add your Service using the Admin API

Issue the following cURL request to add your first Service (pointing to the Mockbin API) to Kong:

  1. curl -i -X POST \
  2. --url http://localhost:8001/services/ \
  3. --data 'name=example-service' \
  4. --data 'url=http://mockbin.org'

You should receive a response similar to:

  1. HTTP/1.1 201 Created
  2. Content-Type: application/json
  3. Connection: keep-alive
  4. {
  5. "host":"mockbin.org",
  6. "created_at":1519130509,
  7. "connect_timeout":60000,
  8. "id":"92956672-f5ea-4e9a-b096-667bf55bc40c",
  9. "protocol":"http",
  10. "name":"example-service",
  11. "read_timeout":60000,
  12. "port":80,
  13. "path":null,
  14. "updated_at":1519130509,
  15. "retries":5,
  16. "write_timeout":60000
  17. }

2. Add a Route for the Service

  1. curl -i -X POST \
  2. --url http://localhost:8001/services/example-service/routes \
  3. --data 'hosts[]=example.com'

The answer should be similar to:

  1. HTTP/1.1 201 Created
  2. Content-Type: application/json
  3. Connection: keep-alive
  4. {
  5. "created_at":1519131139,
  6. "strip_path":true,
  7. "hosts":[
  8. "example.com"
  9. ],
  10. "preserve_host":false,
  11. "regex_priority":0,
  12. "updated_at":1519131139,
  13. "paths":null,
  14. "service":{
  15. "id":"79d7ee6e-9fc7-4b95-aa3b-61d2e17e7516"
  16. },
  17. "methods":null,
  18. "protocols":[
  19. "http",
  20. "https"
  21. ],
  22. "id":"f9ce2ed7-c06e-4e16-bd5d-3a82daef3f9d"
  23. }

Kong is now aware of your Service and ready to proxy requests.

3. Forward your requests through Kong

Issue the following cURL request to verify that Kong is properly forwarding requests to your Service. Note that by default Kong handles proxy requests on port :8000:

  1. curl -i -X GET \
  2. --url http://localhost:8000/ \
  3. --header 'Host: example.com'

A successful response means Kong is now forwarding requests made to http://localhost:8000 to the url we configured in step #1, and is forwarding the response back to us. Kong knows to do this through the header defined in the above cURL request:

  • Host: <given host>

Next Steps

Now that you’ve added your Service to Kong, let’s learn how to enable plugins.

Go to Enabling Plugins ›