Deploy Micronaut Application to Kubernetes using Eclipse JKube

Rohan Kumar
ITNEXT
Published in
5 min readApr 24, 2021

--

Note: This blog is a part of blog Series: Deploying Java applications onto Kubernetes using Eclipse JKube

In previous blogs we have seen how to deploy Spring Boot, Quarkus, Vert.x or any web application based on Tomcat to Kubernetes with ease with the help of Eclipse JKube. Eclipse JKube has support for Micronaut applications too. We’ll do the same thing but for a Micronaut based project this time using Eclipse JKube. One thing to note here is that, the workflow remains unchanged for Eclipse JKube regardless of the framework it’s trying to deploy on top of Kubernetes.

Creating and configuring the sample code:

You can go to Micronaut Launcher website and generate a sample maven project. It provides really nice UI to quickly generate a sample project and add necessary dependencies.

Micronaut Launch Website

Here is how generated pom.xml looks like:

pom.xml for project

We’ll be adding a simple controller for a simple model class Person just like done in Piotr Mińkowski’s sample project here:

Our application source tree would look like this:

eclipse-jkube-micronaut-sample : $ tree . 
.
├── mvnw
├── mvnw.bat
├── pom.xml
├── README.md
└── src
├── main
│ ├── java
│ │ └── in
│ │ └── rohaan
│ │ └── demo
│ │ ├── Application.java
│ │ ├── model
│ │ │ └── Person.java
│ │ └── PersonController.java
│ └── resources
│ ├── application.yml
│ └── logback.xml

Let’s define our model class Person . It would be a simple class for Person entity with fields like id, firstname, lastname etc.

Person class for our Controller Model

Then we’ll be defining a REST controller for this Person entity. We’ll add some basic endpoints for POST and GET operations:

PersonController.java

Once we’ve added all these files to our project. We can go ahead and package our application:

$ mvn clean install

You can then execute the jar present in target directory. You can also go ahead and run it using micronaut maven plugin run goal like this:

$ mvn mn:run

You should be able to see /persons GET endpoint returning all persons created. You can send a POST request to it in order to create a Person.

Build and Push Container image to Docker Hub:

Now that our application is running okay locally let’s try to containerize it into a Docker image and publish it to some container registry. In order to do all that we’ll be adding Eclipse JKube Kubernetes Maven Plugin to our pom.xml . You can find the latest version on maven central.

<plugin>
<groupId>
org.eclipse.jkube</groupId>
<artifactId>
kubernetes-maven-plugin</artifactId>
<version>
${jkube.version}</version>
</plugin
>

You can build the image using mvn k8s:build goal, by default Eclipse JKube generates image name of your application in format ${project.groupId}/${project.artifactId}:${project.version} but you can override this by specifying jkube.generator.name property like this:

<properties>  ...
<docker.user>
rohankanojia</docker.user>
<jkube.generator.name>
${docker.user}/${project.artifactId}:${project.version}
</jkube.generator.name>
<properties>

Then you can proceed with image building process using Eclipse JKube’s build goal:

$ mvn k8s:build
Building Docker Image

Now in order to push local image to docker hub you would need to configure your registry credentials first. As specified in Kubernetes Maven Plugin Docs, you can configure registry via docker login , via plugin’s XML configuration or via .m2/settings.xml just like maven repositories. I used docker login which will store my credentials in .docker/config.json . After this, you can proceed with pushing the image like this:

$ mvn k8s:push
Pushing Image to Docker Hub

You can check by logging into your docker hub to check whether image got pushed successfully or not. In my case, image was pushed to docker hub:

Image pushed to Docker Hub

Deploying Application to Kubernetes:

Now that we have our application’s container image set up. We can now proceed to deploying our application to Kubernetes. If you have access to some Kubernetes cluster, you can log into the cluster in order to deploy it. I’ll be using minikube for deploying the application. In order to start my cluster I’ll run minikube start

$ minikube start

In order to deploy to Kubernetes, we need to generate Kubernetes YAML manifests for resources like Deployment , Service etc. We’ll use Eclipse JKube’s resource goal for this. Then we’ll apply these generated manifests to Kubernetes Cluster with apply goal.

In order to override default Service type to be NodePort for my local testing, I’ll add this property to my pom.xml :

<jkube.enricher.jkube-service.type>
NodePort
</jkube.enricher.jkube-service.type>

Then you can deploy to Kubernetes with this goal:

$ mvn k8s:resource k8s:apply
mvn k8s:resource k8s:apply goal

You can then check the application URL with minikube service like this:

minikube service

Testing Deployed Application:

You can check whether application is running okay by doing simple CURL to add a person and view existing persons:

eclipse-jkube-micronaut-sample : $ curl -XPOST \
> -H 'Content-Type:application/json' \
> -d '{"id":1,"firstName":"John","lastName":"Snow","age":29,"gender":"Male"}' \
> http://192.168.39.47:31142/persons | jq .

{
"id": 1,
"firstName": "John",
"lastName": "Snow",
"age": 29,
"gender": "Male"
}
eclipse-jkube-micronaut-sample : $ curl http://192.168.39.47:31142/persons | jq .
[
{
"id": 1,
"firstName": "John",
"lastName": "Snow",
"age": 29,
"gender": "Male"
}
]

Removing the deployed application:

Once you’re done testing, you can clean up all resources that were created by undeploy goal like this:

$ mvn k8s:undeploy
Kubernetes Maven Plugin Undeploy goal

Conclusion:

Today we learned how Eclipse JKube simplifies development on top of Kubernetes for yet another framework: Micronaut

You can find all code used in this blog in this repository:

Do you have something in mind which can help us improve this project? We value your feedback a lot so please report bugs, ask for improvements…don’t be shy and join our welcoming community:

--

--