Deploying Eclipse Jetty Web applications to Kubernetes using Eclipse JKube

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

Deploying Eclipse Jetty Web Applications to Kubernetes using Eclipse JKube
Deploying Jetty Web Apps to 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 Eclipse Jetty based web applications too. We’ll do the same thing but for a Jetty 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.

Today we’ll be deploying an official jetty hello world quickstart to Kubernetes using Eclipse JKube. But before we do that, we need to set up a Kubernetes Cluster. If you already have access to a Kubernetes Cluster, you won’t need to go over this step.

Setting up Kubernetes Environment:

You can log into your Kubernetes Cluster or can set up a single node Kubernetes Cluster using minikube. I am using minikube. I started it using this command:

$ minikube start

Once, minikube is running. We need build docker image in minikube. If you were using some other Kubernetes Cluster, you would need to build and push to some docker registry. You need to expose minikube VM’s internal docker daemon to your terminal session using the following command:

$ eval $(minikube -p minikube docker-env)

Getting the Eclipse Jetty Application:

I’ll be using one of Eclipse Jetty official Quickstarts. It’s a simple hello world application:

We’ll clone the project first:

$ git clone https://github.com/jetty-project/jetty-helloworld-webapp.git

Building the Application:

We’ll compile project afterwards:

$ mvn clean install

Adding Kubernetes Maven Plugin to pom.xml:

We’ll be using Eclipse JKube’s Kubernetes Maven Plugin which can be used by maven projects to handle their workloads on Kubernetes. We’ll be adding the following configuration in our pom.xml :

<plugin>
<groupId>org.eclipse.jkube</groupId>
<artifactId>kubernetes-maven-plugin</artifactId>
<version>1.0.2</version>
<!-- Connect k8s:resource & k8s:build to lifecycle phases -->
<executions>
<execution>
<id>jkube</id>
<goals>
<goal>resource</goal>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>

Note that we’re binding k8s:build (docker image build) and k8s:resource (Kubernetes Manifest generation) to plugin executions. So whenever you package your application using the old mvn package command. It will also build docker image and generate manifests to you. Packaging application in cloud native terms is basically creating a Docker Image for your application anyway.

Deploying Application to Kubernetes:

Now we have kubernetes-maven-plugin added to our pom.xml , we can go ahead and hit plugin goal. But wait, there is one issue; Our application would be deployed into Kubernetes but it won’t be accessible from outside the cluster. By default Eclipse JKube creates a Kubernetes Service of type ClusterIP meaning it is visible within Cluster only. We’ll override this behavior by adding this property:

<plugin>
<groupId>org.eclipse.jkube</groupId>
<artifactId>kubernetes-maven-plugin</artifactId>
<version>1.0.2</version>
<configuration>
<enricher>
<config>
<jkube-service>

<type>NodePort</type>
</jkube-service>
</config>

</enricher>
</configuration>
...
</plugin>

Now you can deploy by running this maven goal:

$ mvn k8s:deploy

You can check if docker image was built and kubenetes resources got deployed into the cluster like this:

jetty-helloworld-webapp : $ kubectl get pods
NAME READY STATUS RESTARTS AGE
jetty-helloworld-webapp-667c58d64 1/1 Running 0 54s
jetty-helloworld-webapp : $ docker images | grep jetty-helloworld
demo/jetty-helloworld-webapp 1.0 b7951b10de About a minute ago 308MB

If everything is running fine. You can access your application like this:

jetty-helloworld-webapp : $ minikube service jetty-helloworld-webapp
|-----------|-------------|------------|---------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|-------------------------|-------------|--------------|
| default | jetty-helloworld-webapp | http/8080 | <app-url> |
|-----------|-------------------------|-------------|---------------|
🎉 Opening service default/jetty-helloworld-webapp in default browser...

It would open up the application in your default browser. You can also check it via your terminal like this:

jetty-helloworld-webapp : $ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
jetty-helloworld-webapp NodePort 10.97.224.208 <none> 8080:30481/TCP 26s
jetty-helloworld-webapp : $ curl `minikube ip`:30481/
Hello World

Once everything is done, you can simply undeploy your application from Kubernetes using k8s:undeploy goal:

$ mvn k8s:undeploy

You can find all the code changes made in my fork of this repository:

Conclusion:

Today we learned how Eclipse JKube smoothens development on top of Kubernetes for yet another framework: Eclipse Jetty.

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:

Software Engineer, Red Hat (https://about.me/rohankanojia)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store