How to do Weighted Round-Robin Load Balancing with ngrok Cloud Edges
The purpose of Load balancing is to improve application performance and reduce burden by distributing incoming traffic across servers. This leads to faster response times for user-facing applications.
Weighted Round-robin load balancing while similar to round-robin load balancing here you can assign a numeric weight to all of the applications behind the load balancer. Weights can be assigned based on factors such as the application state, server’s processing power or total bandwidth.
Prerequisites
To follow this guide, you will need:
- An ngrok Pro or Enterprise account.
- A local computer with
ngrok
installed by following our installation guides.
If you are going to be following along using ngrok CLI, you will need:
- An ngrok API key configured on your ngrok agent.
If you are going to be following along using CURL, you will need:
- An ngrok API key as an environment variable named
NGROK_API_KEY
.
Step 1 — Create a Reserved Domain
We're going to be using ngrok edges to load balance across multiple tunnels.
To start load balancing traffic with ngrok edges, you need to have a reserved domain.
Let’s reserve a subdomain on ngrok.app
:
- ngrok CLI
- CURL
ngrok api reserved-domains create \
--domain ${NGROK_SUBDOMAIN}.ngrok.app
- Replace or set
NGROK_SUBDOMAIN
as the subdomain you'd like to use for this guide.
curl \
-X POST https://api.ngrok.com/reserved_domains \
-H "Authorization: Bearer ${NGROK_API_KEY}" \
-H "Content-Type: application/json" \
-H "Ngrok-Version: 2" \
-d @- <<BODY
{
"name":"${NGROK_SUBDOMAIN}.ngrok.app",
}
BODY
- Replace or set
NGROK_API_KEY
to your ngrok API key. - Replace or set
NGROK_SUBDOMAIN
as the subdomain you'd like to use for this guide.
After running, you should see the following:
200 OK
{
"id":"rd_2MT5Bqt0UzU0mFQ0zr8m1UQWCfm",
...
}
When you have completed this step, you can move on to the next step.
Step 2 — Create a ngrok Cloud Edge
Now we can create a ngrok Cloud Edge that points to our
newly reserved domain on port 443
:
- ngrok CLI
- CURL
ngrok api edges https create \
--description "my edge" \
--hostports "${NGROK_SUBDOMAIN}.ngrok.app:443"
- Replace or set
NGROK_SUBDOMAIN
with the value used in the previous step.
curl \
-X POST https://api.ngrok.com/edges/https \
-H "Authorization: Bearer {NGROK_API_KEY}" \
-H "Content-Type: application/json" \
-H "Ngrok-Version: 2" \
-d @- <<BODY
{
"description": "",
"metadata": "{}",
"hostports": ["{NGROK_SUBDOMAIN}.ngrok.app:443"]
}
BODY
- Replace or set
NGROK_API_KEY
to your ngrok API key. - Replace or set
NGROK_SUBDOMAIN
with the value used in the previous step.
After running, you should see:
{
"id": "edghts_2MT9nzunlKgQ0KRfmA5QK5iiI1J",
...
"hostports": [
"${NGROK_SUBDOMAIN}.ngrok.app:443"
],
...
}
Export the id
value as a new environment variable named NGROK_EDGE_ID
:
export NGROK_EDGE_ID={your_id_here}
Now that we have an edge, we can move onto the next step.
Step 3 — Create Tunnel Group Backends
Now that we have an edge we can begin to build the parts to do weighted load balancing.
Lets create two tunnel-group backends to do traffic management on our edge.
Create our first backend with label region=us
:
- ngrok CLI
- CURL
ngrok api backends tunnel-group create \
--labels region=us \
--description "us tunnel group"
curl \
-X POST https://api.ngrok.com/backends/tunnel_group \
-H "Authorization: Bearer ${NGROK_API_KEY}" \
-H "Content-Type: application/json" \
-H "Ngrok-Version: 2" \
-d @- <<BODY
{
"description": "us tunnel group",
"metadata": "{}",
"labels": {
"region": "us"
}
}
BODY
- Replace or set
NGROK_API_KEY
to your ngrok API key.
You will get something back like:
{
"id":"bkdtg_2MT8j8IV1adEW9TbG5c9ypJwCHY",
...
}
Export the returned id
as a variable:
export NGROK_US_BACKEND_ID={your_id_here}
Create our second backend with label region=eu
:
- ngrok CLI
- CURL
ngrok api backends tunnel-group create \
--labels region=eu \
--description "eu tunnel group"
curl \
-X POST https://api.ngrok.com/backends/tunnel_group \
-H "Authorization: Bearer ${NGROK_API_KEY}" \
-H "Content-Type: application/json" \
-H "Ngrok-Version: 2" \
-d @- <<BODY
{
"description": "eu tunnel group",
"metadata": "{}",
"labels": {
"group": "eu"
}
}
BODY
- Replace or set
NGROK_API_KEY
to your ngrok API key.
You will get something back like:
{
"id":"bkdtg_2MT8j8IV1adEW9TbG5c9ypJwCHY",
...
}
Export the returned id
as a variable:
export NGROK_EU_BACKEND_ID={your_id_here}
Now that we have our tunnel-group backends, we can move onto the next step.
Step 4 - Create a Weighted Backend
Now that we have our two region tunnel groups we need to create a weighted backend to distribute incoming traffic on our edge between them:
- ngrok CLI
- CURL
ngrok api backends weighted create \
--description "weighted round-robin guide" \
--backends "${NGROK_US_BACKEND_ID}=50" \
--backends "${NGROK_EU_BACKEND_ID}=50"
- Replace or set
NGROK_US_BACKEND_ID
to the id returned in step 3. - Replace or set
NGROK_EU_BACKEND_ID
to the id returned in step 3. - ℹ️ Backend weights can be any valid integer between
0
to10000
curl \
-X POST https://api.ngrok.com/backends/weighted \
-H "Authorization: Bearer {NGROK_API_KEY}" \
-H "Content-Type: application/json" \
-H "Ngrok-Version: 2" \
-d @- <<BODY
{
"description": "weighted round-robin guide",
"metadata": "{}",
"backends": {
"{NGROK_US_BACKEND_ID}": 50,
"{NGROK_EU_BACKEND_ID}": 50
}
}
BODY
- Replace or set
NGROK_API_KEY
to your ngrok API key. - Replace or set
NGROK_US_BACKEND_ID
to the id returned in step 3. - Replace or set
NGROK_EU_BACKEND_ID
to the id returned in step 3.