What you’ll need
- The vcluster CLI installed locally.
- An existing remote or local Kubernetes cluster or minikube to create a new demo cluster locally.
- An ngrok account.
- kubectl and Helm 3.0.0+ installed on your local workstation.
- The ngrok Kubernetes Operator installed on your cluster.
- A reserved domain from the ngrok dashboard or API; this guide refers to it as
<NGROK_DOMAIN>.
Set up a local virtual cluster with vcluster
To follow along with this guide, you need a remote or local Kubernetes cluster with vcluster installed. If you have an existing cluster with vcluster set up, you can skip this step and proceed to Install a sample application. If you don’t have a cluster already, create one locally with minikube and install vcluster.-
Create a local Kubernetes cluster with minikube.
-
Use the
minikubeCLI to ensure your new local cluster is running properly. -
Create a new vcluster with the name
my-vcluster, which creates a new namespace calledvcluster-my-clusterand automatically switches the active kube context to use your new vcluster. -
To ensure your new local cluster is running properly, get the namespaces for your instance.
Your list of namespaces in the
my-vclustercontext should look something like this.If you are not connected to your new vcluster and its kube context, you can runvcluster connect my-vclusterto try again. You now have a vcluster installed on your local minikube cluster.Reference: These steps are partially based on Loft’s guide for using the ngrok Kubernetes Operator with vcluster for preview environments.
Install a sample application
At this point, you have a functional vcluster with the ngrok Kubernetes Operator running and authenticated with your ngrok credentials. To demonstrate how the Operator simplifies routing external traffic to your primary cluster, virtual cluster, and ultimately an exposed service or endpoint, you can install a sample application.-
Reserve a domain for ingress if you don’t have one already.
Navigate to the Domains section of the ngrok dashboard and click Create Domain or New Domain.
This guide refers to this as
<NGROK_DOMAIN>for the remainder of this guide. By creating a subdomain on the ngrok network, you provide a public route to accept HTTP, HTTPS, and TLS traffic. -
Create a new Kubernetes manifest (
tinyllama.yaml) with the below contents. This manifest defines the tinyllama demo LLM application from ngrok-samples/tinyllama (service and deployment), then configures the ngrok Kubernetes Operator to connect thetinyllamaservice to the ngrok network. Be sure to replace<NGROK_DOMAIN>with the domain you reserved a moment ago. -
Apply the
tinyllama.yamlmanifest to your vcluster. -
Access your tinyllama demo app by navigating to your domain (for example,
https://one-two-three.ngrok.app). ngrok’s edge and your Operator route traffic to your app from any device or external network as long as your vcluster is running.
Add OAuth protection to your demo app
To take your ingress a little further, assume you want to add edge security, in the form of Google OAuth, to the endpoint where your tinyllama application is running. With the Traffic Policy system and theoauth action, ngrok manages OAuth protection entirely at the ngrok cloud service.
This means you don’t need to add any additional services to your cluster, or alter routes, to ensure ngrok’s edge authenticates and authorizes all requests before allowing ingress and access to your endpoint.
To enable the oauth action, you’ll create a new NgrokTrafficPolicy custom resource and apply it to your entire Ingress with an annotation.
You can also apply the policy to just a specific backend or as the default backend for an Ingress—see the documentation on using the Operator with Ingresses.
-
Edit your existing
tinyllama.yamlmanifest with the following, leaving theServiceandDeploymentas they were. Note the newannotationsfield and theNgrokTrafficPolicyCR. -
Re-apply your
tinyllama.yamlconfiguration. - When you open your demo app again, you’ll be asked to log in via Google. That’s a start, but what if you want to authenticate only yourself or colleagues?
-
Use expressions and CEL interpolation to filter out and reject OAuth logins that don’t contain
example.com. Update theNgrokTrafficPolicyportion of your manifest after changingexample.comto your domain. - Check out your deployed tinyllama app once again. If you log in with an email that doesn’t match your domain, ngrok rejects your request.
What’s next?
You’ve now used the open source ngrok Kubernetes Operator to add secure access to your endpoint without worrying about IPs, network interfaces, or VPC routing. Because ngrok offloads ingress and middleware execution to its global edge, you can follow the same procedure listed above for any Kubernetes environment, like EKS, GKE, and OpenShift, with similar results. If you want to clean up the work you did for this demo application, the easiest way (and the advantage of virtual clusters in the first place) is to disconnect from your vcluster and then delete it with thevcluster CLI.
That will remove the namespace and all its resources, returning your primary cluster to its initial state.