load-balance-incoming-request-with-kong-ring-balance

Overview:

In the previous sections, you Installed Kong Using Docker , Provisioned, Protected and Rate Limited your API, explored various Configuration Files and Configured Secure Access to your APIs and the Kong Admin Loopback.

Kong provides multiple ways of load balancing requests to multiple backend services: a straightforward DNS-based method, and a more dynamic ring-balancer that also allows for service registry without needing a DNS server.

In this section, you will configure various load balance scenarios using Kong’s Ring Balancer

Screen Shot 2017 08 10 At 11.41.56 Am

Scenario

There a couple of use cases for load balancing Kong to API requests. These include:

  1. Load Balance Incoming Requests (e.g. Round Robin)
  2. Blue/Green Deployments
  3. Canary Releases
  4. Circuit Breaker

In this exercise, you will load balance incoming Kong Requests to the API Endpoint using Kong’s ring balancer.

Note: When using the ring-balancer, the adding and removing of backend services will be handled by Kong, and no DNS updates will be necessary. Kong will act as the service registry. Nodes can be added/deleted with a single HTTP request and will instantly start/stop receiving traffic.

High Level Tasks

  1. Create Upstreams
  2. Create Targets - (Hostname or IP address of your API service - must specify port)
  3. Create API Endpoint
  4. Verify Load Balancing:

Detail Configurations

1. Create Upstream

Create upstreams with this command.

$ http post localhost:8001/upstreams name=mockbin.org

2. Create Targets

Create two targets each with a weight of 100. Mockbin has two servers (104.28.22.173 and 104.28.23.173) This means the requests to these targets is load balanced round robin.

Server 1

$ http post localhost:8001/upstreams/mockbin.org/targets target=104.28.22.173:80 weight=100

Server 2

$ http post localhost:8001/upstreams/mockbin.org/targets target=104.28.23.173:80 weight=100

3. Create API Enpoint

Create a new API endpoint or modify an existing one.

$ http post localhost:8001/apis \
name=lb-demo \
hosts=api.lb.com \
upstream_url=http://mockbin.org/requests


4. Verify Load Balancing

Try the below request numerous times. You should see Mockbin’s IP address alternate 50/50 (round robin)

$ http get localhost:8000 host:api.lb.com X-Auth:vietpass

Important Note: Mockbin doesn’t allow you to access the page via the IP address, so we will get an error. However, we should see the destination IP address alternate between the Mockbin IP addresses (104.28.23.173 and 104.28.22.173).

Summary:

Great! You configured Kong to load balance incoming requests between the API Endpoints using Kong’s ring balance feature. In this example, we used Mockbin’s two public servers and verified requests were load balanced between these two.

When using the ring-balancer, the adding and removing of backend services will be handled by Kong, and no DNS updates will be necessary. Kong will act as the service registry. Nodes can be added/deleted with a single HTTP request and will instantly start/stop receiving traffic.


Optional Exercise

Optionally, modify the above exercise to configure Load Balance Incoming Request with Kong Ring Balance @ 70:30 (instead of round robin).

Bonus Exercises

To expand on Kong’s ring balancer functionality, you can try these bonus exercises. High level steps are provided (detail configurations-coming soon)

Load Balance - Blue/Green Deployment

Load Balance - Canary Releases Deployment

What Next

Lets scale your environment by adding Kong nodes to a cluster.

Scale Kong - Clustering

Edit this page