How to build an Application Loadbalancer for ~ 1$ / 1mio requests

Flexible request routing doesnt need to be expensive

Manuel Villing
3 min readNov 8, 2018
The Basic idea of cloudflare worker application loadbalancing

Intro

Recently i had the need to distribute the load of an App over a cluster of kubernetes nodes, for a while i was using the GCE Application loadbalancer but i felt quickly paying 18$ for and App that does not serve many requests yet is overkill also the GCE loadbalancer felt not very flexible. Since i was using Cloudflare for DDOS protection and SSL termination anyway i gave Cloudflare workers + Cloudflare KV (Key value store) a try for 5$ / month.

Disclaimer: Cloudflare KV is still in Beta and you might need to ask the support to give you access, which can take like 4 days+. Ask for access here.

What to expect

In this post i will give insight about the pros and cons i discovered about Cloudflare Workers and also explain in a short tutorial how Cloudflare Workers can be used in combination with GCE Kubernetes Cluster.

What do you get for 5$ / month

  • 10 mio requests (further requests are billed at 1 mio / 0.50$)
  • 10 mio KV read operations (further reads are billed at 1 mio / 0.50$)

more infos here

Pros:

  • routing based on the country of the ip of the request
  • ssl termination
  • ddos protection
  • much faster then cloud functions (cold starts are not really slow)
  • websocket support
  • streaming responses
  • didnt have any trouble with the cloudflare KV even though is still beta
  • fairly simple to add sticky session

Cons:

  • targets cannot be ips (thats a cloudflare security limitation) hence we are using a stealth domain to work around this
  • cpu time / memory limit hardlimits can be a problem for heavy computation

Tutorial

Requirements for the tutorial

  • Kubernetes Cluster on GCE
  • Cloudflare Account (can be free account, but needs to have Cloudflare Worker Subscription for $5 / month)
  • Cloudflare KV enabled
  • 2 domains (1 main domain, 1 stealth domain (to map ips to names)

Create Cloudflare Worker + KV Store

  1. Create a Worker with the content below
  2. Create a KV space (see screen1 below)
  3. Map KV space to variable in worker (see screen2 below)
screen2 — map KV space to variable
screen1 — add a KV space to store the active nodes
Cloudflare worker code which routes the requests in an simple round-robin fashion

Create Triggers

In this section you will create a lambda function which runs when an instance is added / deleted from the cluster. The function will add / remove the nodes to your Cloudflare loadbalancer.

  1. Go to https://console.cloud.google.com/logs/exports
  2. Create 2 Triggers which publish events to a Pub/Sub Topic called instance_events, use the 2 following filters for the Logs
  3. Create a cloud function with the content of lambda.js (replace the config with your data) with a pub/sub trigger with the topic you created above
resource.type="gce_instance"
jsonPayload.event_subtype="compute.instances.delete"
------resource.type="gce_instance"
jsonPayload.event_subtype="compute.instances.insert"

Future improvements:

  • have an lambda function do health checks and add / remove nodes
  • have an lambda function to clean up unused DNS records
  • implement sticky session with cookies in the cloudflare worker

--

--

Manuel Villing

#freelance #webdeveloper #nodejs #onlinemedienmanagment #javascript #design