DEV Community

Abhishek Gupta
Abhishek Gupta

Posted on • Originally published at abhishek1987.Medium

Kubexpose: A Kubernetes Operator, for fun and profit!

Say you have a web service running as a Kubernetes Deployment. There are a bunch of ways to access it over a public URL, but Kubexpose makes it easy to do so. It's a Kubernetes Operator backed by a Custom Resource Definition and the corresponding controller implementation.

Kubexpose is built using kubebuilder and available on GitHub

To try it out, jump into the next section or scroll down to How it works? to learn more

Quick start

Any Kubernetes cluster will work (minikube, kind, Docker Desktop, on the cloud, whatever...).

To deploy the operator and required components:

kubectl apply -f https://raw.githubusercontent.com/abhirockzz/kubexpose-operator/master/kubexpose-all-in-one.yaml
Enter fullscreen mode Exit fullscreen mode

Make sure the Operator is up and running:

export OPERATOR_NAMESPACE=kubexpose-operator-system

# check Pods
kubectl get pods -n $OPERATOR_NAMESPACE

# check logs
kubectl logs -f $(kubectl get pods --namespace $OPERATOR_NAMESPACE -o=jsonpath='{.items[0].metadata.name}') -c manager -n $OPERATOR_NAMESPACE
Enter fullscreen mode Exit fullscreen mode

Create a nginx Deployment to test things out — this is the one you want to expose over the internet using a public URL).

Along with it, create a kubexpose resource — which will help you access ngnix Deployment over the Internet!

kubectl apply -f https://raw.githubusercontent.com/abhirockzz/kubexpose-operator/master/quickstart/nginx.yaml

kubectl apply -f https://raw.githubusercontent.com/abhirockzz/kubexpose-operator/master/quickstart/kubexpose.yaml
Enter fullscreen mode Exit fullscreen mode

Wait for a few seconds and check the public URL at which the Nginx Deployment can be accessed:

kubectl get kubexpose/kubexpose-test -o=jsonpath='{.status.url}'
Enter fullscreen mode Exit fullscreen mode

Access the public URL using your browser or test it using curl

Confirm that the Service and Deployment have been created as well:

kubectl get svc/nginx-test-svc-kubexpose-test
kubectl get deployment/nginx-test-expose-kubexpose-test
Enter fullscreen mode Exit fullscreen mode

You can try out other scenarios such as trying to Deployment and/or Service - the Operator will reconcile or bring things back to the state as specified in the resource.

To delete the kubexpose resource:

kubectl delete kubexpose/kubexpose-test
Enter fullscreen mode Exit fullscreen mode

This will also delete the Service and Deployment which were created for this resource

To uninstall the Operator:

kubectl delete -f https://raw.githubusercontent.com/abhirockzz/kubexpose-operator/master/kubexpose-all-in-one.yaml
Enter fullscreen mode Exit fullscreen mode

This will delete the CRD, kubexpose operator and other resources.

How does it work?

Behind the scenes, Kubexpose uses the awesome ngrok project to get the job done!
When you create a kubexpose resource, the operator:

  • Creates a ClusterIP type Service for the Deployment you want to access (naming format: <deployment name>-svc-<kubexpose resource name>)
  • Creates a Deployment (using this ngrok Docker image) that runs ngrok - which is configured to point to the Service (naming format: <deployment name>-expose-<kubexpose resource name>). It's equivalent to starting ngrok as such: ngrok http foo-svc-bar 80

The Deployment and Service and owned and managed by the Kubexpose resource instance.

Enjoy!

Top comments (0)