How to run multiple service on kong with different ports

Hello,
I am running kong gateway on docker. I want to create 2 different services on kong gateway having different ports. How can I do that ?
For Example, there are 2 url

  1. www.google.com
  2. www.facebook.com

I want to create proxy for them by creating 2 different services so that I can access them like:

  1. http://localhost:8000 → proxy for google
  2. http://localhost:9000 → proxy for facebook

In Short, If I want to create some other service like this then how can we run that on other port ?

Anyone can give any input ?

@Arpit_Khandelwal I believe you can solve this using the KONG_PROXY_LISTEN and the route hosts configurations.

I tried the following. I ran a quickstart of the gateway using the following quickstart command binding the desired listen ports (port 8000 is bound by default)

curl -Ls https://get.konghq.com/quickstart | bash -s -- -e KONG_PROXY_LISTEN='0.0.0.0:8000,0.0.0.0:9000' -p 9000:9000

Then installed two services:

curl -i -s -X POST http://localhost:8001/services --data name=google_service --data url='https://google.com'
curl -i -s -X POST http://localhost:8001/services --data name=facebook_service --data url='https://facebook.com'

And two routes this way:

curl -i -X POST http://localhost:8001/services/google_service/routes --data name=google_route --data 'hosts[]=localhost:8000'
curl -i -X POST http://localhost:8001/services/facebook_service/routes --data name=facebook_route --data 'hosts[]=localhost:9000'

Then try routing requests to the desired data plane port:

curl localhost:8000
curl localhost:9000

Hope this helps.

1 Like

@rick It worked, thanks for this !!

But my further use case is to enable OAuth 2.0 plugin having client credentials enable for these services and routes but when I am enabling the plugin and trying to fetch access token by

curl -k -X POST https://localhost:8443/oauth2/token \
--data "client_id=456" \
--data "client_secret=789" \
--data "grant_type=client_credentials"

I am getting error: [“message”:“no Route matched with those values”]
This is because of these route hosts configured. For conformation, I have tried removing them for routes and it worked.

Any idea how can we solve that and get access token ?

Route which is working with oauth2.0 plugin

{
  "methods": [
    "GET",
    "POST"
  ],
  "protocols": [
    "http",
    "https"
  ],
  "regex_priority": 0,
  "paths": null,
  "created_at": 1726744945,
  "request_buffering": true,
  "response_buffering": true,
  "service": {
    "id": "64a57a49-151e-41b2-a3e5-4c66bc06829d"
  },
  "strip_path": true,
  "https_redirect_status_code": 426,
  "updated_at": 1726745067,
  "snis": null,
  "id": "6a92f4a5-2317-4fa6-a2be-4c92b564733c",
  "tags": [],
  "path_handling": "v0",
  "sources": null,
  "headers": null,
  "hosts": null,
  "destinations": null,
  "preserve_host": false,
  "name": "BoardRoute"
}

Route which is not working:

{
  "methods": [
    "GET",
    "POST"
  ],
  "protocols": [
    "http",
    "https"
  ],
  "regex_priority": 0,
  "paths": null,
  "request_buffering": true,
  "response_buffering": true,
  "service": {
    "id": "64a57a49-151e-41b2-a3e5-4c66bc06829d"
  },
  "strip_path": true,
  "https_redirect_status_code": 426,
  "snis": null,
  "id": "6a92f4a5-2317-4fa6-a2be-4c92b564733c",
  "tags": [],
  "path_handling": "v0",
  "sources": null,
  "headers": null,
  "hosts": [
    "localhost:8000"
  ],
  "destinations": null,
  "preserve_host": false,
  "name": "BoardRoute"
}

@Arpit_Khandelwal First I appologize I’m not very familiar with OAuth2 or the plugin. However, with this request:

curl -k -X POST https://localhost:8443/oauth2/token \
--data "client_id=456" \
--data "client_secret=789" \
--data "grant_type=client_credentials"

In this case you are trying to route a client request to the TLS configured listener on the dataplane, correct? In which case, I would assume it can’t match w/ the route that has hosts setup on localhost:8000. Did you try setting hosts to localhost:8443?

1 Like

Thanks @rick
I have set “KONG_PROXY_LISTEN=0.0.0.0:8000 ssl, 0.0.0.0:9000 ssl, 0.0.0.0:8443 ssl”
And it helps me to fetch access token from localhost:8000 and localhost:9000 itself instead of 8443.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.