Search code examples
load-balancingkongnginx-upstreams

Kong load balance where upstream has different root paths?


I'm using Kong OSS (not container or k8s deployed) trying to load balance a backend two backend services for testing. My challenge is one requires authentication and different backend path port, whereas the other does not.

Frontend 1 - Kong API Gateway frontend

  • Host: localhost
  • Port: 8000
  • Authentication: none
  • Root Path: /
  • Example Request: POST http://localhost:8000/ {JSON Data}

I'm expecting this front-end to have following backend

Backend A - local API service for testing

  • Host: localhost
  • Port: 80
  • Authentication: none
  • Root Path: /
  • Example Request: POST http://localhost:80/ {JSON Data}

Backend B - hosted SaaS API service

Following the documentation, it seems I need to create 1 service to 2 upstream/targets, but not sure how to handle different upstream root paths? Kong Load Balancing

Can the upstream config dynamically change the backend path based on selected upstream/target? Or maybe instead create a single route that load-balances between two services?

I'm trying to create 1 route with 1 service that has 2 upstream targets with different root paths and request-transformers for specific upstream targets


Solution

  • You can create two services, each service with one route. Both routes uses the same path,

    Service 1: http://httpbin.org

    curl --location --request POST 'http://localhost:8001/services/' \
    --form 'name="httpbin"' \
    --form 'url="http://httpbin.org"'
    

    Route 1: /path

    curl --location --request POST 'http://localhost:8001/services/httpbin/routes/' \
    --form 'paths[]="/testPath"' \
    --form 'name="httpbinget"' \
    --form 'hosts[]="httpbin.org"'
    

    Service 2: http://mockbin.org

    curl --location --request POST 'http://localhost:8001/services/' \
    --form 'name="mockbin-secure"' \
    --form 'url="https://mockbin.org"'
    

    Route 2: /testpath

    curl --location --request POST 'http://localhost:8001/services/mockbin-secure/routes/' \
    --form 'paths[]="/testPath"' \
    --form 'name="mockbin-get"' \
    --form 'hosts[]="mockbin.org"'
    

    Test the API Gateway proxy /testPath

    curl --location --request GET 'http://localhost:8000/testPath/' \
    --header 'Accept: application/vnd.yang.data+json' \
    --header 'Host: httpbin.org'
    
    curl --location --request GET 'http://localhost:8000/testPath/' \
    --header 'Accept: application/vnd.yang.data+json' \
    --header 'Host: mockbin.org'
    

    The 2nd request is expected to fail if it requires authentication, the last step will be to enable authentication plugin on this particular service

    curl -X POST http://localhost:8001/services/mockbin-secure/plugins \
        --data "name=key-auth"  \
        --data "config.key_names=apikey" \
        --data "config.key_in_body=false" \
        --data "config.key_in_header=true" \
        --data "config.key_in_query=true" \
        --data "config.hide_credentials=false" \
        --data "config.run_on_preflight=true"
    

    the apikey needs to be provided when testing the proxy

    curl --location --request GET 'http://localhost:8000/testPath/' \
    --header 'Accept: application/vnd.yang.data+json' \
    --header 'Host: httpbin.org' \
    --header 'api-key: somekey'
    

    I don't have a secure service to test with now, I hope that might help.