PermalinkConfiguring a Service
- Make sure you've installed Kong — It should only take a minute!
- Make sure you've started Kong.
In this section, you’ll add an API to Kong. In order to do this, you must add a Service. A Service in Kong refers to the upstream APIs and microservices it manages.
For the purpose of this guide, you’ll create a Service pointing to the Mockbin API. Mockbin is an “echo” type public website that returns the requests it gets back to the requester as responses. This is helpful in learning how Kong proxies your API requests.
Before you can start making requests against the Service, you have to add a Route to the Service. 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.
Permalink1. Add your Service using the Admin API
Issue the following cURL request to add your first Service (pointing to the Mockbin API) to Kong:
$ curl -i -X POST \
--url http://localhost:8001/services/ \
--data 'name=example-service' \
--data 'url=http://mockbin.org'
You should receive a response similar to:
HTTP/1.1 201 Created
Content-Type: application/json
Connection: keep-alive
{
"host":"mockbin.org",
"created_at":1519130509,
"connect_timeout":60000,
"id":"92956672-f5ea-4e9a-b096-667bf55bc40c",
"protocol":"http",
"name":"example-service",
"read_timeout":60000,
"port":80,
"path":null,
"updated_at":1519130509,
"retries":5,
"write_timeout":60000
}
Permalink2. Add a Route for the Service
$ curl -i -X POST \
--url http://localhost:8001/services/example-service/routes \
--data 'hosts[]=example.com'
The answer should be similar to:
HTTP/1.1 201 Created
Content-Type: application/json
Connection: keep-alive
{
"created_at":1519131139,
"strip_path":true,
"hosts":[
"example.com"
],
"preserve_host":false,
"regex_priority":0,
"updated_at":1519131139,
"paths":null,
"service":{
"id":"79d7ee6e-9fc7-4b95-aa3b-61d2e17e7516"
},
"methods":null,
"protocols":[
"http",
"https"
],
"id":"f9ce2ed7-c06e-4e16-bd5d-3a82daef3f9d"
}
Kong is now aware of your Service and ready to proxy requests.
Permalink3. 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
:
$ curl -i -X GET \
--url http://localhost:8000/ \
--header 'Host: example.com'
A successful response means Kong is now forwarding requests made to
http://localhost:8000
to the url
you configured in step #1,
and is forwarding the response back to you. Kong knows to do this through
the header defined in the above cURL request:
- Host: <given host>
PermalinkNext Steps
Now that you’ve added your Service to Kong, let’s learn how to enable plugins.
Go to Enabling Plugins ›