Load balancing in KONG API Gateway

Muthu picture Muthu · Sep 7, 2016 · Viewed 7.5k times · Source

We have multiple instance of a micro service behind the Kong API gateway where we want to balance the load for the user requests.

Say Micro service 1 is multiplied in multiple instances which are kept behind the KONG API gateway; in such case the request from user 1 should hit the first instance and the request from user 2 should hit some other instance of same service based on their availability (load balancing). (ie) Whether can i have multiple upstream URL for a single API in kong. we dont want to use nginx for load balancing. Please advice how can we solve it.

Answer

flguo picture flguo · Jan 22, 2018

Ring-balancer Strategy can be used in Kong if you don't want DNS-based loadbalancing. For details please refer to Kong Load Balancing Reference!

# create an upstream
$ curl -X POST http://kong:8001/upstreams \
    --data "name=address.v1.service"

# add two targets to the upstream
$ curl -X POST http://kong:8001/upstreams/address.v1.service/targets \
    --data "target=192.168.34.15:80"
    --data "weight=100"
$ curl -X POST http://kong:8001/upstreams/address.v1.service/targets \
    --data "target=192.168.34.16:80"
    --data "weight=50"

# create an API targeting the Blue upstream
$ curl -X POST http://kong:8001/apis/ \
    --data "name=address-service" \
    --data "hosts=address.mydomain.com" \
    --data "upstream_url=http://address.v1.service/address"

Requests with host header set to address.mydomain.com will now be proxied by Kong to the two defined targets; 2/3 of the requests will go to http://192.168.34.15:80/address (weight=100), and 1/3 will go to http://192.168.34.16:80/address (weight=50).