foo
, bar
, and baz
, deployed in a single
Kubernetes cluster, but eventually decide you need to extend to multiple
clusters for reasons like:
- Redundancy and failover
- Load distribution and scalability
- Hosting services close to your customers in different regions
- Deploying some clusters on-premises and others with a cloud provider
https://api.example.com
.
We have another guide for load balancing to
individual K8s
services with
ngrok custom resources, Ingress objects, or Gateway API resources. This guide uses our custom
resources to create endpoints for your services.
1. Install the ngrok Kubernetes Operator
Check out our installation instructions for details on how to use Helm to deploy the open-source Operator to each cluster you’d like to load-balance between.2. Create internal Agent Endpoints on your first cluster
The following YAML manifest creates three internal agent endpoints, each of which will proxy traffic to yourfoo
, bar
, and baz
pods, respectively. When you
use our custom resources to create agent endpoints, pooling is enabled
automatically.
ingress.yaml
and apply to your cluster
with kubectl apply -f ingress.yaml
.
These internal agent endpoints won’t yet be available to public traffic, because
they are only accessible through other ngrok endpoints in your account, but
you’ll fix that in the next two steps.
Repeat the process on any other clusters you want to load balance traffic
between.
4. Create a Cloud Endpoint and add routing with Traffic Policy
If you don’t yet have a reserved domain, go ahead and reserve one in the dashboard. The easiest way to create a cloud endpoint is in the Endpoint section of your ngrok dashboard. Click New, then Cloud Endpoint. Leave the binding as Public and fill in the full URL of the domain you reserved, then click Create Cloud Endpoint. The following traffic policy example:- Uses the
forward-internal
action to route all requests to the/foo
path to the pool you created athttps://foo.internal
. - The pooled endpoints then proxy requests proxies the request to thefoo
Service/Deployment in your cluster. - Does the same for the paths
/bar
and/baz
. - Denies all traffic to paths that don’t (yet) have a service.
https://api.example.com/foo
or any other route, your
traffic will be automatically load-balanced between replicas of your services on
multiple Kubernetes clusters.
What’s next?
With load balancing solved for, you can now add new services or entire Kubernetes clusters by adding new endpoints to existing pools or configuring new routes with traffic policy. Speaking of traffic policy, here are a few common ways folks filter, manage, and orchestrate requests:- Route requests by subdomain or header instead of paths.
- Add authentication with the
basic-auth
,oauth
, orjwt-validation
Traffic Policy actions. - Rate limit requests to prevent abuse and ensure fairness.