How to build an Application Loadbalancer for ~ 1$ / 1mio requests
Flexible request routing doesnt need to be expensive
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
- Create a Worker with the content below
- Create a KV space (see screen1 below)
- Map KV space to variable in worker (see screen2 below)
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.
- Go to https://console.cloud.google.com/logs/exports
- Create 2 Triggers which publish events to a Pub/Sub Topic called instance_events, use the 2 following filters for the Logs
- 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