1

I am configuring multiple services in the same kong.yaml. e.g.

services:
  - host: service1.com
    name: service1
    port: 8000
    route: ...

  - host: service2.com
    name: service2
    port: 9000
    route: ...

When I make a request from the client e.g.

curl -X GET -k https://localhost:8443/v1/service2/api -H "apiKey: service2-api-key"

It keeps getting proxied to service1 by default and getting the following error:

2021/05/14 01:46:40 [error] 26#0: *37325 [lua] balancer.lua:1064: execute(): DNS resolution failed: dns server error: 3 name error. Tried: ["(short)service1.com:(na) - cache-miss","service1.com:33 - cache-miss/scheduled/querying/dns server error: 3 name error","service1.com:1 - cache-miss/scheduled/querying/dns server error: 3 name error","service1.com:5 - cache-miss/scheduled/querying/dns server error: 3 name error"], client: 172.18.0.5, server: kong, request: "GET /v1/service2/api HTTP/2.0", host: "localhost:8443"

From the docs it mentioned that you can add a hosts attribute to the route object and have the client make a request with the host in the header (and this works). e.g.

curl -X GET -k https://localhost:8443/v1/service2/api -H "apiKey: service2-api-key" -H "Host: service2.com"

However, I cannot change how the client makes the request since this is already in production. Is there a way that we can proxy the request without having to change the client's request to include the host (Host: <given host>) in the header?

Also, something to note, if I completely remove service1, then it works, it defaults to service2 route without needing to include the extra Host in the header of the request.

tiger_groove
  • 956
  • 2
  • 17
  • 46

1 Answers1

1

If I correctly understand you want to route /v1/service1/api to your service1 and /v1/service2/api to your service2.

And you need to have the Host header set to service1 or service2. Kong default is "preserve_host": true, so you should get what you want

You need to set the path of your route for each service service1:

  routes:
  - name: my-route1
    paths:
    - /v1/service1/

service2:

  routes:
  - name: my-route2
    paths:
    - /v1/service2/

Then you can test with

curl -i -X GET --header 'kong-debug: 1' http://my-gateway-url.com:8000/v1/service2/api

HTTP/1.1 200 OK
Content-Type: application/octet-stream
Content-Length: 4
Connection: keep-alive
Kong-Route-Id: df6f59df-a244-4b74-b782-02138d2d474a
Kong-Route-Name: route2
Kong-Service-Id: 0f32ae39-d338-4074-a112-c5106c99776d
Kong-Service-Name: service2
Date: Tue, 18 May 2021 20:11:13 GMT
Last-Modified: Tue, 18 May 2021 20:11:09 GMT
Server: SimpleHTTP/0.6 Python/3.8.0

foo

and

curl -i -X GET --header 'kong-debug: 1' http://my-gateway-url.com:8000/v1/service1/api

HTTP/1.1 200 OK
Content-Type: application/octet-stream
Content-Length: 4
Connection: keep-alive
Kong-Route-Id: 92323f17-ac53-4bfb-80af-5264ab389663
Kong-Route-Name: route1
Kong-Service-Id: c402fba2-4cbe-47b0-a2fd-c9f3dd9974e1
Kong-Service-Name: service1
Date: Tue, 18 May 2021 20:11:20 GMT
Last-Modified: Tue, 18 May 2021 20:11:09 GMT
Server: SimpleHTTP/0.6 Python/3.8.0

foo
Ôrel
  • 7,044
  • 3
  • 27
  • 46
  • Just by setting the path and making request to each path doesn't seem like it's enough. If i don't add the `hosts` attribute to each of the paths then Kong doesn't seem to know which service to send the request to. e.g. when I attempt to make a request to `/v1/service1/` for some reason it is attempting to proxy the request to server2, is this expected? I do have `preserve_host: true`. – tiger_groove May 17 '21 at 19:49
  • Can you try to get more details adding `'kong-debug: 1'` to your request to get which service and route are used ? I have added what I get – Ôrel May 18 '21 at 20:13