Building Cloud-Native Applications with Kubebuilder and Kind
In this article, we take a look at installing and creating cloud-native projects using Kubebuilder and Kind.
Join the DZone community and get the full member experience.Join For Free
In this article, we will explore how to use Kubebuilder and Kind to create a local test cluster and an operator. Following that operation, we will then deploy that operator in the cluster and test it. All of the code is included below to port-forward to private endpoints the Kubernetes way. Also, if you want to learn more about the idea and the project check out the Forward operator page here.
Essentially, what the code does is to create an alpine/socat pod. You can specify the host, port, and protocol and it will make a tunnel for you, so then you can use port-forward or a service or ingress or whatever to expose things that are in another private subnet.
While Kind is not actually a requirement, I used that for testing and really liked it, it’s much faster and simpler than Minikube.
Also, if you are interested in how I came up with the idea to make this operator, check out this GitHub issue here.
Create the Project
In this step, we need to create the Kubebuilder project, so in an empty folder we run:
Create the API
Next let’s create an API, something for us to use to run our controller.
Up until this point here, we have only created a boilerplate/basic or empty project with defaults. If you were to test it now, it will work, but it won’t do anything interesting. However, it covers a lot of ground and we should be grateful that such a tool exists.
Add Our Code to the Mix
First, we will add our code to
api/v1beta1/map_types.go, which will add our fields to our type.
Essentially, we just edited the
MapSpec and the
Now, we need to add the code to our controller in
In this controller, we have now added two functions: one to create a pod and the other to modify the entire Reconcile function (this one takes care of checking the status and make the transitions—in other words, it makes a controller work like a controller. Also, have you noticed how the Kubebuilder annotations generates the RBAC config for us? Pretty handy, right?
Starting the Cluster with Kind
Next, we will use Kind to create a local cluster to test:
Could it really be that easy!? Well, yes, it is!
Running Our Operator Locally
For testing, you can run your operator locally like this:
First, we spin up a pod, and launch
nc -l -p 8000.
Now, we edit our manifest and apply it—checking that everything is in place. Then we do the port-forward and launch another
nc localhost 8000 to test if everything went well. First, the manifest:
Then port-forward and test the code.
Making It Publicly Ready
Here, we just build and push the Docker image to Docker Hub or to your favorite public registry.
Then you can install it with
make deploy IMG=kainlite/forward:0.0.1 and uninstall it with
If you spot any errors or have any suggestions, please send me a message so that I can update the code.
This blog was originally published here.
License: MIT License
Published at DZone with permission of Gabriel Garrido. See the original article here.
Opinions expressed by DZone contributors are their own.