Before we had Spring Boot and similar frameworks, a web app container was the main requirement for deploying Java web applications. We now live in the age of microservices, and many Java applications are developed on top of Quarkus, Thorntail, or Spring Boot. But some use cases still require an old-school web application.
In this article, you will learn how to deploy a Java web application (WAR) into a Kubernetes or Red Hat OpenShift cluster using Eclipse JKube. I'll show you how easy it is to make a monolithic Java web application cloud-native, just by adding Eclipse JKube Maven plugins.
The example web application
For the purpose of this article, we'll work with a very simple Spring Web MVC application. The following sample shows the most relevant parts of the Maven project's pom.xml:
<!-- ... --> <packaging>war</packaging> <!-- ... --> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <failOnMissingWebXml>false</failOnMissingWebXml> <!-- ... --> <jkube.enricher.jkube-service.type>NodePort</jkube.enricher.jkube-service.type> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${version.spring}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.eclipse.jkube</groupId> <artifactId>kubernetes-maven-plugin</artifactId> <version>${version.jkube}</version> </plugin> <!-- ... --> </plugins> </build>
First, notice the packaging
element, which indicates that the resulting artifact should be packaged as a war
file, which is the usual format for Java web applications.
In the properties section, notice that the project is configured for Java 11. We'll need a compatible JDK to build the project. We use the failOnMissingWebXml option to configure the maven-war-plugin so that it won't fail due to a missing web.xml
file. (You will see soon what we're using instead.) Finally, there's a JKube-specific property, jkube.enricher.jkube-service.type
. This property configures JKube to create a service-resource manifest using NodePort
as the spec.type
.
In the dependencies section, we find just two dependencies. The spring-webmvc
dependency lets us use the Spring Web MVC framework. The javax.servlet-api
dependency provides compile-time support for the Java Servlet API, which is provided by the web application container at runtime.
Finally, in the plugins section, we've configured the Eclipse JKube dependency. Note that we can use either the kubernetes-maven-plugin
or the openshift-maven-plugin
. The choice of plugin depends on the cluster we want to target, but we only need one of them.
The configuration is simple and straightforward. The only thing different from a typical, old-school Java web application is the Eclipse JKube plugin dependency.
Java classes in the example project
The example project contains three Java classes: ExampleInitializer
, ExampleConfiguration
, and ExampleResource
. First, ExampleInitializer is a WebApplicationInitializer
implementation. We're using it instead of the standard WEB-INF/web.xml
deployment descriptor to configure the ServletContext programmatically.
The following code shows how ExampleInitializer
lets us register Spring's DispatcherServlet
without any additional XML configuration:
final AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.register(ExampleConfiguration.class); context.setServletContext(servletContext); final ServletRegistration.Dynamic dsr = servletContext.addServlet("dispatcher", new DispatcherServlet(context)); dsr.setLoadOnStartup(1); dsr.addMapping("/");
The ExampleConfiguration class is a Spring-specific configuration enabling Spring MVC.
Finally, ExampleResource is a standard Spring @RestController
. It has a single request mapping that responds Hello!!! to any GET
request.
Deploy the web application into Kubernetes
We'll start by deploying the example web application into Kubernetes; then, I'll show you how to make a couple of adjustments and deploy it into OpenShift.
Step 1: Build the application
The first step is to build the project just as we would build any other Maven web application project. Running mvn clean package
generates a new war
artifact in the target directory of target/example-0.0.0-SNAPSHOT.war
.
For the purpose of this example, we'll use Minikube. So that we can pull the image from the cluster without having to push it to a shared registry, we'll use Minikube's docker
daemon, eval $(minikube docker-env)
.
We can now issue the mvn k8s:build
command to build the docker image for our application, as shown in Figure 1.
A new docker image will be tagged as webapp/example:latest
in our docker registry.
Step 2: Create the cluster configuration
Next, we create the required cluster configuration resource manifests and apply them to the kubectl
- configured cluster, as shown in Figure 2.
The previous commands will generate the Kubernetes configuration manifests in target/classes/META-INF/jkube/kubernetes.yml
, and will apply them to the cluster.
Step 3: Verify that the application is running
Finally, we verify that everything is running by entering the commands shown in Figure 3.
Deploy the web application into OpenShift
Following similar steps to those in the previous section, we can seamlessly deploy the example web application to an OpenShift cluster, just by using the openshift-maven-plugin
instead of the kubernetes-maven-plugin
:
<build> <plugins> <plugin> <groupId>org.eclipse.jkube</groupId> <artifactId>openshift-maven-plugin</artifactId> <version>${version.jkube}</version> </plugin> </plugins> </build>
In this case, the plugin prefix is oc
instead of k8s
, but the goals to be run are the same. Note that in this case, the build step uses S2I instead of docker to perform the build.
The developer experience
You've seen how Eclipse JKube lets us easily deploy a web application to the cloud. It also has other features to ease our lives as developers. Let's look at how Eclipse JKube enhances a typical log retrieval.
Log retrieval
If we want to print the logs for the web application that we just deployed, we can simply run mvn k8s:log
. The logs will be printed and followed (or tailed) in the current console, as shown in Figure 6.
The screenshot in Figure 6 shows the log for the application we've just deployed on Kubernetes. You can see that the Apache Tomcat web application container was started and that the web application was deployed into the ROOT context.
Note: By default, Eclipse JKube uses Apache Tomcat as its web application container, via the jkube/jkube-tomcat9-binary-s2i base image. In a follow-up article, I will show you how to use different web application containers (such as Jetty), just by adding container-specific files.
Conclusion
In this article, you've seen how easy it is to convert an old-school Java web application into a full-fledged cloud-native application, just by adding the Eclipse JKube plugin dependency to your Maven POM. The complete source code for the examples is available on GitHub.
If you're interested in learning more about Eclipse JKube, you can visit our main GitHub repository, website, or Gitter channel. And don't forget to follow us on Twitter!
Last updated: September 19, 2023